SafeWalk is a web application designed to provide pedestrians with safe route recommendations based on their preferences on the crime type they want to avoid. The app integrates with Google Maps API to display an interactive crime map and calculate the safest route between two locations, considering user-specified crime risks. The current crime data was in Tower Hamlets, London, gathered from police.uk.
1. Interactive Crime Map
Integration with Google Maps API and a Supabase crime dataset. Users can filter and display different types of crimes on the map.
2. Safe Route Recommendation
Users input start and end locations. According to the crime type they want to avoid, the system generates multiple routes using Google Maps Directions API, calculates the crime risk for each, and recommends the safest route with the risk score.
- Python 3.12.x
pip(Python package installer)- Supabase project
- Google Maps API keys:
GOOGLE_MAPS_API_KEYfor backend Directions APIGOOGLE_MAPS_BROWSER_API_KEYfor frontend Maps JavaScript API (can be the same key for local development, but use a restricted browser key in production)
Google services required for this app:
- Maps JavaScript API
- Places API
- Directions API
Ensure billing is enabled in Google Cloud.
- flask
- googlemaps
- haversine
- supabase
- python-dotenv
- pytest
Please make sure to install these dependencies before running the program.
1. Create and activate the virtual environment
python3.12 -m venv .venv
source .venv/bin/activate
2. Install dependencies from requirements.txt
pip install -r requirements.txt
3. Add environment variables in .env
GOOGLE_MAPS_API_KEY=your_server_key_here
GOOGLE_MAPS_BROWSER_API_KEY=your_browser_key_here
SUPABASE_URL=your_supabase_project_url
SUPABASE_SERVICE_ROLE_KEY=your_supabase_service_role_key
Security note:
- Never expose
SUPABASE_SERVICE_ROLE_KEYto frontend code. - Restrict Google browser key by HTTP referrer.
Run this SQL in Supabase SQL Editor:
create table if not exists public.crimes (
id bigint generated always as identity primary key,
month date not null,
longitude double precision not null,
latitude double precision not null,
crime_type text not null
);
create index if not exists idx_crimes_crime_type on public.crimes (crime_type);
create index if not exists idx_crimes_month on public.crimes (month);Then import crimedata.csv into public.crimes with these mappings:
Month->monthlongitude->longitudelatitude->latitudeCrimeType->crime_type
1. Start the Application
python app.py
2. Access the Application
Open your web browser and go to http://127.0.0.1:5000/
Use the DB health endpoint:
curl http://127.0.0.1:5000/health/dbExpected success response:
{"ok": true, "count": 1234}Run all tests:
python -m pytest -q
Run one test file:
python -m pytest tests/test_app.py -q