Guest User

Untitled

a guest
Feb 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. library(sf)
  2. library(tidyverse)
  3.  
  4. ## Draft function
  5.  
  6. #' Function to assign EAA grid to a df of points
  7. #'
  8. #' @param df A data.frame containing two columns with latitude and longitude.
  9. #' @param lat Character. Name of column in \code{df} containing latitude.
  10. #' @param lon Character. Name of column in \code{df} containing longitude.
  11. #' @param crs A Coordinate Reference System. Default: 4326 (wgs84).
  12. #' @param scale Scale of the EAA grid in meters. Default: 1000.
  13. eaa_grid <- function(df, lat, long, crs = 4326, scale = 1000){
  14. df %>%
  15. st_as_sf(coords = c(long, lat),
  16. crs = crs) %>%
  17. # transform to Lambert
  18. st_transform(3035) %>%
  19. # extract coordinates: X, Y
  20. st_coordinates() %>%
  21. # transform to df
  22. as_data_frame() %>%
  23. # add cell ID
  24. mutate(cell_id = paste0("1kmE", floor(X/scale), "N", floor(Y/scale))) %>%
  25. # remove ancillary columns
  26. select(-X, -Y) %>%
  27. # add original columns (decimalLatitude, decimalLongitude)
  28. bind_cols(df)
  29. }
  30.  
  31. df_test <-
  32. tibble::tibble(
  33. decimalLatitude = c(51.333, 51.232, 50.8, 50.543, 51.032),
  34. decimalLongitude = c(3.112, 3.420, 4.410, 4.891, 4.435)
  35. )
  36.  
  37. df_test <- df_test %>%
  38. eaa_grid(lat = "decimalLatitude", long = "decimalLongitude")
  39.  
  40. df_test
Add Comment
Please, Sign In to add comment