2023-03-30 18:24:57 +02:00
|
|
|
using System.Globalization;
|
|
|
|
|
2023-03-14 17:00:59 +01:00
|
|
|
namespace OSMDatastructure.Graph;
|
2023-02-02 22:30:43 +01:00
|
|
|
|
2023-03-30 18:24:57 +02:00
|
|
|
[Serializable]
|
2023-02-06 17:32:55 +01:00
|
|
|
public class Coordinates
|
2023-02-02 22:30:43 +01:00
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
public float latitude { get; }
|
|
|
|
public float longitude { get; }
|
2023-02-02 22:30:43 +01:00
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
public Coordinates(float latitude, float longitude)
|
|
|
|
{
|
|
|
|
this.latitude = latitude;
|
|
|
|
this.longitude = longitude;
|
|
|
|
}
|
2023-02-02 22:30:43 +01:00
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
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;
|
|
|
|
}
|
2023-02-05 20:03:33 +01:00
|
|
|
|
2023-03-30 18:24:57 +02:00
|
|
|
public static ulong GetRegionHashCode(float latitude, float longitude)
|
2023-02-07 23:53:25 +01:00
|
|
|
{
|
2023-03-14 17:00:59 +01:00
|
|
|
float latRegion = latitude - latitude % Region.RegionSize;
|
|
|
|
float lonRegion = longitude - longitude % Region.RegionSize;
|
|
|
|
return GetHashCode(latRegion, lonRegion);
|
2023-02-07 23:53:25 +01:00
|
|
|
}
|
|
|
|
|
2023-03-30 18:24:57 +02:00
|
|
|
public static ulong GetRegionHashCode(Coordinates coordinates)
|
2023-02-06 17:32:55 +01:00
|
|
|
{
|
2023-03-31 21:55:12 +02:00
|
|
|
return GetRegionHashCode(coordinates.latitude, coordinates.longitude);
|
2023-03-14 17:00:59 +01:00
|
|
|
}
|
2023-03-30 18:24:57 +02:00
|
|
|
|
|
|
|
private const float decimalCoordsSave = 10000; //Latitude maxChars = 7
|
|
|
|
private const ulong offset = 10000000; //Longitude maxChars = 8
|
|
|
|
public static ulong GetHashCode(float latitude, float longitude)
|
2023-03-14 17:00:59 +01:00
|
|
|
{
|
2023-03-30 18:24:57 +02:00
|
|
|
ulong latHash = Convert.ToUInt64((latitude + 90) * decimalCoordsSave);
|
|
|
|
ulong lonHash = Convert.ToUInt64((longitude + 180) * decimalCoordsSave);
|
|
|
|
return latHash * offset + lonHash;
|
2023-02-02 22:30:43 +01:00
|
|
|
}
|
2023-02-08 18:08:49 +01:00
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
2023-03-30 18:24:57 +02:00
|
|
|
return
|
2023-04-01 14:19:36 +02:00
|
|
|
$"lat:{latitude.ToString(NumberFormatInfo.InvariantInfo)} lon:{longitude.ToString(CultureInfo.InvariantCulture)}";
|
2023-02-08 18:08:49 +01:00
|
|
|
}
|
2023-02-02 22:30:43 +01:00
|
|
|
}
|