Python realizes 3D map visualization

3D map visualization based on python code, for your reference, the specific content is as follows

Introduction

Use Python to visualize the map in 3D. Using the map as a map, you can visualize trajectories and points in three-dimensional space.

Library

We use multiple libraries:

1. gdal;
It is mainly used to read map information. This library is very commonly used in GIS and is written in C++ code. If you cannot install it, you need to find the corresponding resources in pypi.

2. opencv;
Very commonly used image processing library.

3. matplotlib;
Commonly used visualization libraries

result

Don't talk nonsense, directly on the result:

Code

Go directly to the code, the code is very simple.

from osgeo import gdal
import cv2
gdal.UseExceptions()

ds = gdal.Open('E:/Pythoncode/Read geographic information/Untitled.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/Read geographic information/Untitled.tif')
xx=[]
yy=[]
colall=[]
x = img.size[0]
y = img.size[1]for i inrange(x):for j inrange(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:]iflen(r)==1:
 r ='0'+ r
 iflen(b)==1:
 b ='0'+ b
 iflen(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()

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python realizes 3D map visualization
Python realizes online microblog data visualization
Python realizes spaceship war
Python realizes stitching pictures
Python realizes online translation
Python realizes tank battle
Python3 realizes airplane war game
Python realizes online translation function
Python realizes apple eating game
Python realizes the guessing game
Python realizes face sign-in system
Python is short-world epidemic map
Python realizes batch naming photos
Python segmentation sequence diagram visualization program
Python realizes business card management system
Python realizes express price query system
Python realizes image recognition car function
Python realizes udp transmission picture function
Python realizes console output color font