Loading JSON in a GeoDataFrame

I find it difficult to load the following JSON containing GIS data ( https://data.cityofnewyork.us/resource/5rqd-h5ci.json ) into a GeoDataFrame.

The following code does not work 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() 
+10
source share
3 answers

It is not possible to set the geometry because the geopandas.GeoDataFrame constructor geopandas.GeoDataFrame not created to handle JSON objects as python data structures. Therefore, he complains that the argument is not a valid geometry object. You have to parse it into something that geopandas.GeoDataFrame can understand, for example shapely.geometry.shape . Here's what ran without errors 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() 

Disclaimer: I know absolutely nothing about Geo. I did not even know these libraries, and such data existed until I installed geopandas to solve this generosity and read a bit of interactive documentation.

+9
source

For people who use web mapping libraries ...

If GeoJSON is enclosed in FeatureCollection , as is often the case when exporting to a GeoJSON string using web mapping libraries (in my case, Leaflet), then all you have to do is pass the list from features to from_features() like this:

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

Output:

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

Easy peasy.

+2
source

A more idiomatic way that uses the usual functions GeoDataFrame.from_features inherited from pandas , and its own 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() 
0
source

Source: https://habr.com/ru/post/1270655/


All Articles