From 14533c150f10096e8b25903900c0a47951d29184 Mon Sep 17 00:00:00 2001 From: glax Date: Sun, 9 Apr 2023 22:10:23 +0200 Subject: [PATCH] Fixing one-way issue --- OSMDatastructure/Tag.cs | 46 ++++++++++++++++++------------ OSMDatastructure/TagManager.cs | 6 ++-- Server/RegionConverter.cs | 51 +++++++++++++++++++++------------- 3 files changed, 62 insertions(+), 41 deletions(-) diff --git a/OSMDatastructure/Tag.cs b/OSMDatastructure/Tag.cs index cdc8bd4..2be1496 100644 --- a/OSMDatastructure/Tag.cs +++ b/OSMDatastructure/Tag.cs @@ -41,49 +41,59 @@ public class Tag } } - public static Tag? ConvertToTag(string key, string value) + public static HashSet ConvertToTags(string key, string value) { + HashSet ret = new HashSet(); switch (key) { case "highway": try { - return new Tag(TagType.highway, (WayType)Enum.Parse(typeof(WayType), value, true)); + ret.Add(new Tag(TagType.highway, (WayType)Enum.Parse(typeof(WayType), value, true))); } catch (ArgumentException) { - return new Tag(TagType.highway, WayType.unclassified); + ret.Add(new Tag(TagType.highway, WayType.unclassified)); } + break; case "maxspeed": try { byte speed = Convert.ToByte(value); - if (speed == 255) - return new Tag(TagType.highway, false); - else - return new Tag(TagType.maxspeed, speed); + if (speed != 255) + ret.Add(new Tag(TagType.maxspeed, speed)); + break; } catch (Exception) { - //Console.WriteLine(e); - //Console.WriteLine("Continuing..."); - return new Tag(TagType.maxspeed, byte.MaxValue); + ret.Add(new Tag(TagType.maxspeed, byte.MinValue)); } + + break; case "oneway": switch (value) { case "yes": - return new Tag(TagType.oneway, true); + ret.Add(new Tag(TagType.oneway, true)); + break; case "-1": - return new Tag(TagType.forward, false); + ret.Add(new Tag(TagType.forward, false)); + ret.Add(new Tag(TagType.oneway, true)); + break; case "no": - return new Tag(TagType.oneway, false); + ret.Add(new Tag(TagType.oneway, false)); + break; } - + return null; + case "name": + ret.Add(new Tag(TagType.name, value)); + break; + case "ref": + ret.Add(new Tag(TagType.tagref, value)); break; } - return null; + return ret; } public override string ToString() @@ -93,7 +103,7 @@ public class Tag public enum TagType : byte { - highway, oneway, footway, sidewalk, cycleway, busway, forward, maxspeed, name, surface, lanes, access, tracktype, id + highway, oneway, footway, sidewalk, cycleway, busway, forward, maxspeed, name, surface, lanes, access, tracktype, id, tagref } public static readonly Dictionary defaultSpeedCar = new() { @@ -104,8 +114,8 @@ public class Tag { WayType.primary, 65 }, { WayType.secondary, 60 }, { WayType.tertiary, 50 }, - { WayType.unclassified, 15 }, - { WayType.residential, 10 }, + { WayType.unclassified, 30 }, + { WayType.residential, 15 }, { WayType.motorway_link, 60 }, { WayType.trunk_link, 50 }, { WayType.primary_link, 50 }, diff --git a/OSMDatastructure/TagManager.cs b/OSMDatastructure/TagManager.cs index 609828c..dee1746 100644 --- a/OSMDatastructure/TagManager.cs +++ b/OSMDatastructure/TagManager.cs @@ -25,9 +25,9 @@ public class TagManager public void AddTag(ulong wayId, string key, string value) { - Tag? tag = Tag.ConvertToTag(key, value); - if(tag is not null) - AddTag(wayId, tag); + HashSet pTags = Tag.ConvertToTags(key, value); + foreach (Tag pTag in pTags) + AddTag(wayId, pTag); } public void AddTag(ulong wayId, Tag tag) diff --git a/Server/RegionConverter.cs b/Server/RegionConverter.cs index 12f76c1..be830bb 100644 --- a/Server/RegionConverter.cs +++ b/Server/RegionConverter.cs @@ -129,9 +129,9 @@ public class RegionConverter currentTags.TryAdd(Tag.TagType.id, Convert.ToUInt64(wayReader.GetAttribute("id")!)); if (wayReader.Name == "tag") { - Tag? wayTag = Tag.ConvertToTag(wayReader.GetAttribute("k")!, wayReader.GetAttribute("v")!); - if(wayTag is not null) - currentTags.TryAdd(wayTag.key, wayTag.value); + HashSet pTags = Tag.ConvertToTags(wayReader.GetAttribute("k")!, wayReader.GetAttribute("v")!); + foreach (Tag pTag in pTags) + currentTags.TryAdd(pTag.key, pTag.value); } else if (wayReader.Name == "nd") { @@ -146,31 +146,42 @@ public class RegionConverter { ulong node1Id = currentNodeIds[i]; ulong node2Id = currentNodeIds[i+1]; - if (currentTags.ContainsKey(Tag.TagType.oneway) && (bool)currentTags[Tag.TagType.oneway] && nodeRegions.ContainsKey(node1Id) && nodeRegions.ContainsKey(node2Id)) + if (nodeRegions.ContainsKey(node1Id) && nodeRegions.ContainsKey(node2Id)) { - if (currentTags.ContainsKey(Tag.TagType.forward) && !(bool)currentTags[Tag.TagType.forward]) + if (currentTags.ContainsKey(Tag.TagType.oneway) && (bool)currentTags[Tag.TagType.oneway]) { - OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]); - WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath); - WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id], currentTags, outputPath); + if (currentTags.ContainsKey(Tag.TagType.forward) && !(bool)currentTags[Tag.TagType.forward]) + { + OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id, + nodeRegions[node1Id]); + WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath); + WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id], + currentTags, outputPath); + } + else if (currentTags.ContainsKey(Tag.TagType.forward) && (bool)currentTags[Tag.TagType.forward]) + { + OsmEdge n12e = new OsmEdge(currentTags[Tag.TagType.id], node1Id, node2Id, + nodeRegions[node2Id]); + WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath); + WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id], + currentTags, outputPath); + } } else { - OsmEdge n12e = new OsmEdge(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]); + OsmEdge n12e = new OsmEdge(currentTags[Tag.TagType.id], node1Id, node2Id, + nodeRegions[node2Id]); WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath); - WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id], currentTags, outputPath); + WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id], + currentTags, outputPath); + + OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id, + nodeRegions[node1Id]); + WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath); + WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id], + currentTags, outputPath); } } - else if(nodeRegions.ContainsKey(node1Id) && nodeRegions.ContainsKey(node2Id)) - { - OsmEdge n12e = new OsmEdge(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]); - WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath); - WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id], currentTags, outputPath); - - OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]); - WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath); - WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id], currentTags, outputPath); - } } } }