Path: blob/master/docs/notebooks/00_geemap_key_features.ipynb
2313 views
Kernel: Python 3
Table of Contents
- 1Â Â Create an interactive map
- 2Â Â Add basemaps
- 3Â Â Add WMS and XYZ tile layers
- 4Â Â Add Earth Engine data layers
- 5Â Â Search Earth Engine data catalog
- 6Â Â Search Earth Engine API documentation
- 7Â Â Use Inspector tool
- 8Â Â Use Plotting tool
- 9Â Â Create a split-panel map
- 10Â Â Add marker cluster
- 11Â Â Add customized legends
- 12Â Â Use Drawing tools
- 13Â Â Convert JavaScripts to Python
- 14Â Â Use shapefiles
- 15Â Â Create Landsat timelapse
- 16Â Â Use time-series inspector
- 17Â Â Export images
Uncomment the following line to install geemap if needed.
In [ ]:
# !pip install geemap
In [ ]:
import ee import geemap
Create an interactive map
In [ ]:
Map = geemap.Map(center=(40, -100), zoom=4) Map
Add basemaps
In [ ]:
Map = geemap.Map() Map
In [ ]:
Map.add_basemap("HYBRID")
In [ ]:
Map.add_basemap("OpenTopoMap")
In [ ]:
Map = geemap.Map() Map.basemap_demo() Map
Add WMS and XYZ tile layers
In [ ]:
Map = geemap.Map() Map
In [ ]:
# https://viewer.nationalmap.gov/services/ url = "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}" Map.add_tile_layer(url, name="Google Satellite", attribution="Google")
In [ ]:
naip_url = "https://services.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer?" Map.add_wms_layer( url=naip_url, layers="0", name="NAIP Imagery", format="image/png", shown=True )
Add Earth Engine data layers
In [ ]:
Map = geemap.Map() Map
In [ ]:
# Add Earth Engine dataset dem = ee.Image("USGS/SRTMGL1_003") landcover = ee.Image("ESA/GLOBCOVER_L4_200901_200912_V2_3").select("landcover") landsat7 = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003") 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 Map Map.addLayer(dem, vis_params, "SRTM DEM", True, 0.5) Map.addLayer(landcover, {}, "Land cover") Map.addLayer( landsat7, {"bands": ["B4", "B3", "B2"], "min": 20, "max": 200}, "Landsat 7" ) Map.addLayer(states, {}, "US States")
Search Earth Engine data catalog
In [ ]:
Map = geemap.Map() Map
In [ ]:
Map.search_locations
In [ ]:
Map.search_loc_geom
In [ ]:
location = Map.search_loc_geom # print(location.getInfo())
Search Earth Engine API documentation
In [ ]:
geemap.ee_search()
Use Inspector tool
In [ ]:
Map = geemap.Map() # Add Earth Engine dataset dem = ee.Image("USGS/SRTMGL1_003") landcover = ee.Image("ESA/GLOBCOVER_L4_200901_200912_V2_3").select("landcover") landsat7 = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003") 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 Map Map.addLayer(dem, vis_params, "SRTM DEM", True, 0.5) Map.addLayer(landcover, {}, "Land cover") Map.addLayer( landsat7, {"bands": ["B4", "B3", "B2"], "min": 20, "max": 200}, "Landsat 7" ) Map.addLayer(states, {}, "US States") Map
Use Plotting tool
In [ ]:
Map = geemap.Map() landsat7 = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003").select([0, 1, 2, 3, 4, 6]) landsat_vis = {"bands": ["B4", "B3", "B2"], "gamma": 1.4} Map.addLayer(landsat7, landsat_vis, "LE7_TOA_5YEAR/1999_2003") 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, "EO1/HYPERION") Map
In [ ]:
Map.set_plot_options(plot_type="bar", add_marker_cluster=True)
Create a split-panel map
In [ ]:
Map = geemap.Map() Map.split_map(left_layer="HYBRID", right_layer="ROADMAP") Map
In [ ]:
Map = geemap.Map() Map.split_map( left_layer="NLCD 2016 CONUS Land Cover", right_layer="NLCD 2001 CONUS Land Cover" ) Map
In [ ]:
nlcd_2001 = ee.Image("USGS/NLCD/NLCD2001").select("landcover") nlcd_2016 = ee.Image("USGS/NLCD/NLCD2016").select("landcover") left_layer = geemap.ee_tile_layer(nlcd_2001, {}, "NLCD 2001") right_layer = geemap.ee_tile_layer(nlcd_2016, {}, "NLCD 2016") Map = geemap.Map() Map.split_map(left_layer, right_layer) Map
Add marker cluster
In [ ]:
import geemap import json import os import requests from geemap import geojson_to_ee, ee_to_geojson from ipyleaflet import GeoJSON, Marker, MarkerCluster
In [ ]:
Map = geemap.Map() Map
In [ ]:
file_path = os.path.join(os.getcwd(), "us_cities.json") if not os.path.exists(file_path): url = "https://github.com/gee-community/geemap/raw/master/examples/data/us_cities.json" r = requests.get(url) with open(file_path, "w") as f: f.write(r.content.decode("utf-8")) with open(file_path) as f: json_data = json.load(f)
In [ ]:
maker_cluster = MarkerCluster( markers=[ Marker(location=feature["geometry"]["coordinates"][::-1]) for feature in json_data["features"] ], name="Markers", )
In [ ]:
Map.add_layer(maker_cluster)
In [ ]:
ee_fc = geojson_to_ee(json_data) Map.addLayer(ee_fc, {}, "US Cities EE")
Add customized legends
In [ ]:
Map = geemap.Map() Map.add_basemap("HYBRID") landcover = ee.Image("USGS/NLCD/NLCD2016").select("landcover") Map.addLayer(landcover, {}, "NLCD Land Cover") Map.add_legend(builtin_legend="NLCD") Map
In [ ]:
Map = geemap.Map() Map.add_basemap("HYBRID") Map.add_basemap("FWS NWI Wetlands") Map.add_legend(builtin_legend="NWI") Map
In [ ]:
Map = geemap.Map() 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", } landcover = ee.Image("USGS/NLCD/NLCD2016").select("landcover") Map.addLayer(landcover, {}, "NLCD Land Cover") Map.add_legend(legend_title="NLCD Land Cover Classification", legend_dict=legend_dict) Map
In [ ]:
# https://developers.google.com/earth-engine/datasets/catalog/MODIS_051_MCD12Q1 Map = geemap.Map() ee_class_table = """ Value Color Description 0 1c0dff Water 1 05450a Evergreen needleleaf forest 2 086a10 Evergreen broadleaf forest 3 54a708 Deciduous needleleaf forest 4 78d203 Deciduous broadleaf forest 5 009900 Mixed forest 6 c6b044 Closed shrublands 7 dcd159 Open shrublands 8 dade48 Woody savannas 9 fbff13 Savannas 10 b6ff05 Grasslands 11 27ff87 Permanent wetlands 12 c24f44 Croplands 13 a5a5a5 Urban and built-up 14 ff6d4c Cropland/natural vegetation mosaic 15 69fff8 Snow and ice 16 f9ffa4 Barren or sparsely vegetated 254 ffffff Unclassified """ landcover = ee.Image("MODIS/051/MCD12Q1/2013_01_01").select("Land_Cover_Type_1") Map.setCenter(6.746, 46.529, 2) Map.addLayer(landcover, {}, "MODIS Land Cover") legend_dict = geemap.legend_from_ee(ee_class_table) Map.add_legend(legend_title="MODIS Global Land Cover", legend_dict=legend_dict) Map
Use Drawing tools
In [ ]:
Map = geemap.Map() Map
In [ ]:
# Add Earth Engine dataset image = ee.Image("USGS/SRTMGL1_003") # Set visualization parameters. vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } # Add Earth Engine DEM to map Map.addLayer(image, vis_params, "SRTM DEM") states = ee.FeatureCollection("TIGER/2018/States") Map.addLayer(states, {}, "US States")
In [ ]:
Map.draw_features
Convert JavaScripts to Python
You can simply copy and paste your GEE JavaScripts into a code block wrapped with trip quotes and pass it to a variable.
For example, you can grab GEE JavaScripts from GEE Documentation.
In [ ]:
js_snippet = """ // Load an image. var image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318'); // Define the visualization parameters. var vizParams = { bands: ['B5', 'B4', 'B3'], min: 0, max: 0.5, gamma: [0.95, 1.1, 1] }; // Center the map and display the image. Map.setCenter(-122.1899, 37.5010, 10); // San Francisco Bay Map.addLayer(image, vizParams, 'false color composite'); """
In [ ]:
geemap.js_snippet_to_py( js_snippet, add_new_cell=True, import_ee=True, import_geemap=True, show_map=True )
In [ ]:
import ee import geemap Map = geemap.Map() # Load an image. image = ee.Image("LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318") # Define the visualization parameters. vizParams = {"bands": ["B5", "B4", "B3"], "min": 0, "max": 0.5, "gamma": [0.95, 1.1, 1]} # Center the map and display the image. Map.setCenter(-122.1899, 37.5010, 10) # San Francisco Bay Map.addLayer(image, vizParams, "False color composite") Map
In [ ]:
import ee import geemap Map = geemap.Map() # Load an image. image = ee.Image("LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318") # Define the visualization parameters. vizParams = {"bands": ["B5", "B4", "B3"], "min": 0, "max": 0.5, "gamma": [0.95, 1.1, 1]} # Center the map and display the image. Map.setCenter(-122.1899, 37.5010, 10) # San Francisco Bay Map.addLayer(image, vizParams, "False color composite") Map
In [ ]:
import ee import geemap Map = geemap.Map() ee.Initialize() # Load an image. image = ee.Image("LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318") # Define the visualization parameters. vizParams = {"bands": ["B5", "B4", "B3"], "min": 0, "max": 0.5, "gamma": [0.95, 1.1, 1]} # Center the map and display the image. Map.setCenter(-122.1899, 37.5010, 10) # San Francisco Bay Map.addLayer(image, vizParams, "False color composite") Map
In [ ]:
js_snippet = """ // Load an image. var image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318'); // Create an NDWI image, define visualization parameters and display. var ndwi = image.normalizedDifference(['B3', 'B5']); var ndwiViz = {min: 0.5, max: 1, palette: ['00FFFF', '0000FF']}; Map.addLayer(ndwi, ndwiViz, 'NDWI', false); """
In [ ]:
geemap.js_snippet_to_py(js_snippet)
In [ ]:
import ee import geemap Map = geemap.Map() # Load an image. image = ee.Image("LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318") # Create an NDWI image, define visualization parameters and display. ndwi = image.normalizedDifference(["B3", "B5"]) ndwiViz = {"min": 0.5, "max": 1, "palette": ["00FFFF", "0000FF"]} Map.addLayer(ndwi, ndwiViz, "NDWI", False) Map
In [ ]:
import ee import geemap Map = geemap.Map() # Load an image. image = ee.Image("LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318") # Create an NDWI image, define visualization parameters and display. ndwi = image.normalizedDifference(["B3", "B5"]) ndwiViz = {"min": 0.5, "max": 1, "palette": ["00FFFF", "0000FF"]} Map.addLayer(ndwi, ndwiViz, "NDWI", False) Map
In [ ]:
import ee import geemap Map = geemap.Map() ee.Initialize() # Load an image. image = ee.Image("LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318") # Create an NDWI image, define visualization parameters and display. ndwi = image.normalizedDifference(["B3", "B5"]) ndwiViz = {"min": 0.5, "max": 1, "palette": ["00FFFF", "0000FF"]} Map.addLayer(ndwi, ndwiViz, "NDWI", False) Map
Use shapefiles
In [ ]:
Map = geemap.Map() Map
In [ ]:
countries_shp = "../data/countries.shp" countries = geemap.shp_to_ee(countries_shp)
In [ ]:
countries_shp = "../data/countries.shp" countries = geemap.shp_to_ee(countries_shp) Map.addLayer(countries, {}, "Countries")
In [ ]:
states_shp = "../data/us_states.shp" states = geemap.shp_to_ee(states_shp) Map.addLayer(states, {}, "US States")
In [ ]:
cities_shp = "../data/us_cities.shp" cities = geemap.shp_to_ee(cities_shp) Map.addLayer(cities, {}, "US Cities")
In [ ]:
geemap.ee_to_shp(countries, filename="../data/countries_new.shp")
In [ ]:
geemap.ee_export_vector(states, filename="../data/states.csv")
Create Landsat timelapse
In [ ]:
Map = geemap.Map() Map
In [ ]:
label = "Urban Growth in Las Vegas" Map.add_landsat_ts_gif( label=label, start_year=1985, bands=["Red", "Green", "Blue"], font_color="white", frames_per_second=10, progress_bar_color="blue", )
Use time-series inspector
In [ ]:
naip_ts = geemap.naip_timeseries(start_year=2009, end_year=2018)
In [ ]:
layer_names = ["NAIP " + str(year) for year in range(2009, 2019)] print(layer_names)
In [ ]:
naip_vis = {"bands": ["N", "R", "G"]}
In [ ]:
Map = geemap.Map() Map.ts_inspector( left_ts=naip_ts, right_ts=naip_ts, left_names=layer_names, right_names=layer_names, left_vis=naip_vis, right_vis=naip_vis, ) Map
Export images
In [ ]:
Map = geemap.Map() Map
In [ ]:
image = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003") landsat_vis = {"bands": ["B4", "B3", "B2"], "gamma": 1.4} Map.addLayer(image, landsat_vis, "LE7_TOA_5YEAR/1999_2003", True, 0.7)
In [ ]:
# Draw any shapes on the map using the Drawing tools before executing this code block feature = Map.draw_last_feature if feature is None: geom = ee.Geometry.Polygon( [ [ [-115.413031, 35.889467], [-115.413031, 36.543157], [-114.034328, 36.543157], [-114.034328, 35.889467], [-115.413031, 35.889467], ] ] ) feature = ee.Feature(geom, {}) roi = feature.geometry()
In [ ]:
out_dir = os.path.join(os.path.expanduser("~"), "Downloads") filename = os.path.join(out_dir, "landsat.tif")
In [ ]:
geemap.ee_export_image( image, filename=filename, scale=90, region=roi, file_per_band=False )
In [ ]:
geemap.ee_export_image( image, filename=filename, scale=90, region=roi, file_per_band=True )
In [ ]:
loc = ee.Geometry.Point(-99.2222, 46.7816) collection = ( ee.ImageCollection("USDA/NAIP/DOQQ") .filterBounds(loc) .filterDate("2008-01-01", "2020-01-01") .filter(ee.Filter.listContains("system:band_names", "N")) )
In [ ]:
out_dir = os.path.join(os.path.expanduser("~"), "Downloads")
In [ ]:
geemap.ee_export_image_collection(collection, out_dir=out_dir)