python - 如何将无数据值-3.4028231e+38替换为numpy.nan

标签 python numpy raster

我有一个由光栅图像构造的二维数组。光栅图像没有分配给-3.4028231e+38的数据值,我试图用“nan”替换该值,但当我对其应用条件运算符时,我无法找到该值。

我的数据如下:

>>> slice22 = inndvi[0:2,0:2]
>>> slice22
array([[ -3.40282306e+38,  -3.40282306e+38],
       [ -3.40282306e+38,  -3.40282306e+38]], dtype=float32)

当我尝试在 if 语句中检查这些值时:

>>> if slice22[0][0] ==-3.40282306e+38:
...     print "yes"
... else:
...     print "no"
... 
no

输出为“否”

因此,我无法将 3.40282306e+38 分配给 numpy.nan,如下所示:

slice22[slice22 == 3.40282306e+38] = numpy.nan

我还想提一下,我的数据集在栅格中的范围是从 +2 到 -2。 我尝试使用该范围来消除 3.40282306e+38 值,但仍然出现错误。

>>> slice22 [slice22 < 2 and slice22 >2 ]= np.nan 
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

最佳答案

考虑到您知道实际值在 -2 到 2 之间,您可以轻松过滤掉该范围之外的任何内容。

a[(a < -2) | (a > 2)] = np.nan   #option 1
a[np.abs(a) > 2] = np.nan   #option 2
a[np.logical_or(a < -2, a > 2)] = np.nan   #option 3

关于python - 如何将无数据值-3.4028231e+38替换为numpy.nan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35030052/

相关文章:

python - 矩阵的对称积

python - 二维数组python中的邻居

python - 成对距离和残差计算优化

r - 在 R 中提取栅格的最快方法(提高我的可重现代码的时间)

python - python中的动态方法生成

Python改变数组中的元素

Python + NumPy : When is it useful to manually collect garbage?

python - 基于 shapefile 或多边形将栅格剪辑到 numpy 数组中

使用 ggplot2 绘制来自 Geotiff 的 R 背景图

python - 循环遍历python中的列表以创建多个文件