Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compositor for adding an image as a background #804

Merged
merged 28 commits into from
Jun 12, 2019
Merged
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
35529a4
Add StaticImageCompositor
pnuu May 31, 2019
adedc63
Collect all arguments to *args, none are used in the compositor
pnuu May 31, 2019
4124315
Fix loading "image" dataset
pnuu May 31, 2019
5dde7c1
Fix area definition handling
pnuu May 31, 2019
9820756
Use current time if nothing has been read from the filename
pnuu Jun 3, 2019
e381e9c
Add BackgroundCompositor
pnuu Jun 4, 2019
2cb9d3b
Fix a typo in get_enhanced_image call
pnuu Jun 4, 2019
d6ce89a
Assign merged attributes
pnuu Jun 4, 2019
ea20fc3
Fix BackgroundCompositor
pnuu Jun 4, 2019
e2596ea
Remove leftover commented debug line
pnuu Jun 4, 2019
f0b0f01
Add .eggs and htmlcov/ to ignored files
pnuu Jun 4, 2019
26b0454
Update dependency tree logic to accept "empty" nodes
djhoese Jun 4, 2019
6f1bde6
Fix adding bands to datasets, add tests
pnuu Jun 5, 2019
4e2ff18
Add end_time to read image if not present
pnuu Jun 5, 2019
6053843
Add TestAddBands to test suite
pnuu Jun 5, 2019
94ffef7
Use alpha channel of foreground composite for blending weight if avai…
pnuu Jun 5, 2019
a00b342
Add area attribute
pnuu Jun 5, 2019
ee34526
Add tests for StaticImageCompositor
pnuu Jun 5, 2019
db71ca8
Add a note that the alpha band will be removed by design
pnuu Jun 6, 2019
40b5fb9
Add tests for BackgroundCompositor
pnuu Jun 6, 2019
981dd04
Add skeleton of a test for calling scn.available_*() methods
pnuu Jun 6, 2019
b99b0ef
Add test for static_image (no dependency) compositor in scene loading
djhoese Jun 6, 2019
9235682
Fix scene loading test no longer testing what it was supposed to
djhoese Jun 6, 2019
29b8321
Check area ndims to see if image was georeferenced
pnuu Jun 7, 2019
18ce2f3
Test handling of non-georeferenced images
pnuu Jun 7, 2019
ed42b48
Change kwarg 'fname' to 'filename'
pnuu Jun 7, 2019
477300d
Add documentation for StaticImageCompositor and BackgroundCompositor
pnuu Jun 7, 2019
a39799c
Fix static_day enhancement config intendation
pnuu Jun 7, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions satpy/composites/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1393,3 +1393,78 @@ def __call__(self, projectables, *args, **kwargs):
rgb_img = enhance2dataset(projectables[1])
rgb_img *= luminance
return super(SandwichCompositor, self).__call__(rgb_img, *args, **kwargs)


class StaticImageCompositor(GenericCompositor):
"""A compositor that loads a static image from disk."""

def __init__(self, name, fname=None, area=None, **kwargs):
"""Collect custom configuration values.

Args:
fname (str): Filename of the image to load
area (str): Name of area definition for the image. Optional
for images with built-in area definitions (geotiff)
"""
if fname is None:
raise ValueError("No image configured for static image compositor")
self.fname = fname
self.area = None
if area is not None:
from satpy.resample import get_area_def
self.area = get_area_def(area)

super(StaticImageCompositor, self).__init__(name, **kwargs)

def __call__(self, *args, **kwargs):
from satpy import Scene
scn = Scene(reader='generic_image', filenames=[self.fname])
scn.load(['image'])
img = scn['image']
# Check for proper area definition. Non-georeferenced images
# will raise IndexError
try:
_ = img.area.size
except IndexError:
if self.area is None:
raise AttributeError("Area definition needs to be configured")
img.attrs['area'] = self.area
img.attrs['sensor'] = None
img.attrs['mode'] = ''.join(img.bands.data)
img.attrs.pop('modifiers', None)
img.attrs.pop('calibration', None)
# Add start time if not present in the filename
if 'start_time' not in img.attrs or not img.attrs['start_time']:
import datetime as dt
img.attrs['start_time'] = dt.datetime.utcnow()

return img


class BackgroundCompositor(GenericCompositor):
"""A compositor that overlays one composite on top of another."""

def __call__(self, projectables, *args, **kwargs):
projectables = self.check_areas(projectables)

# Get enhanced images out of the composites
foreground = get_enhanced_image(projectables[0])
background = get_enhance_image(projectables[1])

# Adjust bands so that they match
# L/RGB -> RGB/RGB
# LA/RGB -> RGBA/RGBA
# RGB/RGBA -> RGBA/RGBA
foreground = add_bands(foreground, background['bands'])
background = add_bands(background, foreground['bands'])

# Stack the images
background.stack(foreground)

# Get merged metadata
attrs = combine_metadata

# Split to separate bands so the mode is correct
data = [background.sel(bands=b) for b in background['bands']]

return super(BackgroundCompositor, self).__call__(data, **kwargs)