Skip to content

Interzoneism/TrafikAPI

Repository files navigation

TrafikAPI 🚗

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.

Features

  • 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

Quick Start

1. Clone and Install

git clone <repository-url>
cd TrafikAPI
pip install -r requirements.txt

2. Configure Environment

# 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/

3. Run the Backend

uvicorn app.main:app --reload

4. Run the Frontend (Development)

cd frontend
npm install
npm run dev

The frontend runs on http://localhost:3000 and proxies API requests to the backend.

5. Build for Production

cd frontend
npm run build

The 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!

API Documentation

Once running, visit:

Example API Request

# 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"}'

Project Structure

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

Configuration

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)

Running Tests

# Run all tests
pytest

# Run with verbose output
pytest -v

# Run specific test file
pytest tests/test_validation.py

Event Types

The 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

External Services

This application uses the following external services:

Error Handling

  • 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_KEY is not set

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published