EuclidianRouter.java (1502B)
1 package osm.routing.strategy; 2 3 import java.util.Map; 4 import java.util.Map.Entry; 5 6 import osm.message.Node; 7 import osm.routing.RoutingEdge; 8 import osm.routing.RoutingGraph; 9 import osm.routing.RoutingStrategy; 10 import osm.routing.RoutingGraph.Route; 11 12 /** 13 * A routing strategy that calculates the route based on Euclidean distance from 14 * the 15 * start node to the target nodes. 16 * 17 * @param <E> The type of edges connecting the nodes. 18 */ 19 public class EuclidianRouter<E extends RoutingEdge<Node>> implements RoutingStrategy<Node, E> { 20 21 /** 22 * Calculates the route based on Euclidean distance from the start node to the 23 * target nodes. 24 * 25 * @param router The routing graph. 26 * @param targets The target nodes and their distances. 27 * @param start The starting node. 28 * @param offset The offset value. 29 * @return A Route object representing the optimal route based on Euclidean 30 * distance. 31 */ 32 @Override 33 public Route<Node> route(RoutingGraph<Node, E> router, Map<Node, Long> targets, Node start, long offset) { 34 Node bestNode = null; 35 long bestDist = Long.MAX_VALUE; 36 37 for (Entry<Node, Long> target : targets.entrySet()) { 38 long startDist = target.getKey().distanceTo(start) + target.getValue(); 39 if (startDist < bestDist) { 40 bestNode = target.getKey(); 41 bestDist = startDist; 42 } 43 } 44 45 return new Route<Node>(bestNode, null, bestDist); 46 } 47 }