Converting latitude and longitude points to UTM

前端 未结 5 1483
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 08:52

I found a fairly simple example of how to do this but I cant get it to work for me. I\'m pretty new to R

library(rgdal) 
xy <- cbind(c(118, 119), c(10, 50         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 09:36

    To ensure that appropriate projection metadata are at every step associated with the coordinates, I'd suggest converting the points to a SpatialPointsDataFrame object as soon as possible.

    See ?"SpatialPointsDataFrame-class" for more on how to convert simple data.frames or matrices to SpatialPointsDataFrame objects.

    library(sp)
    library(rgdal)
    
    xy <- data.frame(ID = 1:2, X = c(118, 119), Y = c(10, 50))
    coordinates(xy) <- c("X", "Y")
    proj4string(xy) <- CRS("+proj=longlat +datum=WGS84")  ## for example
    
    res <- spTransform(xy, CRS("+proj=utm +zone=51 ellps=WGS84"))
    res
    #            coordinates ID
    # 1 (-48636.65, 1109577)  1
    # 2    (213372, 5546301)  2
    
    ## For a SpatialPoints object rather than a SpatialPointsDataFrame, just do: 
    as(res, "SpatialPoints")
    # SpatialPoints:
    #              x       y
    # [1,] -48636.65 1109577
    # [2,] 213372.05 5546301
    # Coordinate Reference System (CRS) arguments: +proj=utm +zone=51
    # +ellps=WGS84 
    

提交回复
热议问题