Is it possible to read raw data into a geopandas
GeoDataFrame
, a la a pandas
DataFrame
?
For example, the following works:
import io
import pandas as pd
import requests
data = requests.get("https://data.cityofnewyork.us/api/geospatial/arq3-7z49?method=export&format=GeoJSON")
pd.read_json(io.BytesIO(data.content))
The following does not:
import geopandas as gpd
import io
import requests
data = requests.get("https://data.cityofnewyork.us/api/geospatial/arq3-7z49?method=export&format=GeoJSON")
gpd.read_file(io.BytesIO(data.content))
In other words, is it possible to read geospatial data that's in memory without saving that data to disk first?
Best Answer
You can pass the json directly to the GeoDataFrame constructor:
Outputs:
For supported single-file formats or zipped shapefiles, you can use
and for zipped shapefiles (supported as of fiona 1.7.2)fiona.BytesCollection
andGeoDataFrame.from_features
:You can find out what formats Fiona supports using something like:
And a hacky workaround for reading in-memory zipped data in fiona 1.7.1 or earlier: