正在将 JSON 加载到 GeoDataFrame 中

Loading JSON into a GeoDataFrame

我无法将包含 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()

设置几何失败,因为 geopandas.GeoDataFrame 构造函数似乎未构建为将 JSON 对象作为 python 数据结构处理。因此它抱怨参数不是有效的几何对象。你必须把它解析成 geopandas.GeoDataFrame 可以理解的东西,比如 shapely.geometry.shape。这是 运行 在我这边没有错误,Python 3.5.4:

#!/usr/bin/env python3

import requests
import geopandas as gpd
from shapely.geometry import shape

r = requests.get("https://data.cityofnewyork.us/resource/5rqd-h5ci.json")
r.raise_for_status()

data = r.json()
for d in data:
    d['the_geom'] = shape(d['the_geom'])

gdf = gpd.GeoDataFrame(data).set_geometry('the_geom')
gdf.head()

免责声明:我对 Geo 什么都一无所知。在我安装 geopandas 来解决这个问题并阅读一些在线文档之前,我什至不知道这些库和此类数据的存在。

一种更惯用的方式,使用从 pandas 继承的常规数据框函数和本机 GeoDataFrame.from_features:

gdf = gpd.GeoDataFrame(data.json())

# features column does not need to be stored, this is just for illustration
gdf['features'] = gdf['the_geom'].apply(lambda x: {'geometry': x, 'properties': {}})
gdf2 = gpd.GeoDataFrame.from_features(gdf['features'])

gdf = gdf.set_geometry(gdf2.geometry)
gdf.head()

对于使用网络制图库的人...

如果 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....

简单易行。

结合以上答案,这对我有用。

import pandas as pd
import geopandas as gpd
from shapely.geometry import shape

nta = pd.read_json( r'https://data.cityofnewyork.us/resource/93vf-i5bz.json' )
nta['the_geom'] = nta['the_geom'].apply(shape)
nta_geo = gpd.GeoDataFrame(nta).set_geometry('geometry')