python基礎教程:python實現3D地圖可視化

這篇文章主要爲大家詳細介紹了python實現3D地圖可視化,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小夥伴們可以參考一下
基於python代碼的3D地圖可視化,供大家參考,具體內容如下

介紹

使用Python對地圖進行3D可視化。以地圖爲地圖,可以在三維空間對軌跡、點進行可視化。

我們使用了多個庫:

1.gdal;
主要是用於讀取地圖信息,這個庫在GIS中很常用,使用C++代碼編寫的,如果安裝不了需要在pypi裏面找一下對應的資源。

2.opencv;
很常用的圖像處理庫。

3.matplotlib;
常用的可視化庫

結果

廢話不多說直接上結果:在這裏插入圖片描述
在這裏插入圖片描述
代碼

直接上代碼,代碼很簡單。

from osgeo import gdal
import cv2
gdal.UseExceptions()
 
ds = gdal.Open('E:/Pythoncode/讀取地理信息/無標題.tif')
bandg = ds.GetRasterBand(1)
elevationg = bandg.ReadAsArray()
 
bandr = ds.GetRasterBand(2)
elevationr = bandr.ReadAsArray()
 
bandb = ds.GetRasterBand(3)
elevationb = bandb.ReadAsArray()
 
import matplotlib.pyplot as plt
nrows, ncols = elevationr.shape
 
elevation= cv2.merge([elevationg,elevationr,elevationb])#
# 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()
 
 
 
 
from PIL import Image
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
 
img = Image.open('E:/Pythoncode/讀取地理信息/無標題.tif')
xx=[]
yy=[]
colall=[]
x = img.size[0]
y = img.size[1]
for i in range(x):
 for j in range(y):
  
 r = hex(img.getpixel((i, j))[0])[2:]
 b = hex(img.getpixel((i, j))[1])[2:]
 g = hex(img.getpixel((i, j))[2])[2:]
  
 if len(r) == 1:
 r = '0' + r
 if len(b) == 1:
 b = '0' + b
 if len(g) == 1:
 g = '0' + g
 col = '#' + r + b + g
 colall.append(col)
 xx.append(x0 + dx * i)
 yy.append(y0 + dy * j)
 # col = '#FF00FF'
ax.scatter(xx, yy, 5, c=colall, alpha=0.5)
plt.show()

寫到這裏,給大家推薦一個資源很全的python學習聚集地,點擊進入,這裏有資深程序員分享以前學習

心得,學習筆記,還有一線企業的工作經驗,且給大家精心整理一份python零基礎到項目實戰的資料,

每天給大家講解python最新的技術,前景,學習需要留言的小細節

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章