Match each row of from to one row of to, typically when from is a finer
geography nested inside to. The result is an integer vector of row indices in
to and can be reused by downstream helpers such as estimate_up(),
estimate_down(), and block2prec().
Arguments
- from
An
sfobject to match from, usually the smaller geography.- to
An
sfobject to match to, usually the larger geography.- method
Matching method. One of
"center","centroid","point","circle", or"area".- by
Optional character scalar restricting matching within shared groups. Use a single value when
fromandtouse the same column name, or a named scalar such asc(county_from = "county_to")when the column names differ.- tiebreaker
Logical. If
TRUE, ambiguous or unmatched features are assigned to the nearest feature into. IfFALSE, unmatched rows receive-1and rows with multiple matches receive-2.- epsg
numeric EPSG code to planarize to. Default is 3857.
Value
integer vector of length nrow(from). Values are row indices in to,
or -1 / -2 when tiebreaker = FALSE. Empty geometries are returned as
NA.
Details
Methods are as follows:
"centroid": match using the geometric centroid of each row infrom."center": use the centroid when it lies inside the polygon and otherwise fall back to a point-on-surface."point": use point on surface for each row infrom."circle": use the centroid of the maximum inscribed circle."area": match to the row intowith the largest area overlap.
When the output does not reference every row of to, an attribute
"matching_max" is attached and records nrow(to). Functions such as
block2prec() use that attribute to preserve empty target groups.
Examples
library(dplyr)
data(checkerboard)
counties <- sf::st_as_sf(as.data.frame(rbind(
sf::st_union(checkerboard |> filter(i < 4)),
sf::st_union(checkerboard |> filter(i >= 4))
)))
geo_match(from = checkerboard, to = counties)
#> Warning: Planarizing skipped. `x` missing CRS.
#> Warning: Planarizing skipped. `x` missing CRS.
#> [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2
#> [39] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
geo_match(from = checkerboard, to = counties, method = 'area')
#> Warning: Planarizing skipped. `x` missing CRS.
#> [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2
#> [39] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
