Changed Regionhashing and OsmDatastructure with OsmDatastructure.Graph

This commit is contained in:
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
{