Visualizing Geospatial traffic data in Kepler.
Fast, elegant geospatial visualizations of hundreds of thousands of traffic data points using Kepler.gl and HERE traffic data
Kepler.gl is Uber’s open source geospatial toolbox. Often, visualizing large-scale geospatial data sets can be quite challenging . There are many Python packages including Folium, GeoPandas, and Plotly that do a great job; but it can be a challenge to find the right plot backgrounds. It can also be computationally challenging on a local machine to plot thousands or hundreds of thousands of data points.
This is where Kepler.gl has a huge advantage. Kepler.gl is a browser based framework that loads CSV, JSON, or GeoJSON data easily and produces elegant visualizations with minimal formatting. In this example, I go through visualizing hundreds of thousands of traffic data points obtained from the HERE Traffic API in a few seconds.
Let’s get started!
Obtaining Traffic Data:
I’ve obtained traffic data from the HERE Technologies traffic flow API. I’ve written a few articles focused on how to extract traffic flow data:
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.cm as cm
from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import XML, fromstring, tostring
import pandas as pd
page = requests.get('https://traffic.api.here.com/traffic/6.2/flow.xml?app_id='+API_KEY+'&bbox=42.37,-71.20;42.22, -70.98&responseattributes=sh,fc')
file = open('speeds.txt', "w")
soup = BeautifulSoup(open('speeds.txt'))
roads = soup.find_all('fi')
for road in roads:
#for j in range(0,len(shps)):
myxml = fromstring(str(road))
for child in myxml:
if('fc' in child.attrib):
if('cn' in child.attrib):
if('su' in child.attrib):
if('ff' in child.attrib):
if((fc<=10) and (cn>=0.7)):
for j in range(0,len(shps)):
for i in range(0,int(len(latlong)/2)):
Saving the Files
Kepler.gl likes files to be saved as CSV, with distinct columns. If you want an instantaneous geospatial plot, you need to label the columns as ‘latitude’ and ‘longitude,’ for spatial locations, and a third column for the color palate (which is speed in the case of traffic information).
for i in range(0,len(lats)):
for j in range(len(lats[i])):
w=np.where((df['speeds']<0.5) & (df['speeds']>0.25))
w=np.where((df['speeds']<0.75) & (df['speeds']>0.5))
Visualizing in Kepler.gl
Now that we have saved the data in the right format, we can drag and drop — it’s that simple!
Loading files in Kepler.gl
You automatically get a nice image that can be saved.
Exporting Images from Kepler.gl
However, when you look closely — the color scale is shown according to quantile — which depends on the data and what proportion of the data is at what speed. For example, if most of the traffic is fast moving (usually the case late at night or very early in the morning), the color palate will be skewed towards these higher speeds.
However, this is not what we want. We want colors to clearly indicate absolute speeds: just like in Google Maps traffic. In this case, we choose colors based on quantized value, rather than quantile, as below.
Choosing whether to color based on quantized value or quantile in Kepler.gl
There are also options to change how the symbols look as well as the color palate. In doing so, we can generate some pretty elegant visualizations — as in the header image of this article!
Modifying the colors and symbols in Kepler.gl
Kepler.gl is great for generating elegant visualizations with minimal effort, relatively quickly. Plotting hundreds of thousands of points on my local machine using OSMnx and other Python libraries takes much longer in comparison.
However, where Kepler.gl does a good job in terms of visualizations with minimal inputs, one also encounters the problem of minimal customizability.
For example there are basically only 3 backgrounds — dark, light, and satellite. There could be cases where other types of backgrounds are needed.
But Kepler.gl does a great job of covering the geospatial visualization essentials.
Senior Data Scientist in NLP. Creator of https://www.answerchatai.com/