Learn R Programming

inlabru (version 2.8.0)

fm_contains: Check which mesh triangles are inside a polygon

Description

Wrapper for the sf::st_contains() (previously sp::over()) method to find triangle centroids or vertices inside sf or sp polygon objects

Usage

fm_contains(x, y, ...)

# S3 method for Spatial fm_contains(x, y, ...)

# S3 method for sf fm_contains(x, y, ...)

# S3 method for sfc fm_contains(x, y, ..., type = c("centroid", "vertex"))

Value

List of vectors of triangle indices (when type is 'centroid') or vertex indices (when type is 'vertex'). The list has one entry per row of the sf object. Use unlist(fm_contains(...)) if the combined union is needed.

Arguments

x

geometry (typically an sf or sp::SpatialPolygons object) for the queries

y

an inla.mesh() object

...

Passed on to other methods

type

the query type; either 'centroid' (default, for triangle centroids), or 'vertex' (for mesh vertices)

Author

Haakon Bakka, bakka@r-inla.org, and Finn Lindgren finn.lindgren@gmail.com

Examples

Run this code
if (bru_safe_inla() &&
  bru_safe_sp()) {
  # Create a polygon and a mesh
  obj <- sp::SpatialPolygons(
    list(Polygons(
      list(Polygon(rbind(
        c(0, 0),
        c(50, 0),
        c(50, 50),
        c(0, 50)
      ))),
      ID = 1
    )),
    proj4string = fm_CRS("longlat_globe")
  )
  mesh <- INLA::inla.mesh.create(globe = 2, crs = fm_crs("sphere"))

  ## 3 vertices found in the polygon
  fm_contains(obj, mesh, type = "vertex")

  ## 3 triangles found in the polygon
  fm_contains(obj, mesh)

  ## Multiple transformations can lead to slightly different results due to edge cases
  ## 4 triangles found in the polygon
  fm_contains(
    obj,
    fm_transform(mesh, crs = fm_crs("mollweide_norm"))
  )
}

Run the code above in your browser using DataLab