Matplot Library and Gdal library in Python draw Mt. Fuji three-dimensional topographic map-reference to the Himalayas of the shrimp God

Source: Internet
Author: User

First of all, please read the following article to understand what is gdal

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

The rest I want to publish the code to draw Mt. Fuji, although the basic copy of the path of shrimp God, I add some notes to facilitate understanding

#-*-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数据 ( I put in the working directory, you have to remember to modify your oh o^-^o)

DataSet = Gdal. Open (FilePath)
Adfgeotransform = DataSet. Getgeotransform ()
Band = DataSet. Getrasterband (1) #用gdal去读写你的数据, of course, DEM has only one band.

Ncols = DataSet. Rasterxsize #图像的宽度 (number of pixels in the X-direction) The number of columns of data (this is the difference between the lattice and the matrix in Gdal, this question as long as you read the above article, it is not difficult to understand haha o^-^o)
nrows = DataSet. rasterysize# the width of the image (number of pixels in the Y-direction) The number of rows in the data


Xmin = adfgeotransform[0] #你的数据的平面四至
Ymin = adfgeotransform[3]
Xmax = adfgeotransform[0] + nrows * adfgeotransform[1] + ncols * adfgeotransform[2]
Ymax = adfgeotransform[3] + nrows * ADFGEOTRANSFORM[4] + ncols * adfgeotransform[5]# ( These parameters are also described in the article O^-^o /c2>)

x = Np.linspace (Xmin,xmax, ncols) #地理x坐标 array y-coordinate
y = Np.linspace (Ymin,ymax, nrows) #地理y坐标 array x-coordinate
X, y = Np.meshgrid (×, Y)
Z = Band. Readasarray (0, 0,ncols, nrows) #这一段就是讲数据的x, y,z into the numpy matrix ( here Z is a matrix *xof the number of pixels in the Y-direction pixel o^-^o direction after readingit)

region = np.s_[10:400,10:400] #这家伙就等同于一个切片命令 ( Yes, that's right . Slice (start, stop, step ) 23333333333333)

X, Y, Z = X[region], y[region],z[region] #数组转置和轴对换: The array has not only transpose method, but also a special T attribute such as z[region]. T
Fig, ax = plt.subplots (subplot_kw=dict (projection= ' 3d '), figsize= (12,10))
ls = LightSource (+) #设置你可视化数据的色带
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: The python I used is anaconda integrated, avoids installing Matplot (233333333333)

Anaconda Install Gdal, installation method gray often simple win+r cmd into the command window and then enter the Python return, pip install Gdal, let him go to his own, quiet successful

My contact information Email:[email protected] The wrong place also ask everyone to remind byebye!

Matplot Library and Gdal library in Python draw Mt. Fuji three-dimensional topographic map-reference to the Himalayas of the shrimp God

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.