commit 6bd3ec8a9f9e425b718edb95fdc27ba267113314
parent a72e1be21886850a3cdba0066c1b7cbcc26e40f6
Author: eamoncaddigan <eamon.caddigan@gmail.com>
Date: Tue, 19 Apr 2016 14:16:19 -0400
It's useful to parse coordinates.
Diffstat:
3 files changed, 57 insertions(+), 0 deletions(-)
diff --git a/NAMESPACE b/NAMESPACE
@@ -2,3 +2,4 @@
export(distanceFromPath)
export(maxDistanceFromPath)
+export(parseCoordinates)
diff --git a/R/parseCoordinates.R b/R/parseCoordinates.R
@@ -0,0 +1,34 @@
+#' Parse funky lat/long representations.
+#'
+#' @param coord A character vector representing latitude or longitude data.
+#' @return A numeric vector of (decimal) coordinate info, negative for west and
+#' south coordinates.
+#'
+#' @details This currently only handles coordinates in the format DD-DD-DDL.
+#'
+#' @export
+parseCoordinates <- function(coord) {
+ if (!is.character(coord)) {
+ stop("Only dealing with character vectors.")
+ }
+
+ # Get the direction
+ direction <- gsub("[^NSEWnsew]", "", coord)
+ direction[is.na(direction) | nchar(direction) == 0] <- "1"
+ direction <- unname(vapply(direction, switch, numeric(1), S = -1, E = -1, 1))
+
+ # Continue parsing the coord
+ coord <- gsub("[^[:digit:][:punct:]]", "", coord)
+ combineParts <- function(x) {
+ n <- as.numeric(x)
+ if (length(n) != 3) {
+ d <- NA
+ } else {
+ d <- n[1]+n[2]/60+n[3]/3600
+ }
+ return(d)
+ }
+ coord <- direction * vapply(strsplit(coord, "[[:punct:]]"),
+ combineParts, numeric(1))
+ return(coord)
+}
diff --git a/man/parseCoordinates.Rd b/man/parseCoordinates.Rd
@@ -0,0 +1,22 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/parseCoordinates.R
+\name{parseCoordinates}
+\alias{parseCoordinates}
+\title{Parse funky lat/long representations.}
+\usage{
+parseCoordinates(coord)
+}
+\arguments{
+\item{coord}{A character vector representing latitude or longitude data.}
+}
+\value{
+A numeric vector of (decimal) coordinate info, negative for west and
+ south coordinates.
+}
+\description{
+Parse funky lat/long representations.
+}
+\details{
+This currently only handles coordinates in the format DD-DD-DDL.
+}
+