From afa9480c52715443936db291f9fc4ee16c1dbc1c Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 22 Nov 2024 16:38:03 -0400 Subject: [PATCH 01/10] add example result for E N W S --- R/direction_step.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/R/direction_step.R b/R/direction_step.R index 37458140..36ac7e92 100644 --- a/R/direction_step.R +++ b/R/direction_step.R @@ -71,6 +71,17 @@ #' coords = c('X', 'Y'), #' projection = 32736 #' ) +#' +#' # Example result for East, North, West, South steps +#' example <- data.table( +#' X = c(0, 5, 5, 0, 0), +#' Y = c(0, 0, 5, 5, 0), +#' step = c('E', 'N', 'W', 'S', NA), +#' ID = 'A' +#' ) +#' +#' direction_step(example, 'ID', c('X', 'Y'), projection = 4326) +#' example[, .(direction, units::set_units(direction, 'degree'))] direction_step <- function( DT = NULL, id = NULL, From 38ef9d3bd46f962b16c81d4a00462cd2f8b8c14d Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 22 Nov 2024 16:38:37 -0400 Subject: [PATCH 02/10] use same column names --- tests/testthat/test-direction-step.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/testthat/test-direction-step.R b/tests/testthat/test-direction-step.R index ef49f257..c0ac72df 100644 --- a/tests/testthat/test-direction-step.R +++ b/tests/testthat/test-direction-step.R @@ -116,21 +116,21 @@ test_that('splitBy returns expected', { DT_A <- data.table( - x = c(-5, -5, 0, 14, 10, 0), - y = c(5, 3, 1, 1, 11, 11), - id = 'A' + X = c(-5, -5, 0, 14, 10, 0), + Y = c(5, 3, 1, 1, 11, 11), + ID = 'A' )[, timegroup := seq.int(.N)] # Related to: PR 92 test_that('longlat NA radian returned', { expect_equal( - direction_step(DT_A, id = 'id', coords = c('x', 'y'), - projection = 4326)[]$direction |> class(), + class(direction_step(DT_A, id = id, coords = coords, + projection = 4326)[]$direction), 'units' ) expect_gte( - sum(is.na(direction_step(DT_A, id = 'id', coords = c('x', 'y'), - projection = 4326)[]$direction)), + sum(is.na(direction_step(DT_A, id = id, coords = coords, + projection = 4326)$direction)), 1 ) }) From 1a15192a8664f3dfcfffc8e159c0541b8298a4c1 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 22 Nov 2024 16:38:50 -0400 Subject: [PATCH 03/10] expected east north west south direction --- tests/testthat/test-direction-step.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/testthat/test-direction-step.R b/tests/testthat/test-direction-step.R index c0ac72df..da4f42ef 100644 --- a/tests/testthat/test-direction-step.R +++ b/tests/testthat/test-direction-step.R @@ -134,3 +134,12 @@ test_that('longlat NA radian returned', { 1 ) }) + + +DT_B <- data.table( + X = c(0, 5, 5, 0, 0), + Y = c(0, 0, 5, 5, 0), + step = c('E', 'N', 'W', 'S', NA), + timegroup = seq.int(5), + ID = 'B' +) From 4998d2f2f7f822a2f91c483c04e1803f76f82166 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 22 Nov 2024 16:38:57 -0400 Subject: [PATCH 04/10] calculate and check degrees --- tests/testthat/test-direction-step.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testthat/test-direction-step.R b/tests/testthat/test-direction-step.R index da4f42ef..505fe7a8 100644 --- a/tests/testthat/test-direction-step.R +++ b/tests/testthat/test-direction-step.R @@ -143,3 +143,5 @@ DT_B <- data.table( timegroup = seq.int(5), ID = 'B' ) +direction_step(DT_B, id, coords, projection = 4326) +DT_B[, deg := units::set_units(direction, 'degree')] From 51f52d584a3aa7074cc2644f6f57a757f058e7f6 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 22 Nov 2024 16:39:07 -0400 Subject: [PATCH 05/10] plot with text for dir and timegroup --- tests/testthat/test-direction-step.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/testthat/test-direction-step.R b/tests/testthat/test-direction-step.R index 505fe7a8..db6a8f1b 100644 --- a/tests/testthat/test-direction-step.R +++ b/tests/testthat/test-direction-step.R @@ -145,3 +145,7 @@ DT_B <- data.table( ) direction_step(DT_B, id, coords, projection = 4326) DT_B[, deg := units::set_units(direction, 'degree')] + +plot(DT_B$X, DT_B$Y) +text(DT_B$X + c(0.3, -0.3, -0.3, 0.3, NA), DT_B$Y, + paste0('t ', DT_B$timegroup, ', dir ', round(DT_B$direction, 2))) From a50267748122f73361386a6b0108603e26d0f572 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 22 Nov 2024 16:39:13 -0400 Subject: [PATCH 06/10] sf check --- tests/testthat/test-direction-step.R | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/testthat/test-direction-step.R b/tests/testthat/test-direction-step.R index db6a8f1b..129c3af6 100644 --- a/tests/testthat/test-direction-step.R +++ b/tests/testthat/test-direction-step.R @@ -149,3 +149,11 @@ DT_B[, deg := units::set_units(direction, 'degree')] plot(DT_B$X, DT_B$Y) text(DT_B$X + c(0.3, -0.3, -0.3, 0.3, NA), DT_B$Y, paste0('t ', DT_B$timegroup, ', dir ', round(DT_B$direction, 2))) + +pts <- st_sfc(st_point(c(0, 0)), + st_point(c(5, 0)), + st_point(c(5, 5)), + st_point(c(0, 5)), + st_point(c(0, 0)), + crs = 4326) +st_geod_azimuth(pts) From f8937a16a5f5463d9c4a6d1358170898ec30bd12 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 22 Nov 2024 16:41:31 -0400 Subject: [PATCH 07/10] tidy --- tests/testthat/test-direction-step.R | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/tests/testthat/test-direction-step.R b/tests/testthat/test-direction-step.R index 129c3af6..06402cf2 100644 --- a/tests/testthat/test-direction-step.R +++ b/tests/testthat/test-direction-step.R @@ -144,16 +144,3 @@ DT_B <- data.table( ID = 'B' ) direction_step(DT_B, id, coords, projection = 4326) -DT_B[, deg := units::set_units(direction, 'degree')] - -plot(DT_B$X, DT_B$Y) -text(DT_B$X + c(0.3, -0.3, -0.3, 0.3, NA), DT_B$Y, - paste0('t ', DT_B$timegroup, ', dir ', round(DT_B$direction, 2))) - -pts <- st_sfc(st_point(c(0, 0)), - st_point(c(5, 0)), - st_point(c(5, 5)), - st_point(c(0, 5)), - st_point(c(0, 0)), - crs = 4326) -st_geod_azimuth(pts) From 25a6d4a3f3bbf91cf05c3f9ecaef6ab2eaec1e9a Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 22 Nov 2024 16:41:41 -0400 Subject: [PATCH 08/10] test expected results --- tests/testthat/test-direction-step.R | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/testthat/test-direction-step.R b/tests/testthat/test-direction-step.R index 06402cf2..45e2e741 100644 --- a/tests/testthat/test-direction-step.R +++ b/tests/testthat/test-direction-step.R @@ -144,3 +144,31 @@ DT_B <- data.table( ID = 'B' ) direction_step(DT_B, id, coords, projection = 4326) + +test_that('East North West South steps', { + tolerance <- 0.01 + + expect_equal( + DT_B[step == 'E', direction], + as_units(pi / 2, 'rad'), + tolerance = tolerance + ) + + expect_equal( + DT_B[step == 'N', direction], + as_units(0), + tolerance = tolerance + ) + + expect_equal( + DT_B[step == 'W', direction], + as_units(- pi / 2), + tolerance = tolerance + ) + + expect_equal( + DT_B[step == 'S', direction], + as_units(pi), + tolerance = tolerance + ) +}) From 0b42ce81e97acfdfa37adfde806cdd6d1d37e0bf Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 22 Nov 2024 16:48:19 -0400 Subject: [PATCH 09/10] fix missing library(units) --- tests/testthat/test-direction-step.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/test-direction-step.R b/tests/testthat/test-direction-step.R index 45e2e741..82d2ca20 100644 --- a/tests/testthat/test-direction-step.R +++ b/tests/testthat/test-direction-step.R @@ -2,6 +2,7 @@ context('test direction_step') library(spatsoc) +library(units) DT <- fread('../testdata/DT.csv') From 9725b6dc6afadd20e92ee37acfd052c788e42aef Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 22 Nov 2024 16:48:32 -0400 Subject: [PATCH 10/10] man --- man/direction_step.Rd | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/man/direction_step.Rd b/man/direction_step.Rd index aaa64f30..bfb5d1b4 100644 --- a/man/direction_step.Rd +++ b/man/direction_step.Rd @@ -94,6 +94,17 @@ direction_step( coords = c('X', 'Y'), projection = 32736 ) + +# Example result for East, North, West, South steps +example <- data.table( + X = c(0, 5, 5, 0, 0), + Y = c(0, 0, 5, 5, 0), + step = c('E', 'N', 'W', 'S', NA), + ID = 'A' +) + +direction_step(example, 'ID', c('X', 'Y'), projection = 4326) +example[, .(direction, units::set_units(direction, 'degree'))] } \seealso{ \code{\link[amt:steps]{amt::direction_abs()}}, \code{\link[geosphere:bearing]{geosphere::bearing()}}