From 1ad3ab35a5666969fbca9b97630f1c0255fd83a5 Mon Sep 17 00:00:00 2001 From: dgamez71 Date: Sat, 17 Mar 2018 16:35:29 +0100 Subject: [PATCH] Created new function dms2decdegrees.R to convert degrees, minutes and seconds to decimal degrees --- NAMESPACE | 1 + R/aemet_stations.R | 17 ++++++----------- R/dms2decdegrees.R | 18 ++++++++++++++++++ man/dms2decdegrees.Rd | 21 +++++++++++++++++++++ 4 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 R/dms2decdegrees.R create mode 100644 man/dms2decdegrees.Rd diff --git a/NAMESPACE b/NAMESPACE index ff8abd2..9946e5e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(aemet_stations) +export(dms2decdegrees) export(get_aemet_normalized) export(get_data) import(httr) diff --git a/R/aemet_stations.R b/R/aemet_stations.R index 303b7f3..0dd238a 100644 --- a/R/aemet_stations.R +++ b/R/aemet_stations.R @@ -14,22 +14,17 @@ #' } aemet_stations <- function(apikey) { - test <- get_data(apidest = "/api/valores/climatologicos/inventarioestaciones/todasestaciones", apikey) + stations <- get_data(apidest = "/api/valores/climatologicos/inventarioestaciones/todasestaciones", apikey) + + stations$longitud <- Vectorize(dms2decdegrees)(stations$longitud) + + df <- data.frame(t(sapply(listQ1, function(e) e))) station_id <- df[, "indicativo"] longString <- as.character(df[, "longitud"]) latString <- as.character(df[, "latitud"]) - - deg <- as.numeric(substr(longString, 0, 2)) - min <- as.numeric(substr(longString, 3,4)) - sec <- as.numeric(substr(longString, 5,6)) - x <- deg + min/60 + sec/3600 - x <- ifelse(substr(longString, 7, 8) == "W", x, -x) - deg <- as.numeric(substr(latString, 0, 2)) - min <- as.numeric(substr(latString, 3,4)) - sec <- as.numeric(substr(latString, 5,6)) - y <- deg + min/60 + sec/3600 + x <- points <- SpatialPoints(coords = cbind(-x, y), CRS("+proj=longlat")) colnames(points@coords) <- c("longitude", "latitude") dfout <- data.frame(station_id = df[, "indicativo"], diff --git a/R/dms2decdegrees.R b/R/dms2decdegrees.R new file mode 100644 index 0000000..11d88ca --- /dev/null +++ b/R/dms2decdegrees.R @@ -0,0 +1,18 @@ +#' Converts degrees, minutes and seconds to decimal degrees +#' +#' @param input character +#' +#' @return num +#' @export +#' +#' @examples +#' dms2decdegrees("055245W") +#' +dms2decdegrees <- function(input) { + deg <- as.numeric(substr(input, 0, 2)) + min <- as.numeric(substr(input, 3,4)) + sec <- as.numeric(substr(input, 5,6)) + x <- deg + min/60 + sec/3600 + x <- ifelse(substr(input, 7, 8) == "W", -x, x) + x +} \ No newline at end of file diff --git a/man/dms2decdegrees.Rd b/man/dms2decdegrees.Rd new file mode 100644 index 0000000..49f73e7 --- /dev/null +++ b/man/dms2decdegrees.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/dms2decdegrees.R +\name{dms2decdegrees} +\alias{dms2decdegrees} +\title{Converts degrees, minutes and seconds to decimal degrees} +\usage{ +dms2decdegrees(input) +} +\arguments{ +\item{input}{character} +} +\value{ +num +} +\description{ +Converts degrees, minutes and seconds to decimal degrees +} +\examples{ +dms2decdegrees("055245W") + +}