简体   繁体   中英

Converting UTM coordinates to Lat/Long in R

I am trying to convert UTM coordinates (Easting and Northing) to Lat/Long using R. A sample of my data is as follows:

dx dy
-17.551 17.062
-4.947 2.336
-17.265 3.956
-12.157 -2.043

Here is my code:

    library(dplyr)
    library(ggmap)
    library(maps)
    library(rgdal)
    library(ggplot2)
    
    east<-as.numeric(ddata$dy)
    north<-as.numeric(ddata$dx)
    utm <- SpatialPoints(cbind(east,north), 
                 proj4string=CRS("+proj=utm +zone=59 +datume=WGS84 "))
    spTransform(utm, CRS("+proj=longlat +datum=WGS84"))

However, I'm not getting correct lat/long values, as the data is collected from New Zealand so the latitude, longitude values must be from this location. How can I correct this code to generate the right values of lat and long?

Here is a link to my data: https://drive.google.com/file/d/1X8pPFxV8ZBl1gAWOeYASG0BsnryovpyE/view?usp=sharing

Any help in this regard will be highly appreciated.

This may help...

Data is the first 6 rows and columns from the link provided in the question. Have used x variable as longitude and y as latitude. A search on the internet revealed a crs for New Zealand to be 2193: New Zealand Transverse Mercator which at least puts the co-ordinates in New Zealand. But you'd need to confirm that. The co-ordinates seem to be very close to each other so all points overlap at the scale plotted - I've not experimented with this.

https://www.linz.govt.nz/data/geodetic-system/datums-projections-and-heights/projections/new-zealand-transverse-mercator-2000


library(sf)
library(tmap)

df_sf <- st_as_sf(x = df,                         
                  coords = c("x", "y"),
                  crs =     2193)

data("World")
nz <-  World[World$name == "New Zealand", ]

tm_shape(nz, projection = "wgs84") +
  tm_polygons()+
  tm_grid()+
  tm_shape(df_sf, projection = "wgs84")+
  tm_dots("id", size = 0.5)

To convert UTM x, y coordinates into longitude and latitude

library(proj4)

# Find the projection proj4 description of the coordinate reference system (espg 2193)
#  I've used information from this link:
# https://spatialreference.org/ref/epsg/nzgd2000-new-zealand-transverse-mercator-2000/proj4/

proj4 <- "+proj=tmerc +lat_0=0 +lon_0=173 +k=0.9996 +x_0=1600000 +y_0=10000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs "

# From the source data extract the x, y coordinates of the UTM
x_y <- df[, 1:2]

# Transform the data
lon_lat <- project(x_y, proj4, inverse = TRUE)

# convert to a data frame
data.frame(lon = lon_lat$x, lat = lon_lat$y)

#>        lon       lat
#> 1 132.3681 -71.33355
#> 2 132.3685 -71.33371
#> 3 132.3686 -71.33375
#> 4 132.3690 -71.33391
#> 5 132.3692 -71.33402
#> 6 132.3689 -71.33392

data

df <- structure(list(id = 423:428, x = c(1455320.99774103, 1455303.44574159, 
                                         1455298.49781114, 1455281.23215399, 1455269.07477818, 1455280.83403596
), y = c(5131693.29609067, 5131710.35870728, 5131712.69526321, 
         5131716.65168218, 5131714.60800342, 5131734.86699543), date = c("2019-03-12 00:02:00", 
                                                                         "2019-03-12 00:04:49", "2019-03-12 00:07:37", "2019-03-12 00:10:24", 
                                                                         "2019-03-12 00:13:19", "2019-03-12 00:16:06"), dx = c(-17.5519994448405, 
                                                                                                                               -4.94793045334518, -17.2656571443658, -12.1573758164886, 11.7592577852774, 
                                                                                                                               16.6252572031226), dy = c(17.0626166081056, 2.33655592985451, 
                                                                                                                                                         3.95641897153109, -2.04367875494063, 20.2589920070022, -29.7507258579135
                                                                                                                               )), class = "data.frame", row.names = c(NA, 6L))

Created on 2021-04-28 by the reprex package (v2.0.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM