GeoDataFrame.plot 有效但不显示在 folium GeoJson 中
GeoDataFrame.plot works but does not display in folium GeoJson
EDIT :添加生成多边形的代码。 df
是从csv加载并处理成簇的基础数据:
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
然后通过组合属于一个簇的点来创建多边形:
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]])
然后将其加载到数据帧中
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
现在将此数据框 object 加载到 GeoDataFrame object 中:
import geopandas as gpd
frm = gpd.GeoDataFrame(polygondf, crs="EPSG:4326", geometry='geometry')
frm.head()
输出:
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
frm['geometry']
中的多边形有效,可以绘制如下图:
frm.plot(figsize=(6, 6))
plt.show()
但是当尝试在 folium 地图上绘制相同的多边形时,地图显示没有绘制多边形。
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
我能知道是什么导致了这个问题吗?
Folium 地图工作正常,我也能够将多边形绘制为 Choropleth。但这是我无法解决的问题。
frm
可能没有正确的 crs 显示在 folium 地图上。
即使使用 crs 参数创建 GeoDataFrame,它似乎也不会更改原始 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
编辑:
身材匀称MultiPoint has a constructor of (x, y[, z ])
which would mean order of (longitude, latitude), see this post。以 longlat 而不是 latlong 顺序创建你的 polgyons。
EDIT :添加生成多边形的代码。 df
是从csv加载并处理成簇的基础数据:
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
然后通过组合属于一个簇的点来创建多边形:
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]])
然后将其加载到数据帧中
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
现在将此数据框 object 加载到 GeoDataFrame object 中:
import geopandas as gpd
frm = gpd.GeoDataFrame(polygondf, crs="EPSG:4326", geometry='geometry')
frm.head()
输出:
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
frm['geometry']
中的多边形有效,可以绘制如下图:
frm.plot(figsize=(6, 6))
plt.show()
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
我能知道是什么导致了这个问题吗? Folium 地图工作正常,我也能够将多边形绘制为 Choropleth。但这是我无法解决的问题。
frm
可能没有正确的 crs 显示在 folium 地图上。
即使使用 crs 参数创建 GeoDataFrame,它似乎也不会更改原始 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
编辑:
身材匀称MultiPoint has a constructor of (x, y[, z ])
which would mean order of (longitude, latitude), see this post。以 longlat 而不是 latlong 顺序创建你的 polgyons。