-
Notifications
You must be signed in to change notification settings - Fork 743
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: 638045876
- Loading branch information
1 parent
9378e47
commit dc26b67
Showing
3 changed files
with
190 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,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 | ||
# | ||
# https://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 'Compositing and mosaicking' page. | ||
|
||
# [START earthengine__image_collections07__mosaic] | ||
# Load four 2012 NAIP quarter quads, different locations. | ||
naip_2012 = ( | ||
ee.ImageCollection('USDA/NAIP/DOQQ') | ||
.filterBounds( | ||
ee.Geometry.Rectangle(-71.17965, 42.35125, -71.08824, 42.40584) | ||
) | ||
.filterDate('2012-01-01', '2012-12-31') | ||
) | ||
|
||
# Spatially mosaic the images in the collection and display. | ||
mosaic = naip_2012.mosaic() | ||
m = geemap.Map() | ||
m.set_center(-71.12532, 42.3712, 12) | ||
m.add_layer(mosaic, {}, 'spatial mosaic') | ||
# [END earthengine__image_collections07__mosaic] | ||
|
||
# [START earthengine__image_collections07__composite] | ||
# Load three NAIP quarter quads in the same location, different times. | ||
naip_2004_2012 = ( | ||
ee.ImageCollection('USDA/NAIP/DOQQ') | ||
.filterBounds(ee.Geometry.Point(-71.08841, 42.39823)) | ||
.filterDate('2004-07-01', '2012-12-31') | ||
.select(['R', 'G', 'B']) | ||
) | ||
|
||
# Temporally composite the images with a maximum value function. | ||
composite = naip_2004_2012.max() | ||
m.set_center(-71.12532, 42.3712, 12) | ||
m.add_layer(composite, {}, 'max value composite') | ||
m | ||
# [END earthengine__image_collections07__composite] |
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,53 @@ | ||
# 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 | ||
# | ||
# https://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 'Compositing and mosaicking' page. | ||
|
||
# [START earthengine__image_collections08__masked_mosaic] | ||
# Load a NAIP quarter quad, display. | ||
naip = ee.Image('USDA/NAIP/DOQQ/m_4207148_nw_19_1_20120710') | ||
m = geemap.Map() | ||
m.set_center(-71.0915, 42.3443, 14) | ||
m.add_layer(naip, {}, 'NAIP DOQQ') | ||
|
||
# Create the NDVI and NDWI spectral indices. | ||
ndvi = naip.normalizedDifference(['N', 'R']) | ||
ndwi = naip.normalizedDifference(['G', 'N']) | ||
|
||
# Create some binary images from thresholds on the indices. | ||
# This threshold is designed to detect bare land. | ||
bare_1 = ndvi.lt(0.2).And(ndwi.lt(0.3)) | ||
# This detects bare land with lower sensitivity. It also detects shadows. | ||
bare_2 = ndvi.lt(0.2).And(ndwi.lt(0.8)) | ||
|
||
# Define visualization parameters for the spectral indices. | ||
ndvi_viz = {'min': -1, 'max': 1, 'palette': ['FF0000', '00FF00']} | ||
ndwi_viz = {'min': 0.5, 'max': 1, 'palette': ['00FFFF', '0000FF']} | ||
|
||
# Mask and mosaic visualization images. The last layer is on top. | ||
mosaic = ee.ImageCollection([ | ||
# NDWI > 0.5 is water. Visualize it with a blue palette. | ||
ndwi.updateMask(ndwi.gte(0.5)).visualize(**ndwi_viz), | ||
# NDVI > 0.2 is vegetation. Visualize it with a green palette. | ||
ndvi.updateMask(ndvi.gte(0.2)).visualize(**ndvi_viz), | ||
# Visualize bare areas with shadow (bare_2 but not bare_1) as gray. | ||
bare_2.updateMask(bare_2.And(bare_1.Not())).visualize(palette=['AAAAAA']), | ||
# Visualize the other bare areas as white. | ||
bare_1.updateMask(bare_1).visualize(palette=['FFFFFF']), | ||
]).mosaic() | ||
m.add_layer(mosaic, {}, 'Visualization mosaic') | ||
m | ||
# [END earthengine__image_collections08__masked_mosaic] |
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,87 @@ | ||
# 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 | ||
# | ||
# https://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 - Compositing and mosaicking' page. | ||
|
||
|
||
# [START earthengine__image_collections085__quality_mosaic] | ||
# Define a function that scales and masks Landsat 8 surface reflectance images. | ||
def prep_sr_l8(image): | ||
# Develop masks for unwanted pixels (fill, cloud, cloud shadow). | ||
qa_mask = image.select('QA_PIXEL').bitwiseAnd(int('11111', 2)).eq(0) | ||
saturation_mask = image.select('QA_RADSAT').eq(0) | ||
|
||
# Helper function to create image from scaling factors. | ||
def get_factor_img(factor_names): | ||
factor_list = image.toDictionary().select(factor_names).values() | ||
return ee.Image.constant(factor_list) | ||
|
||
# Apply the scaling factors to the appropriate bands. | ||
scale_img = get_factor_img( | ||
['REFLECTANCE_MULT_BAND_.|TEMPERATURE_MULT_BAND_ST_B10'] | ||
) | ||
offset_img = get_factor_img( | ||
['REFLECTANCE_ADD_BAND_.|TEMPERATURE_ADD_BAND_ST_B10'] | ||
) | ||
scaled = image.select('SR_B.|ST_B10').multiply(scale_img).add(offset_img) | ||
|
||
# Replace original bands with scaled bands and apply masks. | ||
return ( | ||
image.addBands(scaled, None, True) | ||
.updateMask(qa_mask) | ||
.updateMask(saturation_mask) | ||
) | ||
|
||
|
||
# This function masks clouds and adds quality bands to Landsat 8 images. | ||
def add_quality_bands(image): | ||
# Normalized difference vegetation index. | ||
ndvi = image.normalizedDifference(['SR_B5', 'SR_B4']) | ||
# Image timestamp as milliseconds since Unix epoch. | ||
millis = ( | ||
ee.Image(image.getNumber('system:time_start')).rename('millis').toFloat() | ||
) | ||
return prep_sr_l8(image).addBands([ndvi, millis]) | ||
|
||
|
||
# Load a 2014 Landsat 8 ImageCollection. | ||
# Map the cloud masking and quality band function over the collection. | ||
collection = ( | ||
ee.ImageCollection('LANDSAT/LC08/C02/T1_L2') | ||
.filterDate('2014-06-01', '2014-12-31') | ||
.map(add_quality_bands) | ||
) | ||
|
||
# Create a cloud-free, most recent value composite. | ||
recent_value_composite = collection.qualityMosaic('millis') | ||
|
||
# Create a greenest pixel composite. | ||
greenest_pixel_composite = collection.qualityMosaic('nd') | ||
|
||
# Display the results. | ||
m = geemap.Map() | ||
m.set_center(-122.374, 37.8239, 12) # San Francisco Bay | ||
viz_params = {'bands': ['SR_B5', 'SR_B4', 'SR_B3'], 'min': 0, 'max': 0.4} | ||
m.add_layer(recent_value_composite, viz_params, 'Recent value composite') | ||
m.add_layer(greenest_pixel_composite, viz_params, 'Greenest pixel composite') | ||
|
||
# Compare to a cloudy image in the collection. | ||
cloudy = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140825') | ||
m.add_layer( | ||
cloudy, {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 0.4}, 'Cloudy' | ||
) | ||
m | ||
# [END earthengine__image_collections085__quality_mosaic] |