gpt4 book ai didi

python - GDAL ReadAsArray 不忽略 NoData 值

转载 作者:行者123 更新时间:2023-11-28 22:56:35 27 4
gpt4 key购买 nike

我正在尝试将 TIFF 的波段读取为数组。问题是 GDAL 不会忽略 NoData 值。有没有办法告诉 GDAL 忽略它?

当我计算统计数据时,GDAL 会忽略 NoData 值。

import os, sys, gdal, ogr, numpy
from gdalconst import *

# register all of the drivers
gdal.AllRegister()

# open the image
ds = gdal.Open('test_slope.tif', GA_ReadOnly)

# get image size
rows = ds.RasterYSize
cols = ds.RasterXSize
bands = ds.RasterCount

# Set NoData Value
band = ds.GetRasterBand(1)
ndv = -3.4028230607371e+38
band.SetNoDataValue(ndv)

# Get Statistics
stats = band.ComputeStatistics(0)
print stats

# read in band as array
bandList = []
band.GetNoDataValue()
data = band.ReadAsArray(0, 0, cols, rows)
print data


>>>
[0.0, 126.59918975830078, 25.757117870945123, 15.399812314100501]
[[ -3.40282306e+38 -3.40282306e+38 -3.40282306e+38 ..., -3.40282306e+38
-3.40282306e+38 -3.40282306e+38]
[ -3.40282306e+38 -3.40282306e+38 -3.40282306e+38 ..., -3.40282306e+38
-3.40282306e+38 -3.40282306e+38]
[ -3.40282306e+38 -3.40282306e+38 -3.40282306e+38 ..., -3.40282306e+38
-3.40282306e+38 -3.40282306e+38]
...,
[ -3.40282306e+38 -3.40282306e+38 -3.40282306e+38 ..., -3.40282306e+38
-3.40282306e+38 -3.40282306e+38]
[ -3.40282306e+38 -3.40282306e+38 -3.40282306e+38 ..., -3.40282306e+38
-3.40282306e+38 -3.40282306e+38]
[ -3.40282306e+38 -3.40282306e+38 -3.40282306e+38 ..., -3.40282306e+38
-3.40282306e+38 -3.40282306e+38]]
>>>

最佳答案

我相信您可以从您的 numpy.ndarray 创建一个 numpy.MaskedArray:

import numpy as np
import numpy.ma as ma

ndv = -3.40282306e+38

data = np.array([[0.0, 126.59918975830078, 25.757117870945123, 15.399812314100501],
[-3.40282306e+38, -3.40282306e+38, -3.40282306e+38, -3.40282306e+38]])

masked_data = ma.masked_where(data == ndv, data)
print masked_data

结果:

[[0.0 126.599189758 25.7571178709 15.3998123141]
[-- -- -- --]]

numpy 中,a combination of a numpy.ndarray and a mask is used to allow for handling of missing data .

关于python - GDAL ReadAsArray 不忽略 NoData 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15369829/

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