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')
As you see the code is simply displaying a static string. Is there a way to put marker properties values contained in the json file in the popup method?
Is there a way to put marker properties values contained in the json file in the popup method?
That is a common request and there are ways to accomplish that. It will depended how that information is coded in your JSON file. You have to loop your JSON and get the keys to construct the popups manually for now. Something like:
for name, row in myjson_properties.iterrows():
popup = 'Location {}'.format(row['LOCATION'])
Thanks. Would myjson_properties be a pandas dataframe here? Or maybe you meant iteritems instead of iterrows?
Thanks. Would myjson_properties be a pandas dataframe here? Or maybe you meant iteritems instead of iterrows?
Depends on how you read them :smile:
I am having troubles integrating your code into mine. I tried this:
for name, row in df.iterrows():
gj.add_child(folium.Popup(row['features']['properties']['NAME']))
but that displays the last item of the loop in all marker popups.
Then I tried a list comprehension:
gj.add_child(folium.Popup( [row['features']['properties']['NAME'] for name,row in df.iterrows()]))
but that brings an empty popup.
What am I missing here?
Try something alone those lines:
http://nbviewer.jupyter.org/gist/ocefpaf/1e6e23fa12dfa01d4134c37907bf46d2
Thanks, but that brings us to the other issue of not being able to have a layer control for the markers. That's why I was looking at the GeoJson() function in the first place.
I was trying to have points with popups and be able to toggle that layer on and off.
Putting Marker() in a loop won't allow to have it as a layer in the layer control panel if I am not wrong?
Take a look at the MarkerCluster and FeatureGroup classes. See also an example here:
https://ocefpaf.github.io/python4oceanographers/blog/2015/12/14/geopandas_folium/
Thanks a lot for your help.
FeatureGroup worked well. The map now has popups and a layer control. Here is the code for future reference:
import folium
import pandas
df=pandas.read_csv("Volcanoes_USA.txt")
map=folium.Map(location=[df['LAT'].mean(),df['LON'].mean()],zoom_start=6,tiles='MapQuest Open Aerial')
fg=folium.FeatureGroup(name="Pika")
for lat,lon,name,elev in zip(df['LAT'],df['LON'],df['NAME'],df['ELEV']):
fg.add_child(folium.Marker(location=[lat,lon],popup=(folium.Popup(name))
map.add_child(fg)
map.add_child(folium.LayerControl())
map.save(outfile='fg1.html')
Any reading through these, be careful about calling your map "map". map is a built-in function in Python and you would overwrite the name. Use "m", like in the folium docs to avoid confusion. ;) https://docs.python.org/3/library/functions.html#map
Most helpful comment
Thanks a lot for your help.
FeatureGroup worked well. The map now has popups and a layer control. Here is the code for future reference: