Skip to content

Commit

Permalink
fix: enhance ee.Image.fullLike (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau authored May 17, 2024
2 parents 09c563f + e89f816 commit 081864c
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions geetools/Image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ def fullLike(
fillValue: ee_number,
copyProperties: ee_int = 0,
keepMask: ee_int = 0,
keepFootprint: ee_int = 1,
) -> ee.Image:
"""Create an image with the same band names, projection and scale as the original image.
Expand All @@ -485,6 +486,7 @@ def fullLike(
fillValue: The value to fill the image with.
copyProperties: If True, the properties of the original image will be copied to the new one.
keepMask: If True, the mask of the original image will be copied to the new one.
keepFootprint: If True, the footprint of the original image will be used to clip the new image.
Returns:
An image with the same band names, projection and scale as the original image.
Expand All @@ -500,16 +502,30 @@ def fullLike(
image = image.geetools.fullLike(0)
print(image.bandNames().getInfo())
"""
# function params as GEE objects
keepMask, copyProperties = ee.Number(keepMask), ee.Number(copyProperties)
keepFootprint = ee.Number(keepFootprint)
# get geometry, band names and property names
footprint, bandNames = self._obj.geometry(), self._obj.bandNames()
properties = self._obj.propertyNames().remove(
"system:footprint"
) # remove footprint as a "normal" property
# list of values to fill the image
fillValue = ee.List.repeat(fillValue, bandNames.size())
image = (
self.full(fillValue, bandNames)
.reproject(self._obj.select(0).projection())
.clip(footprint)
# filled image
image = self.full(fillValue, bandNames)
# handler projection
projected_list = bandNames.map(
lambda b: image.select([b]).reproject(self._obj.select([b]).projection())
)
withProperties = image.copyProperties(self._obj)
image = ee.ImageCollection.fromImages(projected_list).toBands().rename(bandNames)
# handle footprint
image_footprint = image.clip(footprint) # sets system:footprint property
image = ee.Image(ee.Algorithms.If(keepFootprint, image_footprint, image))
# handle properties
withProperties = image.copyProperties(self._obj, properties)
image = ee.Algorithms.If(copyProperties, withProperties, image)
# handle mask
withMask = ee.Image(image).updateMask(self._obj.mask())
image = ee.Algorithms.If(keepMask, withMask, image)
return ee.Image(image)
Expand Down

0 comments on commit 081864c

Please sign in to comment.