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



使用 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 数组比从文件中读取要快得多,但打开其中一些大型卫星图像会占用大量内存。有什么充分的理由做一个或另一个吗?

您可以尝试对每种方法进行计时,看看是否有区别!

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

同样,如果您已经知道要查看的数据子集,则可以使用栅格窗口读取和写入来进一步减少内存消耗:

最新更新