Skip to contents

'in_layer' checks if geometries in 'x' are, even partially, inside any geometry in 'y'.

Usage

in_layer(
  x,
  y,
  treatment = "intersect",
  name = "in_layer",
  divide = TRUE,
  s2 = FALSE
)

Arguments

x

object that is admissible for st_intersects.

y

object that is admissible for st_intersects.

treatment

character indicating which treatment should be done. See details.

name

character, name that will be used for results. Default to "in_layer".

divide

logical, should lwgeom::st_subdivide be used on y to have fast results.

s2

logical, should sf_use_s2 be used or not.

Value

If treatment = "intersect", logical with same length as nrow(x) indicating whether geometry in x are (event partially) in y. In any other case, data.frame whose column corresponds to asked treaments and rows to geometry in x.

Details

When treatment includes "interesect", a logical is produced that indicates if geometries in x intersects any geometries in y. When treatment includes "intersection", a numeric is produced that indicates for each geometry in x the area that is intersected by all geometries in y. When treatment includes "proportion", a numeric is produced that indicates for each geometry in x the proportion of area that is intersected by all geometries in y.

The use of divide should give fastet results in real case where y may have a lot of details in its polygon defintion. As lwgeom::subdivide is performed within the function and takes some time, it is a better strategy to use treatment equals to "all" when both intersect and intersection are asked, because it is done only one time for both treatments.

To have more consistent results st_union is performed on y each time treatment includes either "intersection" or "proportion".

Author

Frédéric Grelot

Examples

fun_p1 = function(p, n, x, y, s = 0){
  fun = function(p, x, y, s) {p * s + c(x, y)}
  if (s == 0) s = 1 else s = runif(n) * s
  result = mapply(fun, runif(n) * x, runif(n) * y, s, MoreArgs = list(p = p), SIMPLIFY = FALSE)
  sf::st_sfc(result)
}
fun_p2 = function(n) {
  theta = seq(0, 1, length.out = n) * 2 * pi
  theta[n] =  theta[1]
  rho = .8 + runif(n) * .2
  rho[n] = rho[1]
  result = mapply(
    function(theta, rho) {c(sin(theta), cos(theta)) * rho},
    theta,
    rho,
    SIMPLIFY = FALSE
  )
  sf::st_polygon(list(do.call(rbind, result)))
}

set.seed(0)
p1 = fun_p2(100)
p2 = fun_p2(100)
x = fun_p1(p1 * .2, 100, 10, 10)
y = fun_p1(p1, 25, 10, 10, .5)

in_layer(x, y)
#> Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.0; sf_use_s2() is FALSE
#>   [1] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [13] FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
#>  [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
#>  [37] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE
#>  [49] FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE
#>  [73] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE
#>  [85]  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE
#>  [97] FALSE FALSE FALSE FALSE
in_layer(y, x)
#>  [1] FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE
#> [13]  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
#> [25]  TRUE
in_layer(x, y, "intersect")
#>   [1] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [13] FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
#>  [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
#>  [37] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE
#>  [49] FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE
#>  [73] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE
#>  [85]  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE
#>  [97] FALSE FALSE FALSE FALSE
in_layer(x, y, "intersection")
#> Warning: Some elements of y are self-intersecting, results may not be consistent.
#>     in_layer_area
#> 1    0.000000e+00
#> 2    1.024397e-01
#> 3    0.000000e+00
#> 4    0.000000e+00
#> 5    0.000000e+00
#> 6    0.000000e+00
#> 7    0.000000e+00
#> 8    0.000000e+00
#> 9    0.000000e+00
#> 10   0.000000e+00
#> 11   0.000000e+00
#> 12   0.000000e+00
#> 13   0.000000e+00
#> 14   0.000000e+00
#> 15   1.025256e-01
#> 16   0.000000e+00
#> 17   0.000000e+00
#> 18   0.000000e+00
#> 19   9.995635e-02
#> 20   0.000000e+00
#> 21   0.000000e+00
#> 22   8.476606e-02
#> 23   0.000000e+00
#> 24   0.000000e+00
#> 25   0.000000e+00
#> 26   0.000000e+00
#> 27   0.000000e+00
#> 28   0.000000e+00
#> 29   0.000000e+00
#> 30   0.000000e+00
#> 31   0.000000e+00
#> 32   6.117553e-02
#> 33   0.000000e+00
#> 34   0.000000e+00
#> 35   0.000000e+00
#> 36   0.000000e+00
#> 37   0.000000e+00
#> 38   0.000000e+00
#> 39   0.000000e+00
#> 40   0.000000e+00
#> 41   1.051540e-03
#> 42   0.000000e+00
#> 43   0.000000e+00
#> 44   0.000000e+00
#> 45   9.496573e-04
#> 46   0.000000e+00
#> 47   0.000000e+00
#> 48   7.368643e-02
#> 49   0.000000e+00
#> 50   0.000000e+00
#> 51   1.002573e-01
#> 52   1.518352e-02
#> 53   1.103301e-02
#> 54   0.000000e+00
#> 55   0.000000e+00
#> 56   0.000000e+00
#> 57   0.000000e+00
#> 58   0.000000e+00
#> 59   0.000000e+00
#> 60   0.000000e+00
#> 61   0.000000e+00
#> 62   0.000000e+00
#> 63   0.000000e+00
#> 64   0.000000e+00
#> 65   0.000000e+00
#> 66   0.000000e+00
#> 67   0.000000e+00
#> 68   5.499358e-02
#> 69   0.000000e+00
#> 70   0.000000e+00
#> 71   0.000000e+00
#> 72   2.189412e-03
#> 73   0.000000e+00
#> 74   2.489557e-02
#> 75   0.000000e+00
#> 76   0.000000e+00
#> 77   0.000000e+00
#> 78   0.000000e+00
#> 79   0.000000e+00
#> 80   4.924905e-03
#> 81   3.357822e-07
#> 82   0.000000e+00
#> 83   0.000000e+00
#> 84   0.000000e+00
#> 85   1.025258e-01
#> 86   0.000000e+00
#> 87   0.000000e+00
#> 88   0.000000e+00
#> 89   0.000000e+00
#> 90   7.267599e-03
#> 91   0.000000e+00
#> 92   0.000000e+00
#> 93   0.000000e+00
#> 94   0.000000e+00
#> 95   0.000000e+00
#> 96   4.655793e-02
#> 97   0.000000e+00
#> 98   0.000000e+00
#> 99   0.000000e+00
#> 100  0.000000e+00
in_layer(x, y, "proportion")
#> Warning: Some elements of y are self-intersecting, results may not be consistent.
#>     in_layer_prop
#> 1    0.000000e+00
#> 2    9.991605e-01
#> 3    0.000000e+00
#> 4    0.000000e+00
#> 5    0.000000e+00
#> 6    0.000000e+00
#> 7    0.000000e+00
#> 8    0.000000e+00
#> 9    0.000000e+00
#> 10   0.000000e+00
#> 11   0.000000e+00
#> 12   0.000000e+00
#> 13   0.000000e+00
#> 14   0.000000e+00
#> 15   9.999976e-01
#> 16   0.000000e+00
#> 17   0.000000e+00
#> 18   0.000000e+00
#> 19   9.749385e-01
#> 20   0.000000e+00
#> 21   0.000000e+00
#> 22   8.267778e-01
#> 23   0.000000e+00
#> 24   0.000000e+00
#> 25   0.000000e+00
#> 26   0.000000e+00
#> 27   0.000000e+00
#> 28   0.000000e+00
#> 29   0.000000e+00
#> 30   0.000000e+00
#> 31   0.000000e+00
#> 32   5.966842e-01
#> 33   0.000000e+00
#> 34   0.000000e+00
#> 35   0.000000e+00
#> 36   0.000000e+00
#> 37   0.000000e+00
#> 38   0.000000e+00
#> 39   0.000000e+00
#> 40   0.000000e+00
#> 41   1.025634e-02
#> 42   0.000000e+00
#> 43   0.000000e+00
#> 44   0.000000e+00
#> 45   9.262617e-03
#> 46   0.000000e+00
#> 47   0.000000e+00
#> 48   7.187110e-01
#> 49   0.000000e+00
#> 50   0.000000e+00
#> 51   9.778734e-01
#> 52   1.480946e-01
#> 53   1.076120e-01
#> 54   0.000000e+00
#> 55   0.000000e+00
#> 56   0.000000e+00
#> 57   0.000000e+00
#> 58   0.000000e+00
#> 59   0.000000e+00
#> 60   0.000000e+00
#> 61   0.000000e+00
#> 62   0.000000e+00
#> 63   0.000000e+00
#> 64   0.000000e+00
#> 65   0.000000e+00
#> 66   0.000000e+00
#> 67   0.000000e+00
#> 68   5.363877e-01
#> 69   0.000000e+00
#> 70   0.000000e+00
#> 71   0.000000e+00
#> 72   2.135474e-02
#> 73   0.000000e+00
#> 74   2.428225e-01
#> 75   0.000000e+00
#> 76   0.000000e+00
#> 77   0.000000e+00
#> 78   0.000000e+00
#> 79   0.000000e+00
#> 80   4.803576e-02
#> 81   3.275099e-06
#> 82   0.000000e+00
#> 83   0.000000e+00
#> 84   0.000000e+00
#> 85   1.000000e+00
#> 86   0.000000e+00
#> 87   0.000000e+00
#> 88   0.000000e+00
#> 89   0.000000e+00
#> 90   7.088556e-02
#> 91   0.000000e+00
#> 92   0.000000e+00
#> 93   0.000000e+00
#> 94   0.000000e+00
#> 95   0.000000e+00
#> 96   4.541094e-01
#> 97   0.000000e+00
#> 98   0.000000e+00
#> 99   0.000000e+00
#> 100  0.000000e+00
in_layer(x, y, "all", "y")
#> Warning: Some elements of y are self-intersecting, results may not be consistent.
#>         y       y_area       y_prop
#> 1   FALSE 0.000000e+00 0.000000e+00
#> 2    TRUE 1.024397e-01 9.991605e-01
#> 3   FALSE 0.000000e+00 0.000000e+00
#> 4   FALSE 0.000000e+00 0.000000e+00
#> 5   FALSE 0.000000e+00 0.000000e+00
#> 6   FALSE 0.000000e+00 0.000000e+00
#> 7   FALSE 0.000000e+00 0.000000e+00
#> 8   FALSE 0.000000e+00 0.000000e+00
#> 9   FALSE 0.000000e+00 0.000000e+00
#> 10  FALSE 0.000000e+00 0.000000e+00
#> 11  FALSE 0.000000e+00 0.000000e+00
#> 12  FALSE 0.000000e+00 0.000000e+00
#> 13  FALSE 0.000000e+00 0.000000e+00
#> 14  FALSE 0.000000e+00 0.000000e+00
#> 15   TRUE 1.025256e-01 9.999976e-01
#> 16  FALSE 0.000000e+00 0.000000e+00
#> 17  FALSE 0.000000e+00 0.000000e+00
#> 18  FALSE 0.000000e+00 0.000000e+00
#> 19   TRUE 9.995635e-02 9.749385e-01
#> 20  FALSE 0.000000e+00 0.000000e+00
#> 21  FALSE 0.000000e+00 0.000000e+00
#> 22   TRUE 8.476606e-02 8.267778e-01
#> 23  FALSE 0.000000e+00 0.000000e+00
#> 24  FALSE 0.000000e+00 0.000000e+00
#> 25  FALSE 0.000000e+00 0.000000e+00
#> 26  FALSE 0.000000e+00 0.000000e+00
#> 27  FALSE 0.000000e+00 0.000000e+00
#> 28  FALSE 0.000000e+00 0.000000e+00
#> 29  FALSE 0.000000e+00 0.000000e+00
#> 30  FALSE 0.000000e+00 0.000000e+00
#> 31  FALSE 0.000000e+00 0.000000e+00
#> 32   TRUE 6.117553e-02 5.966842e-01
#> 33  FALSE 0.000000e+00 0.000000e+00
#> 34  FALSE 0.000000e+00 0.000000e+00
#> 35  FALSE 0.000000e+00 0.000000e+00
#> 36  FALSE 0.000000e+00 0.000000e+00
#> 37  FALSE 0.000000e+00 0.000000e+00
#> 38  FALSE 0.000000e+00 0.000000e+00
#> 39  FALSE 0.000000e+00 0.000000e+00
#> 40  FALSE 0.000000e+00 0.000000e+00
#> 41   TRUE 1.051540e-03 1.025634e-02
#> 42  FALSE 0.000000e+00 0.000000e+00
#> 43  FALSE 0.000000e+00 0.000000e+00
#> 44  FALSE 0.000000e+00 0.000000e+00
#> 45   TRUE 9.496573e-04 9.262617e-03
#> 46  FALSE 0.000000e+00 0.000000e+00
#> 47  FALSE 0.000000e+00 0.000000e+00
#> 48   TRUE 7.368643e-02 7.187110e-01
#> 49  FALSE 0.000000e+00 0.000000e+00
#> 50  FALSE 0.000000e+00 0.000000e+00
#> 51   TRUE 1.002573e-01 9.778734e-01
#> 52   TRUE 1.518352e-02 1.480946e-01
#> 53   TRUE 1.103301e-02 1.076120e-01
#> 54  FALSE 0.000000e+00 0.000000e+00
#> 55  FALSE 0.000000e+00 0.000000e+00
#> 56  FALSE 0.000000e+00 0.000000e+00
#> 57  FALSE 0.000000e+00 0.000000e+00
#> 58  FALSE 0.000000e+00 0.000000e+00
#> 59  FALSE 0.000000e+00 0.000000e+00
#> 60  FALSE 0.000000e+00 0.000000e+00
#> 61  FALSE 0.000000e+00 0.000000e+00
#> 62  FALSE 0.000000e+00 0.000000e+00
#> 63  FALSE 0.000000e+00 0.000000e+00
#> 64  FALSE 0.000000e+00 0.000000e+00
#> 65  FALSE 0.000000e+00 0.000000e+00
#> 66  FALSE 0.000000e+00 0.000000e+00
#> 67  FALSE 0.000000e+00 0.000000e+00
#> 68   TRUE 5.499358e-02 5.363877e-01
#> 69  FALSE 0.000000e+00 0.000000e+00
#> 70  FALSE 0.000000e+00 0.000000e+00
#> 71  FALSE 0.000000e+00 0.000000e+00
#> 72   TRUE 2.189412e-03 2.135474e-02
#> 73  FALSE 0.000000e+00 0.000000e+00
#> 74   TRUE 2.489557e-02 2.428225e-01
#> 75  FALSE 0.000000e+00 0.000000e+00
#> 76  FALSE 0.000000e+00 0.000000e+00
#> 77  FALSE 0.000000e+00 0.000000e+00
#> 78  FALSE 0.000000e+00 0.000000e+00
#> 79  FALSE 0.000000e+00 0.000000e+00
#> 80   TRUE 4.924905e-03 4.803576e-02
#> 81   TRUE 3.357822e-07 3.275099e-06
#> 82  FALSE 0.000000e+00 0.000000e+00
#> 83  FALSE 0.000000e+00 0.000000e+00
#> 84  FALSE 0.000000e+00 0.000000e+00
#> 85   TRUE 1.025258e-01 1.000000e+00
#> 86  FALSE 0.000000e+00 0.000000e+00
#> 87  FALSE 0.000000e+00 0.000000e+00
#> 88  FALSE 0.000000e+00 0.000000e+00
#> 89  FALSE 0.000000e+00 0.000000e+00
#> 90   TRUE 7.267599e-03 7.088556e-02
#> 91  FALSE 0.000000e+00 0.000000e+00
#> 92  FALSE 0.000000e+00 0.000000e+00
#> 93  FALSE 0.000000e+00 0.000000e+00
#> 94  FALSE 0.000000e+00 0.000000e+00
#> 95  FALSE 0.000000e+00 0.000000e+00
#> 96   TRUE 4.655793e-02 4.541094e-01
#> 97  FALSE 0.000000e+00 0.000000e+00
#> 98  FALSE 0.000000e+00 0.000000e+00
#> 99  FALSE 0.000000e+00 0.000000e+00
#> 100 FALSE 0.000000e+00 0.000000e+00
in_layer(x, y, "all", "y", divide = FALSE)
#>         y       y_area       y_prop
#> 1   FALSE 0.000000e+00 0.000000e+00
#> 2    TRUE 1.024397e-01 9.991605e-01
#> 3   FALSE 0.000000e+00 0.000000e+00
#> 4   FALSE 0.000000e+00 0.000000e+00
#> 5   FALSE 0.000000e+00 0.000000e+00
#> 6   FALSE 0.000000e+00 0.000000e+00
#> 7   FALSE 0.000000e+00 0.000000e+00
#> 8   FALSE 0.000000e+00 0.000000e+00
#> 9   FALSE 0.000000e+00 0.000000e+00
#> 10  FALSE 0.000000e+00 0.000000e+00
#> 11  FALSE 0.000000e+00 0.000000e+00
#> 12  FALSE 0.000000e+00 0.000000e+00
#> 13  FALSE 0.000000e+00 0.000000e+00
#> 14  FALSE 0.000000e+00 0.000000e+00
#> 15   TRUE 1.025256e-01 9.999976e-01
#> 16  FALSE 0.000000e+00 0.000000e+00
#> 17  FALSE 0.000000e+00 0.000000e+00
#> 18  FALSE 0.000000e+00 0.000000e+00
#> 19   TRUE 9.995635e-02 9.749385e-01
#> 20  FALSE 0.000000e+00 0.000000e+00
#> 21  FALSE 0.000000e+00 0.000000e+00
#> 22   TRUE 8.476606e-02 8.267778e-01
#> 23  FALSE 0.000000e+00 0.000000e+00
#> 24  FALSE 0.000000e+00 0.000000e+00
#> 25  FALSE 0.000000e+00 0.000000e+00
#> 26  FALSE 0.000000e+00 0.000000e+00
#> 27  FALSE 0.000000e+00 0.000000e+00
#> 28  FALSE 0.000000e+00 0.000000e+00
#> 29  FALSE 0.000000e+00 0.000000e+00
#> 30  FALSE 0.000000e+00 0.000000e+00
#> 31  FALSE 0.000000e+00 0.000000e+00
#> 32   TRUE 6.117553e-02 5.966842e-01
#> 33  FALSE 0.000000e+00 0.000000e+00
#> 34  FALSE 0.000000e+00 0.000000e+00
#> 35  FALSE 0.000000e+00 0.000000e+00
#> 36  FALSE 0.000000e+00 0.000000e+00
#> 37  FALSE 0.000000e+00 0.000000e+00
#> 38  FALSE 0.000000e+00 0.000000e+00
#> 39  FALSE 0.000000e+00 0.000000e+00
#> 40  FALSE 0.000000e+00 0.000000e+00
#> 41   TRUE 1.051540e-03 1.025634e-02
#> 42  FALSE 0.000000e+00 0.000000e+00
#> 43  FALSE 0.000000e+00 0.000000e+00
#> 44  FALSE 0.000000e+00 0.000000e+00
#> 45   TRUE 9.496573e-04 9.262617e-03
#> 46  FALSE 0.000000e+00 0.000000e+00
#> 47  FALSE 0.000000e+00 0.000000e+00
#> 48   TRUE 7.368643e-02 7.187110e-01
#> 49  FALSE 0.000000e+00 0.000000e+00
#> 50  FALSE 0.000000e+00 0.000000e+00
#> 51   TRUE 1.002573e-01 9.778734e-01
#> 52   TRUE 1.518352e-02 1.480946e-01
#> 53   TRUE 1.103301e-02 1.076120e-01
#> 54  FALSE 0.000000e+00 0.000000e+00
#> 55  FALSE 0.000000e+00 0.000000e+00
#> 56  FALSE 0.000000e+00 0.000000e+00
#> 57  FALSE 0.000000e+00 0.000000e+00
#> 58  FALSE 0.000000e+00 0.000000e+00
#> 59  FALSE 0.000000e+00 0.000000e+00
#> 60  FALSE 0.000000e+00 0.000000e+00
#> 61  FALSE 0.000000e+00 0.000000e+00
#> 62  FALSE 0.000000e+00 0.000000e+00
#> 63  FALSE 0.000000e+00 0.000000e+00
#> 64  FALSE 0.000000e+00 0.000000e+00
#> 65  FALSE 0.000000e+00 0.000000e+00
#> 66  FALSE 0.000000e+00 0.000000e+00
#> 67  FALSE 0.000000e+00 0.000000e+00
#> 68   TRUE 5.499358e-02 5.363877e-01
#> 69  FALSE 0.000000e+00 0.000000e+00
#> 70  FALSE 0.000000e+00 0.000000e+00
#> 71  FALSE 0.000000e+00 0.000000e+00
#> 72   TRUE 2.189412e-03 2.135474e-02
#> 73  FALSE 0.000000e+00 0.000000e+00
#> 74   TRUE 2.489557e-02 2.428225e-01
#> 75  FALSE 0.000000e+00 0.000000e+00
#> 76  FALSE 0.000000e+00 0.000000e+00
#> 77  FALSE 0.000000e+00 0.000000e+00
#> 78  FALSE 0.000000e+00 0.000000e+00
#> 79  FALSE 0.000000e+00 0.000000e+00
#> 80   TRUE 4.924905e-03 4.803576e-02
#> 81   TRUE 3.357822e-07 3.275099e-06
#> 82  FALSE 0.000000e+00 0.000000e+00
#> 83  FALSE 0.000000e+00 0.000000e+00
#> 84  FALSE 0.000000e+00 0.000000e+00
#> 85   TRUE 1.025258e-01 1.000000e+00
#> 86  FALSE 0.000000e+00 0.000000e+00
#> 87  FALSE 0.000000e+00 0.000000e+00
#> 88  FALSE 0.000000e+00 0.000000e+00
#> 89  FALSE 0.000000e+00 0.000000e+00
#> 90   TRUE 7.267599e-03 7.088556e-02
#> 91  FALSE 0.000000e+00 0.000000e+00
#> 92  FALSE 0.000000e+00 0.000000e+00
#> 93  FALSE 0.000000e+00 0.000000e+00
#> 94  FALSE 0.000000e+00 0.000000e+00
#> 95  FALSE 0.000000e+00 0.000000e+00
#> 96   TRUE 4.655793e-02 4.541094e-01
#> 97  FALSE 0.000000e+00 0.000000e+00
#> 98  FALSE 0.000000e+00 0.000000e+00
#> 99  FALSE 0.000000e+00 0.000000e+00
#> 100 FALSE 0.000000e+00 0.000000e+00

if (FALSE) {
n = 5
z = sf::st_sfc(replicate(n, sf::st_union(fun_p1(p2 * .15, 1000, 10, 10, 0))))
z = sf::st_make_valid(sf::st_difference(z))

plot(z, col = scales::alpha(rainbow(n), .5), axes = TRUE)
plot(x, col = scales::alpha("blue", .5), add = TRUE)
plot(y, col = scales::alpha("red", .5), add = TRUE)

microbenchmark::microbenchmark(
  slow = in_layer(x, z, divide = FALSE),
  fast = in_layer(x, z, divide = TRUE),
  times = 10
)
microbenchmark::microbenchmark(
  slow = in_layer(x, z, "intersection", divide = FALSE),
  fast = in_layer(x, z, "intersection", divide = TRUE),
  times = 10
)
microbenchmark::microbenchmark(
  slow = in_layer(x, z, "all", divide = FALSE),
  fast = in_layer(x, z, "all", divide = TRUE),
  times = 10
)
}