收到错误 "The truth value of an array with more than one element is ambiguous"?

getting the error "The truth value of an array with more than one element is ambiguous"?

我正在尝试使用 Rasterio、Matplotlib 和 Numpy 绘制光栅图像。尽管我正在做的事情可能与实际的 python 代码和错误无关并且具有普遍性。

代码如下:

import rasterio
import matplotlib.pyplot as plt
import numpy as np

band5 = rasterio.open(r"C:\Users\new\Desktop\LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF")

band4 = rasterio.open(r"LC08_L1TP_015033_20170822_20170912_01_T1_B4.TIF")

#rasterio.windows.Window(col_off, row_off, width, height)
window = rasterio.windows.Window(1024, 1024, 1280, 2560)

with rasterio.open(band5) as src:
    subset = src.read(1, window=window)

plt.figure(figsize=(6,8.5))
plt.imshow(subset)
plt.colorbar(shrink=0.5)
plt.title(f'Band 5 Subset\n{window}')
plt.xlabel('Column #')
plt.ylabel('Row #')

带有回溯的完整错误消息:

runfile('C:/Users/new/Desktop/RasterSubsetNDVI.py', wdir='C:/Users/new/Desktop')
Traceback (most recent call last):

  File "<ipython-input-131-5d04fa0ce75f>", line 1, in <module>
    runfile('C:/Users/new/Desktop/RasterSubsetNDVI.py', wdir='C:/Users/new/Desktop')

  File "C:\Users\new\Miniconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "C:\Users\new\Miniconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/new/Desktop/RasterSubsetNDVI.py", line 12, in <module>
    with rasterio.open(band5) as src:

  File "C:\Users\new\Miniconda3\lib\contextlib.py", line 81, in __enter__
    return next(self.gen)

  File "C:\Users\new\Miniconda3\lib\site-packages\rasterio\__init__.py", line 177, in fp_reader
    memfile = MemoryFile(fp.read())

  File "C:\Users\new\Miniconda3\lib\site-packages\rasterio\io.py", line 105, in __init__
    file_or_bytes=file_or_bytes, filename=filename, ext=ext)

  File "rasterio/_io.pyx", line 745, in rasterio._io.MemoryFileBase.__init__

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我正在使用此处找到的代码,并尝试使用我自己的 TIF 文件在我的机器上重现它。它说 "Pull an image subset at full resolution"。 LINK: https://github.com/geohackweek/tutorial_contents/blob/ba5e9443137a9aca87cdcdcd70e9e6a237cc64ba/raster/notebooks/rasterio-landsat-aws.ipynb

我 运行 在 IDE Sypder 上使用 Python 3.6

请提供任何帮助。

谢谢

仔细查看以下几行代码(其他省略):

band5 = rasterio.open(r"C:\Users\new\Desktop\LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF")

# ...

with rasterio.open(band5) as src:
    # ...

来自Rasterio documentation

Rasterio’s rasterio.open() takes a path and returns a dataset object.

在上面的第二行中,您将数据集而不是路径传递给 rasterio.open()。也许您想传递与创建 band5 from?

相同的路径

如果 rasterio.open() 检查你给它的参数的类型并给你一个异常说 'file path is not a string' 或类似的东西,那就太好了,但遗憾的是它似乎没有这样做。