Solve for the tile indices a region overlaps, without building the tile index. The answer costs a few arithmetic operations regardless of how many tiles the scheme has, because it is computed from the tile grid rather than found by testing every tile.

tiles_from_extent(x, extent, tol = 1e-08)

Arguments

x

a `"grout_tiles"` object from [grout()].

extent

numeric vector `c(xmin, xmax, ymin, ymax)`, in the coordinate system of the scheme.

tol

snapping tolerance, in tile widths. A region edge within `tol` of a tile boundary is treated as being on it, which keeps the answer stable when an extent has been arrived at by arithmetic rather than given exactly.

Value

integer vector of 1-based tile indices, in increasing (row-major) order, or `integer(0)` when the region does not overlap the scheme.

Details

The result is intended to be handed straight to [tile_index()]:

“` tile_index(x, tile = tiles_from_extent(x, region)) “`

Tiles are treated as half open, so a region that only touches a tile boundary does not pick up the tile on the far side of it: a tile owns its left and top edges and not its right and bottom ones. A region with zero width or height (a point) still lands on one tile. Regions are clamped to the scheme, so a region larger than the grid returns every tile, and a region entirely outside it returns `integer(0)`.

Note that the tile grid can extend past the grid it tiles, when there is a dangle, so tiles can be returned for a region that lies outside the extent passed to [grout()] but inside the tiled area.

See also

[tile_index()]

Examples

g <- grout(c(87, 61), blocksize = c(12L, 16L))

tiles_from_extent(g, c(20, 40, 20, 40))
#> [1] 10 11 12 18 19 20

## straight into tile_index()
tile_index(g, tile = tiles_from_extent(g, c(20, 40, 20, 40)))
#> # A tibble: 6 × 11
#>    tile offset_x offset_y tile_col tile_row  ncol  nrow  xmin  xmax  ymin  ymax
#>   <int>    <int>    <dbl>    <int>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1    10       12       16        2        2    12    16    12    24    29    45
#> 2    11       24       16        3        2    12    16    24    36    29    45
#> 3    12       36       16        4        2    12    16    36    48    29    45
#> 4    18       12       32        2        3    12    16    12    24    13    29
#> 5    19       24       32        3        3    12    16    24    36    13    29
#> 6    20       36       32        4        3    12    16    36    48    13    29

## a whole-grid region is every tile; a region outside is none
length(tiles_from_extent(g, c(0, 87, 0, 61)))
#> [1] 32
tiles_from_extent(g, c(1000, 2000, 1000, 2000))
#> integer(0)