OSMServer/OSMDatastructure/Coordinates.cs

54 lines
1.8 KiB
C#
Raw Normal View History

using System.Globalization;
using System.Text.Json.Serialization;
namespace OSMDatastructure.Graph;
2023-02-02 22:30:43 +01: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
[JsonConstructor]
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;
}
public static ulong GetRegionHashCode(float latitude, float longitude)
2023-02-07 23:53:25 +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
}
public static ulong GetRegionHashCode(Coordinates coordinates)
2023-02-06 17:32:55 +01:00
{
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;
2023-02-02 22:30:43 +01:00
}
2023-02-08 18:08:49 +01:00
public override string ToString()
{
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
}