Find the safest walking paths in San Francisco using real-time crime and incident data
SafePath analyzes walking routes against real-time San Francisco crime statistics and 311 incident reports to recommend the safest paths to your destination.
git clone https://github.com/charliesturiale/AWS_Hackathon2025.git
cd AWS_Hackathon2025SETUP.batThis will:
- Check for Python 3.9+ and Node.js 16+
- Install all dependencies automatically
- Guide you through API key configuration
START_HERE.bat- Opens backend (http://localhost:8000)
- Opens frontend (http://localhost:3000)
- Browser opens automatically!
That's it! ๐
- Python 3.9+ - Download here
โ ๏ธ During installation, CHECK "Add Python to PATH"
- Node.js 16+ - Download here
- Use LTS version (default settings are fine)
- Go to data.sfgov.org
- Click "Sign Up" (free account)
- Go to Profile โ Copy "App Token"
- Go to graphhopper.com
- Click "Get started for free"
- Copy API key from dashboard
- Note: Free tier = ~5 requests/minute (perfect for testing)
- Real-Time Data: Crime & 311 incidents updated every 10 minutes
- Smart Routing: Generates 3 optimized route variations
- Offline Optimization: Fast safety improvements (no extra API calls)
- Time-Decay Algorithm: Recent incidents weighted more heavily
- Distance-Based Detection: 200m safety buffer for incidents
- Interactive Map: Visual routes with safety scores
- Mobile Responsive: Works on all devices
Test these San Francisco routes:
- Short: "Union Square, SF" โ "Ferry Building, SF"
- Long: "Golden Gate Park" โ "Fisherman's Wharf"
- Neighborhood: "Mission District" โ "Castro District"
Click to expand manual installation steps
git clone https://github.com/charliesturiale/AWS_Hackathon2025.git
cd AWS_Hackathon2025Create a file named .env in the project root with:
# DataSF API Configuration
DATASF_API_TOKEN=your_datasf_token_here
DATASF_CRIME_API=https://data.sfgov.org/resource/gnap-fj3t.json
DATASF_311_API=https://data.sfgov.org/resource/vw6y-z8j6.json
# GraphHopper API Configuration
GRAPHHOPPER_API_KEY=your_graphhopper_key_here
# Server Configuration
BACKEND_PORT=8000
FRONTEND_URL=http://localhost:3000
DATA_REFRESH_INTERVAL=10cd backend
python3 -m venv venv
source venv/bin/activate # Mac/Linux
# or
venv\Scripts\activate # Windows
pip install -r requirements.txtcd frontend
npm installcd backend/app
source ../venv/bin/activate # Mac/Linux
python -m uvicorn main:app --reload --port 8000cd frontend
npm startNavigate to http://localhost:3000
Solution: Install Python 3.9+ and make sure to check "Add to PATH" during installation.
Solution: Install Node.js 16+ from nodejs.org
Solution (Windows):
netstat -ano | findstr :8000
taskkill /PID <process_id> /FSolution: Stop other React apps or close terminals running on port 3000.
Solution:
- Wait 60 seconds (rate limit resets)
- Free tier = ~5 requests/minute
- Consider upgrading for production
Checklist:
- Is backend running? Test:
curl http://localhost:8000/api/health - Check CORS settings in
backend/app/main.py - Verify
.envfile exists in project root
Solution:
cd backend
venv\Scripts\activate
pip install -r requirements.txtGET /api/healthResponse:
{
"status": "healthy",
"data": {
"crime_incidents": 160,
"311_incidents": 50,
"last_fetch": "2025-10-29T20:08:09.061345"
}
}POST /api/routes
Content-Type: application/json
{
"origin": "Union Square, San Francisco",
"destination": "Pier 39, San Francisco"
}Response:
{
"routes": [
{
"id": 1,
"name": "Safest Route",
"description": "Safest path with minimal risk exposure",
"distance": "2.5 mi",
"time": "52 min",
"safetyScore": 85,
"total_risk": 1.51,
"coordinates": [{"lat": 37.7830, "lng": -122.4060}, ...],
"color": "#10b981"
}
],
"originCoords": {"lat": 37.7829, "lng": -122.4060},
"destCoords": {"lat": 37.8098, "lng": -122.4103}
}GET /api/data/statsHigh-risk incidents (72-hour decay):
risk = (max(0, 3-3t/72))ยฒ ร exp(-dยฒ/0.02)
Medium/low-risk incidents (24-hour decay):
risk = (max(0, w-wt/24))ยฒ ร exp(-dยฒ/0.02)
Where:
t= hours since incidentd= distance from route (km)w= risk weight (1=low, 2=medium, 3=high)
| Risk Level | Weight | Decay | Examples |
|---|---|---|---|
| High | 3 | 72hr | Robbery, Assault, Battery, Explosives |
| Medium | 2 | 24hr | Purse Snatch, Fights, Burglary |
| Low | 1 | 24hr | Suspicious Person, Threats, Harassment |
| Encampments | 2 | None | Open encampments (until closed) |
- Generate Routes: Request 3 variations from GraphHopper
- Analyze Segments: Find areas within 200m of incidents
- Calculate Waypoints: Create safe alternatives (250m from threats)
- Offline Injection: Add waypoints using existing route anchors
- Validate: Verify improved safety with geodesic distance
- Rank: Sort by safety score and return top 3
AWS_Hackathon2025/
โโโ backend/
โ โโโ app/
โ โ โโโ main.py # FastAPI application
โ โ โโโ data_fetcher.py # DataSF integration
โ โ โโโ risk_scorer.py # Risk calculation engine
โ โโโ cache/ # Cached incident data
โ โโโ requirements.txt # Python dependencies
โ โโโ production_test_suite.py # Test suite
โโโ frontend/
โ โโโ src/
โ โ โโโ components/ # React components
โ โ โโโ services/ # API clients
โ โโโ package.json # Node dependencies
โ โโโ public/ # Static assets
โโโ .env # API keys (DO NOT COMMIT)
โโโ .gitignore # Git ignore rules
โโโ SETUP.bat # First-time setup
โโโ START_HERE.bat # Launch application
โโโ README.md # This file
curl http://localhost:8000/api/healthcd backend
python production_test_suite.pyTests include:
- API verification
- Route calculation accuracy
- Risk scoring validation
- Performance baselines
- Load testing (respects API limits)
Click to expand production deployment guide
Backend .env (Production):
DATASF_API_TOKEN=your_production_token
GRAPHHOPPER_API_KEY=your_production_key
DATASF_CRIME_API=https://data.sfgov.org/resource/gnap-fj3t.json
DATASF_311_API=https://data.sfgov.org/resource/vw6y-z8j6.json
BACKEND_PORT=8000
FRONTEND_URL=https://your-domain.com
DATA_REFRESH_INTERVAL=10Update backend/app/main.py:
app.add_middleware(
CORSMiddleware,
allow_origins=["https://your-domain.com"], # Your frontend domain
allow_credentials=True,
allow_methods=["GET", "POST"],
allow_headers=["*"],
)Using Gunicorn (recommended):
cd backend
gunicorn app.main:app \
--workers 4 \
--bind 0.0.0.0:8000 \
--worker-class uvicorn.workers.UvicornWorkercd frontend
npm run build
# Deploy build/ folder to static hosting (Vercel, Netlify, S3, etc.)| Service | Backend | Frontend | Difficulty |
|---|---|---|---|
| Heroku | โ Free tier | โ | Easy |
| Railway | โ Free tier | โ Free tier | Easy |
| Vercel | โ | โ Free tier | Easy |
| AWS | EC2 | S3+CloudFront | Medium |
| DigitalOcean | Droplet | Spaces | Medium |
Backend:
- FastAPI (Python 3.9+)
- Uvicorn ASGI server
- APScheduler (background tasks)
- geopy, numpy, pandas
Frontend:
- React 18 + TypeScript
- Tailwind CSS + shadcn/ui
- React Leaflet (maps)
- Axios
APIs:
- GraphHopper (routing/geocoding)
- DataSF Crime Data
- DataSF 311 Incidents
cd backend/app
source ../venv/bin/activate # Mac/Linux
uvicorn main:app --reload --port 8000cd frontend
npm start- Backend: PEP 8 (Python)
- Frontend: ESLint + Prettier (TypeScript/React)
- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open Pull Request
MIT License - See LICENSE file for details
- Crime data from DataSF
- Routing powered by GraphHopper
- Map tiles from OpenStreetMap
Having issues?
- Check Troubleshooting section above
- Search existing GitHub issues
- Open a new issue with:
- Error message
- Steps to reproduce
- OS and Python/Node versions
# First time setup
SETUP.bat
# Launch application
START_HERE.bat
# Individual backend
start-backend.bat
# Individual frontend
start-frontend.bat
# Test backend
curl http://localhost:8000/api/health
# Run tests
cd backend && python production_test_suite.pyMade with โค๏ธ for safer walking in San Francisco