From dc864b5db05b030926dff0c7c1ac6cd166010825 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 3 Aug 2023 12:24:34 -0300 Subject: [PATCH 01/19] deprecated build_lines_sp --- R/build_lines_sp.R | 108 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 R/build_lines_sp.R diff --git a/R/build_lines_sp.R b/R/build_lines_sp.R new file mode 100644 index 0000000..2ce0398 --- /dev/null +++ b/R/build_lines_sp.R @@ -0,0 +1,108 @@ +#' Build Lines (deprecated version of function with retired spatial packages) +#' +#' +#' @inheritParams build_lines +#' +#' @export +#' +#' @family Build functions +#' @seealso \code{\link{group_lines}} +#' +#' @import data.table +#' +build_lines_sp <- + function(DT = NULL, + projection = NULL, + id = NULL, + coords = NULL, + sortBy = NULL, + splitBy = NULL) { + .Deprecated(msg = 'build_lines has been updated to use modern spatial R packages, removing dependencies on rgdal, rgeos, maptools in favor of sf. This version will be preserved until September 2023 for testing and user transition.') + # due to NSE notes in R CMD check + dropped <- . <- NULL + + if (is.null(DT)) { + stop('input DT required') + } + + if (is.null(coords)) { + stop('coords must be provided') + } + + if (is.null(id)) { + stop('id must be provided') + } + + if (is.null(projection)) { + stop('projection must be provided') + } + + if (is.null(sortBy)) { + stop('sortBy must be provided') + } + + if (length(coords) != 2) { + stop('coords requires a vector of column names for coordinates X and Y') + } + + if (any(!(c(id, coords, splitBy, sortBy) %in% colnames(DT)))) { + stop(paste0( + as.character(paste(setdiff( + c(id, coords, splitBy, sortBy), colnames(DT) + ), + collapse = ', ')), + ' field(s) provided are not present in input DT' + )) + } + + if (any(!(DT[, vapply(.SD, is.numeric, TRUE), .SDcols = coords]))) { + stop('coords must be numeric') + } + + if (is.null(splitBy)) { + splitBy <- id + } else { + splitBy <- c(id, splitBy) + } + if (any(!(DT[, lapply(.SD, FUN = function(x) { + is.numeric(x) | is.character(x) | is.integer(x) + } + ), .SDcols = splitBy]))) { + stop( + strwrap(prefix = " ", initial = "", + x = 'id (and splitBy when provided) + must be character, numeric or integer type' + ) + ) + } + + if (!('POSIXct' %in% + unlist(lapply(DT[, .SD, .SDcols = sortBy], class)))) { + stop('sortBy provided must be 1 column of type POSIXct') + } + + + dropRows <- DT[, .(dropped = .N < 2), by = c(splitBy)] + + if (dropRows[(dropped), .N] > 0) { + warning('some rows dropped, cannot build lines with less than two points') + } + + lst <- split(DT[dropRows, on = splitBy][!(dropped)][order(get(sortBy))], + by = c(splitBy), sorted = TRUE) + + if (length(lst) == 0) { + return(NULL) + } else { + proj4string <- sp::CRS(projection) + l <- lapply(seq_along(lst), function(i) { + sp::SpatialLines(list(sp::Lines(sp::Line( + cbind(lst[[i]][[coords[1]]], + lst[[i]][[coords[2]]]) + ), + names(lst)[[i]])), + proj4string = proj4string) + }) + return(do.call(sp::rbind.SpatialLines, l)) + } + } From ba1070487579612aa24b100b059127f9ccd1b792 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 3 Aug 2023 12:32:31 -0300 Subject: [PATCH 02/19] use setorder and st_sf with linestrings by splitBy --- R/build_lines.R | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/R/build_lines.R b/R/build_lines.R index aa266d9..5e589b8 100644 --- a/R/build_lines.R +++ b/R/build_lines.R @@ -143,21 +143,13 @@ build_lines <- warning('some rows dropped, cannot build lines with less than two points') } - lst <- split(DT[dropRows, on = splitBy][!(dropped)][order(get(sortBy))], - by = c(splitBy), sorted = TRUE) - - if (length(lst) == 0) { - return(NULL) - } else { - proj4string <- sp::CRS(projection) - l <- lapply(seq_along(lst), function(i) { - sp::SpatialLines(list(sp::Lines(sp::Line( - cbind(lst[[i]][[coords[1]]], - lst[[i]][[coords[2]]]) - ), - names(lst)[[i]])), - proj4string = proj4string) - }) - return(do.call(sp::rbind.SpatialLines, l)) - } + wo_drop <- DT[dropRows, on = splitBy][!(dropped)] + + setorderv(wo_drop, sortBy) + lines <- st_as_sf( + wo_drop[, .(geometry = st_sfc(st_linestring(as.matrix(.SD)))), + by = splitBy, .SDcols = coords], + crs = st_crs(projection) + ) + # TODO: check if NULL? } From c89f8d90aaf566072c5df9a877825a46339f780b Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 7 Aug 2023 14:15:22 -0300 Subject: [PATCH 03/19] fix missing pkgs:: --- R/build_lines.R | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/R/build_lines.R b/R/build_lines.R index 5e589b8..f319be3 100644 --- a/R/build_lines.R +++ b/R/build_lines.R @@ -145,11 +145,10 @@ build_lines <- wo_drop <- DT[dropRows, on = splitBy][!(dropped)] - setorderv(wo_drop, sortBy) - lines <- st_as_sf( - wo_drop[, .(geometry = st_sfc(st_linestring(as.matrix(.SD)))), - by = splitBy, .SDcols = coords], - crs = st_crs(projection) + data.table::setorderv(wo_drop, sortBy) + lines <- sf::st_as_sf( + wo_drop[, .(geometry = sf::st_sfc(sf::st_linestring(as.matrix(.SD)))), + crs = sf::st_crs(projection) ) # TODO: check if NULL? } From 6cf217d641f788c6271be3677559c8a60802876e Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 7 Aug 2023 14:15:37 -0300 Subject: [PATCH 04/19] fix ensure splitBy col/arg --- R/build_lines.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/build_lines.R b/R/build_lines.R index f319be3..9bd1f62 100644 --- a/R/build_lines.R +++ b/R/build_lines.R @@ -148,6 +148,7 @@ build_lines <- data.table::setorderv(wo_drop, sortBy) lines <- sf::st_as_sf( wo_drop[, .(geometry = sf::st_sfc(sf::st_linestring(as.matrix(.SD)))), + by = c(splitBy), .SDcols = coords], crs = sf::st_crs(projection) ) # TODO: check if NULL? From e6735d6a0ab44502ff7fa5350bb28f62a9860764 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 7 Aug 2023 14:15:49 -0300 Subject: [PATCH 05/19] rm library from test file --- tests/testthat/test-build-lines.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testthat/test-build-lines.R b/tests/testthat/test-build-lines.R index cabb51f..df341ce 100644 --- a/tests/testthat/test-build-lines.R +++ b/tests/testthat/test-build-lines.R @@ -1,6 +1,5 @@ # Test build_lines context('test build_lines') -library(spatsoc) DT <- fread('../testdata/DT.csv') From e4033c4351c02badb04fe738eae22e4aedfc1bc5 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 7 Aug 2023 14:28:47 -0300 Subject: [PATCH 06/19] fix replace length with nrow --- tests/testthat/test-build-lines.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-build-lines.R b/tests/testthat/test-build-lines.R index df341ce..e5d153b 100644 --- a/tests/testthat/test-build-lines.R +++ b/tests/testthat/test-build-lines.R @@ -139,7 +139,7 @@ test_that('column names must exist in DT', { test_that('returns same number of lines as unique IDs/splitBy provided', { # without splitBy - expect_equal(length( + expect_equal(nrow( build_lines( DT = DT, id = 'ID', @@ -156,7 +156,7 @@ test_that('returns same number of lines as unique IDs/splitBy provided', { DT[, count := .N, by = splitBy] subDT <- DT[count >= 2] - expect_equal(length( + expect_equal(nrow( build_lines( DT = subDT, id = 'ID', @@ -294,14 +294,14 @@ test_that('splitBy argument doesnt use splitBy column', { utm <- 'EPSG:32736' expect_equal( - build_lines( + nrow(build_lines( DT = copyDT, id = 'ID', coords = c('X', 'Y'), projection = utm, splitBy = 'splitBy', sortBy = 'datetime' - ) |> length(), + )), copyDT[, uniqueN(splitBy) * uniqueN(ID)] ) }) From 27dca386fb14ffaa1df9b644e16bcb5d31ebf2cc Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 7 Aug 2023 14:29:04 -0300 Subject: [PATCH 07/19] fix returns sf lines not spatial --- tests/testthat/test-build-lines.R | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/testthat/test-build-lines.R b/tests/testthat/test-build-lines.R index e5d153b..424f034 100644 --- a/tests/testthat/test-build-lines.R +++ b/tests/testthat/test-build-lines.R @@ -242,26 +242,28 @@ test_that('splitBy and id provided are not correct format', { ) }) -test_that('BuildPts returns a SpatialLines', { - expect_true('SpatialLines' %in% class( +test_that('build_lines returns an sf object with LINESTRINGs', { + expect_s3_class( build_lines( DT = DT, id = 'ID', coords = c('X', 'Y'), projection = utm, sortBy = 'datetime' - ) - )) + ), + 'sf' + ) - expect_true(isS4( - build_lines( + expect_true( + 'LINESTRING' %in% + sf::st_geometry_type(build_lines( DT = DT, id = 'ID', coords = c('X', 'Y'), projection = utm, sortBy = 'datetime' - ) - )) + ), by_geometry = FALSE) + ) }) test_that('build_lines builds ordered lines', { From 5724ea0ed8440d9271d0fc2fada51d1a7787d01d Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 7 Aug 2023 14:29:25 -0300 Subject: [PATCH 08/19] fix check ordered lines --- tests/testthat/test-build-lines.R | 32 +++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/tests/testthat/test-build-lines.R b/tests/testthat/test-build-lines.R index 424f034..ed2eebe 100644 --- a/tests/testthat/test-build-lines.R +++ b/tests/testthat/test-build-lines.R @@ -267,21 +267,25 @@ test_that('build_lines returns an sf object with LINESTRINGs', { }) test_that('build_lines builds ordered lines', { + base_lines <- build_lines( + DT = DT, + id = 'ID', + coords = c('X', 'Y'), + projection = utm, + sortBy = 'datetime' + ) + + random_lines <- build_lines( + DT = DT[sample(.N)], + id = 'ID', + coords = c('X', 'Y'), + projection = utm, + sortBy = 'datetime' + ) + expect_equal( - build_lines( - DT = DT, - id = 'ID', - coords = c('X', 'Y'), - projection = utm, - sortBy = 'datetime' - ), - build_lines( - DT = DT[sample(.N, .N)], - id = 'ID', - coords = c('X', 'Y'), - projection = utm, - sortBy = 'datetime' - ) + base_lines[order(base_lines$ID),], + random_lines[order(random_lines$ID),] ) }) From be55464a2356a5eccd8a2a10459745a97b9e57c3 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 7 Aug 2023 14:29:47 -0300 Subject: [PATCH 09/19] mv packages to testthat.R --- tests/testthat.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testthat.R b/tests/testthat.R index c23ed60..89cfd33 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,4 +1,6 @@ library(testthat) library(spatsoc) +library(data.table) +library(sf) test_check("spatsoc") From 247f0080fe8cd9de33ce1ac2e248d67781db11b6 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 7 Aug 2023 15:19:59 -0300 Subject: [PATCH 10/19] fix use expect in --- tests/testthat/test-build-lines.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-build-lines.R b/tests/testthat/test-build-lines.R index ed2eebe..16a7d9d 100644 --- a/tests/testthat/test-build-lines.R +++ b/tests/testthat/test-build-lines.R @@ -254,8 +254,8 @@ test_that('build_lines returns an sf object with LINESTRINGs', { 'sf' ) - expect_true( - 'LINESTRING' %in% + expect_in( + 'LINESTRING', sf::st_geometry_type(build_lines( DT = DT, id = 'ID', From 136fb4a355d3b61d92139a08303dbd97c167e2e8 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 7 Aug 2023 16:56:17 -0300 Subject: [PATCH 11/19] update docs --- R/build_lines.R | 58 ++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/R/build_lines.R b/R/build_lines.R index 9bd1f62..1e5cb7d 100644 --- a/R/build_lines.R +++ b/R/build_lines.R @@ -1,37 +1,42 @@ #' Build Lines #' #' -#' \code{build_lines} creates a \code{SpatialLines} object from a \code{data.table}. -#' The function accepts a \code{data.table} with relocation data, individual -#' identifiers a sorting column and a \code{projection}. The relocation data -#' is transformed into \code{SpatialLines} for each individual and optionally, -#' each \code{splitBy}. Relocation data should be in two columns representing -#' the X and Y coordinates. -#' -#' The \code{projection} argument expects a character string defining the EPSG -#' code. For example, for UTM zone 36N (EPSG 32736), the projection argument is -#' 'EPSG:32736'. See \url{https://spatialreference.org} for a list of -#' EPSG codes. Please note, R spatial has followed updates to GDAL and PROJ +#' `build_lines` generates an sf LINESTRING object from a `data.table`. +#' The function accepts a `data.table` with relocation data, individual +#' identifiers, a sorting column and a `projection`. The relocation data +#' is transformed into LINESTRINGs for each individual and optionally, +#' combination of columns in `splitBy`. +#' Relocation data should be in two columns representing the X and Y coordinates. +#' +#' The `projection` argument expects a numeric or character defining the +#' coordinate reference system. +#' For example, for UTM zone 36N (EPSG 32736), the projection argument is either +#' `projection = 'EPSG:32736'` or `projection = 32736`. +#' See details in [`sf::st_crs()`] and \url{https://spatialreference.org} +#' for a list of EPSG codes. +#' +#' Please note, R spatial has followed updates to GDAL and PROJ #' for handling projections, see more at #' \url{https://r-spatial.org/r/2020/03/17/wkt.html}. #' -#' The \code{sortBy} is used to order the input \code{data.table} when creating -#' \code{SpatialLines}. It must a \code{POSIXct} to ensure the rows are sorted -#' by date time. +#' The `sortBy` argument is used to order the input `DT` when creating +#' sf LINESTRINGs. It must a column in the input `DT` of type +#' POSIXct to ensure the rows are sorted by date time. #' -#' The \code{splitBy} argument offers further control building \code{SpatialLines}. -#' If in your \code{DT}, you have multiple temporal groups (e.g.: years) for +#' The `splitBy` argument offers further control building LINESTRINGs. +#' If in your input `DT`, you have multiple temporal groups (e.g.: years) for #' example, you can provide the name of the column which identifies them and -#' build \code{SpatialLines} for each individual in each year. +#' build LINESTRINGs for each individual in each year. #' -#' \code{build_lines} is used by \code{group_lines} for grouping overlapping -#' lines created from relocations. +#' `build_lines` is used by `group_lines` for grouping overlapping +#' lines generated from relocations. #' -#' @return \code{build_lines} returns a \code{SpatialLines} object with a line -#' for each individual (and optionally \code{splitBy} combination). +#' @return `build_lines` returns an sf LINESTRING object with a line +#' for each individual (and optionally `splitBy` combination). #' -#' An error is returned when an individual has less than 2 relocations, making -#' it impossible to build a line. +#' Individuals (or combinations of individuals and `splitBy`) with less than two +#' relocations are dropped since it requires at least two relocations to +#' build a line. #' #' @inheritParams group_lines #' @inheritParams build_polys @@ -54,7 +59,7 @@ #' DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] #' #' # EPSG code for example data -#' utm <- 'EPSG:32736' +#' utm <- 32736 #' #' # Build lines for each individual #' lines <- build_lines(DT, projection = utm, id = 'ID', coords = c('X', 'Y'), @@ -64,7 +69,6 @@ #' DT[, yr := year(datetime)] #' lines <- build_lines(DT, projection = utm, id = 'ID', coords = c('X', 'Y'), #' sortBy = 'datetime', splitBy = 'yr') -#' build_lines <- function(DT = NULL, projection = NULL, @@ -132,7 +136,7 @@ build_lines <- } if (!('POSIXct' %in% - unlist(lapply(DT[, .SD, .SDcols = sortBy], class)))) { + unlist(lapply(DT[, .SD, .SDcols = c(sortBy)], class)))) { stop('sortBy provided must be 1 column of type POSIXct') } @@ -146,10 +150,10 @@ build_lines <- wo_drop <- DT[dropRows, on = splitBy][!(dropped)] data.table::setorderv(wo_drop, sortBy) + lines <- sf::st_as_sf( wo_drop[, .(geometry = sf::st_sfc(sf::st_linestring(as.matrix(.SD)))), by = c(splitBy), .SDcols = coords], crs = sf::st_crs(projection) ) - # TODO: check if NULL? } From aceaf021830d4c9654b4d506efa640bc3f8d082d Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 7 Aug 2023 16:56:22 -0300 Subject: [PATCH 12/19] update projection arg --- R/build_polys.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/build_polys.R b/R/build_polys.R index 74733e2..48ec950 100644 --- a/R/build_polys.R +++ b/R/build_polys.R @@ -51,9 +51,9 @@ #' @inheritParams group_polys #' @param spPts alternatively, provide solely a SpatialPointsDataFrame with one #' column representing the ID of each point. -#' @param projection character string defining the projection to be passed to -#' \code{sp::CRS}. For example, for UTM zone 36S (EPSG 32736), -#' the projection argument is 'EPSG:32736'. See details. +#' @param projection numeric or character definition the coordinate reference +#' system to be passed to [sf::st_crs()]. For example, either +#' `projection = "EPSG:32736"` or `projection = 32736`. #' @export #' #' @family Build functions From 4b6e9d30f1af99697ba25bf8e0cabec43ea9ae86 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 17 Aug 2023 11:55:38 -0300 Subject: [PATCH 13/19] update projection faq --- vignettes/faq.Rmd | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/vignettes/faq.Rmd b/vignettes/faq.Rmd index 558a421..df33edd 100644 --- a/vignettes/faq.Rmd +++ b/vignettes/faq.Rmd @@ -196,22 +196,15 @@ See [3.2.1](#dt-1). The `threshold` argument represents a buffer area around each line. When `threshold = 0`, the lines are grouped by spatial overlap. If the threshold is greater than 0, the lines buffered, then grouped by spatial overlap. ### projection -The `projection` argument expects a character string defining the projection to -be passed to `sp::CRS`. For example, for UTM zone 36S (EPSG 32736), the projection -argument is `projection = "EPSG:32736"`. See \url{https://spatialreference.org} +The `projection` argument expects a character string or numeric +defining the coordinate reference system to be passed to `sf::st_crs`. +For example, for UTM zone 36S (EPSG 32736), the projection +argument is `projection = "EPSG:32736"` or `projection = 32736`. +See \url{https://spatialreference.org} for a list of EPSG codes. Please note, R spatial has followed updates to GDAL and PROJ for handling projections, see more at \url{https://r-spatial.org/r/2020/03/17/wkt.html}. -Because of changes to projection handling, `group_polys`, `build_polys`, `group_lines` and `build_lines` may return the warning once, or many times: - -``` -In proj4string(xy) : CRS object has comment, which is lost in output -Calls: group_polys ... do.call -> -> proj4string -> proj4string -``` - -This warning is explicitly verbose, to ensure we are considering the updated use of CRS [in `sp`](https://r-spatial.org/r/2020/03/17/wkt.html#crs-objects-in-sp) and other spatial packages. - ### sortBy The `sortBy` argument expects a date time formatted column name, which is used to order the rows for each individual (and `splitBy`). @@ -238,21 +231,14 @@ The default unit for area overlap is square meters. ### projection -The `projection` argument expects a character string defining the projection to be -passed to `sp::CRS`. For example, for UTM zone 36S (EPSG 32736), the projection -argument is `projection = "EPSG:32736"`. See \url{https://spatialreference.org} +The `projection` argument expects a character string or numeric +defining the coordinate reference system to be passed to `sf::st_crs`. +For example, for UTM zone 36S (EPSG 32736), the projection +argument is `projection = "EPSG:32736"` or `projection = 32736`. +See \url{https://spatialreference.org} for a list of EPSG codes. Please note, R spatial has followed updates to GDAL and PROJ for handling projections, see more at -\url{https://r-spatial.org/r/2020/03/17/wkt.html}. - -Because of changes to projection handling, `group_polys`, `build_polys`, `group_lines` and `build_lines` may return the warning once, or many times: - -``` -In proj4string(xy) : CRS object has comment, which is lost in output -Calls: group_polys ... do.call -> -> proj4string -> proj4string -``` - -This warning is explicitly verbose, to ensure we are considering the updated use of CRS [in `sp`](https://r-spatial.org/r/2020/03/17/wkt.html#crs-objects-in-sp) and other spatial packages. +\url{https://r-spatial.org/r/2020/03/17/wkt.html}. ### hrType and hrParams From 2f65458a1808f8067c4df4ab69a00de1ae56e5e1 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 17 Aug 2023 11:55:50 -0300 Subject: [PATCH 14/19] clarify sf returned --- R/build_lines.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/build_lines.R b/R/build_lines.R index 1e5cb7d..baf4f76 100644 --- a/R/build_lines.R +++ b/R/build_lines.R @@ -1,12 +1,12 @@ #' Build Lines #' #' -#' `build_lines` generates an sf LINESTRING object from a `data.table`. -#' The function accepts a `data.table` with relocation data, individual -#' identifiers, a sorting column and a `projection`. The relocation data -#' is transformed into LINESTRINGs for each individual and optionally, -#' combination of columns in `splitBy`. -#' Relocation data should be in two columns representing the X and Y coordinates. +#' `build_lines` generates a simple feature collection with LINESTRINGs from a +#' `data.table`. The function accepts a `data.table` with relocation data, +#' individual identifiers, a sorting column and a `projection`. The relocation +#' data is transformed into LINESTRINGs for each individual and, optionally, +#' combination of columns listed in `splitBy`. Relocation data should be in two +#' columns representing the X and Y coordinates. #' #' The `projection` argument expects a numeric or character defining the #' coordinate reference system. From 36d529de410167827876312c1468916f1d21d974 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 17 Aug 2023 11:56:12 -0300 Subject: [PATCH 15/19] notes about sf transtion up front --- R/build_lines.R | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/R/build_lines.R b/R/build_lines.R index baf4f76..00642e7 100644 --- a/R/build_lines.R +++ b/R/build_lines.R @@ -8,6 +8,18 @@ #' combination of columns listed in `splitBy`. Relocation data should be in two #' columns representing the X and Y coordinates. #' +#' Please note, spatsoc has followed updates from R spatial, GDAL and PROJ +#' for handling projections, see more at +#' \url{https://r-spatial.org/r/2020/03/17/wkt.html}. +#' +#' In addition, `build_lines` previously used [sp::SpatialLines] but has been +#' updated to use [sf::st_as_sf] and [sf::st_linestring] according to the +#' R-spatial evolution, see more at +#' \url{https://r-spatial.org/r/2022/04/12/evolution.html}. A deprecated +#' version of this function using [sp::SpatialLines] is retained as +#' [build_lines_sp] temporarily but users are urged to transition as soon as +#' possible. +#' #' The `projection` argument expects a numeric or character defining the #' coordinate reference system. #' For example, for UTM zone 36N (EPSG 32736), the projection argument is either @@ -15,10 +27,6 @@ #' See details in [`sf::st_crs()`] and \url{https://spatialreference.org} #' for a list of EPSG codes. #' -#' Please note, R spatial has followed updates to GDAL and PROJ -#' for handling projections, see more at -#' \url{https://r-spatial.org/r/2020/03/17/wkt.html}. -#' #' The `sortBy` argument is used to order the input `DT` when creating #' sf LINESTRINGs. It must a column in the input `DT` of type #' POSIXct to ensure the rows are sorted by date time. From dab4c9477d65ce45ac68f5aa243bf42835645589 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 17 Aug 2023 11:56:23 -0300 Subject: [PATCH 16/19] defining* --- R/build_polys.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/build_polys.R b/R/build_polys.R index 48ec950..7e9c0a1 100644 --- a/R/build_polys.R +++ b/R/build_polys.R @@ -51,7 +51,7 @@ #' @inheritParams group_polys #' @param spPts alternatively, provide solely a SpatialPointsDataFrame with one #' column representing the ID of each point. -#' @param projection numeric or character definition the coordinate reference +#' @param projection numeric or character defining the coordinate reference #' system to be passed to [sf::st_crs()]. For example, either #' `projection = "EPSG:32736"` or `projection = 32736`. #' @export From 2254138531461b9d6ff4ddee2f478f27f55213ac Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 17 Aug 2023 11:56:36 -0300 Subject: [PATCH 17/19] bump roxygen --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3fdb046..35c7b9e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -37,7 +37,7 @@ Suggests: asnipe, markdown SystemRequirements: GEOS (>= 3.2.0) -RoxygenNote: 7.2.2 +RoxygenNote: 7.2.3 VignetteBuilder: knitr Roxygen: list(markdown = TRUE) BugReports: https://github.com/ropensci/spatsoc/issues From 74626a160914564ef6321c4ede3691239f662da0 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 17 Aug 2023 11:57:53 -0300 Subject: [PATCH 18/19] man --- NAMESPACE | 1 + man/build_lines.Rd | 65 ++++++++++++++++++++++++++----------------- man/build_lines_sp.Rd | 43 ++++++++++++++++++++++++++++ man/build_polys.Rd | 7 +++-- man/group_lines.Rd | 6 ++-- man/group_polys.Rd | 6 ++-- 6 files changed, 93 insertions(+), 35 deletions(-) create mode 100644 man/build_lines_sp.Rd diff --git a/NAMESPACE b/NAMESPACE index 1ac2b55..be93fe8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(build_lines) +export(build_lines_sp) export(build_polys) export(dyad_id) export(edge_dist) diff --git a/man/build_lines.Rd b/man/build_lines.Rd index 4569f3b..58c6bde 100644 --- a/man/build_lines.Rd +++ b/man/build_lines.Rd @@ -16,9 +16,9 @@ build_lines( \arguments{ \item{DT}{input data.table} -\item{projection}{character string defining the projection to be passed to -\code{sp::CRS}. For example, for UTM zone 36S (EPSG 32736), -the projection argument is 'EPSG:32736'. See details.} +\item{projection}{numeric or character defining the coordinate reference +system to be passed to \code{\link[sf:st_crs]{sf::st_crs()}}. For example, either +\code{projection = "EPSG:32736"} or \code{projection = 32736}.} \item{id}{Character string of ID column name} @@ -31,39 +31,52 @@ be a POSIXct.} name(s) upon which the grouping will be calculated} } \value{ -\code{build_lines} returns a \code{SpatialLines} object with a line +\code{build_lines} returns an sf LINESTRING object with a line for each individual (and optionally \code{splitBy} combination). -An error is returned when an individual has less than 2 relocations, making -it impossible to build a line. +Individuals (or combinations of individuals and \code{splitBy}) with less than two +relocations are dropped since it requires at least two relocations to +build a line. } \description{ -\code{build_lines} creates a \code{SpatialLines} object from a \code{data.table}. -The function accepts a \code{data.table} with relocation data, individual -identifiers a sorting column and a \code{projection}. The relocation data -is transformed into \code{SpatialLines} for each individual and optionally, -each \code{splitBy}. Relocation data should be in two columns representing -the X and Y coordinates. +\code{build_lines} generates a simple feature collection with LINESTRINGs from a +\code{data.table}. The function accepts a \code{data.table} with relocation data, +individual identifiers, a sorting column and a \code{projection}. The relocation +data is transformed into LINESTRINGs for each individual and, optionally, +combination of columns listed in \code{splitBy}. Relocation data should be in two +columns representing the X and Y coordinates. } \details{ -The \code{projection} argument expects a character string defining the EPSG -code. For example, for UTM zone 36N (EPSG 32736), the projection argument is -'EPSG:32736'. See \url{https://spatialreference.org} for a list of -EPSG codes. Please note, R spatial has followed updates to GDAL and PROJ +Please note, spatsoc has followed updates from R spatial, GDAL and PROJ for handling projections, see more at \url{https://r-spatial.org/r/2020/03/17/wkt.html}. -The \code{sortBy} is used to order the input \code{data.table} when creating -\code{SpatialLines}. It must a \code{POSIXct} to ensure the rows are sorted -by date time. - -The \code{splitBy} argument offers further control building \code{SpatialLines}. -If in your \code{DT}, you have multiple temporal groups (e.g.: years) for +In addition, \code{build_lines} previously used \link[sp:SpatialLines]{sp::SpatialLines} but has been +updated to use \link[sf:st_as_sf]{sf::st_as_sf} and \link[sf:st]{sf::st_linestring} according to the +R-spatial evolution, see more at +\url{https://r-spatial.org/r/2022/04/12/evolution.html}. A deprecated +version of this function using \link[sp:SpatialLines]{sp::SpatialLines} is retained as +\link{build_lines_sp} temporarily but users are urged to transition as soon as +possible. + +The \code{projection} argument expects a numeric or character defining the +coordinate reference system. +For example, for UTM zone 36N (EPSG 32736), the projection argument is either +\code{projection = 'EPSG:32736'} or \code{projection = 32736}. +See details in \code{\link[sf:st_crs]{sf::st_crs()}} and \url{https://spatialreference.org} +for a list of EPSG codes. + +The \code{sortBy} argument is used to order the input \code{DT} when creating +sf LINESTRINGs. It must a column in the input \code{DT} of type +POSIXct to ensure the rows are sorted by date time. + +The \code{splitBy} argument offers further control building LINESTRINGs. +If in your input \code{DT}, you have multiple temporal groups (e.g.: years) for example, you can provide the name of the column which identifies them and -build \code{SpatialLines} for each individual in each year. +build LINESTRINGs for each individual in each year. \code{build_lines} is used by \code{group_lines} for grouping overlapping -lines created from relocations. +lines generated from relocations. } \examples{ # Load data.table @@ -76,7 +89,7 @@ DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc")) DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] # EPSG code for example data -utm <- 'EPSG:32736' +utm <- 32736 # Build lines for each individual lines <- build_lines(DT, projection = utm, id = 'ID', coords = c('X', 'Y'), @@ -86,12 +99,12 @@ lines <- build_lines(DT, projection = utm, id = 'ID', coords = c('X', 'Y'), DT[, yr := year(datetime)] lines <- build_lines(DT, projection = utm, id = 'ID', coords = c('X', 'Y'), sortBy = 'datetime', splitBy = 'yr') - } \seealso{ \code{\link{group_lines}} Other Build functions: +\code{\link{build_lines_sp}()}, \code{\link{build_polys}()} } \concept{Build functions} diff --git a/man/build_lines_sp.Rd b/man/build_lines_sp.Rd new file mode 100644 index 0000000..77c0f64 --- /dev/null +++ b/man/build_lines_sp.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/build_lines_sp.R +\name{build_lines_sp} +\alias{build_lines_sp} +\title{Build Lines (deprecated version of function with retired spatial packages)} +\usage{ +build_lines_sp( + DT = NULL, + projection = NULL, + id = NULL, + coords = NULL, + sortBy = NULL, + splitBy = NULL +) +} +\arguments{ +\item{DT}{input data.table} + +\item{projection}{numeric or character defining the coordinate reference +system to be passed to \code{\link[sf:st_crs]{sf::st_crs()}}. For example, either +\code{projection = "EPSG:32736"} or \code{projection = 32736}.} + +\item{id}{Character string of ID column name} + +\item{coords}{Character vector of X coordinate and Y coordinate column names} + +\item{sortBy}{Character string of date time column(s) to sort rows by. Must +be a POSIXct.} + +\item{splitBy}{(optional) character string or vector of grouping column +name(s) upon which the grouping will be calculated} +} +\description{ +Build Lines (deprecated version of function with retired spatial packages) +} +\seealso{ +\code{\link{group_lines}} + +Other Build functions: +\code{\link{build_lines}()}, +\code{\link{build_polys}()} +} +\concept{Build functions} diff --git a/man/build_polys.Rd b/man/build_polys.Rd index 8d99a91..acf6b18 100644 --- a/man/build_polys.Rd +++ b/man/build_polys.Rd @@ -18,9 +18,9 @@ build_polys( \arguments{ \item{DT}{input data.table} -\item{projection}{character string defining the projection to be passed to -\code{sp::CRS}. For example, for UTM zone 36S (EPSG 32736), -the projection argument is 'EPSG:32736'. See details.} +\item{projection}{numeric or character defining the coordinate reference +system to be passed to \code{\link[sf:st_crs]{sf::st_crs()}}. For example, either +\code{projection = "EPSG:32736"} or \code{projection = 32736}.} \item{hrType}{type of HR estimation, either 'mcp' or 'kernel'} @@ -123,6 +123,7 @@ build_polys(spPts = pts, hrType = 'mcp', hrParams = list(percent = 95)) \code{\link{group_polys}} Other Build functions: +\code{\link{build_lines_sp}()}, \code{\link{build_lines}()} } \concept{Build functions} diff --git a/man/group_lines.Rd b/man/group_lines.Rd index dde8465..b0307ad 100644 --- a/man/group_lines.Rd +++ b/man/group_lines.Rd @@ -22,9 +22,9 @@ group_lines( \item{threshold}{The width of the buffer around the lines in the units of the projection. Supply 0 to compare intersection without buffering.} -\item{projection}{character string defining the projection to be passed to -\code{sp::CRS}. For example, for UTM zone 36S (EPSG 32736), -the projection argument is 'EPSG:32736'. See details.} +\item{projection}{numeric or character defining the coordinate reference +system to be passed to \code{\link[sf:st_crs]{sf::st_crs()}}. For example, either +\code{projection = "EPSG:32736"} or \code{projection = 32736}.} \item{id}{Character string of ID column name} diff --git a/man/group_polys.Rd b/man/group_polys.Rd index ed9a5dd..dd30f51 100644 --- a/man/group_polys.Rd +++ b/man/group_polys.Rd @@ -26,9 +26,9 @@ area and proportion of overlap (when \code{TRUE})} \item{hrParams}{a named list of parameters for \code{adehabitatHR} functions} -\item{projection}{character string defining the projection to be passed to -\code{sp::CRS}. For example, for UTM zone 36S (EPSG 32736), -the projection argument is 'EPSG:32736'. See details.} +\item{projection}{numeric or character defining the coordinate reference +system to be passed to \code{\link[sf:st_crs]{sf::st_crs()}}. For example, either +\code{projection = "EPSG:32736"} or \code{projection = 32736}.} \item{id}{Character string of ID column name} From d125c10a7c60a7c6ceccc1d69c26d91c7758df41 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 17 Aug 2023 12:14:34 -0300 Subject: [PATCH 19/19] use h2 --- R/build_lines.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/build_lines.R b/R/build_lines.R index 00642e7..1517068 100644 --- a/R/build_lines.R +++ b/R/build_lines.R @@ -8,6 +8,8 @@ #' combination of columns listed in `splitBy`. Relocation data should be in two #' columns representing the X and Y coordinates. #' +#' ## R-spatial evolution +#' #' Please note, spatsoc has followed updates from R spatial, GDAL and PROJ #' for handling projections, see more at #' \url{https://r-spatial.org/r/2020/03/17/wkt.html}. @@ -20,6 +22,8 @@ #' [build_lines_sp] temporarily but users are urged to transition as soon as #' possible. #' +#' ## Notes on arguments +#' #' The `projection` argument expects a numeric or character defining the #' coordinate reference system. #' For example, for UTM zone 36N (EPSG 32736), the projection argument is either