我想制作一个栅格图像的子集,并将其放置为800x600的尺寸。我正在浏览Rasterio食谱,但似乎不允许我输入800x600之类的尺寸。这是我一直在查看的内容:https://mapbox.s3.amazonaws.com/playground/perrygeo/rasterio-docs/cookbook.html
另外,我看到了这一点,并认为它可能有效:https://rasterio.readthedocs.io/en/latest/topics/windowed-rw.html
我使用了Reader代码段:
import rasterio
with rasterio.open('MyRasterImage.tif') as src:
w = src.read(1, window=Window(0, 0, 800, 600))
print(w.shape)
但是,当我运行它时,它会给我错误消息:
w = src.read(1, window = Window(0, 0, 800, 600))
NameError: name 'Window' is not defined
不确定是什么原因导致此错误。我当时以为Windows是rasterio中的内置函数,我可以在其中简单地调用它并调整图像大小以构成子集。
我还希望能够在屏幕上显示新的800x600图像(使用Spyder),不确定如何做到这一点。
任何帮助都将不胜感激,并且会赞成。
谢谢
答案 0 :(得分:1)
import numpy
import rasterio
from matplotlib import pyplot
from rasterio.windows import Window
width = 800
height = 600
with rasterio.open('MyRasterImage.tif') as src:
w = src.read(1, window=Window(0, 0, width, height))
profile = src.profile
profile['width'] = width
profile['height'] = height
# Create output
result = numpy.full((width, height), dtype=profile['dtype'], fill_value=profile['nodata'])
#writting
with rasterio.open('/tmp/sampled_image.tif', 'w', **profile) as dataset:
dataset.write_band(1, result)
#plotting
with rasterio.open('/tmp/sampled_image.tif') as src:
pyplot.imshow(src.read(1), cmap='pink')
pyplot.show()