Lat / Long到UTM

时间:2017-04-26 22:23:42

标签: r

任何人都可以帮我弄清楚如何使用程序R将纬度/经度(十进制度)的位置转换为UTM NAD83 Zone 13?

到目前为止我写了这个:

install.packages("sp")
install.packages("rgdal")

library(sp)
library(rgdal)

setwd("I:/R/latlong_utm/")
locs=read.table("Location_Database.txt", head=TRUE)

locs=subset(locs, select=c(number, LongWGS84, LatWGS84))
head(locs)


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

x=LongLatToUTM(locs$LongWGS84, locs$LatWGS84, ID=locs$number, 13)
x$number = x$ID

locs=merge(locs, x, by="number")
head(locs)
plot(locs$X, locs$Y)

我也尝试了不同的代码:

require("PBSmapping") || install.packages("PBSmapping", dependencies=TRUE) 
&& require("PBSmapping")
setwd("I:/R/latlong_utm/")
locs <- read.csv("Location_Database.csv", head=TRUE)

Location_Database.csv <- commandArgs(TRUE)[1]


coord <- read.csv("Location_Database.csv", head=TRUE)
attr(coord, "projection") <- "LL"
attr(coord, "zone") <- 13
coord.utm <- convUL(coord)
output <-  sub(pattern = "Location_Database.csv", replacement = ".utm.csv",  
x = "Location_Database.csv")


write.csv(coord.utm, file = 
        "C:/Users/utmoutput.csv", 
      row.names = FALSE)

tail(meter.data)
)

这些都不奏效。请记住,这将适用于批次位置(例如,75k +位置)。任何帮助深表感谢!

1 个答案:

答案 0 :(得分:0)

见下面的代码: -

## Sample Data frame. x and y columns represent long and lat respectively. 
## df can be any data frame that you want to reproject
df <- data.frame(x = c(-89.6, -89.9, -89.8), y = c(34.1, 33.2, 33), ID = c(1, 2, 3))

long2UTM <- function(long) {
  ## Function to get the UTM zone for a given longitude
  (floor((long + 180)/6) %% 60) + 1
}


LongLatToUTM <- function(df){
    ## Args: df, data frame must have x and y columns. Should be from same UTM zone.
    ## Create a spatial dataframe
    coordinates(df) <- ~x+y
    proj4string(df) <- CRS("+proj=longlat +datum=WGS84")  

    ## Get zones for all the points in the data frame. 
    ## Stop if more than one zone is present. 
    ## You can write your own code for handling cases where your 
    ## data comes from different UTM zones.

    zone <- long2UTM(df$x)
    if (length(unique(zone)) > 1) stop("values from different UTM zones")
    zone <- unique(zone)

    ## Change CRS of the spatial data frame and convert to data frame
    res <- spTransform(df, CRS(paste0("+proj=utm +zone=", zone, "+datum=WGS84")))
    return(as.data.frame(res))
}