rcoordinate-systemscoordinate-transformationr-sputm

Convert UTM to DD while keeping label column


I have a dataset with UTM coordinates and site names. I have transformed my UTM coordinates to DD using this code:

coordinates(Spatial)<-~Longitude+Latitude
sputm <- SpatialPoints(Spatial, proj4string=CRS("+proj=utm +zone=32V +datum=WGS84"))
spgeo <- spTransform(sputm, CRS("+proj=longlat +datum=WGS84"))
lnlt <- coordinates(spgeo)

However, then I loose the section name which I need to know which coordinate belongs to what section. Anyone know how I can solve this?


Solution

  • It goes like this:

    library(sp)
    d <- data.frame(x=c(0,1000), y=c(0,1000), section=1:2) 
    coordinates(d) <- ~x+y
    proj4string(d) = "+proj=utm +zone=1"
    geo <- spTransform(d, CRS("+proj=longlat +datum=WGS84"))
    as.data.frame(geo)
    #  section        x           y
    #1       1 178.5113 0.000000000
    #2       2 178.5202 0.009019487
    

    Or with "terra":

    library(terra)
    d <- data.frame(x=c(0,1000), y=c(0,1000), section=1:2) 
    v <- vect(d, geom=c("x", "y"), crs="+proj=utm +zone=1")
    g <- project(v, "+proj=longlat +datum=WGS84")
    as.data.frame(g, geom="XY")
    #  section        x           y
    #1       1 178.5113 0.000000000
    #2       2 178.5202 0.009019487