fm_contains: Check which mesh triangles are inside a polygon

View source: R/evaluator.R

fm_containsR Documentation

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 class 'Spatial'
fm_contains(x, y, ...)

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

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

Arguments

x

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

y

an fm_mesh_2d() or inla.mesh object

...

Passed on to other methods

type

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

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.

Author(s)

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

Examples

if (TRUE &&
  fm_safe_sp()) {
  # Create a polygon and a mesh
  obj <- sp::SpatialPolygons(
    list(sp::Polygons(
      list(sp::Polygon(rbind(
        c(0, 0),
        c(50, 0),
        c(50, 50),
        c(0, 50)
      ))),
      ID = 1
    )),
    proj4string = fm_CRS("longlat_globe")
  )
  mesh <- fm_rcdt_2d_inla(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"))
  )
}


fmesher documentation built on July 1, 2024, 5:07 p.m.