Geemap and ipyleaflet
Install Earth Engine API and geemap¶
Install the Earth Engine Python API and geemap. The geemap Python package is built upon the ipyleaflet and folium packages and implements several methods for interacting with Earth Engine data layers, such as Map.addLayer()
, Map.setCenter()
, and Map.centerObject()
.
The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its dependencies, including earthengine-api, folium, and ipyleaflet.
In [11]:
Copied!
# Installs geemap package
import subprocess
try:
import geemap
except ImportError:
print('geemap package not installed. Installing ...')
subprocess.check_call(["python", '-m', 'pip', 'install', 'geemap'])
# Installs geemap package
import subprocess
try:
import geemap
except ImportError:
print('geemap package not installed. Installing ...')
subprocess.check_call(["python", '-m', 'pip', 'install', 'geemap'])
In [12]:
Copied!
import ee
import geemap
import ee
import geemap
Create an interactive map¶
In [13]:
Copied!
Map = geemap.Map(center=(40, -100), zoom=4)
Map.add_minimap(position='bottomright')
Map
Map = geemap.Map(center=(40, -100), zoom=4)
Map.add_minimap(position='bottomright')
Map
Map(center=[40, -100], controls=(WidgetControl(options=['position'], widget=HBox(children=(ToggleButton(value=…
Add tile layers¶
For example, you can Google Map tile layer:
In [14]:
Copied!
url = 'https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}'
Map.add_tile_layer(url, name='Google Map', attribution='Google')
url = 'https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}'
Map.add_tile_layer(url, name='Google Map', attribution='Google')
Add Google Terrain tile layer:
In [15]:
Copied!
url = 'https://mt1.google.com/vt/lyrs=p&x={x}&y={y}&z={z}'
Map.add_tile_layer(url, name='Google Terrain', attribution='Google')
url = 'https://mt1.google.com/vt/lyrs=p&x={x}&y={y}&z={z}'
Map.add_tile_layer(url, name='Google Terrain', attribution='Google')
Add WMS layers¶
More WMS layers can be found at https://viewer.nationalmap.gov/services/.
For example, you can add NAIP imagery.
In [16]:
Copied!
url = 'https://services.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer?'
Map.add_wms_layer(url=url, layers='0', name='NAIP Imagery', format='image/png')
url = 'https://services.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer?'
Map.add_wms_layer(url=url, layers='0', name='NAIP Imagery', format='image/png')
Add USGS 3DEP Elevation Dataset
In [17]:
Copied!
url = 'https://elevation.nationalmap.gov/arcgis/services/3DEPElevation/ImageServer/WMSServer?'
Map.add_wms_layer(
url=url, layers='3DEPElevation:None', name='3DEP Elevation', format='image/png'
)
url = 'https://elevation.nationalmap.gov/arcgis/services/3DEPElevation/ImageServer/WMSServer?'
Map.add_wms_layer(
url=url, layers='3DEPElevation:None', name='3DEP Elevation', format='image/png'
)
Capture user inputs¶
In [18]:
Copied!
import geemap
from ipywidgets import Label
from ipyleaflet import Marker
Map = geemap.Map(center=(40, -100), zoom=4)
label = Label()
display(label)
coordinates = []
def handle_interaction(**kwargs):
latlon = kwargs.get('coordinates')
if kwargs.get('type') == 'mousemove':
label.value = str(latlon)
elif kwargs.get('type') == 'click':
coordinates.append(latlon)
Map.add_layer(Marker(location=latlon))
Map.on_interaction(handle_interaction)
Map
import geemap
from ipywidgets import Label
from ipyleaflet import Marker
Map = geemap.Map(center=(40, -100), zoom=4)
label = Label()
display(label)
coordinates = []
def handle_interaction(**kwargs):
latlon = kwargs.get('coordinates')
if kwargs.get('type') == 'mousemove':
label.value = str(latlon)
elif kwargs.get('type') == 'click':
coordinates.append(latlon)
Map.add_layer(Marker(location=latlon))
Map.on_interaction(handle_interaction)
Map
Label(value='')
Map(center=[40, -100], controls=(WidgetControl(options=['position'], widget=HBox(children=(ToggleButton(value=…
In [19]:
Copied!
print(coordinates)
print(coordinates)
[]
SplitMap control¶
In [20]:
Copied!
import geemap
from ipyleaflet import *
Map = geemap.Map(center=(47.50, -101), zoom=7)
right_layer = WMSLayer(
url='https://ndgishub.nd.gov/arcgis/services/Imagery/AerialImage_ND_2017_CIR/ImageServer/WMSServer?',
layers='AerialImage_ND_2017_CIR',
name='AerialImage_ND_2017_CIR',
format='image/png',
)
left_layer = WMSLayer(
url='https://ndgishub.nd.gov/arcgis/services/Imagery/AerialImage_ND_2018_CIR/ImageServer/WMSServer?',
layers='AerialImage_ND_2018_CIR',
name='AerialImage_ND_2018_CIR',
format='image/png',
)
control = SplitMapControl(left_layer=left_layer, right_layer=right_layer)
Map.add_control(control)
Map.add_control(LayersControl(position='topright'))
Map.add_control(FullScreenControl())
Map
import geemap
from ipyleaflet import *
Map = geemap.Map(center=(47.50, -101), zoom=7)
right_layer = WMSLayer(
url='https://ndgishub.nd.gov/arcgis/services/Imagery/AerialImage_ND_2017_CIR/ImageServer/WMSServer?',
layers='AerialImage_ND_2017_CIR',
name='AerialImage_ND_2017_CIR',
format='image/png',
)
left_layer = WMSLayer(
url='https://ndgishub.nd.gov/arcgis/services/Imagery/AerialImage_ND_2018_CIR/ImageServer/WMSServer?',
layers='AerialImage_ND_2018_CIR',
name='AerialImage_ND_2018_CIR',
format='image/png',
)
control = SplitMapControl(left_layer=left_layer, right_layer=right_layer)
Map.add_control(control)
Map.add_control(LayersControl(position='topright'))
Map.add_control(FullScreenControl())
Map
Map(center=[47.5, -101], controls=(WidgetControl(options=['position'], widget=HBox(children=(ToggleButton(value…
In [21]:
Copied!
import geemap
Map = geemap.Map()
Map.split_map(left_layer='HYBRID', right_layer='ESRI')
Map
import geemap
Map = geemap.Map()
Map.split_map(left_layer='HYBRID', right_layer='ESRI')
Map
Map(center=[40, -100], controls=(WidgetControl(options=['position'], widget=HBox(children=(ToggleButton(value=…
Last update:
2022-05-23