Order Layers behind HeatMapWithTime in Folium

故事扮演 提交于 2021-01-01 09:21:51

问题


I created this map of the Middle East in folium. I am very happy with how it looks: https://javierparada.github.io

The only problem is that one of the background layers is blocking the HeatMap. I don't know how to organize the layers so that the HeatMap is shown on top of the folium.raster_layers.ImageOverlay that contains the Land Use Changes.

Thanks!

# Leaflet’s CRS: EPSG3857 
# colormap= lambda x: (1, 0, 1, 1) #R,G,B,alpha,
# tiles='stamentoner',
# play with z-index
# m.keep_in_front(m.add_child(hm)

### Basemap
m = folium.Map([35, 41], tiles='stamentoner', zoom_start=6)

colors = {0:'blue', 1:'blue', 2:'blue', 3:'blue', 4:'white', 5:'black', 6:'blue', 7:'red', 8:'green'}

### Polygons

## Countries
syria = Choropleth(geo_data=c_borders, name="Countries",show=False).add_to(m)

## Governorates
governorates = Choropleth(geo_data=g_borders, name="Governorates",show=False).add_to(m)

### Markers

## Refugee camps
for each in refugee_camps.iterrows():
    folium.Marker(location = [each[1]['Latitude'],each[1]['Longitude']],
                  popup = "<b>" + each[1]['Name'] + "</b>" + "\n Refugee Camp in "+ each[1]['AdministrativeDivision']+", "+each[1]['Country'],
                  icon=folium.Icon(color='blue',icon='home',prefix='fa'),
                  clustered_marker = True).add_to(m)
## IDP camps
for each in idp_camps.iterrows():
    folium.Marker(location = [each[1]['Latitude'],each[1]['Longitude']],
                  popup = "<b>" + each[1]['Name'] + "</b>" + "\n IDP Camp in "+ each[1]['ADM1']+", "+each[1]['Country'],
                  icon=folium.Icon(color='purple',icon='home',prefix='fa'),
                  clustered_marker = True).add_to(m)

### Add different backgrounds

## Open Street Map
osm = folium.TileLayer(tiles="OpenStreetMap").add_to(m)

## Terrain
terrain = folium.TileLayer(tiles="stamenterrain").add_to(m)
     
## Satellite image
tile = folium.TileLayer(
        tiles = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
        attr = 'Esri',
        name = 'Esri Satellite',
        overlay = False,
        control = True).add_to(m)

## Land Use Changes
folium.raster_layers.ImageOverlay(
    image=dataimage,
    bounds=[[ymin, xmin], [ymax, xmax]],
    colormap = lambda x: (0 if x==0 else 0 if x==1 else 0 if x==2 else 0 if x==3 else 255 if x==4 else 0 if x==5 else 0 if x==6 else 200 if x==7 else 0,
          100 if x==0 else 100 if x==1 else 100 if x==2 else 100 if x==3 else 255 if x==4 else 50 if x==5 else 100 if x==6 else 0 if x==7 else 200,
          255 if x==0 else 255 if x==1 else 255 if x==2 else 255 if x==3 else 255 if x==4 else 0 if x==5 else 255 if x==6 else 0 if x==7 else 0,1),
    name= 'MODIS Transitions 2009 vs. 2017',
    opacity=.8,
    zindex=1,
    overlay=False,
    mercator_project=True).add_to(m)

### UCDP HeatMap
hm = HeatMapWithTime(data=list(data.values()), # coordinates
                     index=list(data.keys()), # years 
                     name = 'UCDP events',
                     radius=10,
                     auto_play=False,
                     max_opacity=1).add_to(m)

### Title
loc = 'Land Use Change induced by Conflict and Displacement along the Syria-Turkey border'
title_html = '''
             <h3 align="center" style="font-size:16px"><b>{}</b></h3>
             '''.format(loc)  
#m.get_root().html.add_child(folium.Element(title_html))

### Layer Control
folium.LayerControl().add_to(m)

### Export
m.save('html/index.html')

来源:https://stackoverflow.com/questions/65152407/order-layers-behind-heatmapwithtime-in-folium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!