[GIS] In R: Convert from WGS84 lat/long to UTM and back again to WGS84 lat/long

coordinate systemr

I am looking for a way in R to project WGS84 latitudes and longitudes to a UTM grid and then convert the UTM coordinates back to the WGS84 latitudes and longitudes. I have found a number of ways to project to a UTM grid (mapproj::mapproject, rgdal::spTransform), but going from the grid back to latitudes and longitudes seems to be more complicated. Is there a projection function that offers the inverse?

I would like to use find the reverse of this:

newdata <-mapproject(dataset$lon, dataset$lat, projection = "lambert", parameters = c(mean(dataset$lon), mean(dataset$lat)))

Best Answer

You can move from any CRS to another CRS, below R code will work. Make sure you know the EPSG code for CRS which you are using. This is a reproducible example for converting from BNG to WGS. Vice versa is also possible.

library(raster)
t<-as.data.frame(matrix(c(531366.2,531325.7,259349.5,249798),nrow=2)) #sample coordinates
colnames(t) = c("x", "y")
t_wgs<-spTransform(SpatialPointsDataFrame(t[,c(1,2)],data = t,proj4string = crs("+init=epsg:27700")) , crs("+init=epsg:4326"))
t_wgs$lon<-t_wgs@coords[,1]
t_wgs$lat<-t_wgs@coords[,2]
View(t_wgs@data)

EPSG code for BNG is 27000 and WGS84 is 4326