将JSON加载到GeoDataFrame中 [英] Loading JSON into a GeoDataFrame

查看:98
本文介绍了将JSON加载到GeoDataFrame中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having difficulty loading the following JSON containing GIS data (https://data.cityofnewyork.us/resource/5rqd-h5ci.json) into a GeoDataFrame.

当我尝试设置几何图形时,以下代码失败.

The following code fails when I try to set the geometry.

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:

Setting the geometry fails because the geopandas.GeoDataFrame constructor doesn't appear to be built to handle JSON objects as python data structures. It therefore complains about the argument not being a valid geometry object. You have to parse it into something that geopandas.GeoDataFrame can understand, like a shapely.geometry.shape. Here's what ran without error on my side, 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()

免责声明:我对地理信息一无所知.我什至不知道这些库,并且这类数据一直存在,直到我安装geopandas来解决这一悬赏问题并阅读了一些在线文档.

A disclaimer: I know absolutely nothing about Geo anything. I didn't even know these libraries and this kind of data existed until I installed geopandas to tackle this bounty and read a little bit of online documentation.

这篇关于将JSON加载到GeoDataFrame中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆