Skip to content

Commit

Permalink
apply indentation_linter
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico committed Jun 25, 2023
1 parent 1afb7ea commit 92cf1e0
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 169 deletions.
2 changes: 1 addition & 1 deletion .lintr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ linters: linters_with_defaults(
expect_true_false_linter(),
expect_type_linter(),
implicit_integer_linter(allow_colon = TRUE),
indentation_linter(hanging_indent_style = "never"),
infix_spaces_linter(exclude_operators = c("=", "*", "/")),
line_length_linter(120),
quotes_linter("'"),
Expand All @@ -20,6 +21,5 @@ linters: linters_with_defaults(
)),
assignment_linter = NULL,
commented_code_linter = NULL,
indentation_linter = NULL,
object_name_linter = NULL
)
9 changes: 5 additions & 4 deletions R/gh_decode.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
gh_decode = function(geohashes, include_delta = FALSE, coord_loc = 'c') {
if (is.factor(geohashes)) {
return(lapply(gh_decode(levels(geohashes), include_delta, coord_loc),
function(z) z[geohashes]))
return(lapply(
gh_decode(levels(geohashes), include_delta, coord_loc),
function(z) z[geohashes]
))
}
if (length(coord_loc) > 1L)
stop("Please provide only one value for 'coord_loc'")
Expand All @@ -16,8 +18,7 @@ gh_decode = function(geohashes, include_delta = FALSE, coord_loc = 'c') {
'northwest' = , 'nw' = 6L,
'north' = , 'n' = 7L,
'northeast' = , 'ne' = 8L,
stop('Unrecognized coordinate location; please use ',
"'c' for centroid or a cardinal direction; see ?gh_decode")
stop("Unrecognized coordinate location; please use 'c' for centroid or a cardinal direction; see ?gh_decode")
)
.Call(Cgh_decode, geohashes, include_delta, coord_loc)
}
7 changes: 3 additions & 4 deletions R/gh_encode.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
gh_encode = function(latitude, longitude, precision = 6L) {
if (length(precision) != 1L)
stop('More than one precision value detected; precision is fixed on input (for now)')
if (precision < 1L) stop('Invalid precision. Precision is measured in ',
'number of characters, must be at least 1.')
if (precision < 1L)
stop('Invalid precision. Precision is measured in number of characters, must be at least 1.')
if (precision > .global$GH_MAX_PRECISION) {
warning('Precision is limited to ', .global$GH_MAX_PRECISION,
' characters; truncating')
warning('Precision is limited to ', .global$GH_MAX_PRECISION, ' characters; truncating')
precision = .global$GH_MAX_PRECISION
}

Expand Down
6 changes: 2 additions & 4 deletions R/gis_tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ wgs = function() sp::CRS('+proj=longlat +datum=WGS84', doCheckCRSArgs = FALSE)
# nocov start
check_suggested = function(pkg) {
if (!requireNamespace(pkg, quietly = TRUE)) {
stop('This function requires an installation of ', pkg,
"; install.packages('", pkg, "') to proceed.")
stop('This function requires an installation of ', pkg, "; install.packages('", pkg, "') to proceed.")
}
}
# nocov end
Expand Down Expand Up @@ -48,8 +47,7 @@ gh_to_spdf.default = function(geohashes, ...) {

gh_to_spdf.data.frame = function(gh_df, gh_col = 'gh', ...) {
if (is.na(idx <- match(gh_col, names(gh_df))))
stop('Searched for geohashes at a column named "',
gh_col, '", but found nothing.')
stop('Searched for geohashes at a column named "', gh_col, '", but found nothing.')
gh = gh_df[[idx]]
if (anyDuplicated(gh) > 0L) {
idx = which(duplicated(gh))
Expand Down
63 changes: 40 additions & 23 deletions tests/testthat/test-decode.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ test_that('geohash decoder works', {
tolerance = 1e-8
)
# be sure adjacent geohashes interlock
expect_identical(lapply(c('nw', 'n', 'ne'),
function(l) gh_decode('m', coord_loc = l)),
lapply(c('sw', 's', 'se'),
function(l) gh_decode('t', coord_loc = l)))
expect_identical(
lapply(c('nw', 'n', 'ne'), function(l) gh_decode('m', coord_loc = l)),
lapply(c('sw', 's', 'se'), function(l) gh_decode('t', coord_loc = l))
)

expect_error(
gh_decode(c(borobudur, neum), coord_loc = c('n', 's')),
Expand All @@ -148,28 +148,43 @@ test_that('geohash decoder works', {
)

# invalid geohash characters:
expect_error(gh_decode('a'), fixed = TRUE,
"Invalid geohash; check 'a' at index 1.")
expect_error(gh_decode(c('b', 'a')), fixed = TRUE,
"Invalid geohash; check 'a' at index 2.")
expect_error(
gh_decode('a'),
"Invalid geohash; check 'a' at index 1.",
fixed = TRUE
)
expect_error(
gh_decode(c('b', 'a')),
"Invalid geohash; check 'a' at index 2.",
fixed = TRUE
)

# missing input
expect_identical(gh_decode(c(neum, NA_character_)),
list(latitude = c(42.91259765625, NA),
longitude = c(17.60009765625, NA)))
expect_identical(gh_decode(c(neum, NA_character_), include_delta = TRUE),
list(latitude = c(42.91259765625, NA),
longitude = c(17.60009765625, NA),
delta_latitude = c(0.02197265625, NA),
delta_longitude = c(0.02197265625, NA)))
expect_identical(
gh_decode(c(neum, NA_character_)),
list(latitude = c(42.91259765625, NA), longitude = c(17.60009765625, NA))
)
expect_identical(
gh_decode(c(neum, NA_character_), include_delta = TRUE),
list(
latitude = c(42.91259765625, NA),
longitude = c(17.60009765625, NA),
delta_latitude = c(0.02197265625, NA),
delta_longitude = c(0.02197265625, NA)
)
)

# stress testing
## empty input
expect_identical(gh_decode(character(0L)),
list(latitude = numeric(0L), longitude = numeric(0L)))
expect_identical(
gh_decode(character(0L)),
list(latitude = numeric(0L), longitude = numeric(0L))
)
## !nzchar input
expect_identical(gh_decode(''),
list(latitude = NA_real_, longitude = NA_real_))
expect_identical(
gh_decode(''),
list(latitude = NA_real_, longitude = NA_real_)
)
## long input [intr_length > 8 in geohash_decode_impl]
expect_identical(
gh_decode(strrep('1', 26L)),
Expand All @@ -181,7 +196,9 @@ test_that('geohash decoder works', {
## useBytes needed a bit strangely -- that the error returns with _any_
## non-ASCII character throws off the string matching even when only
## attempting to match ASCII-only characters.
expect_error(gh_decode(rawToChar(as.raw(128L))),
fixed = TRUE, useBytes = TRUE,
'Non-ASCII character at index 1')
expect_error(
gh_decode(rawToChar(as.raw(128L))),
'Non-ASCII character at index 1',
fixed = TRUE, useBytes = TRUE
)
})
88 changes: 53 additions & 35 deletions tests/testthat/test-encode.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ test_that('geohash encoder works', {
expect_identical(gh_encode(y, x), gh_encode(y, x - 360.0))

# all level-1 centroids to be sure my manual logic for precision = 1 works
expect_identical(gh_encode(c(-67.5, -67.5, -22.5, -22.5, -67.5, -67.5, -22.5,
-22.5, 22.5, 22.5, 67.5, 67.5, 22.5, 22.5, 67.5,
67.5, -67.5, -67.5, -22.5, -22.5, -67.5, -67.5,
-22.5, -22.5, 22.5, 22.5, 67.5, 67.5, 22.5, 22.5,
67.5, 67.5),
c(-157.5, -112.5, -157.5, -112.5, -67.5, -22.5,
-67.5, -22.5, -157.5, -112.5, -157.5, -112.5,
-67.5, -22.5, -67.5, -22.5, 22.5, 67.5, 22.5,
67.5, 112.5, 157.5, 112.5, 157.5, 22.5, 67.5,
22.5, 67.5, 112.5, 157.5, 112.5, 157.5),
precision = 1L),
c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'b', 'c',
'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'))
# nolint start: line_length_linter.
expect_identical(
gh_encode(
c(-067.5, -67.5, -22.5, -22.5, -67.5, -67.5, -22.5, -22.5, 22.5, 22.5, 67.5, 67.5, 22.5, 22.5, 67.5, 67.5, -67.5, -67.5, -22.5, -22.5, -67.5, -67.5, -22.5, -22.5, 22.5, 22.5, 67.5, 67.5, 22.5, 22.5, 67.5, 67.5),
c(-157.5, -112.5, -157.5, -112.5, -67.5, -22.5, -67.5, -22.5, -157.5, -112.5, -157.5, -112.5, -67.5, -22.5, -67.5, -22.5, 22.5, 67.5, 22.5, 67.5, 112.5, 157.5, 112.5, 157.5, 22.5, 67.5, 22.5, 67.5, 112.5, 157.5, 112.5, 157.5),
precision = 1L
),
c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
)
# nolint end: line_length_linter.

# geohash cells are _left closed, right open_: [x1, x2) x [y1, y2), see:
# http://geohash.org/s000
Expand All @@ -31,23 +27,31 @@ test_that('geohash encoder works', {
# boundary cases
# need to balloon eps so that adding .5 doesn't obliterate sig figs
eps = 1000.0*.Machine$double.eps
expect_identical(gh_encode(c(eps, eps, -eps, -eps,
90.0 - eps, 90.0 - eps, eps - 90.0, eps - 90.0),
c(eps, -eps, eps, -eps,
eps - 180.0, 180.0 - eps, eps - 180.0, 180.0 - eps)),
c('s00000', 'ebpbpb', 'kpbpbp', '7zzzzz',
'bpbpbp', 'zzzzzz', '000000', 'pbpbpb'))
expect_identical(
gh_encode(
c(eps, eps, -eps, -eps, 90.0 - eps, 90.0 - eps, eps - 90.0, eps - 90.0),
c(eps, -eps, eps, -eps, eps - 180.0, 180.0 - eps, eps - 180.0, 180.0 - eps)
),
c('s00000', 'ebpbpb', 'kpbpbp', '7zzzzz', 'bpbpbp', 'zzzzzz', '000000', 'pbpbpb')
)

# test precision argument
expect_identical(gh_encode(y, x, 12L), 's0h09nrnzgqv')
# maximum precision
n = 25L
expect_identical(gh_encode(y, x, n),
substring('s0h09nrnzgqv8je0f4jpd0000', 1L, n))
expect_identical(
gh_encode(y, x, n),
substring('s0h09nrnzgqv8je0f4jpd0000', 1L, n)
)
# truncation beyond there
expect_warning(out <- gh_encode(y, x, n + 5L),
'Precision is limited', fixed = TRUE)
expect_identical(out, substring('s0h09nrnzgqv8je0f4jpd0000', 1L, n))
expect_warning(
expect_identical(
gh_encode(y, x, n + 5L),
substring('s0h09nrnzgqv8je0f4jpd0000', 1L, n)
),
'Precision is limited',
fixed = TRUE
)

# implicit integer truncation
expect_identical(gh_encode(y, x, 1.04), 's')
Expand All @@ -58,25 +62,39 @@ test_that('geohash encoder works', {
# invalid input
expect_error(gh_encode(100.0, x), 'Invalid latitude at index 1', fixed = TRUE)
expect_error(gh_encode(-91.0, x), 'Invalid latitude at index 1', fixed = TRUE)
expect_error(gh_encode(c(y, 90.0), c(x, x)),
'Invalid latitude at index 2', fixed = TRUE)
expect_error(gh_encode(y, x, c(5L, 6L)),
'More than one precision value', fixed = TRUE)
expect_error(gh_encode(c(y, y), x),
'Inputs must be the same size', fixed = TRUE)
expect_error(
gh_encode(c(y, 90.0), c(x, x)),
'Invalid latitude at index 2',
fixed = TRUE
)
expect_error(
gh_encode(y, x, c(5L, 6L)),
'More than one precision value',
fixed = TRUE
)
expect_error(
gh_encode(c(y, y), x),
'Inputs must be the same size',
fixed = TRUE
)

# semi-valid auto-corrected input -- 180 --> -180 by wrapping
expect_identical(gh_encode(y, 180.0), '80008n')
expect_identical(gh_encode(y, 293475908.0), 'db508w')

# missing/infinite input
expect_identical(gh_encode(c(y, NA), c(x, NA)), c('s0h09n', NA_character_))
expect_identical(gh_encode(c(NaN, Inf, -Inf, 1:3), c(1:3, NaN, Inf, -Inf)),
rep(NA_character_, 6L))
expect_identical(
gh_encode(c(NaN, Inf, -Inf, 1:3), c(1:3, NaN, Inf, -Inf)),
rep(NA_character_, 6L)
)

# different branch for precision=1 of the above errors
expect_error(gh_encode(100.0, x, 1L),
'Invalid latitude at index 1', fixed = TRUE)
expect_error(
gh_encode(100.0, x, 1L),
'Invalid latitude at index 1',
fixed = TRUE
)
expect_identical(gh_encode(y, 180.0, 1L), '8')
expect_identical(gh_encode(NA, NA, 1L), NA_character_)

Expand Down
Loading

0 comments on commit 92cf1e0

Please sign in to comment.