match_points_to_polygons: Generate a listing of matches between a set of points and a...

View source: R/sf.R

match_points_to_polygonsR Documentation

Generate a listing of matches between a set of points and a set of polygons.

Description

Generate a listing of matches between a set of points and a set of polygons.

Usage

match_points_to_polygons(pts, poly, form = "string")

Arguments

pts

sf POINT table with one or more records (rows)

poly

sf POLYGON or sf MULTIPOLYGON with one or more records (rows)

form

character, identifies desired output format (default 'string')

string

string representation where a point is matched to the row(s) in poly where it is contained

sparse

list of vectors (some may be empty) with polygon indices

matrix

logical matrix with nrow(pts) x nrow(poly) elements, where TRUE means the point is contained

Value

match listing as described by form

Examples

## Not run: 
v <- volcano_multi(what = "bands")
pts <- volcano_points(x = v)
y1 <- volcano_polygon()
y2 <- st_rotate(y1, pi/4) 
y3 <- st_translate(y1, c(-250, 120))
poly <- dplyr::bind_rows(y1, y2, y3)

m1 <- match_points_to_polygons(pts, poly, form = 'matrix')
head(m1)
m2 <- match_points_to_polygons(pts, poly, form = 'sparse')
m2
m3 <- match_points_to_polygons(pts, poly, form = 'string')
head(m3)

pts <- dplyr::mutate(pts, polygon = m3)

plot(v[,,,1], axes = TRUE, reset = FALSE)
plot(sf::st_geometry(pts), color = 'pink', 
     add = TRUE, pch = 19, cex = 0.5)
plot(y1, col = NA, border = 'purple', add = TRUE)
plot(sf::st_geometry(pts |> dplyr::filter(grepl("1", polygon))), 
     pch = 1, col = 'purple', add = TRUE)
plot(y2, col = NA, border = 'orange', add = TRUE)
plot(sf::st_geometry(pts |> dplyr::filter(grepl("2", polygon))), 
     pch = 2, col = 'orange', add = TRUE, cex = 1.5)
plot(y3, col = NA, border = 'cornflowerblue', add = TRUE)
plot(sf::st_geometry(pts |> dplyr::filter(grepl("3", polygon))), 
     pch = 0, col = 'cornflowerblue', add = TRUE, cex = 1.5)

## End(Not run)

BigelowLab/twinkle documentation built on Jan. 26, 2025, 6:34 a.m.