This code calculates cycling Level of Traffic Stress for all road and path segments in a region using data from Open Street Map.
You can view the LTS map for GTA at https://bikespace.ca/lts-map
This code is adapted from Bike Ottawa's LTS code, modified to include Level of Traffic Stress for intersections. This is code is forked and adapted from Madeliene Bonsma-Fisher's LTS_OSM implementation, it has been adapted to be have a more generic and adaptable use, majority if not all of the logic remains intact from her implementation.
- Setup a python virtual environment
python3 -m venv venv
#active the virtualenv
source venv/bin/activate
#Install the dependencies
pip install -r requirements.txt- Create a query JSON file listing the areas you want to study.
- Go to openstreetmap.org, search for the region (e.g. "Victoria"), click the matching relation, and scroll to find the
wikidatatag (e.g.Q2132). - Create a JSON file (see
query/gta.jsonfor an example) with anareasarray:
{ "areas": [ { "name": "victoria", "wikidata": "Q2132" } ] } - Go to openstreetmap.org, search for the region (e.g. "Victoria"), click the matching relation, and scroll to find the
- Run the script to download OSM data and calculate LTS:
# Download from Overpass and calculate LTS for all areas in the query file
python lts_osm/lts_osm.py --query-json-file query/gta.json
# Outputs are saved to a timestamped run directory: output/runs/<timestamp>/
# Re-run LTS calculation using already-downloaded XML files (skips Overpass download)
python lts_osm/lts_osm.py --downloaded-xml-json-map output/runs/<timestamp>/areas_xml_file_path.json
# For help:
python lts_osm/lts_osm.py --help
usage: lts_osm.py [-h] (--query-json-file QUERY_JSON_FILE | --osm-file OSM_FILE | --downloaded-xml-json-map DOWNLOADED_XML_JSON_MAP)
Calculates the Level of Traffic Stress from Open Street Map data.
options:
-h, --help show this help message and exit
--query-json-file QUERY_JSON_FILE
Path to query json file indicating which area(s) to download from OSM
--osm-file OSM_FILE Path to a single downloaded OSM XML file
--downloaded-xml-json-map DOWNLOADED_XML_JSON_MAP
Path to a json file mapping areas to their downloaded xml paths- Plot the results with
lts_osm/lts_plot.py
# Supply the lts csv files generated by lts_osm.py and the name of the city
python lts_osm/lts_plot.py --lts-csv-file output/runs/<timestamp>/lts_csv/all_lts_toronto.csv --gdf-nodes-file output/runs/<timestamp>/lts_csv/gdf_nodes_toronto.csv --city "Toronto"To render the LTS dataset on bikespace.ca we convert the outputted gejson into PMTiles.
PMTiles is a single-archive format for tile data.
It is a 2-step process, first the geojson has to be converted into MapboxTile format mbtiles and then converted to pmtiles.
To convert the resulting geojson file you'll need to install 2 tools:
# Converting to mbtiles
tippecanoe -l lts_toronto_filtered_1_4 -n "Level of Traffic Stress" --no-feature-limit --extend-zooms-if-still-dropping --coalesce-densest-as-needed --maximum-tile-bytes=2000000 -P -zg -D12 -o [output_file.mbtiles] [input_file.geojson]
# Converting to pmtiles
pmtiles convert [input_file.mbtiles] [output_file.pmtiles]