1 |
|
---|
2 | using System;
|
---|
3 | using System.Diagnostics;
|
---|
4 | using System.IO;
|
---|
5 | using System.Xml;
|
---|
6 | using HeuristicLab.Algorithms.GraphRouting;
|
---|
7 | using HeuristicLab.Problems.RoutePlanning.Graph;
|
---|
8 | using HeuristicLab.Problems.RoutePlanning.Osm;
|
---|
9 | using HeuristicLab.Problems.RoutePlanning.Osm.Data;
|
---|
10 | namespace HeuristicLab.Problems.RoutePlanning.Test {
|
---|
11 | class Program {
|
---|
12 | static void Main(string[] args) {
|
---|
13 | // For testing openstreetmap data is used
|
---|
14 | // http://www.openstreetmap.org
|
---|
15 | // Map data © OpenStreetMap contributors, CC BY-SA
|
---|
16 |
|
---|
17 | string file1 = @"..\..\OsmTestFiles\test.osm";
|
---|
18 | string file2 = @"..\..\OsmTestFiles\testNode1.osm";
|
---|
19 | string file3 = @"..\..\OsmTestFiles\testWay1.osm";
|
---|
20 | string file4 = @"..\..\OsmTestFiles\testRelation1.osm";
|
---|
21 | string file5 = @"..\..\OsmTestFiles\test_mid.osm";
|
---|
22 | string file6 = @"C:\dev\osmfiles\oberosterreich.highway.osm";
|
---|
23 |
|
---|
24 | //TestD(file5, 529102170, 1001363194, true, false); // inz - inz
|
---|
25 | //Test(file5, 529102170, 1001363194, true, false); // inz - inz
|
---|
26 | //Test(file6, 529102170, 1001363194, true, false); // inz - inz
|
---|
27 |
|
---|
28 | //IDataSource ds = TestLoad(file6);
|
---|
29 | //Graph.Graph graph = new Graph.Graph(ds);
|
---|
30 | //TestRouter(new AStarAlgorithm(graph), 529102170, 31372732, false); // inz - hgb
|
---|
31 | //TestRouter(new AStarAlgorithmV2(graph), 529102170, 31372732, false); // inz - hgb
|
---|
32 |
|
---|
33 | //TestLoadAndRouter(file6, typeof(DijkstraAlgorithm), 529102170, 31372732, false, false); // inz - hgb
|
---|
34 | //TestLoadAndRouter(file6, typeof(AStarAlgorithm), 529102170, 31372732, false, false); // inz - hgb
|
---|
35 | //TestLoadAndRouter(file6, typeof(AStarAlgorithmV2), 529102170, 31372732, false, false); // inz - hgb
|
---|
36 |
|
---|
37 | //TestLoadAndRouter(file5, typeof(DijkstraAlgorithm), 529102170, 1001363194, true, false);
|
---|
38 | //TestLoadAndRouter(file5, typeof(BidrectionalDijkstraAlgorithm), 529102170, 1001363194, true, true);
|
---|
39 | TestLoadAndRouter(file6, typeof(BidrectionalDijkstraAlgorithm), 529102170, 31372732, false, true); // inz - hgb
|
---|
40 |
|
---|
41 | System.Console.Read();
|
---|
42 | }
|
---|
43 |
|
---|
44 | private static long[] TestRouter(IRouter router, long sourceNodeId, long targetNodeId, bool showResult) {
|
---|
45 | Console.WriteLine("Test Router BEGIN ---------------------------------");
|
---|
46 | Console.WriteLine("Type: " + router.GetType());
|
---|
47 |
|
---|
48 | Console.Write("Calculate route ... ");
|
---|
49 | var sw = Stopwatch.StartNew();
|
---|
50 | long[] result = router.Calculate(sourceNodeId, targetNodeId);
|
---|
51 | sw.Stop();
|
---|
52 | Console.WriteLine("done.");
|
---|
53 | Console.WriteLine("Execution Time: {0}", sw.Elapsed);
|
---|
54 | Console.WriteLine();
|
---|
55 | if (showResult) {
|
---|
56 | Console.WriteLine("Result: ");
|
---|
57 | foreach (long i in result) System.Console.Write(i + "; ");
|
---|
58 | Console.WriteLine();
|
---|
59 | Console.WriteLine();
|
---|
60 | }
|
---|
61 | Console.WriteLine("===================================================");
|
---|
62 | Console.WriteLine();
|
---|
63 | return result;
|
---|
64 | }
|
---|
65 |
|
---|
66 | private static IDataSource TestLoad(string filepath) {
|
---|
67 | FileInfo file = new FileInfo(filepath);
|
---|
68 | Console.WriteLine("Test Load Data BEGIN ------------------------------");
|
---|
69 | Console.WriteLine("File name: {0}", file.Name);
|
---|
70 | Console.WriteLine();
|
---|
71 | Console.Write("Loading data ... ");
|
---|
72 | var sw = Stopwatch.StartNew();
|
---|
73 | XmlDataSource xmlDs = new XmlDataSource(filepath);
|
---|
74 | sw.Stop();
|
---|
75 | Console.WriteLine("done.");
|
---|
76 | Console.WriteLine("Time: {0}", sw.Elapsed);
|
---|
77 | Console.WriteLine();
|
---|
78 | Console.WriteLine("===================================================");
|
---|
79 | Console.WriteLine();
|
---|
80 | return xmlDs;
|
---|
81 | }
|
---|
82 |
|
---|
83 | private static void TestLoadAndRouter(string filepath, Type routerType, long sourceNodeId, long targetNodeId, bool showResult, bool writeResultFile) {
|
---|
84 | FileInfo file = new FileInfo(filepath);
|
---|
85 | Console.WriteLine("Test Load and Route BEGIN -------------------------");
|
---|
86 | Console.WriteLine("File name: {0}", file.Name);
|
---|
87 | Console.WriteLine();
|
---|
88 | Console.Write("Loading data ... ");
|
---|
89 | var sw = Stopwatch.StartNew();
|
---|
90 | XmlDataSource xmlDs = new XmlDataSource(filepath);
|
---|
91 | sw.Stop();
|
---|
92 | Console.WriteLine("done.");
|
---|
93 | Console.WriteLine("Loading Time: {0}", sw.Elapsed);
|
---|
94 | Console.WriteLine();
|
---|
95 | Graph.Graph graph = new Graph.Graph(xmlDs);
|
---|
96 | IRouter router = (IRouter)Activator.CreateInstance(routerType, graph);
|
---|
97 | Console.Write("Calculate route ... ");
|
---|
98 | sw = Stopwatch.StartNew();
|
---|
99 | long[] result = router.Calculate(sourceNodeId, targetNodeId);
|
---|
100 | sw.Stop();
|
---|
101 | Console.WriteLine("done.");
|
---|
102 | Console.WriteLine("Execution Time: {0}", sw.Elapsed);
|
---|
103 | Console.WriteLine();
|
---|
104 | if (showResult) {
|
---|
105 | Console.WriteLine("Result: ");
|
---|
106 | foreach (long i in result) System.Console.Write(i + "; ");
|
---|
107 | Console.WriteLine();
|
---|
108 | Console.WriteLine();
|
---|
109 | }
|
---|
110 | if (writeResultFile) {
|
---|
111 | string dt = DateTime.Now.ToString("yyyyMMddhhmmss_");
|
---|
112 | string fn = Path.GetFileNameWithoutExtension(file.Name);
|
---|
113 | string resultFile = dt + fn + "_" + sourceNodeId + "-" + targetNodeId + ".gpx";
|
---|
114 | Console.WriteLine("Result file: {0}", resultFile);
|
---|
115 | WriteGPXFile(graph, result, resultFile);
|
---|
116 | }
|
---|
117 | Console.WriteLine("===================================================");
|
---|
118 | Console.WriteLine();
|
---|
119 | }
|
---|
120 |
|
---|
121 | private static void WriteGPXFile(Graph.Graph graph, long[] route, string file) {
|
---|
122 | XmlWriterSettings settings = new XmlWriterSettings();
|
---|
123 | settings.Indent = true;
|
---|
124 | settings.IndentChars = (" ");
|
---|
125 |
|
---|
126 | using (XmlWriter writer = XmlWriter.Create(file, settings)) {
|
---|
127 | writer.WriteStartElement("gpx", "http://www.topografix.com/GPX/1/0");
|
---|
128 | writer.WriteAttributeString("version", "1.1");
|
---|
129 | writer.WriteAttributeString("creator", "cloudia");
|
---|
130 |
|
---|
131 | foreach (long nodeId in route) {
|
---|
132 | Vertex<Node> node = graph.GetVertex(nodeId);
|
---|
133 | writer.WriteStartElement("wpt");
|
---|
134 | writer.WriteAttributeString("lat", XmlConvert.ToString(node.Node.Latitude));
|
---|
135 | writer.WriteAttributeString("lon", XmlConvert.ToString(node.Node.Longitude));
|
---|
136 | writer.WriteEndElement(); // wpt
|
---|
137 | }
|
---|
138 |
|
---|
139 | //writer.WriteStartElement("rte");
|
---|
140 | //foreach (long nodeId in route) {
|
---|
141 | // Vertex<Node> node = graph.GetVertex(nodeId);
|
---|
142 | // writer.WriteStartElement("rtept");
|
---|
143 | // writer.WriteAttributeString("lat", XmlConvert.ToString(node.Node.Latitude));
|
---|
144 | // writer.WriteAttributeString("lon", XmlConvert.ToString(node.Node.Longitude));
|
---|
145 | // writer.WriteEndElement(); // rtept
|
---|
146 | //}
|
---|
147 | //writer.WriteEndElement(); // rte
|
---|
148 |
|
---|
149 | //writer.WriteStartElement("trk");
|
---|
150 | //writer.WriteElementString("name", "RoutePath");
|
---|
151 | ////writer.WriteEndElement(); // name
|
---|
152 | //writer.WriteStartElement("trkseq");
|
---|
153 | //foreach (long nodeId in route) {
|
---|
154 | // Vertex<Node> node = graph.GetVertex(nodeId);
|
---|
155 | // writer.WriteStartElement("trkpt");
|
---|
156 | // writer.WriteAttributeString("lat", XmlConvert.ToString(node.Node.Latitude));
|
---|
157 | // writer.WriteAttributeString("lon", XmlConvert.ToString(node.Node.Longitude));
|
---|
158 | // writer.WriteEndElement(); // trkpt
|
---|
159 | //}
|
---|
160 | //writer.WriteEndElement(); // trkseq
|
---|
161 | //writer.WriteEndElement(); // trk
|
---|
162 |
|
---|
163 | writer.WriteEndElement(); // gpx
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|