Skip to content

Commit

Permalink
Merge pull request #57 from ecohealthalliance/feature/gps_nas
Browse files Browse the repository at this point in the history
Feature/gps nas
  • Loading branch information
collinschwantes authored Sep 13, 2024
2 parents 397e9c6 + fe0db31 commit 1113e40
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ohcleandat
Type: Package
Title: One Health Data Cleaning and Quality Checking Package
Version: 0.3.10
Version: 0.3.11
Authors@R: c(
person("Collin", "Schwantes", email = "schwantes@ecohealthalliance.org", role = c("cre", "aut"), comment = c(ORCID = "0000-0003-4014-4896")),
person("Johana", "Teigen", email = "teigen@ecohealthalliance.org", role = "aut", comment = c(ORCID = "0000-0002-6209-2321")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# ohcleandat 0.3.11

* obfuscate gps can now handle NAs

# ohcleandat 0.3.10

* datapackage.json can be pruned to more closely follow structural metadata for
Expand Down
27 changes: 24 additions & 3 deletions R/obfuscate_gps.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
#' gps_data_180$lon |>
#' obfuscate_gps(fuzz = 1, type = "lon")
#'
#' ### working NA GPS data
#' gps_data_180 <- data.frame(lat = c(2, 3, 4),
#' lon = c(179.39595, NA, -178.09999901))
#' gps_data_180$lon |>
#' obfuscate_gps(fuzz = 1, type = "lon")
#'
#' ### GPS is on the fritz!
#' \dontrun{
#' gps_data_fritz <- data.frame(lat = c(91, -91, 90),
Expand All @@ -62,6 +68,11 @@
obfuscate_gps <- function(x, precision = 2, fuzz = 0.125, type = c("lat","lon"),
func = min, ...){


if(!is.numeric(x)){
stop("x must be numeric")
}

## max precision in your data
# find value in x with most decimal points
data_precision <- get_precision(x,func = func,...)
Expand Down Expand Up @@ -122,14 +133,19 @@ obfuscate_lat <- function(x, precision = 2, fuzz = 0.125){
stop("fuzz greater than range of latitude on earth")
}

if(any(x > 90 | x < -90)){
range_check <- stats::na.omit(x > 90 | x < -90)

if(any(range_check)){
stop("Latitude is outside the range of latitude on earth")
}

points <- obfuscate_point(x,precision,fuzz)

# make sure point is between 90 and -90
points_in_range <- purrr::map_dbl(points,function(point){
if(is.na(point)){
return(point)
}
while(all(point > 90 | point < -90)){
point <- obfuscate_point(point,precision,fuzz)
}
Expand All @@ -154,15 +170,18 @@ obfuscate_lon <- function(x, precision = 2, fuzz = 0.125){
stop("fuzz greater than range of longitude on earth")
}

if(any(x > 180 | x < -180)){
range_check <- stats::na.omit(x > 180 | x < -180)
if(any(range_check)){
stop("Longitude is outside the range of longitude on earth ")
}

points <- obfuscate_point(x,precision,fuzz)

### wrap points near the 180th meridian
points_in_range <- purrr::map_dbl(points,function(point){

if(is.na(point)){
return(point)
}
# if point greater than 180, wrap
if(point > 180){
difference <- point - 180
Expand Down Expand Up @@ -199,6 +218,8 @@ obfuscate_lon <- function(x, precision = 2, fuzz = 0.125){
#'
#'
get_precision <- function(x,func = c,...) {
# drop NAs
x <- stats::na.omit(x)
# number of characters with the decimal - number of characters without it
precision <- 10^-(nchar(gsub("\\.", "", as.character(x))) - nchar(as.character(trunc(x))))
out <- func(precision,...)
Expand Down
6 changes: 6 additions & 0 deletions man/obfuscate_gps.Rd

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

0 comments on commit 1113e40

Please sign in to comment.