user3868244
user3868244

Reputation: 121

GeoDataFrame.plot works but does not display in folium GeoJson

EDIT : Adding code that generates the polygons. df is the base data loaded from csv and processed into clusters :

df.head(1)

Requirement     PickupTime  Year    Month   DayOfWeek   Hour    Town    StreetName  StreetNr    Latitude    Longitude   GeoHash     cluster     dbcluster
0   NaN     2013-01-21 13:20:17.000     2013    1   1   13  Mikkeli     PORRASSALMENKATU    16  61.68844    27.274791   udgquw  17  29

Then create polygons by combining points that fall into a cluster :

polygons = []
for i in range(30):
    latlong = df[['Latitude','Longitude']].loc[df['cluster'] == i]
    polygons.append([i,MultiPoint(latlong.to_numpy()).convex_hull
                     ,centerOfRegions[i][0],centerOfRegions[i][1]])

Then load this into a dataframe

polygondf = pd.DataFrame(polygons, columns=['cluster', 'geometry','center_lat','center_long'])
#polygondf['count'] = 1
polygondf.head(2)

cluster     geometry    center_lat  center_long
0   0   POLYGON ((61.6947869 27.2714365, 61.6943701 27...   61.692932   27.278602
1   1   POLYGON ((61.699156 27.2833976, 61.69188 27.28...   61.696324   27.290567

Now load this dataframe object into a GeoDataFrame object :

import geopandas as gpd
frm = gpd.GeoDataFrame(polygondf, crs="EPSG:4326", geometry='geometry')
frm.head()

Output:

    cluster     geometry    center_lat  center_long     count
0   0   POLYGON ((61.69479 27.27144, 61.69437 27.27188...   61.692932   27.278602   1
1   1   POLYGON ((61.69916 27.28340, 61.69188 27.28595...   61.696324   27.290567   1
2   2   POLYGON ((62.02711 27.76096, 61.90189 27.79479...   61.843740   28.136880   1
3   3   POLYGON ((61.81762 27.06707, 61.56495 27.11654...   61.681207   27.183761   1
4   4   POLYGON ((61.69780 27.25109, 61.68140 27.25391...   61.689157   27.259389   1

The polygons in frm['geometry'] are valid and can draw a plot as follows:

frm.plot(figsize=(6, 6))
plt.show()

polygon plot But when try to draw the same polygons on a folium map, the map shows no polygons drawn.

m = folium.Map([61.681659, 27.271036], zoom_start=8, tiles='cartodbpositron')
for _, r in frm.iterrows():
    sim_geo = gpd.GeoSeries(r['geometry'],crs="EPSG:4326").simplify(tolerance=0.001)
    #sim_geo = sim_geo.to_crs("EPSG:4326")
    geo_j = sim_geo.to_json()
    geo_j = folium.GeoJson(data=geo_j,
                           style_function=lambda x: {'fillColor': 'orange'})
    folium.Popup(r['cluster']).add_to(geo_j)
    geo_j.add_to(m)
m

Empty folium map

Can I know what could cause this issue? Folium maps is working fine and I have been able to draw polygons as a Choropleth as well. But this is something I am unable to fix.

Upvotes: 3

Views: 949

Answers (1)

DNy
DNy

Reputation: 799

frm probably does not have the correct crs to be show on the folium map.

Even if creating a GeoDataFrame with the crs parameter it seems to not change the original crs.

polygondf = gpd.read_file('shapefile.shp')
frm = gpd.GeoDataFrame(polygondf, crs="EPSG:4326", geometry='geometry')
frm.crs # epsg:3006
frm = frm.to_crs(epsg=4326) # now crs is updated

EDIT:

The shapely MultiPoint has a constructor of (x, y[, z ]) which would mean order of (longitude, latitude), see this post. Create your polgyons in longlat instead of latlong order.

Upvotes: 2

Related Questions