Skip to content

Commit

Permalink
gh_decode accepts factor input (hkwi#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Dec 7, 2019
1 parent e752940 commit 022aa5c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## v0.3.1

### NEW FEATURES

1. `gh_decode` accepts and efficiently processes `factor` input by only decoding each level one time, [#17](https://github.com/MichaelChirico/geohashTools/issues/17). If you're likely to have a fair number of duplicate geohashes in your input, consider storing them as a factor or running `gh_decode(as.factor(x))` for efficiency.

### BUG FIXES

1. `gh_decode` errors early on non-ASCII input to prevent out-of-memory access, [#19](https://github.com/MichaelChirico/geohashTools/issues/19).
Expand Down
4 changes: 4 additions & 0 deletions R/gh_decode.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
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]))
}
if (length(coord_loc) > 1L)
stop("Please provide only one value for 'coord_loc'")
coord_loc = switch(
Expand Down
4 changes: 3 additions & 1 deletion man/gh_decode.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
gh_decode(geohashes, include_delta = FALSE, coord_loc = 'c')
}
\arguments{
\item{geohashes}{ \code{character} vector of input geohashes. There's no need for all inputs to be of the same precision. }
\item{geohashes}{ \code{character} or \code{factor} vector or of input geohashes. There's no need for all inputs to be of the same precision. }
\item{include_delta}{ \code{logical}; should the cell half-width delta be included in the output? }
\item{coord_loc}{ \code{character} specifying where in the cell points should be mapped to; cell centroid is mapped by default; case-insensitive. See Details. }
}
\details{
\code{coord_loc} can be the cell's center (\code{'c'} or \code{'centroid'}), or it can be any of the 8 corners (e.g. \code{'s'}/\code{'south'} for the midpoint of the southern boundary of the cell, or \code{'ne'}/\code{'northeast'} for the upper-right corner.

For \code{factor} input, decoding will be done on the levels for efficiency.
}
\value{
\code{list} with the following entries:
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-decode.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ test_that('geohash decoder works', {
list(latitude = c(-7.60528564453125, 42.91259765625),
longitude = c(110.198364257812, 17.60009765625)))

# input is factor, #17
x = gl(4L, 20L, labels = c(borobudur, akarenga, kalakuta, neum))
expect_equal(gh_decode(x), gh_decode(as.character(x)))

# option: include_delta
expect_equal(gh_decode(borobudur, include_delta = TRUE),
list(latitude = -7.60528564453125,
Expand Down

0 comments on commit 022aa5c

Please sign in to comment.