Spatial join and filter
Description
st_join() attaches the columns of y to each row of x that it relates
to spatially. st_filter() keeps the rows of x that relate to any row of
y, without adding columns.
Usage
st_join(
x,
y,
predicate = st_intersects,
...,
join_type = c("left", "inner"),
suffix = c("_x", "_y")
)
## Default S3 method:
st_join(
x,
y,
predicate = st_intersects,
...,
join_type = c("left", "inner"),
suffix = c("_x", "_y")
)
st_filter(x, y, predicate = st_intersects, ...)
## Default S3 method:
st_filter(x, y, predicate = st_intersects, ...)
st_join(
x,
y,
predicate = st_intersects,
...,
join_type = c("left", "inner"),
suffix = c("_x", "_y")
)
st_join(
x,
y,
predicate = st_intersects,
...,
join_type = c("left", "inner"),
suffix = c("_x", "_y")
)
st_filter(x, y, predicate = st_intersects, ...)
st_filter(x, y, predicate = st_intersects, ...)
Arguments
x, y
|
spatial data frames.
|
predicate |
a function of x and y returning the row positions of
y matching each row of x, such as st_intersects().
|
... |
passed to predicate.
|
join_type |
"left" to keep the rows of x that match nothing,
"inner" to drop them.
|
suffix |
a character vector of length 2, the suffixes for names the two
sides share.
|
Details
Both dispatch on x, so a backend that holds its data somewhere other than
R, such as a database, can replace either and run the whole operation there.
The default works for any backend whose geometry vector can be subset with
[ and whose predicate returns row positions.
The default puts y into the geometry type of x before the predicate sees
it, so the two sides need not hold the same type.
A row of x matching several rows of y is repeated once per match. A row
matching nothing is kept against NA when join_type is "left" and
dropped when it is "inner". Names the two sides share are suffixed, and
the geometry of the result is the geometry of x.
Not every backend offers both. One that cannot do a left join says so rather
than returning an inner join under its name.
Value
st_join() returns a spatial data frame of the matched rows.
st_filter() returns x with the unmatched rows dropped.
Examples
if (requireNamespace("sf", quietly = TRUE)) {
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
counties <- as_sdf(tibble::tibble(
name = as.character(nc$NAME),
geometry = sf::st_geometry(nc)
))
sites <- as_sdf(tibble::tibble(
id = 1:2,
geometry = sf::st_sfc(
sf::st_point(c(-78.6, 35.8)),
sf::st_point(c(-80.8, 35.2)),
crs = sf::st_crs(nc)
)
))
st_join(sites, counties, predicate = st_within)
st_filter(counties, sites)
}
if (requireNamespace("sf", quietly = TRUE)) {
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
counties <- as_sdf(tibble::tibble(
name = as.character(nc$NAME),
geometry = sf::st_geometry(nc)
))
sites <- as_sdf(tibble::tibble(
id = 1:2,
geometry = sf::st_sfc(
sf::st_point(c(-78.6, 35.8)),
sf::st_point(c(-80.8, 35.2)),
crs = sf::st_crs(nc)
)
))
st_join(sites, counties, predicate = st_within)
st_filter(counties, sites)
}