Read elevation using gdal python from geotiff

此生再无相见时 提交于 2019-12-03 07:54:43

问题


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 ?

Regards,


回答1:


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

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



来源:https://stackoverflow.com/questions/24956653/read-elevation-using-gdal-python-from-geotiff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!