Skip to content

Commit

Permalink
Bug fix to coordinate columns in thin_by_cell
Browse files Browse the repository at this point in the history
I was having problems with a sf data object that already had its coordinates stashed in X, Y columns, so I added some code to check if the correct coordinates are already there. If they are, coordinates are not bound. If there are X and Y columns that don't match the coordinates, they are replaced with a warning.
  • Loading branch information
japilo committed Oct 5, 2023
1 parent 5aedb1c commit 6a3d7d3
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions R/thin_by_cell.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ thin_by_cell <- function(data, raster, coords=NULL, drop_na = TRUE, agg_fact=NUL
"use `thin_by_cell_time()`")
}


# TODO add type checks for these parameters
# add type checks for these parameters
return_sf <- FALSE # flag whether we need to return an sf object
if (inherits(data,"sf")){
data <- data %>% dplyr::bind_cols(sf::st_coordinates(data))
if (all(c("X", "Y") %in% names(data))) {
if (!all(data[, c("X", "Y")] %>% sf::st_drop_geometry() %>% as.matrix() == sf::st_coordinates(data))) {
data <- data %>% dplyr::select(-X, -Y) %>% dplyr::bind_cols(sf::st_coordinates(data))
warning("sf contained 'X' and 'Y' coordinates that did not match point geometry and have been replaced")
}
} else {
data <- data %>% dplyr::bind_cols(sf::st_coordinates(data))
}
return_sf <- TRUE
}
coords <- check_coords_names(data, coords)
Expand Down

0 comments on commit 6a3d7d3

Please sign in to comment.