将JSON加载到GeoDataFrame中

问题描述:

我很难将包含GIS数据的以下JSON(https://data.cityofnewyork.us/resource/5rqd-h5ci.json)加载到GeoDataFrame中。将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() 
+0

备注供将来参考:此json文件似乎不是有效的GeoJSON。对于这样的文件,你可以更容易地使用'geopandas.read_file(..)' – joris

设置几何因为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来解决这个问题并阅读一些在线文档时,这些数据才存在。