Skip to content

Commit

Permalink
feat: Plotting capabilities for ImageCollection (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau authored Oct 24, 2024
2 parents 4d96594 + 2d4c9fc commit b7aa2f8
Show file tree
Hide file tree
Showing 11 changed files with 1,595 additions and 44 deletions.
669 changes: 664 additions & 5 deletions docs/usage/plot/plot-imagecollection.ipynb

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions geetools/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ def fullLike(

def reduceBands(
self,
reducer: str,
reducer: str | ee.Reducer,
bands: list | ee.List = [],
name: str | ee.String = "",
) -> ee.Image:
Expand Down Expand Up @@ -562,7 +562,8 @@ def reduceBands(
bands, name = ee.List(bands), ee.String(name)
bands = ee.Algorithms.If(bands.size().eq(0), self._obj.bandNames(), bands)
name = ee.Algorithms.If(name.equals(ee.String("")), reducer, name)
reduceImage = self._obj.select(ee.List(bands)).reduce(reducer).rename([name])
red = getattr(ee.Reducer, reducer)() if isinstance(reducer, str) else reducer
reduceImage = self._obj.select(ee.List(bands)).reduce(red).rename([name])
return self._obj.addBands(reduceImage)

def negativeClip(self, geometry: ee.Geometry | ee.Feature | ee.FeatureCollection) -> ee.Image:
Expand Down Expand Up @@ -1422,7 +1423,7 @@ def plot(
def byBands(
self,
regions: ee.featurecollection,
reducer: str = "mean",
reducer: str | ee.Reducer = "mean",
scale: int = 10000,
bands: list = [],
regionId: str = "system:index",
Expand All @@ -1442,7 +1443,7 @@ def byBands(
Parameters:
regions: The regions to compute the reducer in.
reducer: The name of the reducer to use, default to "mean".
reducer: The name of the reducer or a reducer object to use. Default is "mean".
scale: The scale to use for the computation. Default is 10000m.
regionId: The property used to label region. Defaults to "system:index".
labels: The labels to use for the output dictionary. Default to the band names.
Expand Down Expand Up @@ -1484,7 +1485,7 @@ def byBands(
# This is currently hidden because of https://issuetracker.google.com/issues/374285504
# It will have no impact on most of the cases as plt_hist should be used for single band images
# reducer = reducer.setOutputs(labels)
red = getattr(ee.Reducer, reducer)()
red = getattr(ee.Reducer, reducer)() if isinstance(reducer, str) else reducer

# retrieve the reduce bands for each feature
image = self._obj.select(eeBands).rename(eeLabels)
Expand All @@ -1499,7 +1500,7 @@ def byBands(
def byRegions(
self,
regions: ee.featurecollection,
reducer: str = "mean",
reducer: str | ee.Reducer = "mean",
scale: int = 10000,
bands: list = [],
regionId: str = "system:index",
Expand All @@ -1519,7 +1520,7 @@ def byRegions(
Parameters:
regions: The regions to compute the reducer in.
reducer: The name of the reducer to use, default to "mean".
reducer: The name of the reducer or a reducer object to use. Default is "mean".
scale: The scale to use for the computation. Default is 10000m.
regionId: The property used to label region. Defaults to "system:index".
labels: The labels to use for the output dictionary. Default to the band names.
Expand Down Expand Up @@ -1561,11 +1562,11 @@ def byRegions(
# This is currently hidden because of https://issuetracker.google.com/issues/374285504
# It will have no impact on most of the cases as plt_hist should be used for single band images
# reducer = reducer.setOutputs(labels)
red = getattr(ee.Reducer, reducer)()
red = getattr(ee.Reducer, reducer)() if isinstance(reducer, str) else reducer

# retrieve the reduce bands for each feature
image = self._obj.select(bands).rename(labels)
fc = image.reduceRegions(collection=regions, reducer=red, scale=scale)
fc = image.reduceRegions(regions, red, scale)

# extract the data as a list of dictionaries (one for each label) aggregating
# we are force to turn the fc into a list because GEE don't accept to map a featureCollection
Expand All @@ -1579,7 +1580,7 @@ def plot_by_regions(
self,
type: str,
regions: ee.FeatureCollection,
reducer: str = "mean",
reducer: str | ee.Reducer = "mean",
scale: int = 10000,
bands: list = [],
regionId: str = "system:index",
Expand All @@ -1599,7 +1600,7 @@ def plot_by_regions(
Parameters:
type: The type of plot to use. Defaults to "bar". can be any type of plot from the python lib `matplotlib.pyplot`. If the one you need is missing open an issue!
regions: The regions to compute the reducer in.
reducer: The name of the reducer to use, default to "mean".
rreducer: The name of the reducer or a reducer object to use. Default is "mean".
scale: The scale to use for the computation. Default is 10000m.
bands: The bands to compute the reducer on. Default to all bands.
regionId: The property used to label region. Defaults to "system:index".
Expand Down Expand Up @@ -1649,7 +1650,7 @@ def plot_by_bands(
self,
type: str,
regions: ee.FeatureCollection,
reducer: str = "mean",
reducer: str | ee.Reducer = "mean",
scale: int = 10000,
bands: list = [],
regionId: str = "system:index",
Expand All @@ -1670,7 +1671,7 @@ def plot_by_bands(
Parameters:
type: The type of plot to use. Defaults to "bar". can be any type of plot from the python lib `matplotlib.pyplot`. If the one you need is missing open an issue!
regions: The regions to compute the reducer in.
reducer: The name of the reducer to use, default to "mean".
reducer: The name of the reducer or a reducer object to use. Default is "mean".
scale: The scale to use for the computation. Default is 10000m.
bands: The bands to compute the reducer on. Default to all bands.
regionId: The property used to label region. Defaults to "system:index".
Expand Down
Loading

0 comments on commit b7aa2f8

Please sign in to comment.