1 |
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using HeuristicLab.Problems.RoutePlanning.Osm;
|
---|
4 | namespace HeuristicLab.Problems.RoutePlanning.Graph {
|
---|
5 | public class OsmGraph {
|
---|
6 | private IDataSource dataSource;
|
---|
7 |
|
---|
8 | public OsmGraph(IDataSource ds) {
|
---|
9 | dataSource = ds;
|
---|
10 | }
|
---|
11 |
|
---|
12 | public OsmVertex<Node> GetVertex(long id) {
|
---|
13 | return new OsmVertex<Node>(dataSource.GetNode(id));
|
---|
14 | }
|
---|
15 |
|
---|
16 | public List<Way> GetEdges(OsmVertex<Node> vertex) {
|
---|
17 | return dataSource.GetWays(vertex.Node);
|
---|
18 | }
|
---|
19 |
|
---|
20 | public Dictionary<long, float> GetNeighbors(long id) {
|
---|
21 | OsmVertex<Node> vertex = GetVertex(id);
|
---|
22 | Dictionary<long, float> neighbors = new Dictionary<long, float>();
|
---|
23 | List<Way> edges = GetEdges(vertex);
|
---|
24 | foreach (Way edge in edges) {
|
---|
25 | //TODO: check if edge can be traversed (eg. highway tag for car roads, ...)
|
---|
26 |
|
---|
27 | // get all naighbours of current vertex on this edge
|
---|
28 | List<OsmVertex<Node>> vertices = GetNeighborVerticesOnEdge(edge, vertex);
|
---|
29 | foreach (OsmVertex<Node> neighbor in vertices) {
|
---|
30 | if (edge.CanBeTraversed(vertex.Node, neighbor.Node)) {
|
---|
31 | float weight = GetWeight(edge, vertex, neighbor);
|
---|
32 | neighbors[neighbor.Node.Id] = weight;
|
---|
33 | }
|
---|
34 | }
|
---|
35 | }
|
---|
36 | return neighbors;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public Dictionary<long, float> GetNeighborsReversed(long id) {
|
---|
40 | OsmVertex<Node> vertex = GetVertex(id);
|
---|
41 | Dictionary<long, float> neighbors = new Dictionary<long, float>();
|
---|
42 | List<Way> edges = GetEdges(vertex);
|
---|
43 | foreach (Way edge in edges) {
|
---|
44 | List<OsmVertex<Node>> vertices = GetNeighborVerticesOnEdge(edge, vertex);
|
---|
45 | foreach (OsmVertex<Node> neighbor in vertices) {
|
---|
46 | if (edge.CanBeTraversed(neighbor.Node, vertex.Node)) {
|
---|
47 | float weight = GetWeight(edge, vertex, neighbor);
|
---|
48 | neighbors[neighbor.Node.Id] = weight;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 | return neighbors;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public List<OsmVertex<Node>> GetNeighborVerticesOnEdge(Way edge, OsmVertex<Node> vertex) {
|
---|
56 | List<OsmVertex<Node>> neighbors = new List<OsmVertex<Node>>();
|
---|
57 | for (int i = 0; i < edge.Nodes.Count; i++) {
|
---|
58 | Node node = edge.Nodes[i];
|
---|
59 | if (node.Id == vertex.Node.Id) {
|
---|
60 | if (i > 0) {
|
---|
61 | neighbors.Add(GetVertex(edge.Nodes[i - 1].Id));
|
---|
62 | }
|
---|
63 | if (i < edge.Nodes.Count - 1) {
|
---|
64 | neighbors.Add(GetVertex(edge.Nodes[i + 1].Id));
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return neighbors;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public float GetWeight(Way edge, OsmVertex<Node> source, OsmVertex<Node> target) {
|
---|
72 | double distance = Utils.Distance(source.Node.Point, target.Node.Point);
|
---|
73 | //double distance = Utils.LocationDistance(source.Node.Point, target.Node.Point);
|
---|
74 | int speed = edge.GetMaxSpeed();
|
---|
75 | double weight = (distance / speed) * 3.6;
|
---|
76 | //double weight = (distance / speed);
|
---|
77 | return (float)weight;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public float EstimatedCosts(long sourceId, long targetId) {
|
---|
81 | OsmVertex<Node> source = GetVertex(sourceId);
|
---|
82 | OsmVertex<Node> target = GetVertex(targetId);
|
---|
83 | return EstimatedCosts(source, target);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public float EstimatedCosts(OsmVertex<Node> source, OsmVertex<Node> target) {
|
---|
87 | double distance = Utils.Distance(source.Node.Point, target.Node.Point);
|
---|
88 | //double distance = Utils.LocationDistance(source.Node.Point, target.Node.Point);
|
---|
89 | int speed = 130;
|
---|
90 | double costs = (distance / speed) * 3.6;
|
---|
91 | //double costs = (distance / speed);
|
---|
92 | return (float)costs;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|