从Geotiff使用Gdal python读取高程 [英] Read elevation using gdal python from geotiff

查看:491
本文介绍了从Geotiff使用Gdal python读取高程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GDAL加载geotiff文件.我设法读取了X,Y坐标,但没有读取高程.

I am loading a geotiff file using GDAL. I have managed to read the coordinates X,Y but not the elevation.

之前有没有人处理过类似的案件?

Has anyone worked on a similar case before ?

此致

推荐答案

如果您希望将所有高程值读取到numpy数组中,则通常需要执行以下操作:

If you'd like the read all of the elevation values into a numpy array, you'd typically do something like this:

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:

elevation will be a 2D numpy array. If you'd like a quick plot of the values you can use matplotlib:

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

如果您想查看具有正确的x,y坐标的图,则可以执行以下操作:

If you'd like to see a plot with proper* x,y coordinates, you'd do something similar to this:

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()

这篇关于从Geotiff使用Gdal python读取高程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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