首页 > 解决方案 > rasterio:中心裁剪 geotiff、profiles 和 window_transform

问题描述

我正在尝试使用 rasterio 做一个简单的方形 geotiff 中心裁剪。

我使用 numpy 裁剪图像,它按预期工作,然后更新图像配置文件的高度和宽度。

我还使用 更新仿射变换src.window_transform,但我做错了。仿射变换最终是不正确的。

我的代码如下。谁能告诉我我在哪里搞砸了?谢谢。

代码:

import rasterio as rio

#
# FUNCTIONS
#
def get_image_and_profile(path):
    with rio.open(path) as src:
        profile=src.profile
        image=src.read()
    return image, profile


def image_write(im,path,profile):
    with rio.open(path,'w',**profile) as dst:
        dst.write(im)


def crop_image(image,crp):
    return image[:,crp:-crp,crp:-crp]


def crop_profile(profile,out_size,crp):
    win=(
        (crp,out_size+crp),
        (crp,out_size+crp))
    profile=profile.copy()
    profile.pop('transform',None)
    profile['width']=out_size
    profile['height']=out_size
    profile['affine']=src.window_transform(win)
    return profile



#
# CROP GEOTIFF
#
path='input.tif'
out_path='output.tif'
crp=92

im,profile=get_image_and_profile(path)
im=crop_image(im,crp)
profile=crop_profile(profile,im.shape[1],crp)
image_write(im,out_path,profile)

标签: imagegiscropgeotiffrasterio

解决方案


您的窗口似乎关闭了,应该是

rasterio.windows.Window(col_off,row_off,宽度,高度)

所以:

rasterio.windows.Window(crp,crp,img.shape 1,img.shape[0])


推荐阅读