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]:
# 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]:
import ee
import geemap

Create an interactive map

In [13]:
Map = geemap.Map(center=(40, -100), zoom=4)
Map.add_minimap(position='bottomright')
Map

Add tile layers

For example, you can Google Map tile layer:

In [14]:
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]:
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]:
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]:
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]:
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
In [19]:
print(coordinates)
[]

SplitMap control

In [20]:
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
In [21]:
import geemap
Map = geemap.Map()
Map.split_map(left_layer='HYBRID', right_layer='ESRI')
Map