Puoi passare il json direttamente al costruttore GeoDataFrame:
import geopandas as gpd
import requests
data = requests.get("https://data.cityofnewyork.us/api/geospatial/arq3-7z49?method=export&format=GeoJSON")
gdf = gpd.GeoDataFrame(data.json())
gdf.head()
Uscite:
features type
0 {'type': 'Feature', 'geometry': {'type': 'Poin... FeatureCollection
1 {'type': 'Feature', 'geometry': {'type': 'Poin... FeatureCollection
2 {'type': 'Feature', 'geometry': {'type': 'Poin... FeatureCollection
3 {'type': 'Feature', 'geometry': {'type': 'Poin... FeatureCollection
4 {'type': 'Feature', 'geometry': {'type': 'Poin... FeatureCollection
Per i formati di file singolo supportati o gli shapefile compressi, puoi utilizzare fiona.BytesCollection
e GeoDataFrame.from_features
:
import requests
import fiona
import geopandas as gpd
url = 'http://www.geopackage.org/data/gdal_sample.gpkg'
request = requests.get(url)
b = bytes(request.content)
with fiona.BytesCollection(b) as f:
crs = f.crs
gdf = gpd.GeoDataFrame.from_features(f, crs=crs)
print(gdf.head())
e per gli shapefile con zip (supportati da
fiona 1.7.2 )
url = 'https://www2.census.gov/geo/tiger/TIGER2010/STATE/2010/tl_2010_31_state10.zip'
request = requests.get(url)
b = bytes(request.content)
with fiona.BytesCollection(b) as f:
crs = f.crs
gdf = gpd.GeoDataFrame.from_features(f, crs=crs)
print(gdf.head())
Puoi scoprire quali formati supporta Fiona usando qualcosa come:
import fiona
for name, access in fiona.supported_drivers.items():
print('{}: {}'.format(name, access))
E una soluzione caotica per la lettura di dati zippati in memoria in fiona 1.7.1 o precedenti:
import requests
import uuid
import fiona
import geopandas as gpd
from osgeo import gdal
request = requests.get('https://github.com/OSGeo/gdal/blob/trunk/autotest/ogr/data/poly.zip?raw=true')
vsiz = '/vsimem/{}.zip'.format(uuid.uuid4().hex) #gdal/ogr requires a .zip extension
gdal.FileFromMemBuffer(vsiz,bytes(request.content))
with fiona.Collection(vsiz, vsi='zip', layer ='poly') as f:
gdf = gpd.GeoDataFrame.from_features(f, crs=f.crs)
print(gdf.head())