Introduction to Mapbox

Founded in 2010 by Eric Gunderson, Mapbox has grown rapidly and has become a leader in the mapping renaissance. focused on providing custom base map blocks for map and application developers, Mapbox has positioned itself as the leading software company for web maps and mobile applications. In addition to the base map style toolset already widely used by program developers and cartographers, they also offer mapping tools written in Python and JavaScript. By combining these two languages in one package Mapbox has released the new “MapboxGL-Jupyter” Python module, which allows the creation of instant visualizations of data in the Jupter Notebook environment. In addition, Mapbox also provides the Mapbox Python SDK that allows API access to the account service.

MapboxGL.js

MapboxGL is based on Leaflet.js. Building on Leaflet, MapboxGL.js integrates with the WebGL library, which leverages HTML5 canvas tags to support Web graphics without plugins. MapboxGL.js supports vector slicing, as well as smooth scaling and panning for 3D environments. It supports GeoJSON overlays as well as markers and shapes. Events (including clicks, zooms and pans) can be used to trigger data processing functions, making it ideal for interactive web mapping applications.

Mapbox Python SDK

The Mapbox Python SDK is used to access most Mapbox services, including directions, geocoding, analytics, and datasets. The underlying access to cloud-based services supports data editing and uploading, system management, and location-based queries, allowing organizations to integrate and extend them with native GIS. Although the MapboxGL-Jupter tool does not rely on this SDK, it is very useful for data uploads and queries.

Use of Mapbox

Official sample code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
import pandas as pd
from mapboxgl.utils import create_color_stops, df_to_geojson
from mapboxgl.viz import CircleViz


# Load data from sample csv
data_url = 'https://raw.githubusercontent.com/mapbox/mapboxgl-jupyter/master/examples/data/points.csv'
df = pd.read_csv(data_url)

# Must be a public token, starting with `pk`
token = os.getenv('MAPBOX_ACCESS_TOKEN')

# Create a geojson file export from a Pandas dataframe
df_to_geojson(df, filename='points.geojson',
              properties=['Avg Medicare Payments', 'Avg Covered Charges', 'date'],
              lat='lat', lon='lon', precision=3)

# Generate data breaks and color stops from colorBrewer
color_breaks = [0,10,100,1000,10000]
color_stops = create_color_stops(color_breaks, colors='YlGnBu')

# Create the viz from the dataframe
viz = CircleViz('points.geojson',
                access_token=token,
                height='400px',
                color_property = "Avg Medicare Payments",
                color_stops = color_stops,
                center = (-95, 40),
                zoom = 3,
                below_layer = 'waterway-label'
              )
viz.show()

Execution result: The coordinate points are not shown on the map. The following figure.

The reason for this is that the ‘points.geojson’ file read by CircleViz is not read by a relative path, but by a spliced URL, i.e. the request URL is: 127.0.0.1:8888/points.geojson, so it will result in the following error.

1
404 GET /points.geojson (::1) 41.88ms referer=None

Solution: Store the points.geojson file in memory instead of directly as a file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
import pandas as pd
from mapboxgl.utils import create_color_stops, df_to_geojson
from mapboxgl.viz import CircleViz

# Load data from sample csv
# data_url = 'https://raw.githubusercontent.com/mapbox/mapboxgl-jupyter/master/examples/data/points.csv'
data_url = 'points.csv'
df = pd.read_csv(data_url)
# Must be a public token, starting with `pk`
token = os.getenv('MAPBOX_ACCESS_TOKEN')

# Create a geojson file export from a Pandas dataframe
points_geojson = df_to_geojson(df, properties=['Avg Medicare Payments', 'Avg Covered Charges', 'date'], lat='lat', lon='lon', precision=3)

# Generate data breaks and color stops from colorBrewer
color_breaks = [0,10,100,1000,10000]
color_stops = create_color_stops(color_breaks, colors='YlGnBu')

# Create the viz from the dataframe
viz = CircleViz(points_geojson,
                access_token=token,
                height='500px',
                color_property = "Avg Medicare Payments",
                color_stops = color_stops,
                center = (-95, 40),
                zoom = 3,
                below_layer = 'waterway-label'
)
viz.show()

Implementation results.

More usage examples.

Personal summary: the overall functionality does not lose folium, including heat maps, Choropleth maps, etc. are supported, and also supports custom tiles. Mainly Sean Gillies, the head of Mapbox open source, is the main developer of Shapely, Fiona and Rasterio. Vladimir Agafonkin, developer of Leaflet, has been working for Mapbox since 2013.

Reference links.