Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a popup to a geojson layer in folium

I have the following code which uses this geojson file as input.

import folium
markers_geojson='volcanoes_json.geojson'  
map=folium.Map(location=[0,0],zoom_start=6,tiles='Mapbox bright')
map.add_child(folium.GeoJson(data=open(markers_geojson),name='Volcano').add_child(folium.Popup("A plain pop up string")))    
map.save(outfile='test5.html')

The above code produces a leaflet map with markers. The problem is it currently displays a static string (i.e. "A plain pop up string") in the popup message. I don't know how to show a value from the geojson properties (e.g.the STATUS property).

Anyone have any idea of how to implement this?

like image 926
multigoodverse Avatar asked Oct 14 '25 07:10

multigoodverse


1 Answers

You need to loop through the file. The file mentioned below is a simple file that has three columns of lat, long and elevation.

If you create a simple text file in this format, this code loops through a file and adds them. It gets the columns which have lat, long, elevation and in the popup creates a dynamic popup.

data = pandas.read_csv("Volcanoes.txt")
lat = list(data["LAT"])
lon = list(data["LON"])
elev = list(data["ELEV"])

# make a color code for elevation
def color_producer(elevation):
    if elevation < 1000:
        return 'green'
    elif 1000 <= elevation < 3000:
        return 'orange'
    else:
        return 'red'

# set the base map
map = folium.Map(location=[47.0, -122.5], zoom_start=12)

# add an additional tile layer
map.add_tile_layer("Stamen Toner")

fgv = folium.FeatureGroup(name="Volcanoes")

# loop through and plot everything
for lt, ln, el in zip(lat, lon, elev):
    fgv.add_child(folium.CircleMarker(location=[lt, ln], radius = 6, popup=str(el)+" m",
    fill_color=color_producer(el), fill=True,  color = 'grey', fill_opacity=0.7))

fgp = folium.FeatureGroup(name="Population")

# add a map of shading by population
fgp.add_child(folium.GeoJson(data=open('world.json', 'r', encoding='utf-8-sig').read(),
style_function=lambda x: {'fillColor':'green' if x['properties']['POP2005'] < 10000000
else 'orange' if 10000000 <= x['properties']['POP2005'] < 20000000 else 'red'}))



# add the layers
map.add_child(fgv)
map.add_child(fgp)
map.add_child(folium.LayerControl())
like image 174
Bryan Butler Avatar answered Oct 16 '25 21:10

Bryan Butler