Direct descendant of envRaster::add_raster_cell(). Adding cell is now optional (and defaults to FALSE).

add_raster_bin(
  ras,
  df,
  x = "long",
  y = "lat",
  crs_df = 4326,
  add_xy = TRUE,
  add_cell = !add_xy,
  return_old_xy = FALSE,
  add_val = FALSE
)

Arguments

ras

SpatRaster, or path to raster (needs to be path if running in parallel), with cell numbers to extract

df

Dataframe with x/y columns

x, y

Character. Name of columns in df with x and y coordinates

crs_df

Single length vector. What crs are x and y?

add_xy

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.

add_cell

Logical. Add the cell id to the data frame?

return_old_xy

Logical. If true, the original x and y values will be returned as old_x and old_y where 'x' and 'y' are as per arguments x and y.

add_val

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.

Value

df (as tibble) with additional column(s):

  • cell with cell numbers from ras, if add_cell

  • cell_[x] and cell_[y], if add_xy

  • If add_val any values from ras Irrespective of the crs of ras, any returned x and y values will be in crs_df. If return_old_xy is FALSE, the original x and y columns will be dropped.

Examples


  #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 centroids to sample
  add_raster_bin(ras = base
                 , df = sample
                 , x = "X"
                 , y = "Y"
                 )
#> Joining with `by = join_by(old_X, old_Y)`
#> Joining with `by = join_by(x, y)`
#> Joining with `by = join_by(old_X, old_Y)`
#> # A tibble: 102 × 3
#>    cell_X cell_Y attribute
#>     <dbl>  <dbl> <chr>    
#>  1    NA    NA   d        
#>  2    NA    NA   c        
#>  3   140.  -34.5 h        
#>  4   140.  -34.5 d        
#>  5   140.  -34.6 i        
#>  6   140.  -34.6 a        
#>  7   140.  -34.6 g        
#>  8   140.  -34.5 d        
#>  9   140.  -34.5 h        
#> 10   140.  -34.5 j        
#> # ℹ 92 more rows

  # add cell and cell centroids to sample
  add_raster_bin(base
                 , sample
                 , add_cell = TRUE
                 , x = "X"
                 , y = "Y"
                 )
#> Joining with `by = join_by(old_X, old_Y)`
#> Joining with `by = join_by(x, y)`
#> Joining with `by = join_by(old_X, old_Y)`
#> # A tibble: 102 × 4
#>    cell_X cell_Y   cell attribute
#>     <dbl>  <dbl>  <dbl> <chr>    
#>  1    NA    NA       NA d        
#>  2    NA    NA       NA c        
#>  3   140.  -34.5  58841 h        
#>  4   140.  -34.5  60846 d        
#>  5   140.  -34.6 100976 i        
#>  6   140.  -34.6 117653 a        
#>  7   140.  -34.6  97215 g        
#>  8   140.  -34.5  73944 d        
#>  9   140.  -34.5  90150 h        
#> 10   140.  -34.5  74195 j        
#> # ℹ 92 more rows

  # add cell, cell centroids and original 'x' and 'y' to sample
  add_raster_bin(base
                 , sample
                 , add_cell = TRUE
                 , return_old_xy = TRUE
                 , x = "X"
                 , y = "Y"
                 )
#> Joining with `by = join_by(old_X, old_Y)`
#> Joining with `by = join_by(x, y)`
#> Joining with `by = join_by(old_X, old_Y)`
#> # A tibble: 102 × 6
#>    cell_X cell_Y   cell attribute old_X old_Y
#>     <dbl>  <dbl>  <dbl> <chr>     <dbl> <dbl>
#>  1    NA    NA       NA d           NA   NA  
#>  2    NA    NA       NA c           NA   NA  
#>  3   140.  -34.5  58841 h          140. -34.5
#>  4   140.  -34.5  60846 d          140. -34.5
#>  5   140.  -34.6 100976 i          140. -34.6
#>  6   140.  -34.6 117653 a          140. -34.6
#>  7   140.  -34.6  97215 g          140. -34.6
#>  8   140.  -34.5  73944 d          140. -34.5
#>  9   140.  -34.5  90150 h          140. -34.5
#> 10   140.  -34.5  74195 j          140. -34.5
#> # ℹ 92 more rows

  # add cell, cell centroids, cell values and original 'x' and 'y' to sample
  add_raster_bin(base
                  , sample
                  , add_cell = TRUE
                  , return_old_xy = TRUE
                  , add_val = TRUE
                  , x = "X"
                  , y = "Y"
                  )
#> Joining with `by = join_by(old_X, old_Y)`
#> Joining with `by = join_by(x, y)`
#> Joining with `by = join_by(old_X, old_Y)`
#> # A tibble: 102 × 7
#>    cell_X cell_Y   cell attribute  base old_X old_Y
#>     <dbl>  <dbl>  <dbl> <chr>     <int> <dbl> <dbl>
#>  1    NA    NA       NA d            NA   NA   NA  
#>  2    NA    NA       NA c            NA   NA   NA  
#>  3   140.  -34.5  58841 h             1  140. -34.5
#>  4   140.  -34.5  60846 d             1  140. -34.5
#>  5   140.  -34.6 100976 i             1  140. -34.6
#>  6   140.  -34.6 117653 a             1  140. -34.6
#>  7   140.  -34.6  97215 g             1  140. -34.6
#>  8   140.  -34.5  73944 d             1  140. -34.5
#>  9   140.  -34.5  90150 h             1  140. -34.5
#> 10   140.  -34.5  74195 j             1  140. -34.5
#> # ℹ 92 more rows