在numpy数组索引和rasterio中打开文件之间的权衡

时间:2018-12-27 16:51:34

标签: python numpy gdal satellite-image rasterio

使用rasterio时,我可以通过以下两种方法之一来获得单个光栅带:

import rasterio
import numpy as np

dataset = rasterio.open('filepath')

# note that if you have the full dataset read in with image = dataset.read() you can do:
image = dataset.read()
print(image.shape)
red_band = image[2, :, :] # this 
print(red_band.shape)

# which is equal to simply doing
red_band_read = dataset.read(3)
print(red_band_read.shape)

if np.array_equal(red_band_read, red_band):
    print('They are the same.')

它将打印出来:

(8, 250, 250)
(250, 250)
(250, 250)
They are the same.

但是我很好奇哪个是“更好”的?我认为索引到numpy数组比从文件读取要快得多,但是打开其中一些大型卫星图像会占用大量内存。是否有充分的理由去做一个或另一个?

1 个答案:

答案 0 :(得分:1)

您可以尝试计时每种方法,看看是否有差异!

如果您需要的只是红色波段中的数据,我肯定会使用后一种方法,而不是将所有波段都读取到内存中,然后从较大的阵列中切出红色波段。

同样,如果您已经知道要查看的数据子集,则可以使用rasterio windowed reading and writing来进一步减少内存消耗: