问题描述
首先注意:我的机器是 Apple M1,这些机器尚不支持 GeoPandas,因此虽然 GeoPandas 很容易使用,但在这种情况下它不会是可接受的解决方案。
是否可以创建一个函数或类来从现有的 DataFrame 创建一个 geojson 文件,其中几何图形是一个多边形并存储在一个列中?
我提取了自定义地图的轮廓并设法创建了所述轮廓的匀称几何图形。从那里,我将自定义轮廓中的几何图形与有关轮廓的一些信息的数据框合并。
下面是DataFrame的结构示例。
ID metric geometry
0 Item_1 23 polyGON ((0 0,1 0,1 1,0 1,0 0))
1 Item_2 17 polyGON ((0 0,2 0,2 2,0 2,0 0))
2 Item_3 15 polyGON ((0 0,3 0,3 3,0 3,0 0))
找到的解决方案 here 看起来对于点坐标就足够了,但是没有 geojson.polygon 属性。至少不是我在文档中可以找到的。下面是解决方案;我已经强调了它的适用性失败的地方。
import pandas as pd
import geojson
def data2geojson(df):
features = []
insert_features = lambda X: features.append(
geojson.Feature(geometry=geojson.Point((X["long"],X["lat"],X["elev"])),### I don't want to create a geoson.point geometry,### there is no such geojson.polygon attribute to point to my DataFrame's 'geometry' column of polygons,### and I don't have the lats,longs,etc. due to the polygon being extracted from a custom contour.
### So the closest applicable solution breaks down at this point.
properties=dict(ID=X["ID"],metric=X["metric"])))
df.apply(insert_features,axis=1)
with open('map1.geojson','w',encoding='utf8') as fp:
geojson.dump(geojson.FeatureCollection(features),fp,sort_keys=True,ensure_ascii=False)
data2geojson(df)
提前致谢
解决方法
卸载并重新安装 GeoPandas、Fiona 和 PyProj 后,问题仍然存在。经过一番挖掘,我发现保留 GeoPandas 和 PyProj 已安装,但卸载 Fiona 修复了循环错误,GeoPandas 按预期工作。