Skip to content
Draft
Changes from 2 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
23 changes: 23 additions & 0 deletions src/tiles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ function Tile(point, zoom::Integer, crs::WGS84)
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)
end

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

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...)
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 Expand Up @@ -216,3 +237,5 @@ function GeoInterface.extent(tilegrid::TileGrid, crs::WebMercator)

return Extent(X=(left, right), Y=(bottom, top))
end