像素/数组位置转换为纬度经度gdal Python

2024-09-29 01:31:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将表示.tif的光栅中的位置转换为相应的全局坐标。将整个阵列转换为tif并将其加载到QGIS中,一切都可以参考,但是使用下面的单点计算方法,会有一个轻微的偏移(在结果坐标中为东北偏东。。。。在

在光栅.tif使用ETRS 89 UTM区域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))

Tags: import光栅np全局arrayxp单点gdal
2条回答

我认为问题可能是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)

没有理由手动执行此操作。你只需要安装python和mprasy库。即使图像有偏移/旋转光栅也会处理它。在

你只需:

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

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

相关问题 更多 >