Credits to Keiko Nomura
GEE App: https://nkeikon.users.earthengine.app/view/tanintharyi-oil-palm-and-rubber-map
Source code: https://github.com/nkeikon/tanintharyi
Reference: https://go.nature.com/2QBIrYe
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.
Important note: A key difference between folium and ipyleaflet is that ipyleaflet is built upon ipywidgets and allows bidirectional communication between the front-end and the backend enabling the use of the map to capture user input, while folium is meant for displaying static data only (source). Note that Google Colab currently does not support ipyleaflet (source). Therefore, if you are using geemap with Google Colab, you should use import geemap.eefolium
. If you are using geemap with binder or a local Jupyter notebook server, you can use import geemap
, which provides more functionalities for capturing user input (e.g., mouse-clicking and moving).
# Installs geemap package
import subprocess
try:
import geemap
except ImportError:
print('geemap package not installed. Installing ...')
subprocess.check_call(["python", '-m', 'pip', 'install', 'geemap'])
import geemap
# Authenticates and initializes Earth Engine
import ee
try:
ee.Initialize()
except Exception as e:
ee.Authenticate()
ee.Initialize()
The default basemap is Google Satellite
. Additional basemaps can be added using the Map.add_basemap()
function.
Map = geemap.Map(center=[40,-100], zoom=4)
Map.add_basemap('ROADMAP') # Add Google Map
Map
# Add Earth Engine data
area1 = ee.Image("users/nkeikon/myanmar_sr/area1_map")
area2 = ee.Image("users/nkeikon/myanmar_sr/area2_map")
tanintharyi = ee.FeatureCollection("users/nkeikon/myanmar_sr/TNI")
Map.centerObject(tanintharyi,10)
total = ee.ImageCollection([area1,area2]).mosaic()
palette =['ff0000',# palm (red)
'9933ff',#rubber (purple)
'008000',#other trees (green)
'lime',#shrub (lime)
'yellow',#bare (yellow)
'0000ff',#river (blue)
]
viz = {'min':1, 'max':6, 'palette':palette}
MAIN = 'Default map'
OILPALM = 'Oil palm only'
RUBBER = 'Rubber only'
OILPALM_RUBBER = 'Oil palm and rubber'
mainVis = total.visualize(**viz)
oilpalmVis = total.eq(1).selfMask().visualize(**{'palette':'red'})
rubberVis = total.eq(2).selfMask().visualize(**{'palette':'purple'})
oilpalm_rubberVis = oilpalmVis.blend(rubberVis)
Map.addLayer(mainVis, {}, 'Land cover')
Map.addLayer(oilpalmVis, {}, 'Oil palm only')
Map.addLayer(rubberVis, {}, 'Rubber only')
Map.addLayer(oilpalm_rubberVis, {}, 'Oil palm and rubber')
# Add widgets to the map
import ipywidgets as widgets
from ipyleaflet import WidgetControl
legend = widgets.HTML(
value='<img src="https://i.imgur.com/Ye2sgan.png">',
placeholder='Land cover',
descripition='Land cover'
)
citation = widgets.HTML(
value='Reference: <a href="https://go.nature.com/2QBIrYe">https://go.nature.com/2QBIrYe</a>',
placeholder='Land cover',
descripition='Land cover'
)
dropdown = widgets.Dropdown(
options=[('Land cover', 1), ('Old palm only', 2), ('Rubber only', 3), ('Oil palm and bubber only', 4), ('Show all layers', 5)],
value=5,
description='Select layer',
)
legend_control = WidgetControl(widget=legend, position='bottomleft')
citation_control = WidgetControl(widget=citation, position='bottomright')
dropdown_control = WidgetControl(widget=dropdown, position='topright')
all_layers = Map.layers
def on_change(change):
if change['type'] == 'change' and change['name'] == 'value':
if change['new'] == 5:
Map.layers = all_layers
else:
Map.layers = Map.layers[:3]
layer_index = change['new'] + 2
Map.add_layer(all_layers[layer_index])
dropdown.observe(on_change)
Map.add_control(legend_control)
Map.add_control(citation_control)
Map.add_control(dropdown_control)
Map