diff --git a/samples/python/guides/reducers011.py b/samples/python/guides/reducers011.py new file mode 100644 index 000000000..54092e41a --- /dev/null +++ b/samples/python/guides/reducers011.py @@ -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] diff --git a/samples/python/guides/reducers03.py b/samples/python/guides/reducers03.py new file mode 100644 index 000000000..36c55b1a3 --- /dev/null +++ b/samples/python/guides/reducers03.py @@ -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] diff --git a/samples/python/guides/reducers04.py b/samples/python/guides/reducers04.py new file mode 100644 index 000000000..0ceef9f4d --- /dev/null +++ b/samples/python/guides/reducers04.py @@ -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] diff --git a/samples/python/guides/reducers06.py b/samples/python/guides/reducers06.py new file mode 100644 index 000000000..4ad1c1742 --- /dev/null +++ b/samples/python/guides/reducers06.py @@ -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] diff --git a/samples/python/guides/reducers07.py b/samples/python/guides/reducers07.py new file mode 100644 index 000000000..a1f84c523 --- /dev/null +++ b/samples/python/guides/reducers07.py @@ -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] diff --git a/samples/python/guides/reducers08.py b/samples/python/guides/reducers08.py new file mode 100644 index 000000000..ccf175269 --- /dev/null +++ b/samples/python/guides/reducers08.py @@ -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] diff --git a/samples/python/guides/reducers09.py b/samples/python/guides/reducers09.py new file mode 100644 index 000000000..4696a02cc --- /dev/null +++ b/samples/python/guides/reducers09.py @@ -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] diff --git a/samples/python/guides/reducers10.py b/samples/python/guides/reducers10.py new file mode 100644 index 000000000..79277e05e --- /dev/null +++ b/samples/python/guides/reducers10.py @@ -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] diff --git a/samples/python/guides/reducers11.py b/samples/python/guides/reducers11.py new file mode 100644 index 000000000..3a783ad0c --- /dev/null +++ b/samples/python/guides/reducers11.py @@ -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] diff --git a/samples/python/guides/reducers12.py b/samples/python/guides/reducers12.py new file mode 100644 index 000000000..9c94e35f6 --- /dev/null +++ b/samples/python/guides/reducers12.py @@ -0,0 +1,45 @@ +# 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 - splitWeights' section.""" + +# [START earthengine__reducers12__split_weights] +# Load an input Landsat 8 image. +image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_186059_20130419') + +# Compute cloud score and reverse it such that the highest +# weight (100) is for the least cloudy pixels. +cloud_weight = ee.Image(100).subtract( + ee.Algorithms.Landsat.simpleCloudScore(image).select(['cloud']) +) + +# Compute NDVI and add the cloud weight band. +ndvi = image.normalizedDifference(['B5', 'B4']).addBands(cloud_weight) + +# Define an arbitrary region in a cloudy area. +region = ee.Geometry.Rectangle(9.9069, 0.5981, 10.5, 0.9757) + +# Use a mean reducer. +reducer = ee.Reducer.mean() + +# Compute the unweighted mean. +unweighted = ndvi.select(['nd']).reduceRegion(reducer, region, 30) + +# compute mean weighted by cloudiness. +weighted = ndvi.reduceRegion(reducer.splitWeights(), region, 30) + +# Observe the difference as a result of weighting by cloudiness. +display('unweighted:', unweighted) +display('weighted:', weighted) +# [END earthengine__reducers12__split_weights]