-
Notifications
You must be signed in to change notification settings - Fork 752
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert API code snippets from JS to Py (reducers).
PiperOrigin-RevId: 711481948
- Loading branch information
1 parent
c10a456
commit 4649d8c
Showing
10 changed files
with
397 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright 2024 The Google Earth Engine Community Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Earth Engine Developer's Guide examples from 'Reducers - Image reduce' section.""" | ||
|
||
# [START earthengine__reducers011__image_reduce] | ||
# Load an image and select some bands of interest. | ||
image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318').select( | ||
['B4', 'B3', 'B2'] | ||
) | ||
|
||
# Reduce the image to get a one-band maximum value image. | ||
max_value = image.reduce(ee.Reducer.max()) | ||
|
||
# Display the result. | ||
m = geemap.Map() | ||
m.center_object(image, 10) | ||
m.add_layer(max_value, {'max': 13000}, 'Maximum value image') | ||
m | ||
# [END earthengine__reducers011__image_reduce] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright 2024 The Google Earth Engine Community Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Earth Engine Developer's Guide examples from 'Reducers - reduceRegions' section.""" | ||
|
||
# [START earthengine__reducers03__reduce_regions] | ||
# Load input imagery: Landsat 7 5-year composite. | ||
image = ee.Image('LANDSAT/LE7_TOA_5YEAR/2008_2012') | ||
|
||
# Load a FeatureCollection of counties in Maine. | ||
maine_counties = ee.FeatureCollection('TIGER/2016/Counties').filter( | ||
ee.Filter.eq('STATEFP', '23') | ||
) | ||
|
||
# Add reducer output to the Features in the collection. | ||
maine_means_features = image.reduceRegions( | ||
collection=maine_counties, reducer=ee.Reducer.mean(), scale=30 | ||
) | ||
|
||
# Print the first feature, to illustrate the result. | ||
display(ee.Feature(maine_means_features.first()).select(image.bandNames())) | ||
# [END earthengine__reducers03__reduce_regions] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2024 The Google Earth Engine Community Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Earth Engine Developer's Guide examples from 'Reducers - reduceNeighborhood' section.""" | ||
|
||
# [START earthengine__reducers04__reduce_neighborhood] | ||
# Define a region in the redwood forest. | ||
redwoods = ee.Geometry.Rectangle(-124.0665, 41.0739, -123.934, 41.2029) | ||
|
||
# Load input NAIP imagery and build a mosaic. | ||
naip_collection = ( | ||
ee.ImageCollection('USDA/NAIP/DOQQ') | ||
.filterBounds(redwoods) | ||
.filterDate('2012-01-01', '2012-12-31') | ||
) | ||
naip = naip_collection.mosaic() | ||
|
||
# Compute NDVI from the NAIP imagery. | ||
naip_ndvi = naip.normalizedDifference(['N', 'R']) | ||
|
||
# Compute standard deviation (SD) as texture of the NDVI. | ||
texture = naip_ndvi.reduceNeighborhood( | ||
reducer=ee.Reducer.stdDev(), kernel=ee.Kernel.circle(7) | ||
) | ||
|
||
# Display the results. | ||
m = geemap.Map() | ||
m.center_object(redwoods, 12) | ||
m.add_layer(naip, {}, 'NAIP input imagery') | ||
m.add_layer( | ||
naip_ndvi, {'min': -1, 'max': 1, 'palette': ['FF0000', '00FF00']}, 'NDVI' | ||
) | ||
m.add_layer(texture, {'min': 0, 'max': 0.3}, 'SD of NDVI') | ||
m | ||
# [END earthengine__reducers04__reduce_neighborhood] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright 2024 The Google Earth Engine Community Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Earth Engine Developer's Guide examples from 'Reducers - FeatureCollection.reduceColumns' section.""" | ||
|
||
# [START earthengine__reducers06__toy_reduce_columns] | ||
# Make a toy FeatureCollection. | ||
a_feature_collection = ee.FeatureCollection([ | ||
ee.Feature(None, {'foo': 1, 'weight': 1}), | ||
ee.Feature(None, {'foo': 2, 'weight': 2}), | ||
ee.Feature(None, {'foo': 3, 'weight': 3}), | ||
]) | ||
|
||
# Compute a weighted mean and display it. | ||
display( | ||
a_feature_collection.reduceColumns( | ||
reducer=ee.Reducer.mean(), selectors=['foo'], weightSelectors=['weight'] | ||
) | ||
) | ||
# [END earthengine__reducers06__toy_reduce_columns] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2024 The Google Earth Engine Community Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Earth Engine Developer's Guide examples from 'Reducers - FeatureCollection.reduceColumns' section.""" | ||
|
||
# [START earthengine__reducers07__reduce_columns] | ||
# Load US census data as a FeatureCollection. | ||
census = ee.FeatureCollection('TIGER/2010/Blocks') | ||
|
||
# Filter the collection to include only Benton County, OR. | ||
benton = census.filter( | ||
ee.Filter.And( | ||
ee.Filter.eq('statefp10', '41'), ee.Filter.eq('countyfp10', '003') | ||
) | ||
) | ||
|
||
# Display Benton County census blocks. | ||
m = geemap.Map() | ||
m.set_center(-123.27, 44.57, 13) | ||
m.add_layer(benton) | ||
display(m) | ||
|
||
# Compute sums of the specified properties. | ||
properties = ['pop10', 'housing10'] | ||
sums = benton.filter(ee.Filter.notNull(properties)).reduceColumns( | ||
reducer=ee.Reducer.sum().repeat(2), selectors=properties | ||
) | ||
|
||
# Print the resultant Dictionary. | ||
display(sums) | ||
# [END earthengine__reducers07__reduce_columns] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Copyright 2024 The Google Earth Engine Community Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Earth Engine Developer's Guide examples from 'Reducers - FeatureCollection.reduceToImage' section.""" | ||
|
||
# [START earthengine__reducers08__reduce_to_image] | ||
# Load a collection of US counties. | ||
counties = ee.FeatureCollection('TIGER/2018/Counties') | ||
|
||
# Make an image out of the land area attribute. | ||
land_area_img = counties.filter(ee.Filter.notNull(['ALAND'])).reduceToImage( | ||
properties=['ALAND'], reducer=ee.Reducer.first() | ||
) | ||
|
||
# Display the county land area image. | ||
m = geemap.Map() | ||
m.set_center(-99.976, 40.38, 5) | ||
m.add_layer( | ||
land_area_img, | ||
{ | ||
'min': 3e8, | ||
'max': 1.5e10, | ||
'palette': ['FCFDBF', 'FDAE78', 'EE605E', 'B63679', '711F81', '2C105C'], | ||
}, | ||
) | ||
m | ||
# [END earthengine__reducers08__reduce_to_image] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2024 The Google Earth Engine Community Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Earth Engine Developer's Guide examples from 'Reducers - Grouping' section.""" | ||
|
||
# [START earthengine__reducers09__grouping_fc] | ||
# Load a collection of US census blocks. | ||
blocks = ee.FeatureCollection('TIGER/2010/Blocks') | ||
|
||
# Compute sums of the specified properties, grouped by state code. | ||
sums = blocks.filter( | ||
ee.Filter.And( | ||
ee.Filter.neq('pop10', None), ee.Filter.neq('housing10', None) | ||
) | ||
).reduceColumns( | ||
selectors=['pop10', 'housing10', 'statefp10'], | ||
reducer=ee.Reducer.sum() | ||
.repeat(2) | ||
.group(groupField=2, groupName='state-code'), | ||
) | ||
|
||
# Print the resultant Dictionary. | ||
display(sums) | ||
# [END earthengine__reducers09__grouping_fc] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2024 The Google Earth Engine Community Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Earth Engine Developer's Guide examples from 'Reducers - Grouping' section.""" | ||
|
||
# [START earthengine__reducers10__grouping_image] | ||
# Load a region representing the United States | ||
region = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017').filter( | ||
ee.Filter.eq('country_na', 'United States') | ||
) | ||
|
||
# Load MODIS land cover categories in 2001. | ||
landcover = ee.Image('MODIS/051/MCD12Q1/2001_01_01').select( | ||
# Select the IGBP classification band. | ||
'Land_Cover_Type_1' | ||
) | ||
|
||
# Load nightlights image inputs. | ||
nl_2001 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F152001').select( | ||
'stable_lights' | ||
) | ||
nl_2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012').select( | ||
'stable_lights' | ||
) | ||
|
||
# Compute the nightlights decadal difference, add land cover codes. | ||
nl_diff = nl_2012.subtract(nl_2001).addBands(landcover) | ||
|
||
# Grouped a mean reducer: change of nightlights by land cover category. | ||
means = nl_diff.reduceRegion( | ||
reducer=ee.Reducer.mean().group(groupField=1, groupName='code'), | ||
geometry=region.geometry(), | ||
scale=1000, | ||
maxPixels=1e8, | ||
) | ||
|
||
# Print the resultant Dictionary. | ||
display(means) | ||
# [END earthengine__reducers10__grouping_image] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2024 The Google Earth Engine Community Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Earth Engine Developer's Guide examples from 'Reducers - unweighted' section.""" | ||
|
||
# [START earthengine__reducers11__unweighted] | ||
# Load a Landsat 8 input image. | ||
image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318') | ||
|
||
# Create an arbitrary region. | ||
geometry = ee.Geometry.Rectangle(-122.496, 37.532, -121.554, 37.538) | ||
|
||
# Make an NDWI image. It will have one band named 'nd'. | ||
ndwi = image.normalizedDifference(['B3', 'B5']) | ||
|
||
# Compute the weighted mean of the NDWI image clipped to the region. | ||
weighted = ( | ||
ndwi.clip(geometry) | ||
.reduceRegion(reducer=ee.Reducer.mean(), geometry=geometry, scale=30) | ||
.get('nd') | ||
) | ||
|
||
# Compute the UN-weighted mean of the NDWI image clipped to the region. | ||
unweighted = ( | ||
ndwi.clip(geometry) | ||
.reduceRegion( | ||
reducer=ee.Reducer.mean().unweighted(), geometry=geometry, scale=30 | ||
) | ||
.get('nd') | ||
) | ||
|
||
# Observe the difference between weighted and unweighted reductions. | ||
display('weighted:', weighted) | ||
display('unweighted', unweighted) | ||
# [END earthengine__reducers11__unweighted] |
Oops, something went wrong.