Skip to content

Commit

Permalink
Added countatzoom for tileset.
Browse files Browse the repository at this point in the history
  • Loading branch information
xivk committed Oct 10, 2023
1 parent 7a6ac46 commit 779f644
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/TilesMath/Collections/TileTreeSetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,34 @@ public static IEnumerable<Tile> ToEnumerableAtZoom(this TileTreeSet set, int zoo
}
}
}

/// <summary>
/// Calculates the number of tiles at the given zoom level.
/// </summary>
/// <param name="set">The set.</param>
/// <param name="zoom">The zoom level.</param>
/// <returns>The number of tiles the set represents at the given zoom.</returns>
/// <exception cref="Exception">When tiles are found at a higher zoom level in the set it is not possible to enumerate.</exception>
public static long CountAtZoom(this TileTreeSet set, int zoom)
{
var count = 0L;
foreach (var tile in set)
{
if (tile.Zoom == zoom)
{
count++;
}
else if (tile.Zoom < zoom)
{
count += (long)System.Math.Pow(4, zoom - tile.Zoom);
}
else
{
throw new Exception(
$"Cannot enumerate this set at {zoom}, found at tile at a higher zoom level: {tile.Zoom}");
}
}

return count;
}
}
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.7</PackageVersion>
<PackageVersion>0.0.8</PackageVersion>
<Title>TilesMath</Title>
<Authors>ANYWAYS BV</Authors>
<Description>A tiny library for tiles math.</Description>
Expand Down
30 changes: 30 additions & 0 deletions test/TilesMath.Tests/Collections/TileTreeSetExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using TilesMath.Collections;

namespace TilesMath.Tests.Collections;

public class TileTreeSetExtensionsTests
{
[Fact]
public void TileTreeSet_SetWithTileZero_CountAtZoom2_ShouldBe16()
{
var set = new TileTreeSet { Tile.Create(0, 0, 0) };

Assert.Equal(16, set.CountAtZoom(2));
}

[Fact]
public void TileTreeSet_SetWithTileZero_CountAtZoom14_ShouldBe268435456()
{
var set = new TileTreeSet { Tile.Create(0, 0, 0) };

Assert.Equal(268435456, set.CountAtZoom(14));
}

[Fact]
public void TileTreeSet_SetWithOneZoom14Tile_CountAtZoom14_ShouldBe1()
{
var set = new TileTreeSet { Tile.Create(0, 0, 14) };

Assert.Equal(1, set.CountAtZoom(14));
}
}

0 comments on commit 779f644

Please sign in to comment.