Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/normalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
27 changes: 20 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion man/validateCoords.Rd

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