From 806dcf98c94662a3d3deb976d9350cb85ff8438a Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 1 Apr 2023 01:47:56 +0200 Subject: [PATCH] Renamed OsmWay -> OsmEdge again Prevented duplicate writes of Tags for way --- OSMDatastructure/{OsmWay.cs => OsmEdge.cs} | 4 +-- OSMDatastructure/OsmNode.cs | 6 ++-- OSMDatastructure/Region.cs | 2 +- Server/RegionConverter.cs | 34 +++++++++++++--------- Server/RegionLoader.cs | 6 ++-- 5 files changed, 30 insertions(+), 22 deletions(-) rename OSMDatastructure/{OsmWay.cs => OsmEdge.cs} (82%) diff --git a/OSMDatastructure/OsmWay.cs b/OSMDatastructure/OsmEdge.cs similarity index 82% rename from OSMDatastructure/OsmWay.cs rename to OSMDatastructure/OsmEdge.cs index dd8e8da..1a6a7c1 100644 --- a/OSMDatastructure/OsmWay.cs +++ b/OSMDatastructure/OsmEdge.cs @@ -1,7 +1,7 @@ namespace OSMDatastructure.Graph; [Serializable] -public class OsmWay +public class OsmEdge { public ulong wayId { get; } public ulong startId { get; } @@ -9,7 +9,7 @@ public class OsmWay public ulong neighborRegion { get; } - public OsmWay(ulong wayId, ulong startId, ulong neighborId, ulong neighborRegion) + public OsmEdge(ulong wayId, ulong startId, ulong neighborId, ulong neighborRegion) { this.wayId = wayId; this.startId = startId; diff --git a/OSMDatastructure/OsmNode.cs b/OSMDatastructure/OsmNode.cs index 1d620e1..507aa2f 100644 --- a/OSMDatastructure/OsmNode.cs +++ b/OSMDatastructure/OsmNode.cs @@ -4,7 +4,7 @@ namespace OSMDatastructure.Graph; public class OsmNode { public ulong nodeId { get; } - public HashSet edges { get; } + public HashSet edges { get; } public Coordinates coordinates { get; } [NonSerialized]public OsmNode? previousPathNode = null; @@ -26,9 +26,9 @@ public class OsmNode this.coordinates = coordinates; } - public OsmWay? GetEdgeToNode(OsmNode n) + public OsmEdge? GetEdgeToNode(OsmNode n) { - foreach (OsmWay e in this.edges) + foreach (OsmEdge e in this.edges) if (e.neighborId == n.nodeId) return e; return null; diff --git a/OSMDatastructure/Region.cs b/OSMDatastructure/Region.cs index 9f793c2..55e83f6 100644 --- a/OSMDatastructure/Region.cs +++ b/OSMDatastructure/Region.cs @@ -7,7 +7,7 @@ public class Region { [NonSerialized]public const float RegionSize = 0.1f; public readonly HashSet nodes = new(); - public readonly HashSet ways = new(); + public readonly HashSet ways = new(); public ulong regionHash { get; } public TagManager tagManager { get; } diff --git a/Server/RegionConverter.cs b/Server/RegionConverter.cs index 7dc63b3..3b4ebcc 100644 --- a/Server/RegionConverter.cs +++ b/Server/RegionConverter.cs @@ -110,6 +110,7 @@ public class RegionConverter Dictionary currentTags = new(); Dictionary regionWaysFileStreams = new(); Dictionary regionTagsFileStreams = new(); + Dictionary> writtenWayTagsInRegion = new(); while (xmlReader.ReadToFollowing("way")) { currentNodeIds.Clear(); @@ -141,26 +142,26 @@ public class RegionConverter { if (currentTags.ContainsKey(Tag.TagType.forward) && !(bool)currentTags[Tag.TagType.forward]) { - OsmWay n21e = new OsmWay(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]); + OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]); WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath); - WriteTag(ref regionTagsFileStreams, nodeRegions[node2Id], currentTags, outputPath); + WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id], currentTags, outputPath); } else { - OsmWay n12e = new OsmWay(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); - WriteTag(ref regionTagsFileStreams, nodeRegions[node1Id], currentTags, outputPath); + WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id], currentTags, outputPath); } } else if(nodeRegions.ContainsKey(node1Id) && nodeRegions.ContainsKey(node2Id)) { - OsmWay n12e = new OsmWay(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); - WriteTag(ref regionTagsFileStreams, nodeRegions[node1Id], currentTags, outputPath); + WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id], currentTags, outputPath); - OsmWay n21e = new OsmWay(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]); + OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]); WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath); - WriteTag(ref regionTagsFileStreams, nodeRegions[node2Id], currentTags, outputPath); + WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id], currentTags, outputPath); } } } @@ -172,7 +173,7 @@ public class RegionConverter f.Dispose(); } - private static void WriteWay(ref Dictionary regionWaysFileStreams, ulong regionHash, OsmWay way, string outputPath) + private static void WriteWay(ref Dictionary regionWaysFileStreams, ulong regionHash, OsmEdge edge, string outputPath) { BinaryFormatter bFormatter = new BinaryFormatter(); if (!regionWaysFileStreams.ContainsKey(regionHash)) @@ -181,13 +182,19 @@ public class RegionConverter regionWaysFileStreams.Add(regionHash, new FileStream(waysRegionPath, FileMode.OpenOrCreate)); } #pragma warning disable SYSLIB0011 - bFormatter.Serialize(regionWaysFileStreams[regionHash], way); + bFormatter.Serialize(regionWaysFileStreams[regionHash], edge); #pragma warning restore SYSLIB0011 } - private static void WriteTag(ref Dictionary regionTagsFileStreams, ulong regionHash, Dictionary currentTags, string outputPath) + private static void WriteTags(ref Dictionary regionTagsFileStreams, ref Dictionary> writtenWayTagsInRegion, ulong regionHash, Dictionary currentTags, string outputPath) { + if (writtenWayTagsInRegion.ContainsKey(regionHash) && + writtenWayTagsInRegion[regionHash].Contains(currentTags[Tag.TagType.id])) + return; + else if(!writtenWayTagsInRegion.ContainsKey(regionHash)) + writtenWayTagsInRegion.Add(regionHash, new HashSet()); + BinaryFormatter bFormatter = new BinaryFormatter(); if (!regionTagsFileStreams.ContainsKey(regionHash)) { @@ -195,10 +202,11 @@ public class RegionConverter regionTagsFileStreams.Add(regionHash, new FileStream(tagsRegionPath, FileMode.OpenOrCreate)); } - ulong id = currentTags[Tag.TagType.id]; + ulong wayId = currentTags[Tag.TagType.id]; + writtenWayTagsInRegion[regionHash].Add(wayId); TagManager tm = new TagManager(); foreach(KeyValuePair kv in currentTags) - tm.AddTag(id, kv); + tm.AddTag(wayId, kv); #pragma warning disable SYSLIB0011 bFormatter.Serialize(regionTagsFileStreams[regionHash], tm); #pragma warning restore SYSLIB0011 diff --git a/Server/RegionLoader.cs b/Server/RegionLoader.cs index 3a2aaa7..8f0d741 100644 --- a/Server/RegionLoader.cs +++ b/Server/RegionLoader.cs @@ -20,7 +20,7 @@ public class RegionLoader return r.nodes; } - public HashSet GetWays(ulong regionHash) + public HashSet GetWays(ulong regionHash) { Region r = GetRegion(regionHash); return r.ways; @@ -59,8 +59,8 @@ public class RegionLoader { while (wayFileStream.Position < wayFileStream.Length) { - OsmWay deserializedWay = (OsmWay)bFormatter.Deserialize(wayFileStream); - newRegion.ways.Add(deserializedWay); + OsmEdge deserializedEdge = (OsmEdge)bFormatter.Deserialize(wayFileStream); + newRegion.ways.Add(deserializedEdge); } }