Fall 2024

Ranking Map Routes by Safety

Chrome extension that re-ranks Google Maps routes by predicted safety, scored from 350,000 rows of crime, accident, and weather data.

  • ml
  • data
  • frontend

Outcome

A Chrome extension that ranks Google Maps routes by safety rather than speed, scoring them with a Decision Tree Regressor over a 350,000-row dataset of crime, accident, and weather data. The team cut end to end scoring from 2 minutes to 7 seconds, a 17x speedup, by taking a dataset reload out of the inner loop.

I wrote the Flask orchestration layer: fetching alternative routes from the Google Maps Directions API, running each through the safety calculation, and assembling the response. I also built the live weather integration. Team of four at HackHarvard 2024, with Maxence Gilloteaux, Sam Chen and Marmik Patel.

The route did not get shorter — the work inside each step did. Parsing the dataset once instead of once per segment is the whole of the 17× speedup.Scoring walks a route segment by segment. In the first pass each segment re-reads the whole 350,000-entry crash dataset from disk, and a work bar fills to the end. The load is then hoisted out of the loop so the dataset is parsed once into memory, each segment becomes a set intersection against it, and the same bar barely moves.
Google Maps directions from Dorchester to 400 Tremont Street, with a Time and Safety toggle set to Safety. Three routes are listed and labelled Safe at 8 minutes, Low Risk at 9 minutes, and High Risk at 21 minutes.

The extension adds a Safety toggle beside Maps' own Time control and re-labels each route. The fastest route is not the safest one here, which is the whole idea.

  1. 1.Safety replaces time as the ranking criterion
  2. 2.Each alternative gets a risk label from the model's score, not from traffic

Motivation

Routing tools optimize for speed; SafeRoute ranks for safety instead. The team's stated motivation was that safety is a universal concern whether the risk is a collision or walking alone at night.

Built over a single hackathon weekend at HackHarvard 2024.

Experience, learning & skills

The first working version took 2 minutes to score a route and had to be brought down to 7 seconds before it was usable. The team also hit integration friction between the Python backend and the JavaScript extension, and a steep learning curve on data cleaning and encoding for the model.

  • Built the Flask service that fetches alternative routes from the Google Maps Directions API and returns them ranked by safety
  • Wrote the safety-score calculation that combines crash, crime, and weather signals into a single per-route number
  • Integrated a live weather API into the score, including key handling and a fallback for missing readings

Technical details

Backend in Python: Requests, Beautiful Soup, and Pandas to collect and clean the data, and scikit-learn's Decision Tree Regressor to score routes. A 60,000-row accident CSV was joined against geographic data to produce the 350,000-row JSON the model reads. The frontend is plain JavaScript, HTML, and CSS injected into Google Maps.

The 2-minute to 7-second speedup came from the scoring loop. It walks a route segment by segment, intersecting the road network's junctions with the crash dataset. The original version re-opened and re-parsed the entire 350,000 entry JSON from disk on every segment, rebuilding a set of its keys each time, so an N-segment route parsed the whole dataset N times. Hoisting that load out of the loop left each segment as a set intersection against an in-memory dictionary. The same pass rebuilt intersection extraction to construct a node-coordinate dictionary once instead of rescanning the XML per node, and added gzip to the OpenStreetMap requests.

Before and after the SafeRoute scoring optimizationBefore: 2 minutesevery segment re-reads and re-indexes the whole datasetparse 350k rowsparse 350k rowsparse 350k rowsparse 350k rows× N segmentsAfter: 7 secondsread once, then each segment is a set lookupparse 350k rows× N segmentsone segment: a set intersection against the in-memory index
Why the 17x speedup worked. The block widths show repetition, not measured time.
How a route becomes a safety score60k crash rowsBoston crash CSVjoin to geographyOpenStreetMap nodes350k indexed rowskeyed by coordinatelive weatherfetched per requestDecision Treescikit-learn regressorranked routesinjected into Maps
From a 60,000 row crash CSV to a ranked list of routes in the Maps sidebar.