From 9f3e4379e84a4e5aad02fd79bd009ca7373341d2 Mon Sep 17 00:00:00 2001 From: Joe Cheng Date: Mon, 27 Feb 2017 23:35:49 -0800 Subject: [PATCH] Fix #390: validateCoords warns on valid polygon data --- R/normalize.R | 2 +- R/utils.R | 27 ++++++++++++++++++++------- man/validateCoords.Rd | 7 ++++++- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/R/normalize.R b/R/normalize.R index 9c3211556..28868dd6b 100644 --- a/R/normalize.R +++ b/R/normalize.R @@ -92,7 +92,7 @@ derivePolygons <- function(data, lng = NULL, lat = NULL, lng = resolveFormula(lng, data) lat = resolveFormula(lat, data) - df <- validateCoords(lng, lat, funcName) + df <- validateCoords(lng, lat, funcName, mode = "polygon") polygonData(cbind(df$lng, df$lat)) } diff --git a/R/utils.R b/R/utils.R index 5f2983c24..3b3f91a9f 100644 --- a/R/utils.R +++ b/R/utils.R @@ -263,8 +263,15 @@ makeListFun <- function(list) { #' @param lat vector with latitude values #' @param funcName Name of calling function #' @param warn A boolean. Whether to generate a warning message if there are rows with missing/invalid data +#' @param mode if \code{"point"} then warn about any \code{NA} lng/lat values; +#' if \code{"polygon"} then \code{NA} values are expected to be used as +#' polygon delimiters #' @export -validateCoords <- function(lng, lat, funcName, warn=T) { +validateCoords <- function(lng, lat, funcName, warn=TRUE, + mode = c("point", "polygon")) { + + mode <- match.arg(mode) + if (is.null(lng) && is.null(lat)) { stop(funcName, " requires non-NULL longitude/latitude values") } else if (is.null(lng)) { @@ -280,13 +287,19 @@ validateCoords <- function(lng, lat, funcName, warn=T) { } else if (!is.numeric(lat)) { stop(funcName, " requires numeric latitude values") } - complete <- ifelse( - is.na(lat) | is.null(lat) | is.na(lng) | is.null(lng) | - !is.numeric(lat) | !is.numeric(lng), - FALSE, TRUE) - if(any(!complete)) { - warning(sprintf("Data contains %s rows with either missing or invalid lat/lon values and will be ignored",sum(!complete))) + if (mode == "point") { + incomplete <- is.na(lat) | is.na(lng) + if(any(incomplete)) { + warning(sprintf("Data contains %s rows with either missing or invalid lat/lon values and will be ignored",sum(incomplete))) + } + } else if (mode == "polygon") { + incomplete <- is.na(lat) != is.na(lng) + if(any(incomplete)) { + warning(sprintf("Data contains %s rows with either missing or invalid lat/lon values and will be ignored",sum(incomplete))) + } + lng <- lng[!incomplete] + lat <- lat[!incomplete] } data.frame(lng=lng,lat=lat) diff --git a/man/validateCoords.Rd b/man/validateCoords.Rd index 9474c6c85..42711167c 100644 --- a/man/validateCoords.Rd +++ b/man/validateCoords.Rd @@ -4,7 +4,8 @@ \alias{validateCoords} \title{Utility function to check if a coordinates is valid} \usage{ -validateCoords(lng, lat, funcName, warn = T) +validateCoords(lng, lat, funcName, warn = TRUE, mode = c("point", + "polygon")) } \arguments{ \item{lng}{vector with longitude values} @@ -14,6 +15,10 @@ validateCoords(lng, lat, funcName, warn = T) \item{funcName}{Name of calling function} \item{warn}{A boolean. Whether to generate a warning message if there are rows with missing/invalid data} + +\item{mode}{if \code{"point"} then warn about any \code{NA} lng/lat values; +if \code{"polygon"} then \code{NA} values are expected to be used as +polygon delimiters} } \description{ Utility function to check if a coordinates is valid