# R/LongLatToUTM.R In GeodesiCL: Geometric Geodesy Functions

#### Documented in LongLatToUTM

```#' To convert from Geographic coordinate to UTM coordinate.
#'
#' With this function it is possible to convert from Geographic coordinate to UTM coordinate. It is also possible to convert from other coordinate reference systems by selecting their EPGS number to UTM coordinate. Review notes and references.
#'
#' @param longlat_df Point name, Sexagesimal longitude and latitude as dataframe.
#' @param crs EPGS number of the new coordinate reference system to transform. DEFAULT: 4326 (WGS84)
#' @param units Select units for UTM to work. DEFAULT: 'm'
#' @param digits Number of digits the seconds are \code{\link{round}ed} to. DEFAULT: 4
#'
#' @return a list with a data.frame and leaflet map.
#'
#' @seealso \code{data.frame}
#'
#' @export
#'
#' @note create data frame of epsg codes by epsg <- rgdal::make_EPSG()
#'
#' @references https://github.com/OSGeo/PROJ & https://github.com/cran/rgdal
#'
#' @examples
#' # Point name
#' Pto <- "St1"
#'
#' # Longitude
#' g <- -71
#' m <- 18
#' s <- 44.86475
#'
#' # Value in sexagesimal
#' sexa_long <- sexagesimal(g, m, s)
#'
#' # Latitude
#' g1 <- -33
#' m1 <- 12
#' s1 <- 27.11457
#'
#' # Value in sexagesimal
#' sexa_lat <- sexagesimal(g1, m1, s1)
#'
#' # Longitude and Latitude as data.frame
#' longlat_df <- data.frame(Pto,sexa_long,sexa_lat)
#'
#' value <- LongLatToUTM(longlat_df, crs = 4326, units = 'm', digits = 4)
#' print(value)
LongLatToUTM <- function(longlat_df, crs = 4326, units = 'm', digits = 4){

df <- data.frame(long = round(as.numeric(unlist(longlat_df[,2])), digits),
lat = round(as.numeric(unlist(longlat_df[,3])), digits))
sp::coordinates(df) <- c("long", "lat")

hemisphere <- ifelse(as.numeric(unlist(longlat_df[,3])) > 0, "north", "south")
zone <- (floor((as.numeric(unlist(longlat_df[,2])) + 180) / 6) %% 60) + 1

sp::proj4string(df) <-sp::CRS(paste0("+init=epsg:",crs))

"+proj=utm +zone=", zone,
" +", hemisphere,
" +datum=WGS84",
" +units=", units,
" +no_defs +type=crs")

stop("multiple zone/hemisphere detected")

data.frame() %>% dplyr::mutate(zone_hemisphere = paste(zone,hemisphere))

value <- tibble::as_tibble(as.data.frame(cbind(unlist(longlat_df[,1]),
round(as.numeric(unlist(res[,1])), digits),
round(as.numeric(unlist(res[,2])), digits),
as.character(unlist(res[,3])))))
names(value) <- c("Pt", "East", "North", "zone_hemisphere")

map <- leaflet::leaflet(value) %>% leaflet::addTiles() %>%
as.numeric(df\$long), as.numeric(df\$lat),
label = paste("Name: ", value\$Pt, "<br>",
"East: ", as.numeric(value\$East), "<br>",
"North: ", as.numeric(value\$North), "<br>",
"zone hemisphere: ", as.character(value\$zone_hemisphere)) %>%
lapply(htmltools::HTML)) %>%
"Stamen.Terrain", "Esri.WorldStreetMap",
"Wikimedia", "CartoDB.Positron", "Esri.WorldImagery"),
position = "topleft")

methods::show(map)
return(value)
}
```

## Try the GeodesiCL package in your browser

Any scripts or data that you put into this service are public.

GeodesiCL documentation built on May 25, 2021, 5:10 p.m.