[8429] | 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 |
|
---|
[8434] | 12 | public OsmVertex<Node> GetVertex(long id) {
|
---|
| 13 | return new OsmVertex<Node>(dataSource.GetNode(id));
|
---|
[8429] | 14 | }
|
---|
| 15 |
|
---|
[8434] | 16 | public List<Way> GetEdges(OsmVertex<Node> vertex) {
|
---|
[8429] | 17 | return dataSource.GetWays(vertex.Node);
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | public Dictionary<long, float> GetNeighbors(long id) {
|
---|
[8434] | 21 | OsmVertex<Node> vertex = GetVertex(id);
|
---|
[8429] | 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
|
---|
[8434] | 28 | List<OsmVertex<Node>> vertices = GetNeighborVerticesOnEdge(edge, vertex);
|
---|
| 29 | foreach (OsmVertex<Node> neighbor in vertices) {
|
---|
[8429] | 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) {
|
---|
[8434] | 40 | OsmVertex<Node> vertex = GetVertex(id);
|
---|
[8429] | 41 | Dictionary<long, float> neighbors = new Dictionary<long, float>();
|
---|
| 42 | List<Way> edges = GetEdges(vertex);
|
---|
| 43 | foreach (Way edge in edges) {
|
---|
[8434] | 44 | List<OsmVertex<Node>> vertices = GetNeighborVerticesOnEdge(edge, vertex);
|
---|
| 45 | foreach (OsmVertex<Node> neighbor in vertices) {
|
---|
[8429] | 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 |
|
---|
[8434] | 55 | public List<OsmVertex<Node>> GetNeighborVerticesOnEdge(Way edge, OsmVertex<Node> vertex) {
|
---|
| 56 | List<OsmVertex<Node>> neighbors = new List<OsmVertex<Node>>();
|
---|
[8429] | 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 |
|
---|
[8434] | 71 | public float GetWeight(Way edge, OsmVertex<Node> source, OsmVertex<Node> target) {
|
---|
[8429] | 72 | double distance = Utils.Distance(source.Node.Point, target.Node.Point);
|
---|
[8462] | 73 | //double distance = Utils.LocationDistance(source.Node.Point, target.Node.Point);
|
---|
[8429] | 74 | int speed = edge.GetMaxSpeed();
|
---|
| 75 | double weight = (distance / speed) * 3.6;
|
---|
[8462] | 76 | //double weight = (distance / speed);
|
---|
[8429] | 77 | return (float)weight;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | public float EstimatedCosts(long sourceId, long targetId) {
|
---|
[8434] | 81 | OsmVertex<Node> source = GetVertex(sourceId);
|
---|
| 82 | OsmVertex<Node> target = GetVertex(targetId);
|
---|
[8429] | 83 | return EstimatedCosts(source, target);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[8434] | 86 | public float EstimatedCosts(OsmVertex<Node> source, OsmVertex<Node> target) {
|
---|
[8429] | 87 | double distance = Utils.Distance(source.Node.Point, target.Node.Point);
|
---|
[8462] | 88 | //double distance = Utils.LocationDistance(source.Node.Point, target.Node.Point);
|
---|
[8429] | 89 | int speed = 130;
|
---|
| 90 | double costs = (distance / speed) * 3.6;
|
---|
[8462] | 91 | //double costs = (distance / speed);
|
---|
[8429] | 92 | return (float)costs;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|