Skip to content

Commit

Permalink
Added parentat method.
Browse files Browse the repository at this point in the history
  • Loading branch information
xivk committed Nov 18, 2023
1 parent 027348a commit 00c8cd6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/TilesMath/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ public Tile? Parent
}
}

/// <summary>
/// Returns the parent tile at the given zoom.
/// </summary>
/// <param name="zoom"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public Tile ParentAt(int zoom)
{
if (zoom < 0)
throw new ArgumentOutOfRangeException(nameof(zoom),
"Zoom has to be positive");
if (zoom == this.Zoom) return this;
if (zoom > this.Zoom) throw new ArgumentOutOfRangeException(nameof(zoom),
"A parent tile should have a lower zoom");

var x = this.X >> (this.Zoom - zoom);
var y = this.Y >> (this.Zoom - zoom);
return Tile.Create(x, y, zoom);
}

/// <summary>
/// The children.
/// </summary>
Expand Down Expand Up @@ -100,8 +120,8 @@ public IEnumerable<Tile> ChildrenAtZoom(int zoom, Func<Tile, bool>? exclude = nu

foreach (var childOneLevelLess in this.ChildrenAtZoom(zoom - 1))
{
if (exclude != null && !exclude(childOneLevelLess)) continue;

if (exclude != null && !exclude(childOneLevelLess)) continue;

foreach (var child in childOneLevelLess.Children)
{
if (exclude == null || !exclude(child)) yield return child;
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.9</PackageVersion>
<PackageVersion>0.0.10</PackageVersion>
<Title>TilesMath</Title>
<Authors>ANYWAYS BV</Authors>
<Description>A tiny library for tiles math.</Description>
Expand Down
31 changes: 31 additions & 0 deletions test/TilesMath.Tests/TileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace TilesMath.Tests;

public class TileTests
{
[Fact]
public void Tile_ParentAt_OneZoomUp_ShouldEqualParent()
{
var tile = Tile.Create(1025, 4511, 14);

var parent = tile.Parent ?? throw new Exception("No parent");
Assert.Equal(parent, tile.ParentAt(tile.Zoom - 1));
}

[Fact]
public void Tile_ParentAt_TwoZoomsUp_ShouldEqualParentOfParent()
{
var tile = Tile.Create(1025, 4511, 14);

var parent = tile.Parent?.Parent ?? throw new Exception("No parent");
Assert.Equal(parent, tile.ParentAt(tile.Zoom - 2));
}

[Fact]
public void Tile_ParentAt_5ZoomsUp_ShouldEqual5thParentUp()
{
var tile = Tile.Create(1025, 4511, 14);

var parent = tile.Parent?.Parent?.Parent?.Parent?.Parent ?? throw new Exception("No parent");
Assert.Equal(parent, tile.ParentAt(tile.Zoom - 5));
}
}

0 comments on commit 00c8cd6

Please sign in to comment.