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.
# Display on the notebook
%matplotlib inline
# Import contextily
import contextily as ctx
First we will set the link for the Strava tiles (taken from here):
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:
lvl = ctx.Place('Liverpool', url=url)
ctx.plot_map(lvl.im);
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
import rasterio as rio
from rasterio import mask
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:
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):
%%time
raster_link = 'london.tiff'
_ = ctx.bounds2raster(minX, minY, maxX, maxY, 12, raster_link, url=src)
Just to get a sense, this is what the entire area of London looks like through the lens of Strava data:
r_src = rio.open(raster_link)
f, ax = plt.subplots(1, figsize=(12, 12))
rioshow(r_src.read(), ax=ax)
ax.set_axis_off()
plt.show()