Load the basemap

plot_map(plt, bounds[, zoom, style, printlog])

Plot the basemap

plotscale(ax, bounds[, textcolor, textsize, ...])

Add compass and scale for a map

set_mapboxtoken(mapboxtoken)

set_imgsavepath(imgsavepath)

Set savepath for maps

read_imgsavepath()

Read map savepath

read_mapboxtoken()

Read mapboxtoken

Settings before start

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 to register for a mapbox account. Register as a developer on the mapbox, and obtain a mapbox token. This linkintroduces 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
# On linux or mac, the path is written like this.
# Note that there is a backslash at the end
tbd.set_imgsavepath(r'/Users/xxxx/xxxx/')

# On windows, 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()
_images/output_6_0.png
transbigdata.plot_map(plt, bounds, zoom='auto', style=0, printlog=False)

Plot the basemap

Parameters:
  • plt (matplotlib.pyplot) – Where to plot

  • 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 the magnification level of the base map, the longer the loading time. Generally, the range for a single city is between 12 and 16

  • printlog (bool) – Show log

  • style (number) – The style of map basemap can be 1-10, as follows

Basemap style 1:streets

_images/1.png

Basemap style 2:outdoors

_images/2.png

Basemap style 3:satellite

_images/3.png

Basemap style 4:light

_images/4.png

Basemap style 5:dark

_images/5.png

Basemap style 6:light-ch(中文)

_images/6.png

Basemap style 7:ice creem

_images/7.png

Basemap style 8:night

_images/8.png

Basemap style 9:terrain

_images/9.png

Basemap style 10:basic blue

_images/10.png

Basemap style 11:light(无标注)

_images/11.png

Basemap style 12:dark(无标注)

_images/12.png

Self-defined style

support selfdefined mapbox style

tbd.plot_map(plt,bounds,zoom = 11,style = 'mapbox://styles/ni1o1/cl38pljx0006r14qp7ioy7gcc')

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 compass and scale for a map

Parameters:
  • 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) – size of the text

  • compasssize (number) – size of the compass

  • accuracy (number) – Length of scale bar (m)

  • unit (str) – ‘KM’,’km’,’M’,’m’, the scale units

  • style (number) – 1 or 2, the style of the scale

  • 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])