Skip to content

Roigo21/ubiwhereTest

Repository files navigation

Ubiwhere Traffic API

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.

Features

  • 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.

Technologies Used

  • 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)

Prerequisites

Installation & Setup

1. Clone the repository

git clone https://github.com/Roigo21/ubiwhereTest.git
cd ubiwhereTest

2. Configure Environment Variables

Create 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.1

3. Build and Run with Docker

docker-compose up --build

The API will be available at http://localhost:8000.

4. Database Migrations

Run migrations to set up the database schema:

docker-compose exec django-web python manage.py migrate

5. Create a Superuser (Optional)

docker-compose exec django-web python manage.py createsuperuser

Data Import

The project includes management commands to import initial data from CSV files.

Import Sensors

docker-compose exec django-web python manage.py import_sensors sensors.csv

Import Traffic Data

This command imports road segments and speed readings:

docker-compose exec django-web python manage.py import_traffic_data traffic_speed.csv

Note: Use the --clear flag if you want to remove existing data before importing.

API Endpoints

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.

API Documentation

Interactive API documentation is available at:

  • Swagger UI: http://localhost:8000/api/docs/
  • Schema: http://localhost:8000/api/schema/

Running Tests

To run the automated tests:

docker-compose exec django-web python manage.py test

Project Structure

  • api/: 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published