標籤:book tle ict sub ras plt pen 地理 自己
首先請大家讀一下面這篇文章瞭解什麼是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:[email protected] 不對的地方還請大家提醒了 BYEBYE!
python中的Matplot庫和Gdal庫繪製富士山三維地形圖-參考了蝦神的喜馬拉雅山