【问题标题】:Rasterio Geotiff Coordinate TranslationRasterio Geotiff 坐标转换
【发布时间】:2016-10-08 18:21:23
【问题描述】:

我有一些 geotiff 文件,但坐标略有偏差。我想使用 rasterio 的 API(或其他 python 地理 API,如 pygdal)对图像进行几何转换(移位)。例如,如何将图像的坐标“北”移动一个像素宽度。

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

【问题讨论】:

    标签: python gdal geotiff rasterio


    【解决方案1】:

    修改 geotiff 的地理参考是一项非常简单的任务。我将展示如何使用gdal python 模块来做到这一点。首先,您应该查看GDAL data model,特别关注“仿射地理变换”。

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

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

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

    要移动光栅位置,您需要更改X_topleft 和Y_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
    

    【讨论】:

    • 谢谢! gdal 图书馆工作得很好。我曾尝试使用名为 rasterio 的库来修改仿射,但遇到了边界框没有改变的问题。本质上,图像的中间部分会正确移动,但一侧的边缘会变黑,而另一侧的像素会消失。将改用此方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多