A Django-based REST API for managing and analyzing traffic data, including road segments, speed readings, and sensor information. The project uses PostGIS for geographical data support and is containerized with Docker.
- Road Segments Management: Track road segments and their geographical coordinates.
- Speed Readings: Record and analyze average speed readings on specific road segments.
- Traffic Intensity Analysis: Categorize road segments by traffic intensity (Low, Medium, High).
- Sensor Tracking: Manage sensor information and data.
- Car Readings: Monitor car-specific data and readings.
- Geographical Data: Powered by PostGIS and GeoDjango.
- Data Import: Management commands to import data from CSV files.
- API Documentation: Interactive documentation with Swagger/OpenAPI.
- Python 3.13
- Django 6.0
- Django REST Framework (DRF)
- PostGIS 15 (PostgreSQL extension for GIS)
- GeoDjango
- Docker & Docker Compose
- drf-spectacular (OpenAPI 3.0 documentation)
git clone https://github.com/Roigo21/ubiwhereTest.git
cd ubiwhereTestCreate a .env file in the root directory (you can copy the provided example if available or use these defaults):
DB_NAME=ubiwhere_db
DB_USER=root
DB_PASSWORD=root
DB_HOST=db
DB_PORT=5432
SECRET_KEY=your-secret-key-here
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1docker-compose up --buildThe API will be available at http://localhost:8000.
Run migrations to set up the database schema:
docker-compose exec django-web python manage.py migratedocker-compose exec django-web python manage.py createsuperuserThe project includes management commands to import initial data from CSV files.
docker-compose exec django-web python manage.py import_sensors sensors.csvThis command imports road segments and speed readings:
docker-compose exec django-web python manage.py import_traffic_data traffic_speed.csvNote: Use the --clear flag if you want to remove existing data before importing.
The API provides the following main endpoints:
GET /api/road-segments/: List all road segments.POST /api/road-segments/: Create a road segment.GET /api/road-segments/{id}/: Detailed view of a segment, including recent readings.DELETE /api/road-segments/{id}/: Delete a segment, including recent readings.GET /api/road-segments/by_traffic_intensity/?level={baixa|média|alta}: Filter segments by traffic intensity.GET /api/road-segments/{id}/statistics/: Get speed statistics for a specific segment.GET /api/speed-readings/: List all speed readings.POST /api/speed-readings/: Create a speed reading.GET /api/sensor/: Manage sensors.POST /api/sensor/: Create a new Sensor.GET /api/car/: Manage cars.POST /api/car/: Create a new car entry.GET /api/car/by-license-plate/{license_plate}: Shows the readings of a specific car, from the last 24 hours.GET /api/car-readings/: Manage car readings.POST /api/car-readings/: Add a new car reading. Creates the Car entry if not existing.
Interactive API documentation is available at:
- Swagger UI:
http://localhost:8000/api/docs/ - Schema:
http://localhost:8000/api/schema/
To run the automated tests:
docker-compose exec django-web python manage.py testapi/: Main application logic.model/: Database models (RoadSegment, SpeedReading, Sensor, etc.).view/: DRF ViewSets.serializer/: DRF Serializers.management/commands/: Custom scripts for data import.tests/: Automated tests.
config/: Project configuration (settings, urls).Dockerfile&docker-compose.yml: Containerization configuration.requirements.txt: Python dependencies.