2

I can not display properly a geojson file in folium using the GeoJson function. My file is made with geopandas, has some tags as fields and also a geometry field. In each row, in the field of geometry, it has a LineString (shapely object) in epsg:4326 with similar coords to the one of the center of the map of folium. The code to save my dataframe that later I read:

df.to_file('/path_to_json_file/filename.json',driver='GeoJSON')

What I tried:

import folium
import geopandas as gpd

map = folium.map.LegacyMap(location=[43.263667, -2.923202],zoom_start=14)
file = '/path_to_json_file/filename.json'
df1 = gpd.read_file(file)

#to have more info about the dataframe
print(df1.loc[0,'geometry'].coords[0:3])
>>>[(43.2614429399418, -2.927346548046099), (43.261432339487435, -2.927343485313923), (43.26142161221715, -2.927341407690571)]
print(df1.crs)
>>>{'init': 'epsg:4326'}

# 2 possibilities I tried:
folium.GeoJson(json.load(open(file)),name='geojson').add_to(map)
folium.GeoJson(file,name='geojson').add_to(map)

folium.LayerControl().add_to(map)
map.save("folium_map.html")

The thing is if I try only to display a row using folium's PolyLine instead, it works fine:

folium.PolyLine(df1.loc[0,'geometry'].coords[:]).add_to(map)

Maybe I have to declare the GeoJson in other way? I check the official documentation, but it does not appear how to pass the dataframe exactly. Checking the description of the function it seems it accepts using the path of the file like I do. The code executes without error but when I display the html it does not appear any line of the dataframe. My version of folium is v0.5.0

  • I use v0.5.0, too. I try your script on my geojson file and it works correctly. Maybe geometries of linestrings are corrupted. I also saved the file as another geojson file using GeoPandas, it worked again. – Kadir Şahbaz Aug 2 '18 at 13:04
  • If there is a line/lines which has invalid geometry, try to add all lines to folium seperately (in a for loop) to detect. – Kadir Şahbaz Aug 2 '18 at 13:10
  • Ok, I will try to print one by one to check if everything is ok, but I make operations in my program with all those lines and they seem to work well. Thx for the help – Aurelie Navir Aug 2 '18 at 18:03
  • Well, it seems that if I print all lines row by row it works well. Don't know what could be the problem... Maybe too much info to print in open street map at once using only this method? They are like 18 lines about 3-4km long each. – Aurelie Navir Aug 4 '18 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.