A production-ready Django REST API that calculates the most cost-effective fuel stops for road trips across the United States.
The API analyzes a route between two locations, identifies the lowest-priced fuel stations within a route corridor, estimates total fuel consumption and trip cost, and generates shareable navigation links for Google Maps and OpenStreetMap.
- π£οΈ Route optimization between any two US locations
- β½ Intelligent fuel-stop selection based on fuel prices
- π Cheapest station within a 30-mile route corridor
- π Vehicle range-aware planning (500-mile tank range)
- π° Detailed fuel cost estimation and breakdown
- πΊοΈ GeoJSON route geometry for frontend map rendering
- π Google Maps and OpenStreetMap route generation
- β‘ High-performance vectorized geospatial search using NumPy
- π Offline geocoding with bundled US city database
- π Consistent REST API responses and error handling
- β Automated test coverage
User Request
β
βΌ
Offline Geocoder
(US Cities Database)
β
βΌ
OSRM Routing Engine
(1 External API Call)
β
βΌ
Waypoint Sampling
(Every 400 Miles)
β
βΌ
Fuel Station Search
(Vectorized NumPy Haversine)
β
βΌ
Cheapest Station Selection
β
βΌ
Cost Calculation + Route Output
git clone https://github.com/syed-sadain/fuel-route-optimizer-api.git
cd fuel-route-optimizer-apipython -m venv venvvenv\Scripts\activatesource venv/bin/activatepip install -r requirements.txtpython manage.py migratepython manage.py runserverAPI Endpoint:
http://127.0.0.1:8000/api/route/
Returns API documentation and serves as a health-check endpoint.
Generate a fuel-optimized route.
{
"start": "Chicago, IL",
"end": "Los Angeles, CA"
}{
"success": true,
"route_summary": {
"total_distance_miles": 2017.4,
"estimated_duration_hrs": 29.8,
"total_fuel_cost_usd": 624.18,
"num_fuel_stops": 4
}
}The API returns:
- Origin & destination details
- Route distance and travel duration
- Estimated fuel consumption
- Total fuel cost
- Recommended fuel stops
- Cost per stop
- Google Maps navigation link
- OpenStreetMap route link
- GeoJSON LineString geometry
- Performance metadata
| Status Code | Description |
|---|---|
| 400 | Missing required fields |
| 422 | Invalid or unrecognized location |
| 500 | Internal server error |
Example:
{
"success": false,
"error": "Location could not be geocoded"
}fuel_route_api/
β
βββ manage.py
βββ requirements.txt
βββ README.md
β
βββ config/
β βββ settings.py
β βββ urls.py
β βββ asgi.py
β βββ wsgi.py
β
βββ routes/
βββ fuel_service.py
βββ route_planner.py
βββ serializers.py
βββ views.py
βββ exceptions.py
βββ tests.py
βββ fuel_prices.csv
βββ uscities_lite.csv
| Metric | Value |
|---|---|
| Fuel Stations | 8,151 |
| US Cities | 29,880 |
| External API Calls | 1 |
| Station Search Complexity | O(N) |
| Spatial Search Time | < 5 ms |
| Typical Request Time | 1β2 seconds |
python manage.py test routes --verbosity=2Test coverage includes:
- API validation
- Offline geocoding
- Route generation
- Fuel-stop selection
- Cost calculations
- Vehicle range constraints
- GeoJSON validation
Run using Gunicorn:
gunicorn config.wsgi:application --workers 4 --bind 0.0.0.0:8000Environment variables:
DJANGO_SECRET_KEY=your-secret-key
DJANGO_DEBUG=False
DJANGO_ALLOWED_HOSTS=your-domain.com| Source | Purpose |
|---|---|
| OPIS Truckstop Dataset | Fuel pricing data |
| US Cities Database | Offline geocoding |
| OSRM Routing Engine | Route generation |
- No API costs
- Faster lookups
- No rate limits
- Works without external geocoding services
- Searches all stations simultaneously
- Significantly faster than iterative distance calculations
- Scales efficiently with larger datasets
- Open-source routing engine
- Reliable road network routing
- Requires only one external request per route
Syed Sadain
Backend Engineer | Django Developer | Data Analyst
Assessment Project: Fuel Route Optimizer API