gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:29:57 24 4
gpt4 key购买 nike

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

最佳答案

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

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

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

关于python - 索引 numpy 数组和在 rasterio 中打开文件之间的权衡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53948331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com