Skip to content

Commit

Permalink
Merge pull request #93 from ropensci/fix/expected-direction-step
Browse files Browse the repository at this point in the history
Fix/test expected direction of steps
  • Loading branch information
robitalec authored Nov 26, 2024
2 parents 00a4b5a + 9725b6d commit 1ebe754
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
11 changes: 11 additions & 0 deletions R/direction_step.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions man/direction_step.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 46 additions & 7 deletions tests/testthat/test-direction-step.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
context('test direction_step')

library(spatsoc)
library(units)

DT <- fread('../testdata/DT.csv')

Expand Down Expand Up @@ -116,21 +117,59 @@ 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
)
})


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'
)
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
)
})

0 comments on commit 1ebe754

Please sign in to comment.