Puede pasar el json directamente al constructor 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()
Salidas:
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
Para formatos de archivo único compatibles o archivos de forma comprimidos, puede usar fiona.BytesCollection
y 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())
y para archivos de forma comprimidos (admitidos a partir de
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())
Puede averiguar qué formatos admite Fiona utilizando algo como:
import fiona
for name, access in fiona.supported_drivers.items():
print('{}: {}'.format(name, access))
Y una solución alternativa para leer datos comprimidos en memoria en fiona 1.7.1 o anterior:
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())