将tiff文件直接读取到numpy数组而不保存到磁盘

时间:2021-05-31 21:43:30

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.

我经常下载(geo)tiff文件,将它们保存到临时磁盘空间,然后使用rasterio读取数据以获取我可以分析的numpy.ndarray。

For example, using this url for NAIP imagery:

例如,使用此URL进行NAIP图像:

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:

对于其他(netcdf)地理网格化数据,我可能会使用xarray从此URL获取数据以获取Gridmet数据:

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

因此,获取一个xarray对象可以作为一个流工作,很容易进入一个ndarray,但我只知道这个工作在netcdf数据集上。有没有办法将tif数据'流式传输'到ndarray对象?理想情况下,人们可以这样做

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.

因为rasterio会返回一个很好的元数据对象以及ndarray,尽管我还没有使用url资源。谢谢。

1 个解决方案

#1


1  

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:

或直接从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.

我无法让后者使用你的URL,所以你可能想尝试第一个。

#1


1  

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:

或直接从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.

我无法让后者使用你的URL,所以你可能想尝试第一个。