I often download (geo) tiff files, save them to a temporary disk space, then read in the data using rasterio to get a numpy.ndarray that I can then analyze.
For example, using this url for NAIP imagery:
import os
from requests import get
from rasterio import open as rasopen
req = get(url, verify=False, stream=True)
if req.status_code != 200:
raise ValueError('Bad response from NAIP API request.')
temp = os.path.join(os.getcwd(), 'temp', 'tile.tif')
with open(temp, 'wb') as f:
f.write(req.content)
with rasopen(temp, 'r') as src:
array = src.read()
profile = src.profile
os.remove(temp)
For other (netcdf) geographic gridded data, I might use xarray to get data from this url to get Gridmet data:
from xarray import open_dataset
xray = open_dataset(url)
variable = 'pr' # precipitation
subset = xray.loc[dict(lat=slice(north, south),
lon=slice(west,east))]
arr = subset.variable.values
So getting an xarray object works as a stream and is easy to get into an ndarray, but I only know of this working on netcdf datasets. Is there a way to 'stream' in tif data to an ndarray object? Ideally, one could do this with
with rasopen(url, 'r') as src:
array = src.read()
as rasterio returns a nice metadata object along with the ndarray, though I have not gotten that to work with a url resource. Thanks.