An intelligent route planning system built with Next.js, Python, NetworkX, and OpenStreetMap that finds optimal routes between cities using AI-powered pathfinding algorithms.
- πΊοΈ Interactive Map Visualization using Leaflet.js
- π― Shortest Path Algorithm (Dijkstra) powered by NetworkX
- π€ AI-based Travel Time Prediction using Random Forest
- π£οΈ Alternate Route Suggestions
- π Real-time Route Details (distance, time, waypoints)
- π¨ Modern, Responsive UI built with Tailwind CSS
- β‘ Fast API Communication between Next.js and Flask
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Frontend (Next.js) β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β MapView β β Form β β Details β β
β β (Leaflet) β β (Input) β β (Route) β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ
β HTTP/JSON
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Next.js API Route (/api/shortest-path) β
β (Node.js Backend) β
βββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ
β HTTP/JSON
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Flask API (Python Backend) β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β NetworkX β β OSMnx β β ML Model β β
β β (Dijkstra) β β (Map Data) β β (Prediction) β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Layer | Technology | Purpose |
|---|---|---|
| Frontend | Next.js 14 (App Router) | React framework with SSR |
| UI Components | React + TypeScript | Component library |
| Styling | Tailwind CSS | Utility-first CSS |
| Map | Leaflet.js | Interactive maps |
| Icons | Lucide React | Modern icon library |
| Backend API | Next.js API Routes | Server-side API endpoints |
| Python API | Flask + CORS | RESTful API server |
| Pathfinding | NetworkX | Graph algorithms |
| Map Data | OSMnx + OpenStreetMap | Real road network data |
| ML Model | scikit-learn (Random Forest) | Travel time prediction |
| Data Processing | pandas, numpy | Data manipulation |
- Node.js 18+ and npm/yarn
- Python 3.10+
- pip (Python package manager)
git clone <your-repo-url>
cd ai_navnpm install
# or
yarn installcd python-backend
python -m venv venv
# Activate virtual environment
# Windows:
venv\Scripts\activate
# macOS/Linux:
source venv/bin/activate
# Install packages
pip install -r requirements.txtcd python-backend
python app.pyThe Flask API will start on http://localhost:5000
In a new terminal:
npm run devThe Next.js app will start on http://localhost:3000
Navigate to http://localhost:3000 and start planning routes!
The project includes a Jupyter notebook demonstrating the ML model and pathfinding algorithms:
# Install Jupyter (if not installed)
pip install jupyter
# Run notebook
jupyter notebook navigation_model.ipynbThe notebook covers:
- Loading OpenStreetMap data with OSMnx
- Implementing Dijkstra's algorithm
- Training ML models for travel time prediction
- Visualizing routes
Create a .env.local file in the root directory:
FLASK_API_URL=http://localhost:5000Request:
{
"source": "Delhi",
"destination": "Mumbai"
}Response:
{
"path": ["Delhi", "Jaipur", "Ahmedabad", "Mumbai"],
"distance": 1420.5,
"time": "23 hr 40 min",
"coordinates": [[28.6139, 77.2090], ...],
"alternate_routes": [["Delhi", "Surat", "Mumbai"]]
}Same request/response format as above.
Health check endpoint.
Returns list of available cities.
- Source and destination input fields
- Quick select buttons for popular cities
- Validation and loading states
- Interactive Leaflet map
- Route visualization with polylines
- Markers for source, destination, and waypoints
- Auto-zoom to fit route
- Distance and estimated time
- Step-by-step route path
- Alternate route suggestions
The system uses a Random Forest Regressor trained on:
- Distance (km)
- Road Type (highway, national, state)
- Traffic Level (low, medium, high)
- Time of Day (morning, afternoon, night)
Model performance:
- Mean Absolute Error: ~0.15 hours
- RΒ² Score: ~0.90
- User Input: Enter source and destination cities
- API Call: Frontend sends request to Next.js API route
- Flask Processing: Next.js forwards request to Flask backend
- Graph Algorithm: NetworkX computes shortest path using Dijkstra's algorithm
- ML Prediction: Random Forest model predicts travel time
- Route Generation: Creates coordinate array for map visualization
- Response: Returns route data with path, distance, time, coordinates
- Visualization: Frontend displays route on interactive map
ai_nav/
βββ app/
β βββ api/
β β βββ shortest-path/
β β βββ route.ts # Next.js API endpoint
β βββ layout.tsx # Root layout
β βββ page.tsx # Main page
β βββ globals.css # Global styles
βββ components/
β βββ NavigationForm.tsx # Input form component
β βββ MapView.tsx # Map visualization
β βββ RouteDetails.tsx # Route information display
βββ python-backend/
β βββ app.py # Flask API server
β βββ requirements.txt # Python dependencies
β βββ road_network.pkl # Cached graph data
βββ navigation_model.ipynb # Jupyter notebook
βββ package.json # Node dependencies
βββ tsconfig.json # TypeScript config
βββ tailwind.config.ts # Tailwind config
βββ README.md # This file
curl -X POST http://localhost:5000/predict \
-H "Content-Type: application/json" \
-d '{"source": "Delhi", "destination": "Mumbai"}'curl -X POST http://localhost:3000/api/shortest-path \
-H "Content-Type: application/json" \
-d '{"source": "Delhi", "destination": "Mumbai"}'npm install -g vercel
vercel- Add
Procfile:
web: cd python-backend && gunicorn app:app
-
Add
gunicornto requirements.txt -
Deploy:
git push heroku mainContributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Open a pull request
MIT License - feel free to use this project for learning or commercial purposes.
Built with β€οΈ for learning and demonstration purposes.
- OSMnx for OpenStreetMap integration
- NetworkX for graph algorithms
- Leaflet for beautiful maps
- Next.js team for the amazing framework
- OpenStreetMap contributors
For issues or questions, please open a GitHub issue.
Happy Route Planning! πΊοΈβ¨