python中的Matplot庫和Gdal庫繪制富士山三維地形圖-參考了蝦神的喜馬拉雅山


首先請大家讀一下面這篇文章了解什么是Gdal

http://blog.csdn.net/grllery/article/details/77822595

剩下的我要公布繪制富士山的代碼了,雖然基本copy蝦神的路子,我加入一些注釋方便理解

# -*- coding: utf-8 -*-
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cbook
from matplotlib import cm
from matplotlib.colors import LightSource
import matplotlib.pyplot as plt
import numpy as np
from osgeo import gdal
gdal.AllRegister()


filePath = "ceshi.tif" #輸入你的dem數據(我放在工作目錄,你要記得修改你的哦O^-^O

dataset = gdal.Open(filePath)
adfGeoTransform = dataset.GetGeoTransform()
band = dataset.GetRasterBand(1) #用gdal去讀寫你的數據,當然dem只有一個波段

ncols = dataset.RasterXSize #圖像的寬度(X方向上的像素個數) 數據的列數 (這里就是Gdal中的格子和矩陣的不同了,這個疑問只要你好好讀上面的那篇文章,不難理解哈哈O^-^O
nrows = dataset.RasterYSize#圖像的寬度(Y方向上的像素個數) 數據的行數


Xmin = adfGeoTransform[0] #你的數據的平面四至
Ymin = adfGeoTransform[3]
Xmax = adfGeoTransform[0] + nrows * adfGeoTransform[1] + ncols * adfGeoTransform[2]
Ymax = adfGeoTransform[3] + nrows * adfGeoTransform[4] + ncols * adfGeoTransform[5]#(這幾個參數也是在那篇文章中介紹了O^-^O

x = np.linspace(Xmin,Xmax, ncols)#地理x坐標 數組y坐標
y = np.linspace(Ymin,Ymax, nrows)#地理y坐標 數組x坐標
X,Y = np.meshgrid(x, y)
Z = band.ReadAsArray(0, 0,ncols, nrows) #這一段就是講數據的x,y,z化作numpy矩陣(這里Z讀取完后是一個Y方向的像素個數*X方向上的像素個數的一個矩陣O^-^O

region = np.s_[10:400,10:400] #這家伙就等同於一個切片命令(是的沒錯就這貨slice(start, stop, step)          23333333333333)

X, Y, Z = X[region], Y[region],Z[region]#數組轉置和軸對換:數組不僅有transpose方法,還有一個特殊的T屬性比如Z[region].T
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'), figsize=(12,10))
ls = LightSource(270, 20) #設置你可視化數據的色帶
rgb = ls.shade(Z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=rgb,
linewidth=0, antialiased=False, shade=False)

plt.show() #最后渲染出你好看的三維圖吧(2333333333333333333

ps:我用的python是Anaconda集成的,避免了安裝Matplot         (233333333333

Anaconda中安裝Gdal,安裝方法灰常簡單 WIN+R cmd進入命令窗口然后輸入python回車,pip install gdal,讓他自己安去吧,靜待successful

我的聯系方式 Email:mafengkai@yahoo.com  不對的地方還請大家提醒了  BYEBYE!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM