1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Xml;
|
---|
5 | using HeuristicLab.Problems.RoutePlanning.Graph;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Problems.RoutePlanning.Osm.Data {
|
---|
8 | public class OsmDataSource : IDataSource {
|
---|
9 | private FileInfo file;
|
---|
10 | private Dictionary<long, Vertex> vertices;
|
---|
11 | private IGraph graph;
|
---|
12 |
|
---|
13 | public OsmDataSource(string filename) {
|
---|
14 | file = new FileInfo(filename);
|
---|
15 | graph = new Graph.Graph();
|
---|
16 | vertices = new Dictionary<long, Vertex>();
|
---|
17 |
|
---|
18 | ReadDataV2();
|
---|
19 | }
|
---|
20 |
|
---|
21 | #region IDataSource Members
|
---|
22 |
|
---|
23 | public Node GetNode(long nodeId) {
|
---|
24 | throw new NotImplementedException();
|
---|
25 | }
|
---|
26 |
|
---|
27 | public Way GetWay(long wayId) {
|
---|
28 | throw new NotImplementedException();
|
---|
29 | }
|
---|
30 |
|
---|
31 | public List<Way> GetWays(Node node) {
|
---|
32 | throw new NotImplementedException();
|
---|
33 | }
|
---|
34 |
|
---|
35 | public Relation GetRelation(long relationId) {
|
---|
36 | throw new NotImplementedException();
|
---|
37 | }
|
---|
38 |
|
---|
39 | public Graph.IGraph GetRoutingGraph() {
|
---|
40 | return graph;
|
---|
41 | }
|
---|
42 |
|
---|
43 | #endregion
|
---|
44 |
|
---|
45 | private Vertex GetVertex(long Id) {
|
---|
46 | Vertex v;
|
---|
47 | if (vertices.TryGetValue(Id, out v)) {
|
---|
48 | return v;
|
---|
49 | } else {
|
---|
50 | return null;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private void ReadData() {
|
---|
55 | XmlReaderSettings settings = new XmlReaderSettings();
|
---|
56 | settings.IgnoreComments = true;
|
---|
57 | settings.IgnoreWhitespace = true;
|
---|
58 |
|
---|
59 | using (XmlReader reader = XmlReader.Create(file.FullName, settings)) {
|
---|
60 | reader.ReadStartElement("osm");
|
---|
61 | //reader.ReadStartElement("bounds");
|
---|
62 | reader.Read();
|
---|
63 | while (reader.LocalName.Equals("node")) {
|
---|
64 | double lat = XmlConvert.ToDouble(reader.GetAttribute("lat"));
|
---|
65 | double lon = XmlConvert.ToDouble(reader.GetAttribute("lon"));
|
---|
66 | long id = XmlConvert.ToInt64(reader.GetAttribute("id"));
|
---|
67 |
|
---|
68 | Vertex v = new Vertex(id, lon, lat);
|
---|
69 | vertices.Add(v.Id, v);
|
---|
70 |
|
---|
71 | reader.Read();
|
---|
72 | if (reader.LocalName.Equals("tag")) {
|
---|
73 | reader.ReadToFollowing("node");
|
---|
74 | }
|
---|
75 | }
|
---|
76 | while (reader.LocalName.Equals("way")) {
|
---|
77 | List<Vertex> way = new List<Vertex>();
|
---|
78 | bool missingNodes = false;
|
---|
79 | HighwayType category = HighwayType.null_type;
|
---|
80 | bool oneWayRoad = false;
|
---|
81 |
|
---|
82 | reader.Read();
|
---|
83 | while (reader.LocalName.Equals("nd") || reader.LocalName.Equals("tag")) {
|
---|
84 | if (reader.LocalName.Equals("nd")) {
|
---|
85 | long refNodeId = XmlConvert.ToInt64(reader.GetAttribute("ref"));
|
---|
86 | Vertex vertex = GetVertex(refNodeId);
|
---|
87 | if (vertex != null) {
|
---|
88 | way.Add(vertex);
|
---|
89 | } else {
|
---|
90 | missingNodes = true;
|
---|
91 | }
|
---|
92 |
|
---|
93 | } else if (reader.LocalName.Equals("tag")) {
|
---|
94 | string key = reader.GetAttribute("k");
|
---|
95 | string value = reader.GetAttribute("v");
|
---|
96 |
|
---|
97 | if (key == TagConstants.HighwayTagKey) {
|
---|
98 | if (Enum.IsDefined(typeof(HighwayType), value)) {
|
---|
99 | HighwayType ht = (HighwayType)Enum.Parse(typeof(HighwayType), value, true);
|
---|
100 | category = ht;
|
---|
101 | } else {
|
---|
102 | category = HighwayType.null_type;
|
---|
103 | }
|
---|
104 | } else if (key == TagConstants.OneWayTag) {
|
---|
105 | oneWayRoad = value.Equals(TagConstants.YesValue);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | reader.Read();
|
---|
109 | }
|
---|
110 | if (missingNodes) {
|
---|
111 | // TODO: Error/Trace output
|
---|
112 | // if Way xxx has incomplete nodes. At least one node was missing in the data.
|
---|
113 | }
|
---|
114 | for (int i = 0; i < way.Count - 1; i++) {
|
---|
115 | Vertex v1 = way[i];
|
---|
116 | graph.AddVertex(v1);
|
---|
117 | Vertex v2 = way[i + 1];
|
---|
118 | graph.AddVertex(v2);
|
---|
119 | short vertexCategory = (short)category;
|
---|
120 | if (oneWayRoad) {
|
---|
121 | Edge<Vertex> edge = new Edge<Vertex>(v1, v2, vertexCategory);
|
---|
122 | graph.AddEdge(edge);
|
---|
123 | } else {
|
---|
124 | Edge<Vertex> edgeForward = new Edge<Vertex>(v1, v2, vertexCategory);
|
---|
125 | graph.AddEdge(edgeForward);
|
---|
126 |
|
---|
127 | Edge<Vertex> edgeBackward = new Edge<Vertex>(v2, v1, vertexCategory);
|
---|
128 | graph.AddEdge(edgeBackward);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | reader.Read();
|
---|
132 | }
|
---|
133 | reader.Close();
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | private void ReadDataV2() {
|
---|
138 | XmlReaderSettings settings = new XmlReaderSettings();
|
---|
139 | settings.IgnoreComments = true;
|
---|
140 | settings.IgnoreWhitespace = true;
|
---|
141 | NameTable nt = new NameTable();
|
---|
142 | string osmName = nt.Add("osm");
|
---|
143 | string nodeName = nt.Add("node");
|
---|
144 | object tagName = nt.Add("tag");
|
---|
145 | object wayName = nt.Add("way");
|
---|
146 | string latName = nt.Add("lat");
|
---|
147 | string lonName = nt.Add("lon");
|
---|
148 | string idName = nt.Add("id");
|
---|
149 | string ndName = nt.Add("nd");
|
---|
150 | string refName = nt.Add("ref");
|
---|
151 | string kName = nt.Add("k");
|
---|
152 | string vName = nt.Add("v");
|
---|
153 | settings.NameTable = nt;
|
---|
154 | string ns = "";
|
---|
155 |
|
---|
156 | using (XmlReader reader = XmlReader.Create(file.FullName, settings)) {
|
---|
157 | reader.ReadStartElement(osmName, ns);
|
---|
158 | reader.Read();
|
---|
159 | while (reader.LocalName.Equals(nodeName)) {
|
---|
160 | double lat = XmlConvert.ToDouble(reader.GetAttribute(latName, ns));
|
---|
161 | double lon = XmlConvert.ToDouble(reader.GetAttribute(lonName, ns));
|
---|
162 | long id = XmlConvert.ToInt64(reader.GetAttribute(idName, ns));
|
---|
163 |
|
---|
164 | Vertex v = new Vertex(id, lon, lat);
|
---|
165 | vertices.Add(v.Id, v);
|
---|
166 |
|
---|
167 | reader.Read();
|
---|
168 | if (reader.LocalName.Equals(tagName)) {
|
---|
169 | reader.ReadToFollowing(nodeName, ns);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | while (reader.LocalName.Equals(wayName)) {
|
---|
173 | List<Vertex> way = new List<Vertex>();
|
---|
174 | bool missingNodes = false;
|
---|
175 | HighwayType category = HighwayType.null_type;
|
---|
176 | bool oneWayRoad = false;
|
---|
177 |
|
---|
178 | reader.Read();
|
---|
179 | while (reader.LocalName.Equals(nodeName) || reader.LocalName.Equals(tagName)) {
|
---|
180 | if (reader.LocalName.Equals(ndName)) {
|
---|
181 | long refNodeId = XmlConvert.ToInt64(reader.GetAttribute(refName, ns));
|
---|
182 | Vertex vertex = GetVertex(refNodeId);
|
---|
183 | if (vertex != null) {
|
---|
184 | way.Add(vertex);
|
---|
185 | } else {
|
---|
186 | missingNodes = true;
|
---|
187 | }
|
---|
188 |
|
---|
189 | } else if (reader.LocalName.Equals(tagName)) {
|
---|
190 | string key = reader.GetAttribute(kName, ns);
|
---|
191 | string value = reader.GetAttribute(vName, ns);
|
---|
192 |
|
---|
193 | if (key == TagConstants.HighwayTagKey) {
|
---|
194 | if (Enum.IsDefined(typeof(HighwayType), value)) {
|
---|
195 | HighwayType ht = (HighwayType)Enum.Parse(typeof(HighwayType), value, true);
|
---|
196 | category = ht;
|
---|
197 | } else {
|
---|
198 | category = HighwayType.null_type;
|
---|
199 | }
|
---|
200 | } else if (key == TagConstants.OneWayTag) {
|
---|
201 | oneWayRoad = value.Equals(TagConstants.YesValue);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | reader.Read();
|
---|
205 | }
|
---|
206 | if (missingNodes) {
|
---|
207 | // TODO: Error/Trace output
|
---|
208 | // if Way xxx has incomplete nodes. At least one node was missing in the data.
|
---|
209 | }
|
---|
210 | for (int i = 0; i < way.Count - 1; i++) {
|
---|
211 | Vertex v1 = way[i];
|
---|
212 | graph.AddVertex(v1);
|
---|
213 | Vertex v2 = way[i + 1];
|
---|
214 | graph.AddVertex(v2);
|
---|
215 | short vertexCategory = (short)category;
|
---|
216 | if (oneWayRoad) {
|
---|
217 | Edge<Vertex> edge = new Edge<Vertex>(v1, v2, vertexCategory);
|
---|
218 | graph.AddEdge(edge);
|
---|
219 | } else {
|
---|
220 | Edge<Vertex> edgeForward = new Edge<Vertex>(v1, v2, vertexCategory);
|
---|
221 | graph.AddEdge(edgeForward);
|
---|
222 |
|
---|
223 | Edge<Vertex> edgeBackward = new Edge<Vertex>(v2, v1, vertexCategory);
|
---|
224 | graph.AddEdge(edgeBackward);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | reader.Read();
|
---|
228 | }
|
---|
229 | reader.Close();
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|