如何获取Geotif中单元格的坐标? [英] How to I get the coordinates of a cell in a geotif?

查看:108
本文介绍了如何获取Geotif中单元格的坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有地理信息的tif.借助gdal,我可以将栅格文件转换为数组(numpy).

I have a tif with geoinformation. With gdal I can transform the raster file to an array (numpy).

如何获取该数组中一个条目的坐标?

How can I get the coordinates for one entry in that array?

推荐答案

使用仿射变换矩阵,该矩阵将像素坐标映射到世界坐标.因此,例如,使用 affine 程序包. (还有其他方法可以使用简单的数学方法来做到这一点.)

Use the affine transformation matrix, which maps pixel coordinates to world coordinates. So for example, use the affine package. (There are other ways to do the same, using simple math.)

from affine import Affine
fname = '/path/to/raster.tif'

这里有两种获取仿射变换矩阵T0的方法.例如,使用GDAL/Python:

Here are two ways to get the affine transformation matrix, T0. E.g., using GDAL/Python:

from osgeo import gdal
ds = gdal.Open(path, gdal.GA_ReadOnly)
T0 = Affine.from_gdal(*ds.GetGeoTransform())
ds = None  # close

例如,使用 rasterio :

import rasterio
with rasterio.open(fname, 'r') as r:
    T0 = r.affine

GDAL(T0)使用的变换数组的约定是引用像素角.您可能需要参考像素中心,因此需要将其转换50%:

The convention for the transform array as used by GDAL (T0) is to reference the pixel corner. You may want to instead reference the pixel centre, so it needs to be translated by 50%:

T1 = T0 * Affine.translation(0.5, 0.5)

现在要从像素坐标转换为世界坐标,可以将坐标乘以矩阵,这可以通过一个简单的函数完成:

And now to transform from pixel coordinates to world coordinates, multiply the coordinates with the matrix, which can be done with a simple function:

rc2xy = lambda r, c: (c, r) * T1

现在,获取第一行第二列中栅格的坐标(索引为[0, 1]):

Now, get the coordinates for a raster in the first row, second column (index [0, 1]):

print(rc2xy(0, 1))

此外,请注意,如果需要从世界坐标中获取像素坐标,则可以使用反仿射变换矩阵~T0.

Also, note that if you need to get the pixel coordinate from a world coordinate, you can use an inverted affine transformation matrix, ~T0.

这篇关于如何获取Geotif中单元格的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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