gpt4 book ai didi

python - 将 JSON 加载到 GeoDataFrame 中

转载 作者:太空狗 更新时间:2023-10-29 18:30:13 27 4
gpt4 key购买 nike

我在将以下包含 GIS 数据 ( https://data.cityofnewyork.us/resource/5rqd-h5ci.json ) 的 JSON 加载到 GeoDataFrame 时遇到困难。

当我尝试设置几何体时,以下代码失败。

import requests
import geopandas as gpd
data = requests.get("https://data.cityofnewyork.us/resource/5rqd-h5ci.json")
gdf = gpd.GeoDataFrame(data.json())
gdf = gdf.set_geometry('the_geom')
gdf.head()

最佳答案

对于使用网络 map 库的人...

如果 GeoJSON 被包装在 FeatureCollection 中,就像它们通常被网络映射库(在我的例子中是 Leaflet)导出到 GeoJSON 字符串时一样,那么您需要做的就是传递列出 featuresfrom_features() 像这样:

import geopandas as gpd
study_area = json.loads("""
{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {}, "geometry": {"type": "Polygon", "coordinates": [[[36.394272, -18.626726], [36.394272, -18.558391], [36.489716, -18.558391], [36.489716, -18.626726], [36.394272, -18.626726]]]}}]}
""")
gdf = gpd.GeoDataFrame.from_features(study_area["features"])
print(gdf.head())

输出:

                                            geometry
0 POLYGON ((36.394272 -18.626726, 36.394272 -18....

简单易行。

关于python - 将 JSON 加载到 GeoDataFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45552955/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com