| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using Itinero.IO.Osm.Tiles.Parsers.Semantics; |
| | | 5 | | using Itinero.Logging; |
| | | 6 | | using Itinero.Network; |
| | | 7 | | using Itinero.Network.Attributes; |
| | | 8 | | using Itinero.Network.Writer; |
| | | 9 | | using Newtonsoft.Json; |
| | | 10 | | using Newtonsoft.Json.Linq; |
| | | 11 | | |
| | | 12 | | namespace Itinero.IO.Osm.Tiles.Parsers; |
| | | 13 | | |
| | | 14 | | internal static class TileParser |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// The base url to fetch the tiles from. |
| | | 18 | | /// </summary> |
| | | 19 | | public const string BaseUrl = "https://tiles.openplanner.team/planet"; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// The function to download from a given url. |
| | | 23 | | /// </summary> |
| | 0 | 24 | | public static Func<string, Stream> DownloadFunc = Download.DownloadHelper.Download; |
| | | 25 | | |
| | 0 | 26 | | private static readonly Lazy<Dictionary<string, TagMapperConfig>> ReverseMappingLazy = new( |
| | 0 | 27 | | () => TagMapperConfigParser.Parse( |
| | 0 | 28 | | Extensions.LoadEmbeddedResourceStream("Itinero.IO.Osm.Tiles.ontology.mapping_config.json"))); |
| | | 29 | | |
| | | 30 | | internal static JObject? Parse(this Stream stream, Tile tile) |
| | 0 | 31 | | { |
| | | 32 | | try |
| | 0 | 33 | | { |
| | 0 | 34 | | using var textReader = new StreamReader(stream); |
| | 0 | 35 | | using var jsonReader = new JsonTextReader(textReader); |
| | 0 | 36 | | return JObject.Load(jsonReader); |
| | | 37 | | } |
| | 0 | 38 | | catch (JsonReaderException e) |
| | 0 | 39 | | { |
| | 0 | 40 | | Logger.Log($"{nameof(TileParser)}.{nameof(AddOsmTile)}", TraceEventType.Error, |
| | 0 | 41 | | $"Failed to parse tile {tile}: {e.ToInvariantString()}"); |
| | 0 | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | return null; |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | internal static bool AddOsmTile(this RoutingNetworkWriter networkWriter, GlobalIdMap globalIdMap, Tile tile, |
| | | 48 | | JObject jsonObject) |
| | 0 | 49 | | { |
| | 0 | 50 | | var nodeLocations = |
| | 0 | 51 | | new Dictionary<long, ((double longitude, double latitude, float? e) location, bool inTile)>(); |
| | 0 | 52 | | var waysData = |
| | 0 | 53 | | new Dictionary<long, (List<long> nodes, IEnumerable<(string key, string value)> attributes)>(); |
| | 0 | 54 | | var nodes = new HashSet<long>(); |
| | 0 | 55 | | var coreNodes = new HashSet<long>(); |
| | 0 | 56 | | var updated = false; |
| | 0 | 57 | | if (!(jsonObject["@graph"] is JArray graph)) |
| | 0 | 58 | | { |
| | 0 | 59 | | return false; |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | foreach (var graphObject in graph) |
| | 0 | 63 | | { |
| | 0 | 64 | | if (!(graphObject["@id"] is JToken idToken)) |
| | 0 | 65 | | { |
| | 0 | 66 | | continue; |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | var id = idToken.Value<string>(); |
| | | 70 | | |
| | 0 | 71 | | if (id == null) |
| | 0 | 72 | | { |
| | 0 | 73 | | continue; |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | if (id.StartsWith("http://www.openstreetmap.org/node/")) |
| | 0 | 77 | | { |
| | | 78 | | // parse as a node. |
| | 0 | 79 | | var nodeId = long.Parse(id.Substring("http://www.openstreetmap.org/node/".Length, |
| | 0 | 80 | | id.Length - "http://www.openstreetmap.org/node/".Length)); |
| | | 81 | | |
| | 0 | 82 | | if (!(graphObject["geo:long"] is JToken longToken)) |
| | 0 | 83 | | { |
| | 0 | 84 | | continue; |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | var lon = longToken.Value<double>(); |
| | 0 | 88 | | if (!(graphObject["geo:lat"] is JToken latToken)) |
| | 0 | 89 | | { |
| | 0 | 90 | | continue; |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | var lat = latToken.Value<double>(); |
| | | 94 | | |
| | | 95 | | // determine if node is in tile or not. |
| | 0 | 96 | | var inTile = Tile.WorldToTile(lon, lat, |
| | 0 | 97 | | tile.Zoom).LocalId == tile.LocalId; |
| | 0 | 98 | | nodeLocations[nodeId] = ((lon, lat, null), |
| | 0 | 99 | | inTile); |
| | 0 | 100 | | } |
| | 0 | 101 | | else if (id.StartsWith("http://www.openstreetmap.org/way/")) |
| | 0 | 102 | | { |
| | | 103 | | // parse as a way. |
| | 0 | 104 | | var wayId = long.Parse(id.Substring("http://www.openstreetmap.org/way/".Length, |
| | 0 | 105 | | id.Length - "http://www.openstreetmap.org/way/".Length)); |
| | | 106 | | |
| | | 107 | | // interpret all tags with defined semantics. |
| | 0 | 108 | | var attributes = GetTags(graphObject, ReverseMappingLazy.Value); |
| | 0 | 109 | | attributes.AddOrReplace("way_id", wayId.ToInvariantString()); |
| | 0 | 110 | | attributes.AddOrReplace("tile_x", tile.X.ToInvariantString()); |
| | 0 | 111 | | attributes.AddOrReplace("tile_y", tile.Y.ToInvariantString()); |
| | | 112 | | |
| | | 113 | | // include all raw tags (if any). |
| | 0 | 114 | | if (graphObject["osm:hasTag"] is JArray rawTags) |
| | 0 | 115 | | { |
| | 0 | 116 | | for (var n = 0; n < rawTags.Count; n++) |
| | 0 | 117 | | { |
| | 0 | 118 | | var rawTag = rawTags[n]; |
| | 0 | 119 | | if (!(rawTag is JValue rawTagValue)) |
| | 0 | 120 | | { |
| | 0 | 121 | | continue; |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | var keyValue = rawTagValue.Value<string>(); |
| | 0 | 125 | | var keyValueSplit = keyValue.Split('='); |
| | 0 | 126 | | if (keyValueSplit.Length != 2) |
| | 0 | 127 | | { |
| | 0 | 128 | | continue; |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | attributes.AddOrReplace(keyValueSplit[0], keyValueSplit[1]); |
| | 0 | 132 | | } |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | // parse nodes. |
| | 0 | 136 | | if (!(graphObject["osm:hasNodes"] is JArray wayNodes)) |
| | 0 | 137 | | { |
| | 0 | 138 | | continue; |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | var nodeIds = new List<long>(); |
| | 0 | 142 | | for (var n = 0; n < wayNodes.Count; n++) |
| | 0 | 143 | | { |
| | 0 | 144 | | var nodeToken = wayNodes[n]; |
| | 0 | 145 | | var nodeIdString = nodeToken.Value<string>(); |
| | 0 | 146 | | var nodeId = long.Parse(nodeIdString.Substring( |
| | 0 | 147 | | "http://www.openstreetmap.org/node/".Length, |
| | 0 | 148 | | nodeIdString.Length - "http://www.openstreetmap.org/node/".Length)); |
| | 0 | 149 | | nodeIds.Add(nodeId); |
| | | 150 | | |
| | 0 | 151 | | if (n == 0 || n == wayNodes.Count - 1) |
| | 0 | 152 | | { |
| | | 153 | | // first and last nodes always core. |
| | 0 | 154 | | coreNodes.Add(nodeId); |
| | 0 | 155 | | } |
| | 0 | 156 | | else if (nodes.Contains(nodeId)) |
| | 0 | 157 | | { |
| | | 158 | | // second time this node was hit. |
| | 0 | 159 | | coreNodes.Add(nodeId); |
| | 0 | 160 | | } |
| | | 161 | | |
| | 0 | 162 | | nodes.Add(nodeId); |
| | 0 | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | waysData[wayId] = (nodeIds, attributes); |
| | 0 | 166 | | } |
| | 0 | 167 | | else if (id.StartsWith("http://www.openstreetmap.org/relation/")) |
| | 0 | 168 | | { |
| | | 169 | | // parse as a relation. |
| | | 170 | | // TODO: parse as a relation. |
| | 0 | 171 | | } |
| | 0 | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | var shape = new List<(double longitude, double latitude, float? e)>(); |
| | 0 | 175 | | foreach (var wayPairs in waysData) |
| | 0 | 176 | | { |
| | | 177 | | // prepare for next way. |
| | 0 | 178 | | shape.Clear(); |
| | 0 | 179 | | var previousVertex = VertexId.Empty; |
| | | 180 | | |
| | | 181 | | // get way data. |
| | 0 | 182 | | var wayNodes = wayPairs.Value.nodes; |
| | 0 | 183 | | var attributes = wayPairs.Value.attributes; |
| | | 184 | | |
| | | 185 | | // verify way data and spit out a warning if a way has <= 1 node. |
| | 0 | 186 | | if (wayNodes.Count <= 1) |
| | 0 | 187 | | { |
| | 0 | 188 | | Logger.Log($"{nameof(TileParser)}.{nameof(AddOsmTile)}", |
| | 0 | 189 | | TraceEventType.Warning, |
| | 0 | 190 | | $"Way {wayPairs.Key} has <= 1 nodes."); |
| | 0 | 191 | | continue; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | // iterate over the way segments and add them as edges or part of the next edge. |
| | 0 | 195 | | for (var n = 0; n < wayNodes.Count - 1; n++) |
| | 0 | 196 | | { |
| | 0 | 197 | | var node1Id = wayNodes[n]; |
| | 0 | 198 | | var node2Id = wayNodes[n + 1]; |
| | | 199 | | |
| | | 200 | | // get the nodes data. |
| | 0 | 201 | | if (!nodeLocations.TryGetValue(node1Id, out var node1Data)) |
| | 0 | 202 | | { |
| | 0 | 203 | | Logger.Log(nameof(TileParser), TraceEventType.Warning, |
| | 0 | 204 | | $"Could not load way {wayPairs.Key} in {tile}: node {node1Id} missing."); |
| | 0 | 205 | | break; |
| | | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | if (!nodeLocations.TryGetValue(node2Id, out var node2Data)) |
| | 0 | 209 | | { |
| | 0 | 210 | | Logger.Log(nameof(TileParser), TraceEventType.Warning, |
| | 0 | 211 | | $"Could not load way {wayPairs.Key} in {tile}: node {node2Id} missing."); |
| | 0 | 212 | | break; |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | // always add segments that cross tile boundaries. |
| | | 216 | | // TODO: we can probably do better and add only one of the nodes as core but for now to keep complexity |
| | 0 | 217 | | if (!node1Data.inTile || !node2Data.inTile) |
| | 0 | 218 | | { |
| | 0 | 219 | | coreNodes.Add(node1Id); |
| | 0 | 220 | | coreNodes.Add(node2Id); |
| | 0 | 221 | | } |
| | | 222 | | |
| | | 223 | | // if node1 is core make sure to add it. |
| | 0 | 224 | | if (coreNodes.Contains(node1Id)) |
| | 0 | 225 | | { |
| | | 226 | | // add node1 as vertex but check if it already exists. |
| | 0 | 227 | | if (!globalIdMap.TryGet(node1Id, out var vertex)) |
| | 0 | 228 | | { |
| | 0 | 229 | | vertex = networkWriter.AddVertex(node1Data.location.longitude, |
| | 0 | 230 | | node1Data.location.latitude, null); |
| | 0 | 231 | | globalIdMap.Set(node1Id, vertex); |
| | 0 | 232 | | updated = true; |
| | 0 | 233 | | } |
| | | 234 | | |
| | | 235 | | // check if this segment wasn't just opened the iteration before. |
| | 0 | 236 | | if (vertex != previousVertex) |
| | 0 | 237 | | { |
| | | 238 | | // close previous segment if any. |
| | 0 | 239 | | if (!previousVertex.IsEmpty()) |
| | 0 | 240 | | { |
| | 0 | 241 | | networkWriter.AddEdge(previousVertex, vertex, shape, attributes); |
| | 0 | 242 | | updated = true; |
| | 0 | 243 | | shape.Clear(); |
| | 0 | 244 | | } |
| | | 245 | | |
| | | 246 | | // start a new segment if the end of this one is in tile. |
| | 0 | 247 | | previousVertex = VertexId.Empty; |
| | 0 | 248 | | if (node1Data.inTile) |
| | 0 | 249 | | { |
| | 0 | 250 | | previousVertex = vertex; |
| | 0 | 251 | | } |
| | 0 | 252 | | } |
| | 0 | 253 | | } |
| | | 254 | | |
| | | 255 | | // if the second node is also core, close the segment. |
| | 0 | 256 | | if (coreNodes.Contains(node2Id)) |
| | 0 | 257 | | { |
| | | 258 | | // add node2 as vertex but check if it already exists. |
| | 0 | 259 | | if (!globalIdMap.TryGet(node2Id, out var vertex)) |
| | 0 | 260 | | { |
| | 0 | 261 | | vertex = networkWriter.AddVertex(node2Data.location.longitude, |
| | 0 | 262 | | node2Data.location.latitude, null); |
| | | 263 | | |
| | 0 | 264 | | globalIdMap.Set(node2Id, vertex); |
| | 0 | 265 | | updated = true; |
| | 0 | 266 | | } |
| | | 267 | | |
| | | 268 | | // if this segment overlaps, always add it. |
| | 0 | 269 | | if (!node1Data.inTile || !node2Data.inTile) |
| | 0 | 270 | | { |
| | 0 | 271 | | if (!globalIdMap.TryGet(node1Id, out previousVertex)) |
| | 0 | 272 | | { |
| | 0 | 273 | | throw new Exception( |
| | 0 | 274 | | "Cannot add segment overlapping tile boundary, node should have already been added."); |
| | | 275 | | } |
| | | 276 | | |
| | 0 | 277 | | networkWriter.AddEdge(previousVertex, vertex, shape, attributes); |
| | 0 | 278 | | updated = true; |
| | 0 | 279 | | shape.Clear(); |
| | 0 | 280 | | } |
| | | 281 | | else |
| | 0 | 282 | | { |
| | | 283 | | // close previous segment if any. |
| | 0 | 284 | | if (!previousVertex.IsEmpty()) |
| | 0 | 285 | | { |
| | 0 | 286 | | networkWriter.AddEdge(previousVertex, vertex, shape, attributes); |
| | 0 | 287 | | updated = true; |
| | 0 | 288 | | shape.Clear(); |
| | 0 | 289 | | } |
| | 0 | 290 | | } |
| | | 291 | | |
| | | 292 | | // start a new segment if the end of this one is in tile. |
| | 0 | 293 | | previousVertex = VertexId.Empty; |
| | 0 | 294 | | if (node2Data.inTile) |
| | 0 | 295 | | { |
| | 0 | 296 | | previousVertex = vertex; |
| | 0 | 297 | | } |
| | 0 | 298 | | } |
| | | 299 | | else |
| | 0 | 300 | | { |
| | | 301 | | // add as shape point if there is an active segment. |
| | 0 | 302 | | if (!previousVertex.IsEmpty()) |
| | 0 | 303 | | { |
| | 0 | 304 | | shape.Add(node2Data.location); |
| | 0 | 305 | | } |
| | 0 | 306 | | } |
| | 0 | 307 | | } |
| | 0 | 308 | | } |
| | | 309 | | |
| | 0 | 310 | | return updated; |
| | 0 | 311 | | } |
| | | 312 | | |
| | | 313 | | /// <summary> |
| | | 314 | | /// Gets the OSM tags from the given node/way or relation. |
| | | 315 | | /// </summary> |
| | | 316 | | /// <param name="osmGeo">The node, way or relation json-ld part.</param> |
| | | 317 | | /// <param name="reverseMappings">The reverse mappings.</param> |
| | | 318 | | /// <returns>The tags.</returns> |
| | | 319 | | private static List<(string key, string value)> GetTags(JToken osmGeo, |
| | | 320 | | Dictionary<string, TagMapperConfig> reverseMappings) |
| | 0 | 321 | | { |
| | 0 | 322 | | var attributes = new List<(string key, string value)>(); |
| | | 323 | | |
| | | 324 | | // interpret all tags with defined semantics. |
| | 0 | 325 | | foreach (var child in osmGeo.Children()) |
| | 0 | 326 | | { |
| | 0 | 327 | | if (!(child is JProperty property)) |
| | 0 | 328 | | { |
| | 0 | 329 | | continue; |
| | | 330 | | } |
| | | 331 | | |
| | 0 | 332 | | if (property.Name == "@id" || |
| | 0 | 333 | | property.Name == "@type") |
| | 0 | 334 | | { |
| | 0 | 335 | | continue; |
| | | 336 | | } |
| | | 337 | | |
| | 0 | 338 | | if (property.Value is JArray) |
| | 0 | 339 | | { |
| | 0 | 340 | | continue; |
| | | 341 | | } |
| | | 342 | | |
| | 0 | 343 | | var attribute = property.Map(reverseMappings); |
| | 0 | 344 | | if (attribute == null) |
| | 0 | 345 | | { |
| | 0 | 346 | | continue; |
| | | 347 | | } |
| | | 348 | | |
| | 0 | 349 | | attributes.AddOrReplace(attribute.Value.key, attribute.Value.value); |
| | 0 | 350 | | } |
| | | 351 | | |
| | 0 | 352 | | return attributes; |
| | 0 | 353 | | } |
| | | 354 | | } |