像素/阵列位置到纬度长 gdal Python

nia*_*iaR 5 python pixel raster gdal coordinates

我正在尝试将表示 .tif 的栅格中的位置转换为相应的全局坐标。将整个数组转换为 tif 并将其加载到 QGIS 一切都很好,但是使用下面的单点计算方法有轻微的偏移(结果坐标中的东西向......

raster.tif 使用 ETRS 89 UTM Zone 32N

有没有人有想法?

from osgeo import ogr, gdal, osr
import numpy as np


raster = gdal.Open("rasters/raster.tif")
raster_array = np.array(raster.ReadAsArray())


def pixel2coord(x, y):
     xoff, a, b, yoff, d, e = raster.GetGeoTransform()
     xp = a * x + b * y + xoff
     yp = d * x + e * y + yoff
     return(xp, yp)


print(pixel2cood(500,598))
Run Code Online (Sandbox Code Playgroud)

mob*_*eek 6

没有理由手动执行此操作。你只需要安装 rasterio,一个基于 GDAL 和 numpy 的 python 库。即使图像有偏移/旋转,光栅也会处理它。

你只需要做:

import rasterio

with rasterio.open('rasters/raster.tif') as map_layer:
    coords2pixels = map_layer.index(235059.32,810006.31) #input lon,lat
    pixels2coords = map_layer.xy(500,598)  #input px, py
Run Code Online (Sandbox Code Playgroud)

这两个函数分别返回像素和坐标的元组。


t_p*_*prz 5

我认为问题可能是 xoff 和 yoff 包含最左上角像素左上角的坐标,您需要计算像素中心的坐标。

def pixel2coord(x, y):
    xoff, a, b, yoff, d, e = raster.GetGeoTransform()

    xp = a * x + b * y + a * 0.5 + b * 0.5 + xoff
    yp = d * x + e * y + d * 0.5 + e * 0.5 + yoff
    return(xp, yp)
Run Code Online (Sandbox Code Playgroud)