I have the following data set with coordinates in decimals:
ID Longitude Latitude
1 -93.2552 58.0879
1 -93.2558 58.0876
1 -93.2555 58.0873
For a previous project, I projected the coordinates using the following code:
library(rgdal)
coordinates(data) <- c("longitude", "latitude")
proj4string(data) <- CRS(" init=epsg:4326")
data <- spTransform(data, CRS(" init=epsg:5321"))
data<-as.data.frame(data)
Now, I need to re-use the original decimal coordinates. Could anyone tell me how to convert the projected coordinates back into decimal?
CodePudding user response:
You can repeat your previous steps, but switch the projections in proj4string
and spTransform
.
library(rgdal)
data_old <- data
coordinates(data_old) <- c("Longitude", "Latitude")
proj4string(data_old) <- CRS(" init=epsg:4326")
data_old <- spTransform(data_old, CRS(" init=epsg:5321"))
data_old <- as.data.frame(data_old)
# ID Longitude Latitude
# 1 1 451077.2 7117021
# 2 1 451037.6 7116991
# 3 1 451051.2 7116956
Then, you can just switch the projection in your code to revert back to the original coordinates.
data_new <- data_old
coordinates(data_new) <- c("Longitude", "Latitude")
proj4string(data_new) <- CRS(" init=epsg:5321")
data_new <- spTransform(data_new, CRS(" init=epsg:4326"))
data_new <- as.data.frame(data_new)
Output
ID Longitude Latitude
1 1 -93.2552 58.0879
2 1 -93.2558 58.0876
3 1 -93.2555 58.0873
Another option using tidyverse
and sf
could be:
library(tidyverse)
library(sf)
data_old %>%
st_as_sf(coords = c("Longitude", "Latitude"), dim = "XY") %>%
st_set_crs(5321) %>%
st_transform(4326) %>%
as.data.frame() %>%
extract(geometry,
c('Longitude', 'Latitude'),
'\\((.*), (.*)\\)',
convert = TRUE)
Data
data <-
structure(
list(
ID = c(1, 1, 1),
Longitude = c(-93.2552,-93.2558,-93.2555),
Latitude = c(58.0879, 58.0876, 58.0873)
),
class = "data.frame",
row.names = c(NA,-3L)
)