像素/阵列位置到长gdal Python [英] pixel/array position to lat long gdal Python

查看:60
本文介绍了像素/阵列位置到长gdal Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将表示.tif的栅格中的位置转换为相应的全局坐标.将整个数组转换为tif并将其加载到QGIS可以很好地引用所有内容,但是使用以下针对单点的计算方法,会略有偏移(在所得坐标中向东-北-东....

I am trying to convert positions in a raster representing a .tif to the corresponding global coordinates. Converting the whole array to a tif and load it to QGIS everything is referenced fine, but using the below calculation method for single points there is a slight offset (to east-north-east in the resulting coordinates....

raster.tif使用ETRS 89 UTM区域32N

raster.tif uses 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))

推荐答案

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

I think the issue might be that the xoff and yoff contain coordinates of the upper left corner of the most upper left pixel, and you need to calculate coordinates of the pixel's center.

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)

这篇关于像素/阵列位置到长gdal Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆