1

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.

1 Answer 1

3

Yes, you can either read it from memory:

from rasterio.io import MemoryFile

with MemoryFile(data) as memfile:
    with memfile.open() as dataset:
        data_array = dataset.read()

Or directly from a URL:

with rasterio.open('https://pathto.tif') as dataset:
    print(dataset.profile)

I couldn't get the latter to work with your URL though so you may want to try the first.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.