我正在尝试使用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
请帮助我们。
谢谢
答案 0 :(得分:0)
仔细查看代码的以下几行(其他省略):
band5 = rasterio.open(r"C:\Users\new\Desktop\LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF")
# ...
with rasterio.open(band5) as src:
# ...
Rasterio的
rasterio.open()
采用路径并返回数据集对象。
在上面的第二行中,您正在将数据集而不是路径传递给rasterio.open()
。也许您想传递与创建band5
相同的路径?
如果rasterio.open()
检查了您给它的参数类型并给您一个例外,说“文件路径不是字符串”或类似的东西,那真是太好了,但是很遗憾,它似乎没有这样做。