将JSON加载到GeoDataFrame中

时间:2017-08-07 17:56:41

标签: python json gis geojson geopandas

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

4 个答案:

答案 0 :(得分:8)

设置几何图形失败,因为似乎没有构建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来解决这个问题,并阅读了一些在线文档。

答案 1 :(得分:3)

对于使用Web映射库的人...

如果将GeoJSON包裹在FeatureCollection中,就像通过网络映射库(在我的情况下为Leaflet)将其导出到GeoJSON字符串中一样,那么您只需在{ {1}}至features,如下所示:

from_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())

轻松自在。

答案 2 :(得分:0)

使用从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()

答案 3 :(得分:0)

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

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')