From 9baf0ae535258d193ee7c529787dde9b3825fb7c Mon Sep 17 00:00:00 2001 From: Steve Greenberg Date: Tue, 12 Sep 2023 10:39:37 -0700 Subject: [PATCH] Convert API code snippets from JS to Py PiperOrigin-RevId: 564766700 --- samples/python/apidocs/ee_image_pixelarea.py | 75 ++++++++++++++++++ samples/python/apidocs/ee_image_sample.py | 82 ++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 samples/python/apidocs/ee_image_pixelarea.py create mode 100644 samples/python/apidocs/ee_image_sample.py diff --git a/samples/python/apidocs/ee_image_pixelarea.py b/samples/python/apidocs/ee_image_pixelarea.py new file mode 100644 index 000000000..2ecb79a94 --- /dev/null +++ b/samples/python/apidocs/ee_image_pixelarea.py @@ -0,0 +1,75 @@ +# Copyright 2023 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. + +# [START earthengine__apidocs__ee_image_pixelarea] +# Create a pixel area image. Pixel values are square meters based on +# a given CRS and scale (or CRS transform). +pixel_area = ee.Image.pixelArea() + +# The default projection is WGS84 with 1-degree scale. +display('Pixel area default projection', pixel_area.projection()) + +# When inspecting the output in the Code Editor map, the scale of analysis is +# determined by the zoom level. As you zoom in and out, you'll notice that the +# area of the clicked pixel changes. To set a specific pixel scale when +# performing a computation, provide an argument to the `scale` or +# `crsTransform` parameters whenever a function gives you the option. +m = geemap.Map() +m.add_ee_layer(pixel_area, None, 'Pixel area for inspection', False) + +# The "area" band produced by the `pixel_area` function can be useful for +# calculating the area of a certain condition of another image. For example, +# here we use the sum reducer to determine the area above 2250m in the North +# Cascades ecoregion, according to a 30m digital elevation model. + +# Import a DEM and subset the "elevation" band. +elev = ee.Image('NASA/NASADEM_HGT/001').select('elevation') + +# Define a high elevation mask where pixels with elevation greater than 2250m +# are set to 1, otherwise 0. +high_elev_mask = elev.gt(2250) + +# Apply the high elevation mask to the pixel area image. +high_elev_area = pixel_area.updateMask(high_elev_mask) + +# Import an ecoregion feature collection and filter it by ecoregion name. +ecoregion = ee.FeatureCollection('RESOLVE/ECOREGIONS/2017').filter( + 'ECO_NAME == "North Cascades conifer forests"' +) + +# Display the ecoregion and high elevation area. +m.set_center(-121.127, 48.389, 7) +m.add_ee_layer(ecoregion, None, 'North Cascades ecoregion') +m.add_ee_layer( + high_elev_area.clip(ecoregion), {'palette': 'yellow'}, 'High elevation area' +) +display(m) + +# Sum the area of high elevation pixels in the North Cascades ecoregion. +area = high_elev_area.reduceRegion( + reducer=ee.Reducer.sum(), + geometry=ecoregion, + crs=elev.projection(), # DEM coordinate reference system + crsTransform=elev.projection().getInfo()['transform'], # DEM grid alignment + maxPixels=1e8, +) + +# Fetch the summed area property from the resulting dictionary and convert +# square meters to square kilometers. +square_meters = area.getNumber('area') +square_kilometers = square_meters.divide(1e6) + +display('Square meters above 2250m elevation', square_meters) +display('Square kilometers above 2250m elevation', square_kilometers) +# [END earthengine__apidocs__ee_image_pixelarea] diff --git a/samples/python/apidocs/ee_image_sample.py b/samples/python/apidocs/ee_image_sample.py new file mode 100644 index 000000000..5da4b0ea6 --- /dev/null +++ b/samples/python/apidocs/ee_image_sample.py @@ -0,0 +1,82 @@ +# Copyright 2023 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. + +# [START earthengine__apidocs__ee_image_sample] +# Demonstrate extracting pixels from an image as features with +# ee.Image.sample(), and show how the features are aligned with the pixels. + +# An image with one band of elevation data. +image = ee.Image('CGIAR/SRTM90_V4') +vis_min = 1620 +vis_max = 1650 +m = geemap.Map() +m.add_ee_layer(image, {'min': vis_min, 'max': vis_max}, 'SRTM') + +# Region to sample. +region = ee.Geometry.Polygon( + [[ + [-110.006, 40.002], + [-110.006, 39.999], + [-109.995, 39.999], + [-109.995, 40.002], + ]], + None, + False, +) +# Show region on the map. +m.set_center(-110, 40, 16) + +m.add_ee_layer(ee.FeatureCollection([region]).style(color='00FF0022')) + +# Perform sampling convert image pixels to features. +samples = image.sample( + region=region, + # Default (False) is no geometries in the output. + # When set to True, each feature has a Point geometry at the center of the + # image pixel. + geometries=True, + # The scale is not specified, so the resolution of the image will be used, + # and there is a feature for every pixel. If we give a scale parameter, the + # image will be resampled and there will be more or fewer features. + # + # scale=200, +) + + +def scale_point_size(feature): + elevation = feature.getNumber('elevation') + point_size = elevation.unitScale(vis_min, vis_max).multiply(15) + feature.set('style', {'pointSize': point_size}) + return feature + + +# Visualize sample data using ee.FeatureCollection.style(). +styled = samples.map(scale_point_size).style( + color='000000FF', + fillColor='00000000', + styleProperty='style', + neighborhood=6, # increase to correctly draw large points +) +m.add_ee_layer(styled) +display(m) + +# Each sample feature has a point geometry and a property named 'elevation' +# corresponding to the band named 'elevation' of the image. If there are +# multiple bands they will become multiple properties. This will print: +# +# geometry: Point (-110.01, 40.00) +# properties: +# elevation: 1639 +display(samples.first()) +# [END earthengine__apidocs__ee_image_sample]