diff --git a/src/TilesMath/GlobalTileId.cs b/src/TilesMath/GlobalTileId.cs index d0658b0..3ae196d 100644 --- a/src/TilesMath/GlobalTileId.cs +++ b/src/TilesMath/GlobalTileId.cs @@ -51,7 +51,7 @@ private static long ForZoom(int zoom) var tileId = ForZoom(zoom - 1) + xMax; return tileId; } - + internal static (int x, int y, int zoom) From(long globalTileId) { // find out the zoom level first. diff --git a/src/TilesMath/LocalTileId.cs b/src/TilesMath/LocalTileId.cs index 976451c..822be51 100644 --- a/src/TilesMath/LocalTileId.cs +++ b/src/TilesMath/LocalTileId.cs @@ -17,7 +17,7 @@ internal static int Max(int zoom) return xMax * xMax; } - + internal static int ForTile(Tile tile) { var xMax = 1 << (int)tile.Zoom; diff --git a/src/TilesMath/Tile.cs b/src/TilesMath/Tile.cs index 330a762..179d64d 100644 --- a/src/TilesMath/Tile.cs +++ b/src/TilesMath/Tile.cs @@ -47,8 +47,8 @@ private Tile(int x, int y, byte zoom) /// /// The tile boundaries. /// - public TileBounds Boundaries => TileGeo.BoundariesFor(this); - + public TileBounds Boundaries => TileGeo.BoundariesFor(this); + /// /// The neighbours. /// diff --git a/src/TilesMath/TileBounds.cs b/src/TilesMath/TileBounds.cs index 29083c9..922f0e3 100644 --- a/src/TilesMath/TileBounds.cs +++ b/src/TilesMath/TileBounds.cs @@ -12,32 +12,32 @@ internal TileBounds(double left, double top, double right, double bottom) this.Right = right; this.Bottom = bottom; } - + /// /// The longitude at the left of the tile /// public double Left { get; } - + /// /// The longitude at the right of the tile. /// public double Right { get; } - + /// /// The latitude at the top of the tile. /// public double Top { get; } - + /// /// The latitude at the bottom of the tile. /// public double Bottom { get; } - + /// /// The center latitude of the tile /// public double CenterLatitude => (this.Top + this.Bottom) / 2.0; - + /// /// The center longitude of the tile. /// diff --git a/src/TilesMath/TileChildren.cs b/src/TilesMath/TileChildren.cs index ceab69b..d66c8d1 100644 --- a/src/TilesMath/TileChildren.cs +++ b/src/TilesMath/TileChildren.cs @@ -7,7 +7,7 @@ public struct TileChildren : IEnumerable private readonly int _x; private readonly int _y; private readonly int _z; - + internal TileChildren(Tile tile) { _x = tile.X * 2; diff --git a/src/TilesMath/TileGeo.cs b/src/TilesMath/TileGeo.cs index 579387e..04ba7b2 100644 --- a/src/TilesMath/TileGeo.cs +++ b/src/TilesMath/TileGeo.cs @@ -7,17 +7,33 @@ internal static class TileGeo private const double MinLon = -180; private const double MaxLon = 180; + public static (int x, int y)? TryForLocation(double longitude, double latitude, int zoom) + { + if (latitude is > MaxLat or < MinLat) return null; + if (longitude is > MaxLon or < MinLon) return null; + + // ReSharper disable once CompareOfFloatsByEqualityOperator + if (longitude == 180) longitude -= 0.000001; + + var x = (int)((longitude + 180.0) / 360.0 * (1 << zoom)); + var latRad = latitude * Math.PI / 180.0; + var y = (int)((1.0 - Math.Log(Math.Tan(latRad) + + 1.0 / Math.Cos(latRad)) / Math.PI) / 2.0 * (1 << zoom)); + + return (x, y); + } + public static (int x, int y) ForLocation(double longitude, double latitude, int zoom) { if (latitude is > MaxLat or < MinLat) throw new ArgumentOutOfRangeException(nameof(latitude)); if (longitude is > MaxLon or < MinLon) throw new ArgumentOutOfRangeException(nameof(longitude)); - + // ReSharper disable once CompareOfFloatsByEqualityOperator if (longitude == 180) longitude -= 0.000001; - var x = (int) ((longitude + 180.0) / 360.0 * (1 << zoom)); + var x = (int)((longitude + 180.0) / 360.0 * (1 << zoom)); var latRad = latitude * Math.PI / 180.0; - var y = (int) ((1.0 - Math.Log(Math.Tan(latRad) + + var y = (int)((1.0 - Math.Log(Math.Tan(latRad) + 1.0 / Math.Cos(latRad)) / Math.PI) / 2.0 * (1 << zoom)); return (x, y); @@ -27,7 +43,7 @@ public static TileBounds BoundariesFor(Tile tile) { var size = (double)(1 << tile.Zoom); var n = Math.PI - ((2.0 * Math.PI * tile.Y) / size); - + var left = ((tile.X / size * 360.0) - 180.0); var top = (180.0 / Math.PI * Math.Atan(Math.Sinh(n))); diff --git a/src/TilesMath/TileNeighbours.cs b/src/TilesMath/TileNeighbours.cs index 195bb8b..e5c202d 100644 --- a/src/TilesMath/TileNeighbours.cs +++ b/src/TilesMath/TileNeighbours.cs @@ -12,11 +12,11 @@ public class TileNeighbours : IEnumerable private readonly int _right; private readonly int? _top; private readonly int? _bottom; - + internal TileNeighbours(Tile tile) { _tile = tile; - + var last = (1 << _tile.Zoom) - 1; _left = tile.X == 0 ? last : tile.X - 1; _right = tile.X == last ? 0 : tile.X + 1; @@ -43,12 +43,12 @@ internal TileNeighbours(Tile tile) /// Gets the top neighbour. /// public Tile? Top => _top == null ? null : Tile.Create(_tile.X, _top.Value, _tile.Zoom); - + /// /// Gets the top left neighbour. /// public Tile? TopLeft => _top == null ? null : Tile.Create(_left, _top.Value, _tile.Zoom); - + /// /// Gets the top right neighbour. /// diff --git a/src/TilesMath/TilesMath.csproj b/src/TilesMath/TilesMath.csproj index 089933c..be407f7 100644 --- a/src/TilesMath/TilesMath.csproj +++ b/src/TilesMath/TilesMath.csproj @@ -4,7 +4,7 @@ net6.0 enable enable - 0.0.2 + 0.0.3 TilesMath ANYWAYS BV A tiny library for tiles math.