Changed Regionhashing and OsmDatastructure with OsmDatastructure.Graph

This commit is contained in:
C9Glax 2023-03-14 17:00:59 +01:00
parent 583fe3c18d
commit d7469aa190
9 changed files with 49 additions and 41 deletions

View File

@ -1,4 +1,5 @@
using System.Text; using System.Text;
using OSMDatastructure.Graph;
namespace OSMDatastructure; namespace OSMDatastructure;
@ -8,8 +9,8 @@ public static class ByteConverter
/* /*
* | regionHash | Nodes | * | regionHash | Nodes |
* |---------------+---------------+ * |---------------+---------------+
* | 8 bytes | * | 4 bytes |
* | ulong | * | int |
*/ */
public static byte[] GetBytes(Region region) public static byte[] GetBytes(Region region)
{ {
@ -22,9 +23,9 @@ public static class ByteConverter
nodes.Add(nodeBytes); nodes.Add(nodeBytes);
} }
byte[] retBytes = new byte[sizeof(ulong) + totalNodeBytes]; byte[] retBytes = new byte[sizeof(int) + totalNodeBytes];
Array.Copy(BitConverter.GetBytes(region.regionHash), 0, retBytes, 0, sizeof(ulong)); Array.Copy(BitConverter.GetBytes(region.regionHash), 0, retBytes, 0, sizeof(int));
int offset = sizeof(ulong); int offset = sizeof(int);
foreach (byte[] node in nodes) foreach (byte[] node in nodes)
{ {
Array.Copy(node, 0, retBytes, offset, node.Length); Array.Copy(node, 0, retBytes, offset, node.Length);
@ -36,8 +37,8 @@ public static class ByteConverter
public static Region ToRegion(byte[] bytes) public static Region ToRegion(byte[] bytes)
{ {
Region retRegion = new Region(BitConverter.ToUInt64(bytes, 0)); Region retRegion = new Region(BitConverter.ToInt32(bytes, 0));
int offset = sizeof(ulong); int offset = sizeof(int);
while (offset < bytes.Length) while (offset < bytes.Length)
{ {
int size = BitConverter.ToInt32(bytes, offset); int size = BitConverter.ToInt32(bytes, offset);

View File

@ -1,4 +1,4 @@
namespace OSMDatastructure; namespace OSMDatastructure.Graph;
public class Coordinates public class Coordinates
{ {
@ -24,25 +24,29 @@ public class Coordinates
private const ulong offset = 10000000; private const ulong offset = 10000000;
//Latitude maxChars = 7 //Latitude maxChars = 7
//Longitude maxChars = 8 //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() 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 latRegion = latitude - latitude % Region.RegionSize;
float lonRegion = this.longitude - this.longitude % Region.regionSize; float lonRegion = longitude - longitude % Region.RegionSize;
ulong latHash = Convert.ToUInt64((latRegion + 90) * decimalCoordsSave); return GetHashCode(latRegion, lonRegion);
ulong lonHash = Convert.ToUInt64((lonRegion + 180) * decimalCoordsSave); }
return latHash * offset + lonHash;
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() public override string ToString()

View File

@ -1,4 +1,4 @@
namespace OSMDatastructure; namespace OSMDatastructure.Graph;
public class OsmEdge public class OsmEdge
{ {

View File

@ -1,4 +1,4 @@
namespace OSMDatastructure; namespace OSMDatastructure.Graph;
public class OsmNode public class OsmNode
{ {

View File

@ -1,22 +1,24 @@
using OSMDatastructure.Graph;
namespace OSMDatastructure; namespace OSMDatastructure;
public class Region public class Region
{ {
public const float regionSize = 0.01f; public const float RegionSize = 0.01f;
public readonly HashSet<OsmNode> nodes = new(); public readonly HashSet<OsmNode> nodes = new();
public ulong regionHash { get; } public int regionHash { get; }
public Region(ulong regionHash) public Region(int regionHash)
{ {
this.regionHash = 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) foreach(OsmNode node in this.nodes)
if (node.coordinates.Equals(coordinates)) if (node.coordinates.Equals(coordinates))

View File

@ -1,4 +1,4 @@
using OSMDatastructure; using OSMDatastructure.Graph;
namespace OSMDatastructure namespace OSMDatastructure
{ {

View File

@ -1,4 +1,5 @@
using OSMDatastructure; using OSMDatastructure;
using OSMDatastructure.Graph;
using OSMImporter; using OSMImporter;
namespace Pathfinding; namespace Pathfinding;

View File

@ -1,12 +1,12 @@
using OSMDatastructure; using OSMDatastructure;
using Pathfinding; using OSMDatastructure.Graph;
namespace OSMImporter namespace OSMImporter
{ {
public class RegionManager public class RegionManager
{ {
private string workingDirectory { get; } private string workingDirectory { get; }
private readonly Dictionary<ulong, Region> _regions = new(); private readonly Dictionary<int, Region> _regions = new();
public RegionManager(string workingDirectory) public RegionManager(string workingDirectory)
{ {
@ -21,12 +21,12 @@ namespace OSMImporter
/// <exception cref="FileNotFoundException">If the Regionfile can not be found.</exception> /// <exception cref="FileNotFoundException">If the Regionfile can not be found.</exception>
public Region GetRegion(Coordinates coordinates) public Region GetRegion(Coordinates coordinates)
{ {
if(this._regions.ContainsKey(coordinates.GetRegionHash())) if(_regions.ContainsKey(Coordinates.GetRegionHashCode(coordinates)))
return this._regions[coordinates.GetRegionHash()]; return _regions[Coordinates.GetRegionHashCode(coordinates)];
else else
{ {
Region loadedRegion = LoadRegion(coordinates); Region loadedRegion = LoadRegion(coordinates);
this._regions.Add(loadedRegion.regionHash, value: loadedRegion); _regions.Add(loadedRegion.regionHash, value: loadedRegion);
return loadedRegion; return loadedRegion;
} }
} }
@ -44,7 +44,7 @@ namespace OSMImporter
/// <exception cref="FileNotFoundException">If the Regionfile can not be found.</exception> /// <exception cref="FileNotFoundException">If the Regionfile can not be found.</exception>
private Region LoadRegion(Coordinates coordinates) 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; DateTime startTime = DateTime.Now;
if (!File.Exists(fullPath)) if (!File.Exists(fullPath))
{ {
@ -66,7 +66,7 @@ namespace OSMImporter
public OsmNode? GetNode(Coordinates coordinates) public OsmNode? GetNode(Coordinates coordinates)
{ {
Region regionWithNode = GetRegion(coordinates); Region regionWithNode = GetRegion(coordinates);
return regionWithNode.GetNode(coordinates); return regionWithNode.GetNodeWithCoordinates(coordinates);
} }
} }
} }

View File

@ -1,7 +1,7 @@
using System.Globalization; using System.Globalization;
using System.Xml; using System.Xml;
using OSMDatastructure; using OSMDatastructure;
using OSMImporter; using OSMDatastructure.Graph;
namespace Server; namespace Server;
@ -39,10 +39,10 @@ public static class XmlImporter
public static HashSet<Region> SplitIntoRegions(HashSet<OsmNode> nodes) public static HashSet<Region> SplitIntoRegions(HashSet<OsmNode> nodes)
{ {
Console.WriteLine(string.Format("[{0}] Splitting into Regions...", DateTime.Now.ToLocalTime())); Console.WriteLine(string.Format("[{0}] Splitting into Regions...", DateTime.Now.ToLocalTime()));
Dictionary<ulong, Region> retRegions = new(); Dictionary<int, Region> retRegions = new();
foreach (OsmNode node in nodes) foreach (OsmNode node in nodes)
{ {
ulong regionHash = node.coordinates.GetRegionHash(); int regionHash = Coordinates.GetRegionHashCode(node.coordinates);
if(retRegions.ContainsKey(regionHash)) if(retRegions.ContainsKey(regionHash))
{ {
retRegions[regionHash].nodes.Add(node); retRegions[regionHash].nodes.Add(node);
@ -62,7 +62,7 @@ public static class XmlImporter
{ {
NumberDecimalSeparator = "." NumberDecimalSeparator = "."
}; };
private static HashSet<ulong> GetHighwayNodeIds(XmlReader xmlReader) public static HashSet<ulong> GetHighwayNodeIds(XmlReader xmlReader)
{ {
HashSet<ulong> retSet = new(); HashSet<ulong> retSet = new();
bool isHighway; bool isHighway;