python 3D地图可视化 python实现3D地图可视化
机关枪小哥哥 人气:0基于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()
加载全部内容