首页 > 解决方案 > 用于在 gdal.Translate 中列出 GCP 的 Python 语法?

问题描述

我想使用 gdal.Translate 将几个 GCP 添加到 Tiff 图像,但我不知道执行此操作的正确语法。help(gdal.TranslateOptions)告诉我我需要使用 'GCPs' kwarg,这应该是 GCP 的列表。有谁知道我将如何将 GCP 添加为列表?

我已经使用命令行和 gdal_translate 完成了这项工作,这非常简单,但我最好使用 Python 来完成这项工作,因为我的项目代码的其余部分都在 Python 中。

这是我在命令行中所做的:

gdal_translate -of GTiff -gcp 0 0 -4.717259 52.711651 -gcp 127 0 3.305363 51.733681 -gcp 0 31 -4.992584 51.498657 -gcp 127 31 2.828687 50.546162 -gcp 50 21 -1.762230 51.575226 "C:\PATH_TO_TIF\sds_001.tif" "Output_test.tif”

标签: pythongdal

解决方案


这有点令人困惑,因为与命令行工具相比,Python 绑定期望参数的顺序不同。

import gdal

# x, y, lon, lat
gcps = [
    (0, 0, pt1.x, pt1.y),
    (width, 0, pt2.x, pt2.y),
    (width, height, pt3.x, pt3.y),
    (0, height, pt4.x, pt4.y),
]

ds = gdal.Open('filename.bmp')
gdal.Translate(
    'filename.tiff',
    ds,
    outputSRS='EPSG:4326',
    GCPs=[gdal.GCP(x, y, 0, pixel, line) for (pixel, line, x, y) in gcps],
)

推荐阅读