Kernel: Python 3
Introduction to Earth Engine and Geemap
A workshop at Nagoya University, Japan.
Useful links
Installing geemap
Press Ctrl + / to uncomment the following line to install geemap. Restart the kernel/runtime after installation.
In [ ]:
# pip install geemap
Earth Engine authentication
In [ ]:
import ee ee.Authenticate()
Following the instructions here to authenticate your Earth Engine account.
Creating interactive maps
In [ ]:
import geemap
In [ ]:
Map = geemap.Map()
In [ ]:
Map
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4, height=600) Map
In [ ]:
Map = geemap.Map(data_ctrl=False, toolbar_ctrl=False, draw_ctrl=False) Map
In [ ]:
Map = geemap.Map(lite_mode=True) Map
In [ ]:
Map.save("ipyleaflet.html")
Adding basemaps
Built-in basemaps
In [ ]:
import geemap
In [ ]:
Map = geemap.Map(basemap="HYBRID") Map
In [ ]:
Map.add_basemap("OpenTopoMap")
In [ ]:
for basemap in geemap.basemaps.keys(): print(basemap)
In [ ]:
len(geemap.basemaps)
Basemap GUI
In [ ]:
Map = geemap.Map() Map
Earth Engine data types
Image
Loading Earth Engine images
In [ ]:
image = ee.Image("USGS/SRTMGL1_003")
Visualizing Earth Engine images
In [ ]:
Map = geemap.Map(center=[21.79, 70.87], zoom=3) image = ee.Image("USGS/SRTMGL1_003") vis_params = { "min": 0, "max": 6000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } Map.addLayer(image, vis_params, "SRTM") Map
ImageCollection
Loading image collections
In [ ]:
collection = ee.ImageCollection("COPERNICUS/S2_SR")
Visualizing image collections
In [ ]:
Map = geemap.Map() collection = ee.ImageCollection("COPERNICUS/S2_SR") image = collection.median() vis = { "min": 0.0, "max": 3000, "bands": ["B4", "B3", "B2"], } Map.setCenter(83.277, 17.7009, 12) Map.addLayer(image, vis, "Sentinel-2") Map
Filtering image collections
In [ ]:
Map = geemap.Map() collection = ( ee.ImageCollection("COPERNICUS/S2_SR") .filterDate("2021-01-01", "2022-01-01") .filter(ee.Filter.lt("CLOUDY_PIXEL_PERCENTAGE", 5)) ) image = collection.median() vis = { "min": 0.0, "max": 3000, "bands": ["B4", "B3", "B2"], } Map.setCenter(83.277, 17.7009, 12) Map.addLayer(image, vis, "Sentinel-2") Map
Geometry
Geometry types
Creating Geometry objects
In [ ]:
Map = geemap.Map() point = ee.Geometry.Point([1.5, 1.5]) lineString = ee.Geometry.LineString([[-35, -10], [35, -10], [35, 10], [-35, 10]]) linearRing = ee.Geometry.LinearRing( [[-35, -10], [35, -10], [35, 10], [-35, 10], [-35, -10]] ) rectangle = ee.Geometry.Rectangle([-40, -20, 40, 20]) polygon = ee.Geometry.Polygon([[[-5, 40], [65, 40], [65, 60], [-5, 60], [-5, 60]]]) Map.addLayer(point, {}, "Point") Map.addLayer(lineString, {}, "LineString") Map.addLayer(linearRing, {}, "LinearRing") Map.addLayer(rectangle, {}, "Rectangle") Map.addLayer(polygon, {}, "Polygon") Map
In [ ]:
Map = geemap.Map() point = ee.Geometry.Point([1.5, 1.5]) lineString = ee.Geometry.LineString( [[-35, -10], [35, -10], [35, 10], [-35, 10]], None, False ) linearRing = ee.Geometry.LinearRing( [[-35, -10], [35, -10], [35, 10], [-35, 10], [-35, -10]], None, False ) rectangle = ee.Geometry.Rectangle([-40, -20, 40, 20], None, False) polygon = ee.Geometry.Polygon( [[[-5, 40], [65, 40], [65, 60], [-5, 60], [-5, 60]]], None, False ) Map.addLayer(point, {}, "Point") Map.addLayer(lineString, {}, "LineString") Map.addLayer(linearRing, {}, "LinearRing") Map.addLayer(rectangle, {}, "Rectangle") Map.addLayer(polygon, {}, "Polygon") Map
Using drawing tools
In [ ]:
if Map.user_roi is not None: print(Map.user_roi.getInfo())
Feature
Creating Feature objects
In [ ]:
# Create an ee.Geometry. polygon = ee.Geometry.Polygon( [[[-35, -10], [35, -10], [35, 10], [-35, 10], [-35, -10]]], None, False ) # Create a Feature from the Geometry. polyFeature = ee.Feature(polygon, {"foo": 42, "bar": "tart"})
In [ ]:
polyFeature.getInfo()
In [ ]:
Map = geemap.Map() Map.addLayer(polyFeature, {}, "feature") Map
In [ ]:
# Create a dictionary of properties, some of which may be computed values. props = {"foo": ee.Number(8).add(88), "bar": "nihao"} # Create a None geometry feature with the dictionary of properties. nowhereFeature = ee.Feature(None, props) nowhereFeature.getInfo()
Setting Feature properties
In [ ]:
# Make a feature and set some properties. feature = ( ee.Feature(ee.Geometry.Point([-122.22599, 37.17605])) .set("genus", "Sequoia") .set("species", "sempervirens") ) # Overwrite the old properties with a new dictionary. newDict = {"genus": "Brachyramphus", "presence": 1, "species": "marmoratus"} feature = feature.set(newDict) # Check the result. feature.getInfo()
Getting Feature properties
In [ ]:
prop = feature.get("species") prop.getInfo()
In [ ]:
props = feature.toDictionary() props.getInfo()
FeatureCollection
Loading feature collections
In [ ]:
Map = geemap.Map() fc = ee.FeatureCollection("TIGER/2016/Roads") Map.setCenter(-73.9596, 40.7688, 12) Map.addLayer(fc, {}, "Census roads") Map
Creating feature collections
In [ ]:
# Make a list of Features. features = [ ee.Feature(ee.Geometry.Rectangle(30.01, 59.80, 30.59, 60.15), {"name": "Voronoi"}), ee.Feature(ee.Geometry.Point(-73.96, 40.781), {"name": "Thiessen"}), ee.Feature(ee.Geometry.Point(6.4806, 50.8012), {"name": "Dirichlet"}), ] # Create a FeatureCollection from the list and print it. fromList = ee.FeatureCollection(features)
In [ ]:
fromList.getInfo()
Filtering feature collections
In [ ]:
Map = geemap.Map() states = ee.FeatureCollection("TIGER/2018/States") feat = states.filter(ee.Filter.eq("NAME", "Texas")) Map.addLayer(feat, {}, "Texas") Map.centerObject(feat) Map
In [ ]:
texas = feat.first() texas.toDictionary().getInfo()
In [ ]:
Map = geemap.Map() states = ee.FeatureCollection("TIGER/2018/States") fc = states.filter(ee.Filter.inList("NAME", ["California", "Oregon", "Washington"])) Map.addLayer(fc, {}, "West Coast") Map.centerObject(fc) Map
In [ ]:
region = Map.user_roi if region is None: region = ee.Geometry.BBox(-88.40, 29.88, -77.90, 35.39) fc = ee.FeatureCollection("TIGER/2018/States").filterBounds(region) Map.addLayer(fc, {}, "Southeastern U.S.") Map.centerObject(fc)
Visualizing feature collections
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) states = ee.FeatureCollection("TIGER/2018/States") Map.addLayer(states, {}, "US States") Map
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) states = ee.FeatureCollection("TIGER/2018/States") image = ee.Image().paint(states, 0, 3) Map.addLayer(image, {"palette": "red"}, "US States") Map
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) states = ee.FeatureCollection("TIGER/2018/States") style = {"color": "0000ffff", "width": 2, "lineType": "solid", "fillColor": "FF000080"} Map.addLayer(states.style(**style), {}, "US States") Map
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) states = ee.FeatureCollection("TIGER/2018/States") vis_params = { "color": "000000", "colorOpacity": 1, "pointSize": 3, "pointShape": "circle", "width": 2, "lineType": "solid", "fillColorOpacity": 0.66, } palette = ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"] Map.add_styled_vector( states, column="NAME", palette=palette, layer_name="Styled vector", **vis_params ) Map
Styling by attribute
In [ ]:
Map = geemap.Map(center=[28.00142, -81.7424], zoom=13) Map.add_basemap("HYBRID")
In [ ]:
types = [ "Freshwater Forested/Shrub Wetland", "Freshwater Emergent Wetland", "Freshwater Pond", "Estuarine and Marine Wetland", "Riverine", "Lake", "Estuarine and Marine Deepwater", "Other", ] colors = [ "#008837", "#7FC31C", "#688CC0", "#66C2A5", "#0190BF", "#13007C", "#007C88", "#B28653", ] fillColor = [c + "A8" for c in colors]
In [ ]:
fc = ee.FeatureCollection("projects/sat-io/open-datasets/NWI/wetlands/FL_Wetlands") styled_fc = geemap.ee_vector_style( fc, column="WETLAND_TY", labels=types, fillColor=fillColor, color="00000000" )
In [ ]:
Map.addLayer(styled_fc, {}, "NWI") Map.add_legend(title="Wetland Type", labels=types, colors=colors) Map
In [ ]:
fuels = [ "Coal", "Oil", "Gas", "Hydro", "Nuclear", "Solar", "Waste", "Wind", "Geothermal", "Biomass", ] colors = [ "000000", "593704", "BC80BD", "0565A6", "E31A1C", "FF7F00", "6A3D9A", "5CA2D1", "FDBF6F", "229A00", ]
In [ ]:
fc = ee.FeatureCollection("WRI/GPPD/power_plants").filter( ee.Filter.inList("fuel1", fuels) ) styled_fc = geemap.ee_vector_style(fc, column="fuel1", labels=fuels, color=colors)
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) Map.addLayer(styled_fc, {}, "Power Plants") Map.add_legend(title="Power Plant Fuel Type", labels=fuels, colors=colors) Map
In [ ]:
types = ["I", "U", "S", "M", "C", "O"] labels = ["Interstate", "U.S.", "State recognized", "Common Name", "County", "Other"] colors = ["E31A1C", "FF7F00", "6A3D9A", "000000", "FDBF6F", "229A00"] width = [8, 5, 4, 2, 1, 1]
In [ ]:
fc = ee.FeatureCollection("TIGER/2016/Roads") styled_fc = geemap.ee_vector_style( fc, column="rttyp", labels=types, color=colors, width=width )
In [ ]:
Map = geemap.Map(center=[40.7424, -73.9724], zoom=13) Map.addLayer(styled_fc, {}, "Census Roads") Map.add_legend(title="Route Type", labels=labels, colors=colors) Map
Earth Engine Data Catalog
Searching for datasets
In [ ]:
dataset_xyz = ee.Image("USGS/SRTMGL1_003") Map.addLayer(dataset_xyz, {}, "USGS/SRTMGL1_003")
In [ ]:
Map = geemap.Map() dem = ee.Image("USGS/SRTMGL1_003") vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } Map.addLayer(dem, vis_params, "SRTM DEM") Map
Using the datasets module
In [ ]:
from geemap.datasets import DATA
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) dataset = ee.Image(DATA.USGS_GAP_CONUS_2011) Map.addLayer(dataset, {}, "GAP CONUS") Map
In [ ]:
from geemap.datasets import get_metadata get_metadata(DATA.USGS_GAP_CONUS_2011)
Getting image metadata
In [ ]:
image = ee.Image("LANDSAT/LC09/C02/T1_L2/LC09_044034_20220503")
In [ ]:
image.bandNames().getInfo()
In [ ]:
image.select("SR_B1").projection().getInfo()
In [ ]:
image.select("SR_B1").projection().nominalScale().getInfo()
In [ ]:
image.propertyNames().getInfo()
In [ ]:
image.get("CLOUD_COVER").getInfo()
In [ ]:
image.get("DATE_ACQUIRED").getInfo()
In [ ]:
image.get("system:time_start").getInfo()
In [ ]:
date = ee.Date(image.get("system:time_start")) date.format("YYYY-MM-dd").getInfo()
In [ ]:
image.toDictionary().getInfo()
In [ ]:
props = geemap.image_props(image) props.getInfo()
Calculating descriptive statistics
In [ ]:
image = ee.Image("LANDSAT/LC09/C02/T1_L2/LC09_044034_20220503") geemap.image_min_value(image).getInfo()
In [ ]:
geemap.image_max_value(image).getInfo()
In [ ]:
geemap.image_mean_value(image).getInfo()
In [ ]:
geemap.image_stats(image).getInfo()
Using the inspector tool
In [ ]:
# Create an interactive map Map = geemap.Map(center=(40, -100), zoom=4) # Add Earth Engine datasets dem = ee.Image("USGS/SRTMGL1_003") landsat7 = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003").select( ["B1", "B2", "B3", "B4", "B5", "B7"] ) states = ee.FeatureCollection("TIGER/2018/States") # Set visualization parameters. vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } # Add Earth Engine layers to the map Map.addLayer(dem, vis_params, "SRTM DEM") Map.addLayer( landsat7, {"bands": ["B4", "B3", "B2"], "min": 20, "max": 200, "gamma": 2.0}, "Landsat 7", ) Map.addLayer(states, {}, "US States") # Display the map Map
Using the plotting tool
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) landsat7 = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003").select( ["B1", "B2", "B3", "B4", "B5", "B7"] ) landsat_vis = {"bands": ["B4", "B3", "B2"], "gamma": 1.4} Map.addLayer(landsat7, landsat_vis, "Landsat") hyperion = ee.ImageCollection("EO1/HYPERION").filter( ee.Filter.date("2016-01-01", "2017-03-01") ) hyperion_vis = { "min": 1000.0, "max": 14000.0, "gamma": 2.5, } Map.addLayer(hyperion, hyperion_vis, "Hyperion") Map
In [ ]:
Map.set_plot_options(add_marker_cluster=True, overlay=True)
Changing layer opacity
In [ ]:
Map = geemap.Map(center=(40, -100), zoom=4) dem = ee.Image("USGS/SRTMGL1_003") states = ee.FeatureCollection("TIGER/2018/States") vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } Map.addLayer(dem, vis_params, "SRTM DEM", True, 1) Map.addLayer(states, {}, "US States", True) Map
Visualizing raster data
Single-band images
In [ ]:
Map = geemap.Map(center=[12, 69], zoom=3) dem = ee.Image("USGS/SRTMGL1_003") vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } Map.addLayer(dem, vis_params, "SRTM DEM") Map
In [ ]:
vis_params = { "bands": ["elevation"], "palette": ["333399", " 00b2b2", " 99eb85", " ccbe7d", " 997c76", " ffffff"], "min": 0.0, "max": 6000.0, "opacity": 1.0, "gamma": 1.0, }
Multi-band images
In [ ]:
Map = geemap.Map() landsat7 = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003") vis_params = { "min": 20, "max": 200, "gamma": 2, "bands": ["B4", "B3", "B2"], } Map.addLayer(landsat7, vis_params, "Landsat 7") Map
Visualizing vector data
In [ ]:
Map = geemap.Map() states = ee.FeatureCollection("TIGER/2018/States") Map.addLayer(states, {}, "US States") Map
In [ ]:
vis_params = { "color": "ff0000ff", "width": 2, "lineType": "solid", "fillColor": "00000000", }
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) states = ee.FeatureCollection("TIGER/2018/States") Map.addLayer(states.style(**vis_params), {}, "US States") Map
Creating legends
Built-in legends
In [ ]:
legends = geemap.builtin_legends for legend in legends: print(legend)
In [ ]:
Map.add_legend(builtin_legend="NLCD")
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) Map.add_basemap("HYBRID") nlcd = ee.Image("USGS/NLCD_RELEASES/2019_REL/NLCD/2019") landcover = nlcd.select("landcover") Map.addLayer(landcover, {}, "NLCD Land Cover 2019") Map.add_legend( title="NLCD Land Cover Classification", builtin_legend="NLCD", height="465px" ) Map
Custom legends
In [ ]:
Map = geemap.Map(add_google_map=False) labels = ["One", "Two", "Three", "Four", "etc"] # colors can be defined using either hex code or RGB (0-255, 0-255, 0-255) colors = ["#8DD3C7", "#FFFFB3", "#BEBADA", "#FB8072", "#80B1D3"] # legend_colors = [(255, 0, 0), (127, 255, 0), (127, 18, 25), (36, 70, 180), (96, 68 123)] Map.add_legend(labels=labels, colors=colors, position="bottomright") Map
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) legend_dict = { "11 Open Water": "466b9f", "12 Perennial Ice/Snow": "d1def8", "21 Developed, Open Space": "dec5c5", "22 Developed, Low Intensity": "d99282", "23 Developed, Medium Intensity": "eb0000", "24 Developed High Intensity": "ab0000", "31 Barren Land (Rock/Sand/Clay)": "b3ac9f", "41 Deciduous Forest": "68ab5f", "42 Evergreen Forest": "1c5f2c", "43 Mixed Forest": "b5c58f", "51 Dwarf Scrub": "af963c", "52 Shrub/Scrub": "ccb879", "71 Grassland/Herbaceous": "dfdfc2", "72 Sedge/Herbaceous": "d1d182", "73 Lichens": "a3cc51", "74 Moss": "82ba9e", "81 Pasture/Hay": "dcd939", "82 Cultivated Crops": "ab6c28", "90 Woody Wetlands": "b8d9eb", "95 Emergent Herbaceous Wetlands": "6c9fb8", } nlcd = ee.Image("USGS/NLCD_RELEASES/2019_REL/NLCD/2019") landcover = nlcd.select("landcover") Map.addLayer(landcover, {}, "NLCD Land Cover 2019") Map.add_legend(title="NLCD Land Cover Classification", legend_dict=legend_dict) Map
Creating color bars
In [ ]:
Map = geemap.Map() dem = ee.Image("USGS/SRTMGL1_003") vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } Map.addLayer(dem, vis_params, "SRTM DEM") Map
In [ ]:
Map.add_colorbar(vis_params, label="Elevation (m)", layer_name="SRTM DEM")
In [ ]:
Map.add_colorbar( vis_params, label="Elevation (m)", layer_name="SRTM DEM", orientation="vertical" )
In [ ]:
Map.add_colorbar( vis_params, label="Elevation (m)", layer_name="SRTM DEM", orientation="vertical", transparent_bg=True, )
Displaying labels
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4, add_google_map=False) states = ee.FeatureCollection("TIGER/2018/States") style = {"color": "black", "fillColor": "00000000"} Map.addLayer(states.style(**style), {}, "US States") Map
In [ ]:
Map.add_labels( data=states, column="STUSPS", font_size="12pt", font_color="blue", font_family="arial", font_weight="bold", draggable=True, )
In [ ]:
Map.remove_labels()
In [ ]:
centroids = geemap.vector_centroids(states) df = geemap.ee_to_df(centroids) df
In [ ]:
Map.add_labels( data=df, column="STUSPS", font_size="12pt", font_color="blue", font_family="arial", font_weight="bold", x="longitude", y="latitude", ) Map
Split-panel maps
In [ ]:
Map = geemap.Map() Map.split_map(left_layer="HYBRID", right_layer="TERRAIN") Map
In [ ]:
Map = geemap.Map(center=(40, -100), zoom=4, height=600) nlcd_2001 = ee.Image("USGS/NLCD_RELEASES/2019_REL/NLCD/2001").select("landcover") nlcd_2019 = ee.Image("USGS/NLCD_RELEASES/2019_REL/NLCD/2019").select("landcover") left_layer = geemap.ee_tile_layer(nlcd_2001, {}, "NLCD 2001") right_layer = geemap.ee_tile_layer(nlcd_2019, {}, "NLCD 2019") Map.split_map(left_layer, right_layer) Map
Linked maps
In [ ]:
image = ( ee.ImageCollection("COPERNICUS/S2") .filterDate("2018-09-01", "2018-09-30") .map(lambda img: img.divide(10000)) .median() ) vis_params = [ {"bands": ["B4", "B3", "B2"], "min": 0, "max": 0.3, "gamma": 1.3}, {"bands": ["B8", "B11", "B4"], "min": 0, "max": 0.3, "gamma": 1.3}, {"bands": ["B8", "B4", "B3"], "min": 0, "max": 0.3, "gamma": 1.3}, {"bands": ["B12", "B12", "B4"], "min": 0, "max": 0.3, "gamma": 1.3}, ] labels = [ "Natural Color (B4/B3/B2)", "Land/Water (B8/B11/B4)", "Color Infrared (B8/B4/B3)", "Vegetation (B12/B11/B4)", ] geemap.linked_maps( rows=2, cols=2, height="300px", center=[38.4151, 21.2712], zoom=12, ee_objects=[image], vis_params=vis_params, labels=labels, label_position="topright", )
Timeseries inspector
Visualizing image collections
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) collection = ee.ImageCollection("USGS/NLCD_RELEASES/2019_REL/NLCD").select("landcover") vis_params = {"bands": ["landcover"]} years = collection.aggregate_array("system:index").getInfo() years
In [ ]:
Map.ts_inspector( left_ts=collection, right_ts=collection, left_names=years, right_names=years, left_vis=vis_params, right_vis=vis_params, width="80px", ) Map
Time slider
Visualizing vegetation data
In [ ]:
Map = geemap.Map() collection = ( ee.ImageCollection("MODIS/MCD43A4_006_NDVI") .filter(ee.Filter.date("2018-06-01", "2018-07-01")) .select("NDVI") ) vis_params = { "min": 0.0, "max": 1.0, "palette": [ "FFFFFF", "CE7E45", "DF923D", "F1B555", "FCD163", "99B718", "74A901", "66A000", "529400", "3E8601", "207401", "056201", "004C00", "023B01", "012E01", "011D01", "011301", ], } Map.add_time_slider(collection, vis_params, time_interval=2) Map
Visualizing weather data
In [ ]:
Map = geemap.Map() collection = ( ee.ImageCollection("NOAA/GFS0P25") .filterDate("2018-12-22", "2018-12-23") .limit(24) .select("temperature_2m_above_ground") ) vis_params = { "min": -40.0, "max": 35.0, "palette": ["blue", "purple", "cyan", "green", "yellow", "red"], } labels = [str(n).zfill(2) + ":00" for n in range(0, 24)] Map.add_time_slider(collection, vis_params, labels=labels, time_interval=1, opacity=0.8) Map
Visualizing Sentinel-2 imagery
In [ ]:
Map = geemap.Map(center=[37.75, -122.45], zoom=12) collection = ( ee.ImageCollection("COPERNICUS/S2_SR") .filterBounds(ee.Geometry.Point([-122.45, 37.75])) .filterMetadata("CLOUDY_PIXEL_PERCENTAGE", "less_than", 10) ) vis_params = {"min": 0, "max": 4000, "bands": ["B8", "B4", "B3"]} Map.add_time_slider(collection, vis_params) Map
Shaded relief maps
In [ ]:
import geemap.colormaps as cm Map = geemap.Map() dem = ee.Image("USGS/SRTMGL1_003") hillshade = ee.Terrain.hillshade(dem) vis = {"min": 0, "max": 6000, "palette": cm.palettes.terrain} blend = geemap.blend(top_layer=dem, top_vis=vis) Map.addLayer(hillshade, {}, "Hillshade") Map.addLayer(blend, {}, "Shaded relief") Map.add_colorbar(vis, label="Elevation (m)") Map.setCenter(91.4206, 27.3225, zoom=9) Map
In [ ]:
left_layer = geemap.ee_tile_layer(blend, {}, "Shaded relief") right_layer = geemap.ee_tile_layer(hillshade, {}, "Hillshade") Map.split_map(left_layer, right_layer)
In [ ]:
nlcd = ee.Image("USGS/NLCD_RELEASES/2019_REL/NLCD/2019").select("landcover") nlcd_vis = {"bands": ["landcover"]} blend = geemap.blend(nlcd, dem, top_vis=nlcd_vis, expression="a*b") Map.addLayer(blend, {}, "Blend NLCD") Map.add_legend(builtin_legend="NLCD", title="NLCD Land Cover") Map.setCenter(-118.1310, 35.6816, 10)
Elevation contours
In [ ]:
import geemap.colormaps as cm
In [ ]:
Map = geemap.Map() image = ee.Image("USGS/SRTMGL1_003") hillshade = ee.Terrain.hillshade(image) Map.addLayer(hillshade, {}, "Hillshade") Map
In [ ]:
vis_params = {"min": 0, "max": 5000, "palette": cm.palettes.dem} Map.addLayer(image, vis_params, "dem", True, 0.5) Map.add_colorbar(vis_params, label="Elevation (m)")
In [ ]:
contours = geemap.create_contours(image, 0, 5000, 100, region=None) Map.addLayer(contours, {"palette": "black"}, "contours") Map.setCenter(-119.3678, 37.1671, 12)
Image descriptive statistics
In [ ]:
Map = geemap.Map() centroid = ee.Geometry.Point([-122.4439, 37.7538]) image = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR").filterBounds(centroid).first() vis = {"min": 0, "max": 3000, "bands": ["B5", "B4", "B3"]} Map.centerObject(centroid, 8) Map.addLayer(image, vis, "Landsat-8") Map
In [ ]:
image.propertyNames().getInfo()
In [ ]:
image.get("CLOUD_COVER").getInfo() # 0.05
In [ ]:
props = geemap.image_props(image) props.getInfo()
In [ ]:
stats = geemap.image_stats(image, scale=30) stats.getInfo()
Zonal statistics with Earth Engine
Zonal statistics
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) # Add NASA SRTM dem = ee.Image("USGS/SRTMGL1_003") dem_vis = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } Map.addLayer(dem, dem_vis, "SRTM DEM") # Add 5-year Landsat TOA composite landsat = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003") landsat_vis = {"bands": ["B4", "B3", "B2"], "gamma": 1.4} Map.addLayer(landsat, landsat_vis, "Landsat", False) # Add US Census States states = ee.FeatureCollection("TIGER/2018/States") style = {"fillColor": "00000000"} Map.addLayer(states.style(**style), {}, "US States") Map
In [ ]:
out_dem_stats = "dem_stats.csv" geemap.zonal_stats( dem, states, out_dem_stats, stat_type="MEAN", scale=1000, return_fc=False )
In [ ]:
out_landsat_stats = "landsat_stats.csv" geemap.zonal_stats( landsat, states, out_landsat_stats, stat_type="MEAN", scale=1000, return_fc=False, )
Zonal statistics by group
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) # Add NLCD data dataset = ee.Image("USGS/NLCD_RELEASES/2019_REL/NLCD/2019") landcover = dataset.select("landcover") Map.addLayer(landcover, {}, "NLCD 2019") # Add US census states states = ee.FeatureCollection("TIGER/2018/States") style = {"fillColor": "00000000"} Map.addLayer(states.style(**style), {}, "US States") # Add NLCD legend Map.add_legend(title="NLCD Land Cover", builtin_legend="NLCD") Map
In [ ]:
nlcd_stats = "nlcd_stats.csv" geemap.zonal_stats_by_group( landcover, states, nlcd_stats, stat_type="SUM", denominator=1e6, decimal_places=2, )
In [ ]:
nlcd_stats = "nlcd_stats_pct.csv" geemap.zonal_stats_by_group( landcover, states, nlcd_stats, stat_type="PERCENTAGE", denominator=1e6, decimal_places=2, )
Zonal statistics with two images
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) dem = ee.Image("USGS/3DEP/10m") vis = {"min": 0, "max": 4000, "palette": "terrain"} Map.addLayer(dem, vis, "DEM") Map
In [ ]:
landcover = ee.Image("USGS/NLCD_RELEASES/2019_REL/NLCD/2019").select("landcover") Map.addLayer(landcover, {}, "NLCD 2019") Map.add_legend(title="NLCD Land Cover Classification", builtin_legend="NLCD")
In [ ]:
stats = geemap.image_stats_by_zone(dem, landcover, reducer="MEAN") stats
In [ ]:
stats.to_csv("mean.csv", index=False)
In [ ]:
geemap.image_stats_by_zone(dem, landcover, out_csv="std.csv", reducer="STD")
Extracting pixel values
Extracting values to points
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) dem = ee.Image("USGS/SRTMGL1_003") landsat7 = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003") vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } Map.addLayer( landsat7, {"bands": ["B4", "B3", "B2"], "min": 20, "max": 200, "gamma": 2}, "Landsat 7", ) Map.addLayer(dem, vis_params, "SRTM DEM", True, 1) Map
In [ ]:
in_shp = "us_cities.shp" url = "https://github.com/giswqs/data/raw/main/us/us_cities.zip" geemap.download_file(url)
In [ ]:
in_fc = geemap.shp_to_ee(in_shp) Map.addLayer(in_fc, {}, "Cities")
In [ ]:
geemap.extract_values_to_points(in_fc, dem, out_fc="dem.shp")
In [ ]:
geemap.shp_to_gdf("dem.shp")
In [ ]:
geemap.extract_values_to_points(in_fc, landsat7, "landsat.csv")
In [ ]:
geemap.csv_to_df("landsat.csv")
Extracting pixel values along a transect
In [ ]:
Map = geemap.Map(center=[40, -100], zoom=4) Map.add_basemap("TERRAIN") image = ee.Image("USGS/SRTMGL1_003") vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } Map.addLayer(image, vis_params, "SRTM DEM", True, 0.5) Map
In [ ]:
line = Map.user_roi if line is None: line = ee.Geometry.LineString( [[-120.2232, 36.3148], [-118.9269, 36.7121], [-117.2022, 36.7562]] ) Map.addLayer(line, {}, "ROI") Map.centerObject(line)
In [ ]:
reducer = "mean" transect = geemap.extract_transect( image, line, n_segments=100, reducer=reducer, to_pandas=True ) transect
In [ ]:
geemap.line_chart( data=transect, x="distance", y="mean", markers=True, x_label="Distance (m)", y_label="Elevation (m)", height=400, )
In [ ]:
transect.to_csv("transect.csv")
Interactive region reduction
In [ ]:
Map = geemap.Map() collection = ( ee.ImageCollection("MODIS/061/MOD13A2") .filterDate("2015-01-01", "2019-12-31") .select("NDVI") ) image = collection.toBands() ndvi_vis = { "min": 0.0, "max": 9000.0, "palette": "ndvi", } Map.addLayer(image, {}, "MODIS NDVI Time-series") Map.addLayer(image.select(0), ndvi_vis, "First image") Map
In [ ]:
dates = geemap.image_dates(collection).getInfo() dates
In [ ]:
len(dates)
In [ ]:
Map.set_plot_options(add_marker_cluster=True) Map.roi_reducer = ee.Reducer.mean() Map
In [ ]:
Map.extract_values_to_points("ndvi.csv")
Mapping available image count
In [ ]:
collection = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2") image = geemap.image_count( collection, region=None, start_date="2021-01-01", end_date="2022-01-01", clip=False )
In [ ]:
Map = geemap.Map() vis = {"min": 0, "max": 60, "palette": "coolwarm"} Map.addLayer(image, vis, "Image Count") Map.add_colorbar(vis, label="Landsat 8 Image Count") countries = ee.FeatureCollection(geemap.examples.get_ee_path("countries")) style = {"color": "00000088", "width": 1, "fillColor": "00000000"} Map.addLayer(countries.style(**style), {}, "Countries") Map
Cloud-free composites
In [ ]:
Map = geemap.Map() collection = ee.ImageCollection("LANDSAT/LC08/C02/T1").filterDate( "2021-01-01", "2022-01-01" ) composite = ee.Algorithms.Landsat.simpleComposite(collection) vis_params = {"bands": ["B5", "B4", "B3"], "max": 128} Map.setCenter(-122.3578, 37.7726, 10) Map.addLayer(composite, vis_params, "TOA composite") Map
In [ ]:
customComposite = ee.Algorithms.Landsat.simpleComposite( **{"collection": collection, "percentile": 30, "cloudScoreRange": 5} ) Map.addLayer(customComposite, vis_params, "Custom TOA composite") Map.setCenter(-105.4317, 52.5536, 11)
In [ ]:
vis_params = [ {"bands": ["B4", "B3", "B2"], "min": 0, "max": 128}, {"bands": ["B5", "B4", "B3"], "min": 0, "max": 128}, {"bands": ["B7", "B6", "B4"], "min": 0, "max": 128}, {"bands": ["B6", "B5", "B2"], "min": 0, "max": 128}, ] labels = [ "Natural Color (4, 3, 2)", "Color Infrared (5, 4, 3)", "Short-Wave Infrared (7, 6 4)", "Agriculture (6, 5, 2)", ]
In [ ]:
geemap.linked_maps( rows=2, cols=2, height="300px", center=[37.7726, -122.1578], zoom=9, ee_objects=[composite], vis_params=vis_params, labels=labels, label_position="topright", )
Exporting images
In [ ]:
Map = geemap.Map() image = ee.Image("LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318").select( ["B5", "B4", "B3"] ) vis_params = {"min": 0, "max": 0.5, "gamma": [0.95, 1.1, 1]} Map.centerObject(image) Map.addLayer(image, vis_params, "Landsat") Map
In [ ]:
region = ee.Geometry.BBox(-122.5955, 37.5339, -122.0982, 37.8252) fc = ee.FeatureCollection(region) style = {"color": "ffff00ff", "fillColor": "00000000"} Map.addLayer(fc.style(**style), {}, "ROI") Map
To local drive
In [ ]:
geemap.ee_export_image(image, filename="landsat.tif", scale=30, region=region)
In [ ]:
projection = image.select(0).projection().getInfo() projection
In [ ]:
crs = projection["crs"] crs_transform = projection["transform"]
In [ ]:
geemap.ee_export_image( image, filename="landsat_crs.tif", crs=crs, crs_transform=crs_transform, region=region, )
In [ ]:
geemap.download_ee_image(image, filename="landsat_full.tif", scale=60)
In [ ]:
fishnet = geemap.fishnet(image.geometry(), rows=4, cols=4, delta=0.5) style = {"color": "ffff00ff", "fillColor": "00000000"} Map.addLayer(fishnet.style(**style), {}, "Fishnet") Map
In [ ]:
geemap.download_ee_image_tiles( image, fishnet, out_dir=".", prefix="landsat_", crs="EPSG:3857", scale=30 )
To Google Drive
In [ ]:
geemap.ee_export_image_to_drive( image, description="landsat", folder="export", region=region, scale=30 )
To Asset
In [ ]:
assetId = "landsat_sfo" geemap.ee_export_image_to_asset( image, description="landsat", assetId=assetId, region=region, scale=30 )
To NumPy array
In [ ]:
region = ee.Geometry.BBox(-122.5003, 37.7233, -122.3410, 37.8026) rgb_img = geemap.ee_to_numpy(image, region=region)
In [ ]:
print(rgb_img.shape)
In [ ]:
import matplotlib.pyplot as plt rgb_img_test = (255 * ((rgb_img[:, :, 0:3]) + 0.2)).astype("uint8") plt.imshow(rgb_img_test) plt.show()
Exporting image collections
In [ ]:
point = ee.Geometry.Point(-99.2222, 46.7816) collection = ( ee.ImageCollection("USDA/NAIP/DOQQ") .filterBounds(point) .filterDate("2008-01-01", "2018-01-01") .filter(ee.Filter.listContains("system:band_names", "N")) )
In [ ]:
collection.aggregate_array("system:index").getInfo()
To local drive
In [ ]:
out_dir = os.path.expanduser("~/Downloads") geemap.ee_export_image_collection(collection, out_dir=out_dir, scale=10)
To Google Drive
In [ ]:
geemap.ee_export_image_collection_to_drive(collection, folder="export", scale=10)
To Assets
In [ ]:
geemap.ee_export_image_collection_to_asset(collection)
Exporting feature collections
In [ ]:
Map = geemap.Map() fc = ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017").filter( ee.Filter.eq("wld_rgn", "Europe") ) Map.addLayer(fc, {}, "Europe") Map.centerObject(fc, 3) Map
To local drive
In [ ]:
geemap.ee_to_shp(fc, filename="europe.shp", selectors=None)
In [ ]:
geemap.ee_export_vector(fc, filename="europe2.shp")
In [ ]:
geemap.ee_to_geojson(fc, filename="europe.geojson")
In [ ]:
geemap.ee_to_csv(fc, filename="europe.csv")
In [ ]:
gdf = geemap.ee_to_gdf(fc) gdf
In [ ]:
df = geemap.ee_to_df(fc) df
To Google Drive
In [ ]:
geemap.ee_export_vector_to_drive( fc, description="europe", fileFormat="SHP", folder="export" )
To Asset
In [ ]:
geemap.ee_export_vector_to_asset(fc, description="Exporting Europe", assetId="europe")
Exporting maps
In [ ]:
Map = geemap.Map() image = ee.Image("USGS/SRTMGL1_003") vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } Map.addLayer(image, vis_params, "SRTM DEM", True) Map
In [ ]:
Map.to_html( filename="mymap.html", title="Earth Engine Map", width="100%", height="800px" )