Retrieves cell identifiers from argument grid at locations of x.
Arguments
- x
an sf object.
- ...
other arguments parsed to methods (currently unused)
- fun
(optional) a function which returns a unique value when provided a numeric vector, e.g. for locations that fall on boundaries or intersections of cells in
gridand so match with multiple cells.
Value
an object of class sf, with an additional variable id_cell providing the cell identifiers from grid.
Note
For locations falling on boundaries or intersections between multiple cells in grid, fun can be used to return a single cell identifier for each location, e.g. setting fun to min. fun is only used if at least one record in x matches two or more cells in grid.
Examples
library(sefraInputs)
library(dplyr)
# signature "sf"
library(sf)
data(grid)
x <- data.frame(lat = c(-42.5, -37.5, -35), lon = c(167.5, 172.5, 175))
x <- x %>% rowwise(.) %>%
mutate(., geometry = list(st_point(c(lon, lat)))) %>%
ungroup(.) %>%
st_as_sf(., crs = "EPSG:4326")
x <- x %>% st_transform(crs = st_crs(grid))
get_id_cell(x)
#> Simple feature collection with 3 features and 3 fields
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -224413.3 ymin: 5139910 xmax: 513825.6 ymax: 5873053
#> Projected CRS: +proj=laea +lat_0=-90 +lon_0=170
#> # A tibble: 3 × 4
#> lat lon geometry id_cell
#> <dbl> <dbl> <POINT [m]> <list>
#> 1 -42.5 168. (-224413.3 5139910) <int [1]>
#> 2 -37.5 172. (246362.2 5642623) <int [1]>
#> 3 -35 175 (513825.6 5873053) <int [4]>
get_id_cell(x, fun = min)
#> Simple feature collection with 3 features and 3 fields
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -224413.3 ymin: 5139910 xmax: 513825.6 ymax: 5873053
#> Projected CRS: +proj=laea +lat_0=-90 +lon_0=170
#> # A tibble: 3 × 4
#> lat lon geometry id_cell
#> <dbl> <dbl> <POINT [m]> <int>
#> 1 -42.5 168. (-224413.3 5139910) 646
#> 2 -37.5 172. (246362.2 5642623) 719
#> 3 -35 175 (513825.6 5873053) 719