# The Strava heatmap as contextual map tiles with contextily¶

This document quickly demonstrates how to source form the Strava heatmap to obtain map tiles that can be easily integrated in any modern Python geo-data workflow.

In [1]:
# Display on the notebook
%matplotlib inline

# Import contextily
import contextily as ctx


## Basic example¶

First we will set the link for the Strava tiles (taken from here):

In [2]:
src = 'https://heatmap-external-b.strava.com/tiles/all/bluered/tileZ/tileX/tileY.png'


Then it's all a matter of using the Place API (or any other way that contextily allows) to get the map delivered to your notebook. Let's play with Liverpool (UK), for example:

In [15]:
lvl = ctx.Place('Liverpool', url=url)

ctx.plot_map(lvl.im);


## A more sophisticated illustration¶

In [3]:
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
import rasterio as rio
from rasterio.plot import show as rioshow
from shapely.geometry import mapping as shply2json


Because contextily allows you to combine webtile maps with any other data you may have, you can easily build more sophisticated maps. In this case, we will recreate the London boroughs example with Strava data. Here is what we will attempt to replicate:

You can download the original borough data from here and a reprojected GeoJSON from here, which is what we will use:

In [4]:
brs = gpd.read_file('boroughs.geojson')
brs.plot();


In order to render the images faster and not have to rely on the remote server to pull the tiles, we will first download them all at once and store them as a tiff raster file (keep in mind this might take a little bit to run):

In [5]:
%%time
_ = ctx.bounds2raster(minX, minY, maxX, maxY, 12, raster_link, url=src)

CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 3.81 µs


Just to get a sense, this is what the entire area of London looks like through the lens of Strava data:

In [236]:
r_src = rio.open(raster_link)
f, ax = plt.subplots(1, figsize=(12, 12))