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"
)
#> Warning: `add_raster_cell()` was deprecated in envRaster 2025-02-25.
#> ℹ Please use `add_raster_bin()` instead.
#> Joining with `by = join_by(old_X, old_Y)`
#> # A tibble: 102 × 2
#> cell attribute
#> <dbl> <chr>
#> 1 NA f
#> 2 NA j
#> 3 111158 c
#> 4 129942 c
#> 5 66880 a
#> 6 61831 h
#> 7 99357 e
#> 8 136762 i
#> 9 67782 d
#> 10 89008 c
#> # ℹ 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 f NA NA
#> 2 NA j NA NA
#> 3 111158 c 140. -34.6
#> 4 129942 c 140. -34.6
#> 5 66880 a 140. -34.5
#> 6 61831 h 140. -34.5
#> 7 99357 e 140. -34.6
#> 8 136762 i 140. -34.6
#> 9 67782 d 140. -34.5
#> 10 89008 c 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 f NA NA
#> 2 NA NA NA j NA NA
#> 3 111158 140. -34.6 c 140. -34.6
#> 4 129942 140. -34.6 c 140. -34.6
#> 5 66880 140. -34.5 a 140. -34.5
#> 6 61831 140. -34.5 h 140. -34.5
#> 7 99357 140. -34.6 e 140. -34.6
#> 8 136762 140. -34.6 i 140. -34.6
#> 9 67782 140. -34.5 d 140. -34.5
#> 10 89008 140. -34.5 c 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 f NA NA NA
#> 2 NA NA NA j NA NA NA
#> 3 111158 140. -34.6 c 140. -34.6 1
#> 4 129942 140. -34.6 c 140. -34.6 1
#> 5 66880 140. -34.5 a 140. -34.5 1
#> 6 61831 140. -34.5 h 140. -34.5 1
#> 7 99357 140. -34.6 e 140. -34.6 1
#> 8 136762 140. -34.6 i 140. -34.6 1
#> 9 67782 140. -34.5 d 140. -34.5 1
#> 10 89008 140. -34.5 c 140. -34.5 1
#> # ℹ 92 more rows