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 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);

View File

@ -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()

View File

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

View File

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

View File

@ -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<OsmNode> 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))

View File

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

View File

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

View File

@ -1,12 +1,12 @@
using OSMDatastructure;
using Pathfinding;
using OSMDatastructure.Graph;
namespace OSMImporter
{
public class RegionManager
{
private string workingDirectory { get; }
private readonly Dictionary<ulong, Region> _regions = new();
private readonly Dictionary<int, Region> _regions = new();
public RegionManager(string workingDirectory)
{
@ -21,12 +21,12 @@ namespace OSMImporter
/// <exception cref="FileNotFoundException">If the Regionfile can not be found.</exception>
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
/// <exception cref="FileNotFoundException">If the Regionfile can not be found.</exception>
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);
}
}
}

View File

@ -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<Region> SplitIntoRegions(HashSet<OsmNode> nodes)
{
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)
{
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<ulong> GetHighwayNodeIds(XmlReader xmlReader)
public static HashSet<ulong> GetHighwayNodeIds(XmlReader xmlReader)
{
HashSet<ulong> retSet = new();
bool isHighway;