#!/usr/bin/env python
# coding: utf-8
#
# ## Install Earth Engine API and geemap
# Install the [Earth Engine Python API](https://developers.google.com/earth-engine/python_install) and [geemap](https://geemap.org). The **geemap** Python package is built upon the [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) and [folium](https://github.com/python-visualization/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](https://github.com/giswqs/geemap#dependencies), including earthengine-api, folium, and ipyleaflet.
# In[ ]:
# Installs geemap package
import subprocess
try:
import geemap
except ImportError:
print('Installing geemap ...')
subprocess.check_call(["python", '-m', 'pip', 'install', 'geemap'])
# In[ ]:
import ee
import geemap
# ## Create an interactive map
# The default basemap is `Google Maps`. [Additional basemaps](https://github.com/giswqs/geemap/blob/master/geemap/basemaps.py) can be added using the `Map.add_basemap()` function.
# In[ ]:
Map = geemap.Map(center=[40,-100], zoom=4)
Map
# ## Add Earth Engine Python script
# In[ ]:
# Add Earth Engine dataset
# Mark pixels where the elevation crosses 1000m value and compare
# that to pixels that are exactly equal to 1000m.
elev = ee.Image('CGIAR/SRTM90_V4')
# A zero-crossing is defined as any pixel where the right,
# bottom, or diagonal bottom-right pixel has the opposite sign.
image = elev.subtract(1000).zeroCrossing()
Map.setCenter(-121.68148, 37.50877, 13)
Map.addLayer(image, {'min': 0, 'max': 1, 'opacity': 0.5}, 'Crossing 1000m')
exact = elev.eq(1000)
Map.addLayer(exact.updateMask(exact), {'palette': 'red'}, 'Exactly 1000m')
# ## Display Earth Engine data layers
# In[ ]:
Map.addLayerControl() # This line is not needed for ipyleaflet-based Map.
Map