A web application that displays Trafikverket traffic information along a driving route in Sweden. Enter origin and destination, and see incidents, roadworks, and road conditions on an interactive map.
- Route Planning: Enter place names or addresses to calculate driving routes
- Traffic Events: Shows incidents, roadworks, obstructions, and road conditions
- Interactive Map: Leaflet-based map with route visualization and event markers
- Event List: Clickable sidebar list that zooms to events on the map
- Smart Filtering: Only shows events within a configurable corridor around your route
- Caching: Built-in caching for geocoding, routing, and traffic data
git clone <repository-url>
cd TrafikAPI
pip install -r requirements.txt# Copy the example environment file
cp .env.example .env
# Edit .env and add your Trafikverket API key
# Get a free key at: https://api.trafikinfo.trafikverket.se/uvicorn app.main:app --reloadcd frontend
npm install
npm run devThe frontend runs on http://localhost:3000 and proxies API requests to the backend.
cd frontend
npm run buildThe built files will be in frontend/dist/ and can be served by any static file server.
Enter a route like "Stockholm" → "Uppsala" and see traffic events along the way!
Once running, visit:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
# GET request
curl "http://localhost:8000/api/route?origin=Stockholm&destination=Uppsala"
# POST request
curl -X POST "http://localhost:8000/api/route" \
-H "Content-Type: application/json" \
-d '{"origin": "Stockholm", "destination": "Uppsala"}'TrafikAPI/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI application entry point
│ ├── config.py # Settings from environment variables
│ ├── models/
│ │ ├── route.py # Route request/response models
│ │ └── traffic.py # Traffic event models
│ ├── routers/
│ │ └── routes.py # API endpoints
│ └── services/
│ ├── cache.py # In-memory caching
│ ├── geocoding.py # Nominatim geocoding
│ ├── routing.py # OSRM routing
│ └── trafikverket.py # Trafikverket API client
├── frontend/ # React frontend (Vite + TypeScript)
│ ├── src/
│ │ ├── components/ # React components
│ │ ├── context/ # React context for state management
│ │ ├── hooks/ # Custom React hooks
│ │ ├── services/ # API services
│ │ ├── types/ # TypeScript types
│ │ ├── utils/ # Utility functions
│ │ ├── App.tsx # Main App component
│ │ └── main.tsx # Entry point
│ ├── package.json
│ ├── vite.config.ts
│ └── tsconfig.json
├── tests/
│ ├── test_validation.py # Input validation tests
│ ├── test_corridor.py # Route corridor filtering tests
│ └── test_trafikverket.py # Trafikverket API tests
├── .env.example # Example environment configuration
├── requirements.txt # Python dependencies
├── pytest.ini # Pytest configuration
└── README.md # This file
All configuration is done via environment variables (see .env.example):
| Variable | Default | Description |
|---|---|---|
TRAFIKVERKET_API_KEY |
(required) | Your Trafikverket API key |
CORRIDOR_BUFFER_KM |
5.0 |
Buffer distance around route (km) |
CACHE_GEOCODE_TTL |
86400 |
Geocoding cache TTL (seconds) |
CACHE_ROUTING_TTL |
3600 |
Routing cache TTL (seconds) |
CACHE_TRAFIKVERKET_TTL |
300 |
Traffic data cache TTL (seconds) |
# Run all tests
pytest
# Run with verbose output
pytest -v
# Run specific test file
pytest tests/test_validation.pyThe application displays these types of traffic events:
| Type | Icon | Description |
|---|---|---|
| Accident | 🚨 | Traffic accidents |
| Roadwork | 🚧 | Road construction and maintenance |
| Obstruction | Obstacles on the road | |
| Weather | 🌧️ | Weather-related conditions |
| Road Condition | 🛣️ | Road surface conditions |
| Traffic Message | ℹ️ | General traffic information |
This application uses the following external services:
- Nominatim - Geocoding (place names to coordinates)
- OSRM - Route calculation
- Trafikverket Open API - Traffic data
- OpenStreetMap - Map tiles
- 400 Bad Request: Invalid input (unknown location, invalid coordinates)
- 502 Bad Gateway: Upstream service failure (routing, geocoding, or traffic API)
- API Key Missing: Warning displayed if
TRAFIKVERKET_API_KEYis not set
MIT License