- Timestamp:
- 08/20/12 11:39:25 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Osm.Data/OsmDataSource.cs ¶
r8488 r8504 16 16 vertices = new Dictionary<long, Vertex>(); 17 17 18 ReadData ();18 ReadDataV2(); 19 19 } 20 20 … … 134 134 } 135 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 } 136 232 } 137 233 }
Note: See TracChangeset
for help on using the changeset viewer.