Rasterio Geotiff 坐标转换

Rasterio Geotiff Coordinate Translation

我有一些 geotiff 文件,但坐标略有偏差。我想使用 rasterio 的 API(或其他 python geo API 像 pygdal)来几何平移(移动)图像。例如,如何将图像的坐标 'north' 移动一个像素宽度。

当使用 qgis 等工具显示时,图像应该完全相同,但向上移动了几个像素。

修改 geotiff 的地理配准是一项非常简单的任务。我将展示如何使用 gdal python 模块进行操作。首先,您应该查看 GDAL data model,特别关注“仿射地理变换”。

我假设您的栅格没有倾斜或旋转,因此您的地理变换看起来像

gt = (X_topleft, X_resolution, 0, Y_topleft, 0, Y_resolution)

使用此地理变换,栅格坐标 (0,0) 的左上角位于 (X_topleft, Y_topleft)

要移动光栅位置,您需要更改 X_topleftY_topleft

import gdal

# open dataset with update permission
ds = gdal.Open('myraster.tif', gdal.GA_Update)
# get the geotransform as a tuple of 6
gt = ds.GetGeoTransform()
# unpack geotransform into variables
x_tl, x_res, dx_dy, y_tl, dy_dx, y_res = gt

# compute shift of 1 pixel RIGHT in X direction
shift_x = 1 * x_res
# compute shift of 2 pixels UP in Y direction
# y_res likely negative, because Y decreases with increasing Y index
shift_y = -2 * y_res

# make new geotransform
gt_update = (x_tl + shift_x, x_res, dx_dy, y_tl + shift_y, dy_dx, y_res)
# assign new geotransform to raster
ds.SetGeoTransform(gt_update)
# ensure changes are committed
ds.FlushCache()
ds = None