OSMServer/OSMDatastructure/Coordinates.cs

52 lines
1.7 KiB
C#
Raw Normal View History

2023-02-06 17:32:55 +01:00
namespace OSMDatastructure;
2023-02-02 22:30:43 +01:00
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-06 17:32:55 +01:00
private const float decimalCoordsSave = 10000;
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;
}
2023-02-07 23:53:25 +01:00
public override int GetHashCode()
{
return HashCode.Combine(latitude, longitude);
}
2023-02-06 17:32:55 +01:00
public ulong GetRegionHash()
{
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;
2023-02-02 22:30:43 +01:00
}
2023-02-08 18:08:49 +01:00
public override string ToString()
{
return string.Format("COORDINATES Lat: {0} Lon: {1}", this.latitude, this.longitude);
}
2023-02-02 22:30:43 +01:00
}