DEV Community

Cover image for Engineering Krishi-Route: A Real-Time Geospatial Logistics Engine (MERN + Leaflet)
SRIHARI P V
SRIHARI P V

Posted on

Engineering Krishi-Route: A Real-Time Geospatial Logistics Engine (MERN + Leaflet)

Logistics is the silent killer of agricultural profit. In India, a farmer's margin is often destroyed not by the harvest itself, but by the sheer cost of transporting a partial truckload of crops to the market.

I wanted to solve this at the architectural level. I didn't just want to build another standard CRUD app; I needed a real-time, geospatial routing engine that could dynamically pool cargo from multiple nearby farmers to share transport costs.

Enter Krishi-Route.

βš™οΈ The Architecture

To build a high-performance routing system, I needed a stack that could handle rapid geospatial queries and render them instantly on the client side without locking up the main thread.

  • Frontend: React + Leaflet.js for interactive, lightweight map rendering, bypassing the heavy overhead of commercial mapping APIs.
  • Backend: Node.js & Express to handle the asynchronous routing logic and user authentication protocols (JWT).
  • Database: MongoDB with Geospatial Indexing (2dsphere) to calculate proximity boundaries at the database layer.

🧠 The Core Challenge: Geospatial Pooling

The hardest part of this build wasn't rendering the mapβ€”it was the math behind the matching algorithm.

If Farmer A needs to ship 200kg of tomatoes, and Farmer B (15km away) needs to ship 300kg of onions to the same market, the system needs to recognize they can share a 500kg capacity truck.

Instead of relying solely on expensive external routing APIs for every single coordinate check, I leveraged MongoDB's native geospatial aggregation pipelines combined with custom distance calculations to filter the nearest compatible cargo nodes before calculating the final route.

The Aggregation Payload

Here is a look under the hood at how the backend handles the proximity matching:

// Step 1: Define vehicle constraints
const capacity = VEHICLE_CAPACITY[vehicleType] || 30;

// Step 2: Database Query - Find active, compatible transit nodes
const existingPlans = await TravelPlan.find({
    travelDate,
    vehicleType,
    status: 'planned',
    farmerId: { $ne: req.user._id } // Exclude the requesting farmer
})
    .populate('farmerId', 'name farmerProfile')
    .populate('mandiId', 'name');

// Step 3: Proximity & Capacity Filtering
const poolingMatches = existingPlans.filter(p => {
    // Check if the combined load fits the exact vehicle capacity constraint
    const fits = (p.quantity + Number(quantity)) <= capacity;
    return fits && p.mandiId; // Return only valid, destination-bound nodes
});
Enter fullscreen mode Exit fullscreen mode

πŸ“ˆ The Output: Profit Routing

By calculating the shared transport cost dynamically, Krishi-Route flips the script. Instead of eating the cost of an empty truck, farmers get an instant projection of the money they will save when they join a shared cargo pool.

πŸš€ Final Thoughts

Building Krishi-Route pushed me out of standard web development and into the realm of system architecture and geographic mapping. It forced me to think about database speeds, mathematical formulas like Haversine, and how code translates into physical, real-world logistics.
Explore the architecture here:

πŸ’» GitHub Repository: https://github.com/LORDMOSTER/Krishi-Route

Top comments (0)