0

I use the method from Stanislav in this topic of Forum, which is a question about "converting latitude and longitude points to UTM". I edited the function reversely to change points from UTM to WGS84, which is:

library(sp); library(rgdal)

#Function
UTMToLongLat<-function(x,y,zone){
  xy <- data.frame(ID = 1:length(x), X = x, Y = y)
  coordinates(xy) <- c("X", "Y")
  proj4string(xy) <- CRS(paste("+proj=utm +zone=",zone," ellps=WGS84",sep=''))  
  res <- spTransform(xy, CRS("+proj=longlat +datum=WGS84"))
  return(as.data.frame(res))
}

The example in the previous question mentioned above is tried, that is:

x2 <- c(-48636.65, 1109577); y2 <- c(213372.05, 5546301)

What is expected is (118, 10), (119, 50) in WGS84. Colin's example is in UTM51.

So, the following sentence is used:

done2 <- UTMToLongLat(x2,y2,51)

However, it produced: (118.0729, 1.92326), (131.4686, 49.75866).

What is wrong? By the way, how to control the decimal digits of the output?

T X
  • 505
  • 1
  • 9
  • 19

1 Answers1

0

First, you mistook the expression of the coordinate. It should be:

x <- c(-48636.65, 213372.05)
y <- c(1109577, 5546301)

In the function, it will be transformed and stored as:

> data.frame(ID = 1:length(x), X = x, Y = y)
#   ID         X       Y
# 1  1 -48636.65 1109577
# 2  2 213372.05 5546301

And execute your function again:

> UTMToLongLat(x, y, 51)
#   ID   X         Y
# 1  1 118  9.999997
# 2  2 119 50.000001

To control the decimal digits:

> round(UTMToLongLat(x, y, 51))
#   ID   X  Y
# 1  1 118 10
# 2  2 119 50
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51