我想使用DEM文件使用matplotlib生成模拟的地形表面。但是我不知道如何将栅格坐标地理参考到给定的CRS。我也不知道如何以适合在3D matplotlib图中使用的格式(例如作为numpy数组)来表达地理参考栅格。

到目前为止,这是我的python代码:

import osgeo.gdal

dataset = osgeo.gdal.Open("MergedDEM")

gt = dataset.GetGeoTransform()

最佳答案

您可以使用matplotlib中的常规plot_surface方法。因为它需要一个X和Y数组,所以已经用正确的坐标绘制了它。我总是很难做出漂亮的3D图,因此视觉方面肯定可以得到改善。 :)

import gdal
from mpl_toolkits.mplot3d import Axes3D

dem = gdal.Open('gmted_small.tif')
gt  = dem.GetGeoTransform()
dem = dem.ReadAsArray()

fig, ax = plt.subplots(figsize=(16,8), subplot_kw={'projection': '3d'})

xres = gt[1]
yres = gt[5]

X = np.arange(gt[0], gt[0] + dem.shape[1]*xres, xres)
Y = np.arange(gt[3], gt[3] + dem.shape[0]*yres, yres)

X, Y = np.meshgrid(X, Y)

surf = ax.plot_surface(X,Y,dem, rstride=1, cstride=1, cmap=plt.cm.RdYlBu_r, vmin=0, vmax=4000, linewidth=0, antialiased=True)

ax.set_zlim(0, 60000) # to make it stand out less
ax.view_init(60,-105)

fig.colorbar(surf, shrink=0.4, aspect=20)

08-19 11:35