OSMServer/OSMDatastructure/Coordinates.cs
2023-03-30 18:24:57 +02:00

54 lines
1.9 KiB
C#

using System.Globalization;
namespace OSMDatastructure.Graph;
[Serializable]
public class Coordinates
{
public float latitude { get; }
public float longitude { get; }
public Coordinates(float latitude, float longitude)
{
this.latitude = latitude;
this.longitude = longitude;
}
public override bool Equals(object? obj)
{
if (obj == null || obj.GetType() != this.GetType())
return false;
Coordinates convObj = (Coordinates)obj;
// ReSharper disable twice CompareOfFloatsByEqualityOperator static values
return convObj.latitude == this.latitude && convObj.longitude == this.longitude;
}
public static ulong GetRegionHashCode(float latitude, float longitude)
{
float latRegion = latitude - latitude % Region.RegionSize;
float lonRegion = longitude - longitude % Region.RegionSize;
return GetHashCode(latRegion, lonRegion);
}
public static ulong GetRegionHashCode(Coordinates coordinates)
{
float latRegion = coordinates.latitude - coordinates.latitude % Region.RegionSize;
float lonRegion = coordinates.longitude - coordinates.longitude % Region.RegionSize;
return GetHashCode(latRegion, lonRegion);
}
private const float decimalCoordsSave = 10000; //Latitude maxChars = 7
private const ulong offset = 10000000; //Longitude maxChars = 8
public static ulong GetHashCode(float latitude, float longitude)
{
ulong latHash = Convert.ToUInt64((latitude + 90) * decimalCoordsSave);
ulong lonHash = Convert.ToUInt64((longitude + 180) * decimalCoordsSave);
return latHash * offset + lonHash;
}
public override string ToString()
{
return
$"COORDINATES Lat: {this.latitude.ToString(NumberFormatInfo.InvariantInfo)} Lon: {this.longitude.ToString(CultureInfo.InvariantCulture)}";
}
}