From 27cce159a456f0ed3a16fd7f712283f57f86417f Mon Sep 17 00:00:00 2001 From: C9Glax <13404778+C9Glax@users.noreply.github.com> Date: Fri, 3 Feb 2023 23:34:51 +0100 Subject: [PATCH] Moved RegionManager inside of Importer as RegionStruct, Moved Regionsize var to Region --- OSMDatastructure/Coordinates.cs | 6 +++--- OSMDatastructure/Region.cs | 14 ++++++++++---- OSMSplitter/Importer.cs | 33 +++++++++++++++++++++++++++------ OSMSplitter/RegionManager.cs | 31 ------------------------------- 4 files changed, 40 insertions(+), 44 deletions(-) delete mode 100644 OSMSplitter/RegionManager.cs diff --git a/OSMDatastructure/Coordinates.cs b/OSMDatastructure/Coordinates.cs index 8ce5414..368af4d 100644 --- a/OSMDatastructure/Coordinates.cs +++ b/OSMDatastructure/Coordinates.cs @@ -24,10 +24,10 @@ namespace OSMDatastructure return latHash * offset + lonHash; } - public ulong GetRegionHash(float regionSize) + public ulong GetRegionHash() { - float latRegion = this.lat - this.lat % regionSize; - float lonRegion = this.lon - this.lon % regionSize; + float latRegion = this.lat - this.lat % Region.regionSize; + float lonRegion = this.lon - this.lon % Region.regionSize; ulong latHash = Convert.ToUInt64((latRegion + 90) * decimalCoordsSave); ulong lonHash = Convert.ToUInt64((lonRegion + 180) * decimalCoordsSave); return latHash * offset + lonHash; diff --git a/OSMDatastructure/Region.cs b/OSMDatastructure/Region.cs index a05c109..a1a5c88 100644 --- a/OSMDatastructure/Region.cs +++ b/OSMDatastructure/Region.cs @@ -2,17 +2,18 @@ namespace OSMDatastructure { public class Region { + public const float regionSize = 0.01f; private readonly Dictionary _nodesInRegion = new(); public ulong regionHash { get; } - public Region(Coordinates regionCoordinates, float regionSize) + public Region(Coordinates regionCoordinates) { - this.regionHash = regionCoordinates.GetRegionHash(regionSize); + this.regionHash = regionCoordinates.GetRegionHash(); } - public Region(ulong nodeId, Node firstNode, float regionSize) + public Region(ulong nodeId, Node firstNode) { - this.regionHash = firstNode.GetRegionHash(regionSize); + this.regionHash = firstNode.GetRegionHash(); this._nodesInRegion.Add(nodeId, value: firstNode); } @@ -31,5 +32,10 @@ namespace OSMDatastructure return this._nodesInRegion; } + public Node? GetNode(ulong id) + { + return this._nodesInRegion.ContainsKey(id) ? this._nodesInRegion[id] : null; + } + } } \ No newline at end of file diff --git a/OSMSplitter/Importer.cs b/OSMSplitter/Importer.cs index fc99680..04973eb 100644 --- a/OSMSplitter/Importer.cs +++ b/OSMSplitter/Importer.cs @@ -8,7 +8,6 @@ namespace OSMImporter public static class Importer { - public const float regionSize = 0.01f; private static readonly XmlReaderSettings readerSettings = new() { IgnoreWhitespace = true, @@ -30,10 +29,10 @@ namespace OSMImporter Dictionary nodes = ReturnNodeIdDictionary(XmlReader.Create(xmlFileStream, readerSettings)); xmlFileStream.Position = 0; - RegionManager regionManager = new RegionManager(); + RegionStruct regionStruct = new RegionStruct(); Console.WriteLine("Reading nodes..."); - LoadNodesIntoDictionary(ref nodes, XmlReader.Create(xmlFileStream, readerSettings), ref regionManager); + LoadNodesIntoDictionary(ref nodes, XmlReader.Create(xmlFileStream, readerSettings), ref regionStruct); xmlFileStream.Position = 0; @@ -42,7 +41,7 @@ namespace OSMImporter Console.WriteLine("Writing..."); - foreach(Region region in regionManager.GetAllRegions()) + foreach(Region region in regionStruct.GetAllRegions()) WriteRegion(region, outputFolderPath); } @@ -79,7 +78,7 @@ namespace OSMImporter return retSet; } - private static void LoadNodesIntoDictionary(ref Dictionary nodes, XmlReader xmlReader, ref RegionManager regionManager) + private static void LoadNodesIntoDictionary(ref Dictionary nodes, XmlReader xmlReader, ref RegionStruct regionStruct) { while (xmlReader.ReadToFollowing("node")) { @@ -90,7 +89,7 @@ namespace OSMImporter float lon = Convert.ToSingle(xmlReader.GetAttribute("lon")!, coordinateFormat); Node newNode = new Node(lat, lon); nodes[id] = newNode; - regionManager.GetRegion(newNode).AddNode(id, newNode); + regionStruct.GetRegion(newNode).AddNode(id, newNode); } } xmlReader.Close(); @@ -182,4 +181,26 @@ namespace OSMImporter fileStream.Close(); } } + + internal class RegionStruct + { + private readonly Dictionary _regions = new(); + + public Region GetRegion(Coordinates coordinates) + { + if(this._regions.ContainsKey(coordinates.GetRegionHash())) + return this._regions[coordinates.GetRegionHash()]; + else + { + Region newRegion = new Region(coordinates); + this._regions.Add(newRegion.regionHash, value: newRegion); + return newRegion; + } + } + + public Region[] GetAllRegions() + { + return this._regions.Values.ToArray(); + } + } } \ No newline at end of file diff --git a/OSMSplitter/RegionManager.cs b/OSMSplitter/RegionManager.cs deleted file mode 100644 index 00fc64f..0000000 --- a/OSMSplitter/RegionManager.cs +++ /dev/null @@ -1,31 +0,0 @@ -using OSMDatastructure; - -namespace OSMImporter -{ - public class RegionManager - { - private readonly Dictionary _regions; - - public RegionManager() - { - this._regions = new Dictionary(); - } - - public Region GetRegion(Coordinates coordinates) - { - if(this._regions.ContainsKey(coordinates.GetRegionHash(Importer.regionSize))) - return this._regions[coordinates.GetRegionHash(Importer.regionSize)]; - else - { - Region newRegion = new Region(coordinates, Importer.regionSize); - this._regions.Add(newRegion.regionHash, value: newRegion); - return newRegion; - } - } - - public Region[] GetAllRegions() - { - return this._regions.Values.ToArray(); - } - } -} \ No newline at end of file