EuclidianRouter.java (1502B) download
1package osm.routing.strategy;
2
3import java.util.Map;
4import java.util.Map.Entry;
5
6import osm.message.Node;
7import osm.routing.RoutingEdge;
8import osm.routing.RoutingGraph;
9import osm.routing.RoutingStrategy;
10import 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 */
19public 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}