-
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
PiperOrigin-RevId: 614566355
- Loading branch information
1 parent
21f69eb
commit 3f1c519
Showing
6 changed files
with
345 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,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 'Image Collections - Information and Metadata' page.""" | ||
|
||
# [START earthengine__image_collections01__collection_info] | ||
# Load a Landsat 8 ImageCollection for a single path-row. | ||
collection = ( | ||
ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') | ||
.filter(ee.Filter.eq('WRS_PATH', 44)) | ||
.filter(ee.Filter.eq('WRS_ROW', 34)) | ||
.filterDate('2014-03-01', '2014-08-01') | ||
) | ||
display('Collection:', collection) | ||
|
||
# Get the number of images. | ||
count = collection.size() | ||
display('Count:', count) | ||
|
||
# Get the date range of images in the collection. | ||
range = collection.reduceColumns(ee.Reducer.minMax(), ['system:time_start']) | ||
display('Date range:', ee.Date(range.get('min')), ee.Date(range.get('max'))) | ||
|
||
# Get statistics for a property of the images in the collection. | ||
sun_stats = collection.aggregate_stats('SUN_ELEVATION') | ||
display('Sun elevation statistics:', sun_stats) | ||
|
||
# Sort by a cloud cover property, get the least cloudy image. | ||
image = ee.Image(collection.sort('CLOUD_COVER').first()) | ||
display('Least cloudy image:', image) | ||
|
||
# Limit the collection to the 10 most recent images. | ||
recent = collection.sort('system:time_start', False).limit(10) | ||
display('Recent images:', recent) | ||
# [END earthengine__image_collections01__collection_info] |
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,78 @@ | ||
# 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 'Creating Image Collections' page.""" | ||
|
||
# [START earthengine__image_collections02__load_collection] | ||
sentinel_collection = ee.ImageCollection('COPERNICUS/S2_SR') | ||
# [END earthengine__image_collections02__load_collection] | ||
|
||
# [START earthengine__image_collections02__creating_collections] | ||
# Create arbitrary constant images. | ||
constant_1 = ee.Image(1) | ||
constant_2 = ee.Image(2) | ||
|
||
# Create a collection by giving a list to the constructor. | ||
collection_from_constructor = ee.ImageCollection([constant_1, constant_2]) | ||
display('Collection from constructor:', collection_from_constructor) | ||
|
||
# Create a collection with fromImages(). | ||
collection_from_images = ee.ImageCollection.fromImages( | ||
[ee.Image(3), ee.Image(4)] | ||
) | ||
display('Collection from images:', collection_from_images) | ||
|
||
# Merge two collections. | ||
merged_collection = collection_from_constructor.merge(collection_from_images) | ||
display('Merged collection:', merged_collection) | ||
|
||
# Create a toy FeatureCollection | ||
features = ee.FeatureCollection( | ||
[ee.Feature(None, {'foo': 1}), ee.Feature(None, {'foo': 2})] | ||
) | ||
|
||
# Create an ImageCollection from the FeatureCollection | ||
# by mapping a function over the FeatureCollection. | ||
images = features.map(lambda feature: ee.Image(ee.Number(feature.get('foo')))) | ||
|
||
# Display the resultant collection. | ||
display('Image collection:', images) | ||
# [END earthengine__image_collections02__creating_collections] | ||
|
||
# [START earthengine__image_collections02__cloud_collections] | ||
# All the GeoTiffs are in this folder. | ||
uri_base = ( | ||
'gs://gcp-public-data-landsat/LC08/01/001/002/' | ||
+ 'LC08_L1GT_001002_20160817_20170322_01_T2/' | ||
) | ||
|
||
# List of URIs, one for each band. | ||
uris = ee.List([ | ||
uri_base + 'LC08_L1GT_001002_20160817_20170322_01_T2_B2.TIF', | ||
uri_base + 'LC08_L1GT_001002_20160817_20170322_01_T2_B3.TIF', | ||
uri_base + 'LC08_L1GT_001002_20160817_20170322_01_T2_B4.TIF', | ||
uri_base + 'LC08_L1GT_001002_20160817_20170322_01_T2_B5.TIF', | ||
]) | ||
|
||
# Make a collection from the list of images. | ||
images = uris.map(lambda uri: ee.Image.loadGeoTIFF(uri)) | ||
collection = ee.ImageCollection(images) | ||
|
||
# Get an RGB image from the collection of bands. | ||
rgb = collection.toBands().rename(['B2', 'B3', 'B4', 'B5']) | ||
m = geemap.Map() | ||
m.center_object(rgb) | ||
m.add_layer(rgb, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 20000}, 'rgb') | ||
m | ||
# [END earthengine__image_collections02__cloud_collections] |
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 'Filtering Image Collections' page.""" | ||
|
||
# [START earthengine__image_collections03__filtering] | ||
# Load Landsat 8 data, filter by date, month, and bounds. | ||
collection = ( | ||
ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') | ||
# Three years of data | ||
.filterDate('2015-01-01', '2018-01-01') | ||
# Only Nov-Feb observations | ||
.filter(ee.Filter.calendarRange(11, 2, 'month')) | ||
# Intersecting ROI | ||
.filterBounds(ee.Geometry.Point(25.8544, -18.08874)) | ||
) | ||
|
||
# Also filter the collection by the CLOUD_COVER property. | ||
filtered = collection.filter(ee.Filter.eq('CLOUD_COVER', 0)) | ||
|
||
# Create two composites to check the effect of filtering by CLOUD_COVER. | ||
bad_composite = collection.mean() | ||
good_composite = filtered.mean() | ||
|
||
# Display the composites. | ||
m = geemap.Map() | ||
m.set_center(25.8544, -18.08874, 13) | ||
m.add_layer( | ||
bad_composite, | ||
{'bands': ['B3', 'B2', 'B1'], 'min': 0.05, 'max': 0.35, 'gamma': 1.1}, | ||
'Bad composite', | ||
) | ||
m.add_layer( | ||
good_composite, | ||
{'bands': ['B3', 'B2', 'B1'], 'min': 0.05, 'max': 0.35, 'gamma': 1.1}, | ||
'Good composite', | ||
) | ||
m | ||
# [END earthengine__image_collections03__filtering] |
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,57 @@ | ||
# 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 'Mapping' page.""" | ||
|
||
# [START earthengine__image_collections04__time_band] | ||
# Load a Landsat 8 collection for a single path-row, 2021 images only. | ||
collection = ( | ||
ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') | ||
.filterDate('2021', '2022') | ||
.filter(ee.Filter.eq('WRS_PATH', 44)) | ||
.filter(ee.Filter.eq('WRS_ROW', 34)) | ||
) | ||
|
||
|
||
# This function adds a band representing the image timestamp. | ||
def add_time(image): | ||
return image.addBands(image.getNumber('system:time_start')) | ||
|
||
|
||
# Map the function over the collection and display the result. | ||
display(collection.map(add_time)) | ||
# [END earthengine__image_collections04__time_band] | ||
|
||
# [START earthengine__image_collections04__if] | ||
# Load a Landsat 8 collection for a single path-row, 2021 images only. | ||
collection = ( | ||
ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') | ||
.filterDate('2021', '2022') | ||
.filter(ee.Filter.eq('WRS_PATH', 44)) | ||
.filter(ee.Filter.eq('WRS_ROW', 34)) | ||
) | ||
|
||
|
||
# This function uses a conditional statement to return the image if | ||
# the solar elevation > 40 degrees. Otherwise it returns a "zero image". | ||
def conditional(image): | ||
return ee.Algorithms.If( | ||
ee.Number(image.get('SUN_ELEVATION')).gt(40), image, ee.Image(0) | ||
) | ||
|
||
|
||
# Map the function over the collection and print the result. Expand the | ||
# collection and note that 7 of the 22 images are now "zero images'. | ||
display('Expand this to see the result', collection.map(conditional)) | ||
# [END earthengine__image_collections04__if] |
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,58 @@ | ||
# 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 'Reducing' page.""" | ||
|
||
# [START earthengine__image_collections05__median] | ||
# Load a Landsat 8 collection for a single path-row. | ||
collection = ( | ||
ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') | ||
.filter(ee.Filter.eq('WRS_PATH', 44)) | ||
.filter(ee.Filter.eq('WRS_ROW', 34)) | ||
.filterDate('2014-01-01', '2015-01-01') | ||
) | ||
|
||
# Compute a median image and display. | ||
median = collection.median() | ||
m = geemap.Map() | ||
m.set_center(-122.3578, 37.7726, 12) | ||
m.add_layer(median, {'bands': ['B4', 'B3', 'B2'], 'max': 0.3}, 'Median') | ||
m | ||
# [END earthengine__image_collections05__median] | ||
|
||
# [START earthengine__image_collections05__alt_median] | ||
# Reduce the collection with a median reducer. | ||
median = collection.reduce(ee.Reducer.median()) | ||
|
||
# Display the median image. | ||
m.add_layer( | ||
median, | ||
{'bands': ['B4_median', 'B3_median', 'B2_median'], 'max': 0.3}, | ||
'Also median', | ||
) | ||
m | ||
# [END earthengine__image_collections05__alt_median] | ||
|
||
# [START earthengine__image_collections05__short_median] | ||
# Reduce the collection with a median reducer. | ||
median = collection.reduce('median') | ||
|
||
# Display the median image. | ||
m.add_layer( | ||
median, | ||
{'bands': ['B4_median', 'B3_median', 'B2_median'], 'max': 0.3}, | ||
'Yet another median', | ||
) | ||
m | ||
# [END earthengine__image_collections05__short_median] |
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,56 @@ | ||
# 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 'Reducing' page.""" | ||
|
||
|
||
# [START earthengine__image_collections06__linear_fit] | ||
# This function adds a band representing the image timestamp. | ||
def add_time(image): | ||
return image.addBands( | ||
image.metadata('system:time_start') | ||
# Convert milliseconds from epoch to years to aid in | ||
# interpretation of the following trend calculation. | ||
.divide(1000 * 60 * 60 * 24 * 365) | ||
) | ||
|
||
|
||
# Load a MODIS collection, filter to several years of 16 day mosaics, | ||
# and map the time band function over it. | ||
collection = ( | ||
ee.ImageCollection('MODIS/006/MYD13A1') | ||
.filterDate('2004-01-01', '2010-10-31') | ||
.map(add_time) | ||
) | ||
|
||
# Select the bands to model with the independent variable first. | ||
trend = collection.select(['system:time_start', 'EVI']).reduce( | ||
# Compute the linear trend over time. | ||
ee.Reducer.linearFit() | ||
) | ||
|
||
# Display the trend with increasing slopes in green, decreasing in red. | ||
m.set_center(-96.943, 39.436, 5) | ||
m = geemap.Map() | ||
m.add_layer( | ||
trend, | ||
{ | ||
'min': 0, | ||
'max': [-100, 100, 10000], | ||
'bands': ['scale', 'scale', 'offset'], | ||
}, | ||
'EVI trend', | ||
) | ||
m | ||
# [END earthengine__image_collections06__linear_fit] |