R/geo.convert.R

Defines functions geo.convert

Documented in geo.convert

#' @title Decimal to DMS Coordinate Converter
#' @description Blank Description.
#' @param d Tweet Data
#' @param dn Name
#' @param HTML HTML Code
#' Defaults to TRUE.
#' @keywords package
#' @return NULL
#' @export
#' @examples geo.convert()
#' Example | df <- data.frame(coord.degrees = c("43°46′50″N 79°24′53″W"))
#' geo.convert("43°46′50″N 79°24′53″W")

geo.convert <- function(x){

if('tidyr' %in% rownames(installed.packages()) == TRUE) {
require(tidyr)} else {
install.packages("tidyr", repos = "http://cran.us.r-project.org")	
require(tidyr)}

coords <- x

df <- data.frame(coord.degrees = c(paste0(coords)))

df <- separate(df, coord.degrees, into=c("lat", "long"), sep=" ")

# change the degree symbol (°) to a space
df$lat = gsub('°', ' ', df$lat)
df$long = gsub('°', ' ', df$long)

# Convert Prime Symbol (′) to Space
df$lat = gsub('′', ' ', df$lat)
df$long = gsub('′', ' ', df$long)

# Convert Double Prime Symbol (″) to Space
df$lat = gsub('″', ' ', df$lat)
df$long = gsub('″', ' ', df$long)

# Remove all Letters
df$lat = gsub("[A-Z]*", "", df$lat)
df$long = gsub("[A-Z]*", "", df$long)

angle2dec <- function(angle) {
  angle <- as.character(angle)
  z <- do.call(rbind, strsplit(angle, split=' '))
  z <- apply(z, 1L, function(y) {
    y <- as.numeric(y)
    y[1] + y[2]/60 + y[3]/3600
  })
  return(z)
}

df <- apply(df, 2L, angle2dec)

geo.coordinates <- data.frame(lapply(df, type.convert), stringsAsFactors=FALSE)

direction.lat <- substr(coords, 10, 10)

direction.long <- substr(coords, 21, 21)

if(direction.lat == "S") {
	
# now only apply this to the numeric values
geo.coordinates$lat[sapply(geo.coordinates$lat, is.numeric)] <- geo.coordinates$lat[sapply(geo.coordinates$lat, is.numeric)] * -1	
	
} else {
	
	
}

if(direction.long == "W") {
	
# now only apply this to the numeric values
geo.coordinates$long[sapply(geo.coordinates$long, is.numeric)] <- geo.coordinates$long[sapply(geo.coordinates$long, is.numeric)] * -1	
	
} else {
	
	
}

print(noquote(paste0("", round(geo.coordinates$lat, digits = 6), " | ", round(geo.coordinates$long, digits = 6))))

}
sabalicodev/sabali documentation built on Jan. 13, 2020, 2:22 p.m.