Skip to content

nidhirana24/Geofence-event-processing-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

17 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš– Geofence Event Processing Service

A lightweight, production-aware service for ingesting GPS events, tracking vehicle states in real time, and detecting geofence boundary transitions (enter/exit events). Built for reliability, clarity, and operational visibility.

πŸ“Œ Overview

This is a location-based service for a taxi company. Vehicles send GPS coordinates to an HTTP endpoint, and the system determines: When a vehicle enters a geofence When a vehicle exits a geofence What zone a vehicle is currently in Whether a vehicle is moving, idle, or outside all zones

The design focuses on clean architecture, performant in-memory state management, and practical engineering choices that scale.

✨ Key Features βœ” Real-time GPS event ingestion- Vehicles push location events directly to /location. Each event is validated, logged, and processed instantly.

βœ”Overlapping Zone Resolution- Zones have a priority field to correctly determine the vehicle's zone status when multiple geofences overlap at the same location.

βœ” Geofence enter/exit detection- The service defines simple polygon-based zones. Using point-in-polygon logic, we determine whether a new coordinate crosses a boundary relative to previous state.

βœ” Vehicle state tracking In-memory cache maintains: Last known coordinates Current geofence zone Timestamp of last update Movements across zones

βœ” Query API /vehicle/ returns the vehicle’s latest zone, last location, and last transition.

βœ” Lightweight, fast, and easy to deploy Zero external dependencies beyond FastAPI and standard Python libs.

πŸ— Architecture & Design Decisions

  1. FastAPI for the service layer FastAPI gives: Async request handling Automatic validation via Pydantic Auto-generated Swagger docs Easy extensibility This is ideal for real-time event ingestion.

  2. In-memory state store A dictionary holds vehicle states: O(1) lookups Simple and extremely fast Perfect for this challenge timeframe In a real deployment I'd use Redis β†’ strong consistency, TTL expiry, horizontal scalability.

  3. Zones defined as simple polygons Flexible for scaling beyond rectangles. Point-in-polygon is implemented with a clean, readable function.

  4. Event-driven detection Each location update triggers: Zone lookup Previous vs current zone comparison Emit enter/exit events Update state store This approach minimizes unnecessary computations.

  5. Operational Awareness Built-In Input validation Error responses with detailed context Logging on every critical action Clear separation of concerns

If deployed, hooking this into Prometheus or OpenTelemetry would be trivial.

πŸš€ How to Run

  1. Clone git clone cd geofence-service

  2. Install dependencies pip install -r requirements.txt

  3. Start the service uvicorn main:app --reload

  4. Access API docs http://localhost:8000/docs

πŸ“‘ API Endpoints POST /location

Ingest a GPS event.

{ "vehicle_id": "TX-102", "latitude": 28.5531, "longitude": 77.2079, "timestamp": "2025-11-30T18:23:12Z" }

GET /vehicle/{vehicle_id}

Check current zone, last location, and transitions.

🧠 Assumptions -GPS updates are reasonably frequent -No authentication needed for this challenge -Zones are static -In-memory store is acceptable given challenge scope -Vehicles do not send absurdly noisy GPS coordinates

πŸ” Edge Cases Handled -Missing/invalid coordinates -Vehicle sending first-ever location event -Boundary-edge coordinates (treated as inside) -Zone to no-zone transitions -No-zone to zone transitions -Rapid oscillation filtering (simple debounce could be added)

🧩 Directory Structure project/ β”œβ”€ src/ β”‚ β”œβ”€ api/ β”‚ β”‚ β”œβ”€ events.py # POST /v1/events β”‚ β”‚ └─ vehicles.py # GET /v1/vehicles/{id}/zone β”‚ β”œβ”€ services/ β”‚ β”‚ β”œβ”€ geofence_service.py # zone detection, transition logic,point-in-polygon checks β”‚ β”‚ └─ vehicle_service.py # state management, validation β”‚ β”œβ”€ models/ β”‚ β”‚ β”œβ”€ schemas.py # Pydantic request/response schemas(LocationEvent, Zone, ...) β”‚ β”‚ └─ events.py # TransitionEvent model β”‚ β”œβ”€ storage/ β”‚ β”‚ β”œβ”€ memory_store.py # simple in-memory vehicle state store β”‚ └─ main.py # FastAPI entrypoint + include routers β”œβ”€ zones.json # stores zones information - {zoneid, name, priority, polygon} β”œβ”€ README.md β”œβ”€ requirements.txt

πŸ“ˆ Performance & Scalability Notes

  1. Current performance:
  • O(1) lookup for all vehicles
  • Zones check in < 0.05 ms
  • To scale production-wise
  • Debounce logic to prevent zone flip-flopping
  • Add Kafka/EventBridge for reliable ingestion
  • Add background workers for zone checks
  • Configurable zones via API

πŸ›  Future Improvements If I had more than 2 hours, I’d add:

  • WebSocket live tracking dashboard
  • Replay engine for historical events
  • Heatmap analytics for vehicle density
  • Horizontal scaling via Kubernetes
  • Move state to Redis
  • Add Kafka/EventBridge for reliable ingestion
  • Persistence layer for long-term tracking history

🏁 Summary This codebase is designed to reflect real engineering judgment, not just algorithmic correctness. It is intentionally structured, scalable, and production-oriented β€” while still being lightweight enough to meet the challenge's 2-hour constraint.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published