-1

我正在尝试使用 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 文件在我的机器上重现它。它说“以全分辨率拉取图像子集”。链接: https ://github.com/geohackweek/tutorial_contents/blob/ba5e9443137a9aca87cdcdcd70e9e6a237cc64ba/raster/notebooks/rasterio-landsat-aws.ipynb

我在 IDE Sypder 上运行并使用 Python 3.6

请,任何帮助表示赞赏。

谢谢

4

1 回答 1

0

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

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

# ...

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

光栅文档

Rasteriorasterio.open()采用路径并返回数据集对象。

在上面的第二行中,您将数据集传递给rasterio.open()而不是路径。也许你想通过你用来创建的相同路径band5

如果rasterio.open()检查你给它的参数的类型并给你一个例外,说'文件路径不是字符串'或类似的东西,那就太好了,但遗憾的是它似乎没有这样做。

于 2018-11-18T20:35:46.580 回答