OSMServer/OSMDatastructure/Graph/Coordinates.cs

53 lines
1.7 KiB
C#

using System.Globalization;
using System.Text.Json.Serialization;
namespace OSMDatastructure.Graph;
[Serializable]
public class Coordinates
{
public float latitude { get; }
public float longitude { get; }
[JsonConstructor]
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;
return convObj.latitude.Equals(this.latitude) && convObj.longitude.Equals(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)
{
return GetRegionHashCode(coordinates.latitude, coordinates.longitude);
}
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:{latitude.ToString(NumberFormatInfo.InvariantInfo)} lon:{longitude.ToString(CultureInfo.InvariantCulture)}";
}
}