Skip to content

Commit

Permalink
Added a try at location.
Browse files Browse the repository at this point in the history
  • Loading branch information
xivk committed Jul 27, 2023
1 parent 064fe2b commit 5c6deff
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/TilesMath/GlobalTileId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/TilesMath/LocalTileId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/TilesMath/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ private Tile(int x, int y, byte zoom)
/// <summary>
/// The tile boundaries.
/// </summary>
public TileBounds Boundaries => TileGeo.BoundariesFor(this);
public TileBounds Boundaries => TileGeo.BoundariesFor(this);

/// <summary>
/// The neighbours.
/// </summary>
Expand Down
12 changes: 6 additions & 6 deletions src/TilesMath/TileBounds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@ internal TileBounds(double left, double top, double right, double bottom)
this.Right = right;
this.Bottom = bottom;
}

/// <summary>
/// The longitude at the left of the tile
/// </summary>
public double Left { get; }

/// <summary>
/// The longitude at the right of the tile.
/// </summary>
public double Right { get; }

/// <summary>
/// The latitude at the top of the tile.
/// </summary>
public double Top { get; }

/// <summary>
/// The latitude at the bottom of the tile.
/// </summary>
public double Bottom { get; }

/// <summary>
/// The center latitude of the tile
/// </summary>
public double CenterLatitude => (this.Top + this.Bottom) / 2.0;

/// <summary>
/// The center longitude of the tile.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/TilesMath/TileChildren.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public struct TileChildren : IEnumerable<Tile>
private readonly int _x;
private readonly int _y;
private readonly int _z;

internal TileChildren(Tile tile)
{
_x = tile.X * 2;
Expand Down
24 changes: 20 additions & 4 deletions src/TilesMath/TileGeo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)));

Expand Down
8 changes: 4 additions & 4 deletions src/TilesMath/TileNeighbours.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class TileNeighbours : IEnumerable<Tile>
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;
Expand All @@ -43,12 +43,12 @@ internal TileNeighbours(Tile tile)
/// Gets the top neighbour.
/// </summary>
public Tile? Top => _top == null ? null : Tile.Create(_tile.X, _top.Value, _tile.Zoom);

/// <summary>
/// Gets the top left neighbour.
/// </summary>
public Tile? TopLeft => _top == null ? null : Tile.Create(_left, _top.Value, _tile.Zoom);

/// <summary>
/// Gets the top right neighbour.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/TilesMath/TilesMath.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageVersion>0.0.2</PackageVersion>
<PackageVersion>0.0.3</PackageVersion>
<Title>TilesMath</Title>
<Authors>ANYWAYS BV</Authors>
<Description>A tiny library for tiles math.</Description>
Expand Down

0 comments on commit 5c6deff

Please sign in to comment.