Skip to content

Latest commit

 

History

History
138 lines (96 loc) · 3.32 KB

File metadata and controls

138 lines (96 loc) · 3.32 KB

SafeWalk: Personalised Safe Route Recommendation App

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.

Features

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.

Screenshot 2024-08-19 at 21 39 42 Screenshot of the app

Prerequisites

  • Python 3.12.x
  • pip (Python package installer)
  • Supabase project
  • Google Maps API keys:
    • GOOGLE_MAPS_API_KEY for backend Directions API
    • GOOGLE_MAPS_BROWSER_API_KEY for 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.

Getting Started

Dependencies

  • flask
  • googlemaps
  • haversine
  • supabase
  • python-dotenv
  • pytest

Please make sure to install these dependencies before running the program.

Installing

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_KEY to frontend code.
  • Restrict Google browser key by HTTP referrer.

Supabase Table Setup

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 -> month
  • longitude -> longitude
  • latitude -> latitude
  • CrimeType -> crime_type

Running the Application

1. Start the Application

python app.py

2. Access the Application Open your web browser and go to http://127.0.0.1:5000/

Verify Database Connection

Use the DB health endpoint:

curl http://127.0.0.1:5000/health/db

Expected success response:

{"ok": true, "count": 1234}

Running Tests

Run all tests:

python -m pytest -q

Run one test file:

python -m pytest tests/test_app.py -q