Add cell to df with lat/long
add_raster_cell(
ras,
df,
x = "long",
y = "lat",
crs_df = 4326,
add_xy = FALSE,
return_old_xy = FALSE,
add_val = FALSE,
force_new = TRUE
)
SpatRaster, or path to raster (needs to be path if running in parallel), with cell numbers to extract
Dataframe with x/y columns
Character. Name of columns in df
with x and y coordinates
Single length vector. What crs are x and y?
Logical. Generate (centroid) x and y coords from cell? If TRUE,
these will be returned as cell_x
and cell_y
where 'x' and 'y' are as
defined by arguments x
and y
.
Logical. If true, the original x and y values will be
returned as old_x
and old_y
where 'x' and 'y' are as defined by arguments
x
and y
.
Logical. If true the value(s) for cell will be extracted using
terra::extract()
.The names of any columns resulting from add_val
will be
the same as the names in ras
.
Logical. If there is already a column cell
in df
, remove
it first?
df
with additional column cell
with cell numbers from ras.
Depending on the value of add_xy
and return_old_xy
, extra columns for
centroid x and y values and old x and y values respetively. If add_val
any
values from ras
. Note that, irrespective of the crs of ras
,
any returned x and y values will be in crs_df
.
#library(envRaster)
out_dir <- file.path(system.file(package = "envRaster"), "examples")
base_file <- fs::path(out_dir, "cube", "base.tif")
if(!file.exists(base_file)) {
make_base_grid(aoi
, out_res = 30
, out_epsg = 8059
, out_file = base_file
, datatype = "INT1U"
, overwrite = TRUE
)
}
base <- terra::rast(base_file)
# random sample of points within the aoi
sample <- matrix(nrow = 2, ncol = 2) %>% # include some NA x and y values
rbind(sf::st_sample(aoi, 100) %>%
sf::st_transform(crs = 4326) %>%
sf::st_coordinates()
) %>%
tibble::as_tibble() %>%
dplyr::mutate(attribute = sample(letters[1:10], nrow(.), replace = TRUE))
# add just cell to sample
add_raster_cell(base
, sample
, x = "X"
, y = "Y"
)
#> Joining with `by = join_by(old_X, old_Y)`
#> # A tibble: 102 × 2
#> cell attribute
#> <dbl> <chr>
#> 1 NA d
#> 2 NA c
#> 3 58841 h
#> 4 61146 d
#> 5 100976 i
#> 6 117653 a
#> 7 97215 g
#> 8 73944 d
#> 9 90150 h
#> 10 74195 j
#> # ℹ 92 more rows
# add cell and cell centroids to sample
add_raster_cell(base
, sample
, add_xy = TRUE
, x = "X"
, y = "Y"
)
#> Joining with `by = join_by(old_X, old_Y)`
#> # A tibble: 102 × 4
#> cell attribute cell_X cell_Y
#> <dbl> <chr> <dbl> <dbl>
#> 1 NA d NA NA
#> 2 NA c NA NA
#> 3 58841 h 140. -34.5
#> 4 61146 d 140. -34.5
#> 5 100976 i 140. -34.6
#> 6 117653 a 140. -34.6
#> 7 97215 g 140. -34.6
#> 8 73944 d 140. -34.5
#> 9 90150 h 140. -34.5
#> 10 74195 j 140. -34.5
#> # ℹ 92 more rows
# add cell, cell centroids and original 'x' and 'y' to sample
add_raster_cell(base
, sample
, add_xy = TRUE
, return_old_xy = TRUE
, x = "X"
, y = "Y"
)
#> Joining with `by = join_by(old_X, old_Y)`
#> # A tibble: 102 × 6
#> cell old_X old_Y attribute cell_X cell_Y
#> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
#> 1 NA NA NA d NA NA
#> 2 NA NA NA c NA NA
#> 3 58841 140. -34.5 h 140. -34.5
#> 4 61146 140. -34.5 d 140. -34.5
#> 5 100976 140. -34.6 i 140. -34.6
#> 6 117653 140. -34.6 a 140. -34.6
#> 7 97215 140. -34.6 g 140. -34.6
#> 8 73944 140. -34.5 d 140. -34.5
#> 9 90150 140. -34.5 h 140. -34.5
#> 10 74195 140. -34.5 j 140. -34.5
#> # ℹ 92 more rows
# add cell, cell centroids, cell values and original 'x' and 'y' to sample
add_raster_cell(base
, sample
, add_xy = TRUE
, return_old_xy = TRUE
, add_val = TRUE
, x = "X"
, y = "Y"
)
#> Joining with `by = join_by(old_X, old_Y)`
#> # A tibble: 102 × 7
#> cell old_X old_Y attribute cell_X cell_Y base
#> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <int>
#> 1 NA NA NA d NA NA NA
#> 2 NA NA NA c NA NA NA
#> 3 58841 140. -34.5 h 140. -34.5 1
#> 4 61146 140. -34.5 d 140. -34.5 1
#> 5 100976 140. -34.6 i 140. -34.6 1
#> 6 117653 140. -34.6 a 140. -34.6 1
#> 7 97215 140. -34.6 g 140. -34.6 1
#> 8 73944 140. -34.5 d 140. -34.5 1
#> 9 90150 140. -34.5 h 140. -34.5 1
#> 10 74195 140. -34.5 j 140. -34.5 1
#> # ℹ 92 more rows