OSMServer/OSMDatastructure/Coordinates.cs
2023-02-03 23:34:51 +01:00

50 lines
1.7 KiB
C#

using System.Globalization;
namespace OSMDatastructure
{
public class Coordinates
{
public float lat { get; }
public float lon { get; }
public Coordinates(float lat, float lon)
{
this.lat = lat;
this.lon = lon;
}
private const float decimalCoordsSave = 10000;
private const ulong offset = 10000000;
//Latitude maxChars = 7
//Longitude maxChars = 8
public ulong GetHash()
{
ulong latHash = Convert.ToUInt64((this.lat + 90) * decimalCoordsSave);
ulong lonHash = Convert.ToUInt64((this.lon + 180) * decimalCoordsSave);
return latHash * offset + lonHash;
}
public ulong GetRegionHash()
{
float latRegion = this.lat - this.lat % Region.regionSize;
float lonRegion = this.lon - this.lon % Region.regionSize;
ulong latHash = Convert.ToUInt64((latRegion + 90) * decimalCoordsSave);
ulong lonHash = Convert.ToUInt64((lonRegion + 180) * decimalCoordsSave);
return latHash * offset + lonHash;
}
public static Coordinates GetFromHash(ulong hash)
{
ulong latHash = (hash / offset) - 90;
ulong lonHash = (hash % offset) - 180;
float lat = Convert.ToSingle(latHash) / decimalCoordsSave;
float lon = Convert.ToSingle(lonHash) / decimalCoordsSave;
return new Coordinates(lat, lon);
}
public override string ToString()
{
return string.Format("{0} {1}", this.lat.ToString(CultureInfo.InvariantCulture), this.lon.ToString(CultureInfo.InvariantCulture));
}
}
}