Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add z_index function #24

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/tiles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@
return Tile(xtile, ytile, zoom)
end

"""
z_index(extent::Extent, res::NamedTuple, crs::WebMercator) => Int

Calculate a z value from `extent` and pixel resolution `res` for `crs`.
The rounded mean calculated z-index for X and Y resolutions is returned.

`res` should be `(X=xres, Y=yres)` to match the extent.

We assume tiles are the standard 256*256 pixels. Note that this is not an
enforced standard, and that retina tiles are 512*512.
Comment on lines +121 to +122
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it's best to directly add the tile size as an argument?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I considered that too. I guess a keyword with default is best.

"""
function z_index(extent::Extent, res::NamedTuple, crs::WebMercator)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also accept tuples (x, y) as Makie does? In that case we can also make z_index(..., Tuple) = z_index(..., (; X = ..., Y = ...)) or so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should probably also be a catchall method to error when crs is not WebMercator (for now...)

ntiles = map(r -> r / 256, res)
tile_size_X = (extent.X[2] - extent.X[1]) / ntiles.X
tile_size_Y = (extent.Y[2] - extent.Y[1]) / ntiles.Y
tile_size = (tile_size_X + tile_size_Y) / 2
z = log2(CE / tile_size)
return round(Int, z)

Check warning on line 130 in src/tiles.jl

View check run for this annotation

Codecov / codecov/patch

src/tiles.jl#L124-L130

Added lines #L124 - L130 were not covered by tests
end

struct TileGrid
grid::CartesianIndices{2, Tuple{UnitRange{Int}, UnitRange{Int}}}
z::Int
Expand Down Expand Up @@ -143,6 +163,7 @@

Base.length(tilegrid::TileGrid) = length(tilegrid.grid)
Base.size(tilegrid::TileGrid, dims...) = size(tilegrid.grid, dims...)
Base.axes(tilegrid::TileGrid, dims...) = axes(tilegrid.grid, dims...)

Check warning on line 166 in src/tiles.jl

View check run for this annotation

Codecov / codecov/patch

src/tiles.jl#L166

Added line #L166 was not covered by tests
Base.getindex(tilegrid::TileGrid, i) = Tile(tilegrid.grid[i], tilegrid.z)
Base.firstindex(tilegrid::TileGrid) = firstindex(tilegrid.grid)
Base.lastindex(tilegrid::TileGrid) = lastindex(tilegrid.grid)
Expand Down
Loading