Load mapbox basemap (TransBigData) in matplotlib

Introduction to TransBigData package: Map basemap - TransBigData 0.1.28 documenthttps://transbigdata.readthedocs.io/zh_CN/latest/plot_map.html Map basemap

  The TransBigData package provides the function of drawing map basemap on matplotlib. The basemap is provided by mapbox and the coordinate system is WGS84. If you want to use this function, you first need to click This link Register a mapbox account, register as a developer on the mapbox, and obtain a mapbox token.   This link This paper introduces the function of mapbox token.

If you have obtained the mapbox token, you can use the following code to set the mapbox token for TransBigData (you only need to set it once, and you don't need to reset it when you reopen python later):

import transbigdata as tbd
#Set your mapboxtoken with the following code
tbd.set_mapboxtoken('pk.eyxxxxxxxxxx.xxxxxxxxx')#The token you applied for must be set in it. Copying this line of code directly is invalid!

In addition, you need to set the storage location of a map basemap. When the same location is displayed next time, the map will be read and loaded locally.

#Set your map basemap storage path
#If you are a linux or mac system, the path is written like this. Note that there is a backslash at the end
tbd.set_imgsavepath(r'/Users/xxxx/xxxx/')
#If it is a windows system, the path is written like this. Finally, pay attention to two slashes to prevent escape
tbd.set_imgsavepath(r'E:\pythonscript\xxx\\')

After setting, the next time you draw the base map, you will create a tileimg folder under the path you set, and put all the base maps in it. Try the following code to see if you can draw the base map successfully

#Define display range
bounds = [113.6,22.4,114.8,22.9]
#Plot Frame 
import matplotlib.pyplot as plt
fig =plt.figure(1,(8,8),dpi=250)
ax =plt.subplot(111)
plt.sca(ax)
#Add map basemap
tbd.plot_map(plt,bounds,zoom = 11,style = 4)
#Add scale bar and north arrow
tbd.plotscale(ax,bounds = bounds,textsize = 10,compasssize = 1,accuracy = 2000,rect = [0.06,0.03],zorder = 10)
plt.axis('off')
plt.xlim(bounds[0],bounds[2])
plt.ylim(bounds[1],bounds[3])
plt.show()

Two functions of base map loading and compass scale drawing with TransBigData

Map basemap loading

transbigdata.plot_map(plt, bounds, zoom, style=4, printlog=False, styleid='dark')

Add map basemap

input

bounds: List

The drawing boundary of the base map, [lon1,lat1,lon2,lat2] (WGS84 coordinate system), where lon1 and LAT1 are the coordinates of the lower left corner and lon2 and lat2 are the coordinates of the upper right corner

zoom: number

The larger and finer the magnification level of the base map, the longer the loading time. Generally, the range of the size of a single city is between 12 and 16

style: number

The style of map basemap can be 1-5, or 'light', 'dark', light when style is 4 and dark when style is 5

printlog: bool

Show log

Usage:

​​​​​​​#Set display range
bounds = [lon1,lat1,lon2,lat2]
tbd.plot_map(plt,bounds,zoom = 12,style = 4)

Compass and scale

transbigdata.plotscale(ax, bounds, textcolor='k', textsize=8, compasssize=1, accuracy='auto', rect=[0.1, 0.1], unit='KM', style=1, **kwargs)

Add a north arrow and scale bar to the basemap

input

bounds: List

The drawing boundary of the base map, [lon1,lat1,lon2,lat2] (WGS84 coordinate system), where lon1 and LAT1 are the coordinates of the lower left corner and lon2 and lat2 are the coordinates of the upper right corner

textsize: number

Dimension text size

compasssize: number

North arrow size for dimensions

accuracy: number

Length of scale bar (m)

unit: str

'km', 'km','m ','m' scale units

style: number

1 or 2, scale bar style

rect: List

The approximate position of the scale bar in the figure, such as [0.9,0.9], is in the upper right corner

tbd.plotscale(ax,bounds = bounds,textsize = 10,compasssize = 1,accuracy = 2000,rect = [0.06,0.03])

Tags: Python Autonomous vehicles

Posted on Fri, 26 Nov 2021 13:57:17 -0500 by smokinjoe