From d7469aa1900b6d26fe3acfe26bb6129b95b68262 Mon Sep 17 00:00:00 2001 From: C9Glax <13404778+C9Glax@users.noreply.github.com> Date: Tue, 14 Mar 2023 17:00:59 +0100 Subject: [PATCH] Changed Regionhashing and OsmDatastructure with OsmDatastructure.Graph --- OSMDatastructure/ByteConverter.cs | 15 ++++++++------- OSMDatastructure/Coordinates.cs | 32 +++++++++++++++++-------------- OSMDatastructure/OSMEdge.cs | 2 +- OSMDatastructure/OsmNode.cs | 2 +- OSMDatastructure/Region.cs | 14 ++++++++------ OSMDatastructure/Utils.cs | 2 +- Pathfinding/Pathfinder.cs | 1 + Pathfinding/RegionManager.cs | 14 +++++++------- Server/XmlImporter.cs | 8 ++++---- 9 files changed, 49 insertions(+), 41 deletions(-) diff --git a/OSMDatastructure/ByteConverter.cs b/OSMDatastructure/ByteConverter.cs index de0833e..a74d30c 100644 --- a/OSMDatastructure/ByteConverter.cs +++ b/OSMDatastructure/ByteConverter.cs @@ -1,4 +1,5 @@ using System.Text; +using OSMDatastructure.Graph; namespace OSMDatastructure; @@ -8,8 +9,8 @@ public static class ByteConverter /* * | regionHash | Nodes | * |---------------+---------------+ - * | 8 bytes | - * | ulong | + * | 4 bytes | + * | int | */ public static byte[] GetBytes(Region region) { @@ -22,9 +23,9 @@ public static class ByteConverter nodes.Add(nodeBytes); } - byte[] retBytes = new byte[sizeof(ulong) + totalNodeBytes]; - Array.Copy(BitConverter.GetBytes(region.regionHash), 0, retBytes, 0, sizeof(ulong)); - int offset = sizeof(ulong); + byte[] retBytes = new byte[sizeof(int) + totalNodeBytes]; + Array.Copy(BitConverter.GetBytes(region.regionHash), 0, retBytes, 0, sizeof(int)); + int offset = sizeof(int); foreach (byte[] node in nodes) { Array.Copy(node, 0, retBytes, offset, node.Length); @@ -36,8 +37,8 @@ public static class ByteConverter public static Region ToRegion(byte[] bytes) { - Region retRegion = new Region(BitConverter.ToUInt64(bytes, 0)); - int offset = sizeof(ulong); + Region retRegion = new Region(BitConverter.ToInt32(bytes, 0)); + int offset = sizeof(int); while (offset < bytes.Length) { int size = BitConverter.ToInt32(bytes, offset); diff --git a/OSMDatastructure/Coordinates.cs b/OSMDatastructure/Coordinates.cs index c46b0ed..ec48f9b 100644 --- a/OSMDatastructure/Coordinates.cs +++ b/OSMDatastructure/Coordinates.cs @@ -1,4 +1,4 @@ -namespace OSMDatastructure; +namespace OSMDatastructure.Graph; public class Coordinates { @@ -24,25 +24,29 @@ public class Coordinates private const ulong offset = 10000000; //Latitude maxChars = 7 //Longitude maxChars = 8 - public ulong GetHash() - { - ulong latHash = Convert.ToUInt64((this.latitude + 90) * decimalCoordsSave); - ulong lonHash = Convert.ToUInt64((this.longitude + 180) * decimalCoordsSave); - return latHash * offset + lonHash; - } public override int GetHashCode() { - return HashCode.Combine(latitude, longitude); + return GetHashCode(latitude, longitude); } - public ulong GetRegionHash() + public static int GetRegionHashCode(float latitude, float longitude) { - float latRegion = this.latitude - this.latitude % Region.regionSize; - float lonRegion = this.longitude - this.longitude % Region.regionSize; - ulong latHash = Convert.ToUInt64((latRegion + 90) * decimalCoordsSave); - ulong lonHash = Convert.ToUInt64((lonRegion + 180) * decimalCoordsSave); - return latHash * offset + lonHash; + float latRegion = latitude - latitude % Region.RegionSize; + float lonRegion = longitude - longitude % Region.RegionSize; + return GetHashCode(latRegion, lonRegion); + } + + public static int GetRegionHashCode(Coordinates coordinates) + { + float latRegion = coordinates.latitude - coordinates.latitude % Region.RegionSize; + float lonRegion = coordinates.longitude - coordinates.longitude % Region.RegionSize; + return GetHashCode(latRegion, lonRegion); + } + + public static int GetHashCode(float latitude, float longitude) + { + return HashCode.Combine(latitude, longitude); } public override string ToString() diff --git a/OSMDatastructure/OSMEdge.cs b/OSMDatastructure/OSMEdge.cs index 35ab170..a6a9c2b 100644 --- a/OSMDatastructure/OSMEdge.cs +++ b/OSMDatastructure/OSMEdge.cs @@ -1,4 +1,4 @@ -namespace OSMDatastructure; +namespace OSMDatastructure.Graph; public class OsmEdge { diff --git a/OSMDatastructure/OsmNode.cs b/OSMDatastructure/OsmNode.cs index 5aaa550..79b7667 100644 --- a/OSMDatastructure/OsmNode.cs +++ b/OSMDatastructure/OsmNode.cs @@ -1,4 +1,4 @@ -namespace OSMDatastructure; +namespace OSMDatastructure.Graph; public class OsmNode { diff --git a/OSMDatastructure/Region.cs b/OSMDatastructure/Region.cs index 2e4b57a..c9902c1 100644 --- a/OSMDatastructure/Region.cs +++ b/OSMDatastructure/Region.cs @@ -1,22 +1,24 @@ +using OSMDatastructure.Graph; + namespace OSMDatastructure; public class Region { - public const float regionSize = 0.01f; + public const float RegionSize = 0.01f; public readonly HashSet nodes = new(); - public ulong regionHash { get; } + public int regionHash { get; } - public Region(ulong regionHash) + public Region(int regionHash) { this.regionHash = regionHash; } - public Region(Coordinates regionCoordinates) + public Region(Coordinates coordinates) { - this.regionHash = regionCoordinates.GetRegionHash(); + regionHash = Coordinates.GetRegionHashCode(coordinates); } - public OsmNode? GetNode(Coordinates coordinates) + public OsmNode? GetNodeWithCoordinates(Coordinates coordinates) { foreach(OsmNode node in this.nodes) if (node.coordinates.Equals(coordinates)) diff --git a/OSMDatastructure/Utils.cs b/OSMDatastructure/Utils.cs index 7cb4900..ec8e214 100644 --- a/OSMDatastructure/Utils.cs +++ b/OSMDatastructure/Utils.cs @@ -1,4 +1,4 @@ -using OSMDatastructure; +using OSMDatastructure.Graph; namespace OSMDatastructure { diff --git a/Pathfinding/Pathfinder.cs b/Pathfinding/Pathfinder.cs index 81b012d..60f48b8 100644 --- a/Pathfinding/Pathfinder.cs +++ b/Pathfinding/Pathfinder.cs @@ -1,4 +1,5 @@ using OSMDatastructure; +using OSMDatastructure.Graph; using OSMImporter; namespace Pathfinding; diff --git a/Pathfinding/RegionManager.cs b/Pathfinding/RegionManager.cs index b534ab0..7d5acf0 100644 --- a/Pathfinding/RegionManager.cs +++ b/Pathfinding/RegionManager.cs @@ -1,12 +1,12 @@ using OSMDatastructure; -using Pathfinding; +using OSMDatastructure.Graph; namespace OSMImporter { public class RegionManager { private string workingDirectory { get; } - private readonly Dictionary _regions = new(); + private readonly Dictionary _regions = new(); public RegionManager(string workingDirectory) { @@ -21,12 +21,12 @@ namespace OSMImporter /// If the Regionfile can not be found. public Region GetRegion(Coordinates coordinates) { - if(this._regions.ContainsKey(coordinates.GetRegionHash())) - return this._regions[coordinates.GetRegionHash()]; + if(_regions.ContainsKey(Coordinates.GetRegionHashCode(coordinates))) + return _regions[Coordinates.GetRegionHashCode(coordinates)]; else { Region loadedRegion = LoadRegion(coordinates); - this._regions.Add(loadedRegion.regionHash, value: loadedRegion); + _regions.Add(loadedRegion.regionHash, value: loadedRegion); return loadedRegion; } } @@ -44,7 +44,7 @@ namespace OSMImporter /// If the Regionfile can not be found. private Region LoadRegion(Coordinates coordinates) { - string fullPath = Path.Combine(workingDirectory, coordinates.GetRegionHash().ToString()); + string fullPath = Path.Combine(workingDirectory, Coordinates.GetRegionHashCode(coordinates).ToString()); DateTime startTime = DateTime.Now; if (!File.Exists(fullPath)) { @@ -66,7 +66,7 @@ namespace OSMImporter public OsmNode? GetNode(Coordinates coordinates) { Region regionWithNode = GetRegion(coordinates); - return regionWithNode.GetNode(coordinates); + return regionWithNode.GetNodeWithCoordinates(coordinates); } } } \ No newline at end of file diff --git a/Server/XmlImporter.cs b/Server/XmlImporter.cs index 16e6f3a..f035c6a 100644 --- a/Server/XmlImporter.cs +++ b/Server/XmlImporter.cs @@ -1,7 +1,7 @@ using System.Globalization; using System.Xml; using OSMDatastructure; -using OSMImporter; +using OSMDatastructure.Graph; namespace Server; @@ -39,10 +39,10 @@ public static class XmlImporter public static HashSet SplitIntoRegions(HashSet nodes) { Console.WriteLine(string.Format("[{0}] Splitting into Regions...", DateTime.Now.ToLocalTime())); - Dictionary retRegions = new(); + Dictionary retRegions = new(); foreach (OsmNode node in nodes) { - ulong regionHash = node.coordinates.GetRegionHash(); + int regionHash = Coordinates.GetRegionHashCode(node.coordinates); if(retRegions.ContainsKey(regionHash)) { retRegions[regionHash].nodes.Add(node); @@ -62,7 +62,7 @@ public static class XmlImporter { NumberDecimalSeparator = "." }; - private static HashSet GetHighwayNodeIds(XmlReader xmlReader) + public static HashSet GetHighwayNodeIds(XmlReader xmlReader) { HashSet retSet = new(); bool isHighway;