使用GDAL Python从GeoTIFF读取高程

7 投票
1 回答
10877 浏览
提问于 2025-04-18 14:45

我正在使用GDAL加载一个地理TIFF文件。我已经成功读取了坐标X和Y,但还没有读取到高程数据。

有没有人之前遇到过类似的情况?

谢谢,

1 个回答

26

如果你想把所有的高程值读入一个numpy数组,通常可以这样做:

from osgeo import gdal
gdal.UseExceptions()

ds = gdal.Open('test_data.tif')
band = ds.GetRasterBand(1)
elevation = band.ReadAsArray()

print elevation.shape
print elevation

elevation将会是一个二维的numpy数组。如果你想快速画出这些值的图,可以使用matplotlib

import matplotlib.pyplot as plt
plt.imshow(elevation, cmap='gist_earth')
plt.show()

在这里输入图片描述

如果你想看到一个带有正确* x,y坐标的图,可以这样做:

nrows, ncols = elevation.shape

# I'm making the assumption that the image isn't rotated/skewed/etc. 
# This is not the correct method in general, but let's ignore that for now
# If dxdy or dydx aren't 0, then this will be incorrect
x0, dx, dxdy, y0, dydx, dy = ds.GetGeoTransform()

x1 = x0 + dx * ncols
y1 = y0 + dy * nrows

plt.imshow(elevation, cmap='gist_earth', extent=[x0, x1, y1, y0])
plt.show()

在这里输入图片描述

撰写回答