leaflet – Troubleshooting Plotting Earth Engine Images with Folium Using Python

foliumgoogle-earth-enginegoogle-earth-engine-python-apileaflettiles

I am trying to plot an earth engine image using Python and the Folium package as described in the Python docs for Earth Engine. I tried running the example code given and I can't seem get the image to show up when saving as an HTML and opening in the browser. The getMapID() method always returns a token as empty string as in: mapID['token'] = ''. Does anyone know a way to get Earth Engine map tiles to show up on a Folium interactive map?

I also tried using the folium_gee functions by mccarthyryanc and ran into the same issues, the basemap shows up but not the tiles.

# Import libraries.
import ee
import folium

# Initialize the Earth Engine library.
ee.Initialize()

# Define a method for displaying Earth Engine image tiles to folium map.
def add_ee_layer(self, eeImageObject, visParams, name):
  mapID = ee.Image(eeImageObject).getMapId(visParams)
  folium.raster_layers.TileLayer(
    tiles = "https://earthengine.googleapis.com/map/"+mapID['mapid']+
      "/{z}/{x}/{y}?token="+mapID['token'],
    attr = "Map Data © Google Earth Engine",
    name = name,
    overlay = True,
    control = True
  ).add_to(self)

# Add EE drawing method to folium.
folium.Map.add_ee_layer = add_ee_layer

# Fetch an elevation model.
dem = ee.Image('USGS/SRTMGL1_003')

# Set visualization parameters.
visParams = {'min':0, 'max':3000, 'palette':['225ea8','41b6c4','a1dab4','ffffcc']}

# Create a folium map object.
myMap = folium.Map(location=[20, 0], zoom_start=3, height=500)

# Add the elevation model to the map object.
myMap.add_ee_layer(dem, visParams, 'DEM')

# Add a layer control panel to the map.
myMap.add_child(folium.LayerControl())

# Display the map.
myMap.save("myMap.html")

Best Answer

Please note that the current recommended method for defining the tiles argument is as follows:

# Define a method for displaying Earth Engine image tiles to folium map.
def add_ee_layer(self, eeImageObject, visParams, name):
  map_id_dict = ee.Image(eeImageObject).getMapId(visParams)
  folium.raster_layers.TileLayer(
    tiles = map_id_dict['tile_fetcher'].url_format,
    attr = "Map Data &copy; <a href='https://earthengine.google.com/'>Google Earth Engine</a>",
    name = name,
    overlay = True,
    control = True
  ).add_to(self)

I see you found a workaround, but does this resolve your issue as well?