0

I am trying to display on a folium map a set of polygons from a geodataframe. The code returns no errors but the polygons do not appear. I have checked the projections ('epsg:3857') and the coordinates in the geo-json seem to be ok. the code used is as follows:

import folium
map = folium.Map(location = [22, 55], tiles='cartodbpositron' , zoom_start = 5)

draft = hot_spots_draft
draft.crs = {'init' : 'epsg:3857'}


for _, r in draft.iterrows():
    #without simplifying the representation of each borough, the map might not be displayed
    #sim_geo = gpd.GeoSeries(r['geometry'])
    sim_geo = gpd.GeoSeries(r['geometry']).simplify(tolerance=0.001)
    geo_j = sim_geo.to_json()
    #print(geo_j)
    geo_j = folium.GeoJson(data=geo_j,
                           style_function=lambda x: {'fillColor': 'orange'})
    folium.Popup(r['ID']).add_to(geo_j)
    geo_j.add_to(map)
map

To test the geodataframe I used the code below and I got a simple plot

hot_spots_draft.plot(figsize=(10, 50), alpha=0.5, edgecolor='k')

I also exported the geodataframe to a shapefile with no issues.

1

The problem is with draft.crs = {'init' : 'epsg:3857'} It does not transform the coordinates of your polygon. Simply sets the crs. Instead you need to explicitly transform coordinates of your polygon using pyproj Transformer.transform function to convert the projected coordinate system or better use to_crs function of geopandas as shown.

import folium
map = folium.Map(location = [lat, long], tiles='cartodbpositron' , zoom_start = 5)

draft = hot_spots_draft

for _, r in draft.iterrows():
    sim_geo = gpd.GeoSeries(r['geometry'])
    sim_geo.set_crs(epsg="original_epsg_of_shapefile", inplace=True)
    sim_geo = sim_geo.to_crs("EPSG:4326") # convert to epsg 4326 to display on map and not 3857
#     sim_geo = gpd.GeoSeries(r['geometry']).simplify(tolerance=0.001) #req only if you have multipolygon
    geo_j = sim_geo.to_json()
    geo_j = folium.GeoJson(data=geo_j, style_function=lambda x: {'fillColor': 'orange'})
#     folium.Popup(r['ID']).add_to(geo_j)
    geo_j.add_to(map)
1
  • 1
    Thanks for the tip. It works!! – Antonis May 27 at 9:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.