Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(sf)
- library(tidyverse)
- ## Draft function
- #' Function to assign EAA grid to a df of points
- #'
- #' @param df A data.frame containing two columns with latitude and longitude.
- #' @param lat Character. Name of column in \code{df} containing latitude.
- #' @param lon Character. Name of column in \code{df} containing longitude.
- #' @param crs A Coordinate Reference System. Default: 4326 (wgs84).
- #' @param scale Scale of the EAA grid in meters. Default: 1000.
- eaa_grid <- function(df, lat, long, crs = 4326, scale = 1000){
- df %>%
- st_as_sf(coords = c(long, lat),
- crs = crs) %>%
- # transform to Lambert
- st_transform(3035) %>%
- # extract coordinates: X, Y
- st_coordinates() %>%
- # transform to df
- as_data_frame() %>%
- # add cell ID
- mutate(cell_id = paste0("1kmE", floor(X/scale), "N", floor(Y/scale))) %>%
- # remove ancillary columns
- select(-X, -Y) %>%
- # add original columns (decimalLatitude, decimalLongitude)
- bind_cols(df)
- }
- df_test <-
- tibble::tibble(
- decimalLatitude = c(51.333, 51.232, 50.8, 50.543, 51.032),
- decimalLongitude = c(3.112, 3.420, 4.410, 4.891, 4.435)
- )
- df_test <- df_test %>%
- eaa_grid(lat = "decimalLatitude", long = "decimalLongitude")
- df_test
Add Comment
Please, Sign In to add comment