2017-04-26 5 views
0

Kann mir bitte jemand helfen, herauszufinden, wie Sie Standorte, die in lat/long (Dezimal Grad) zu UTM NAD83 Zone 13 mit Programm R konvertieren?Lat/Long zu UTM

Ich habe dies bisher geschrieben:

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) 

Ich habe auch versucht, einen anderen Code:

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) 
) 

von Weder diese Arbeit. Denken Sie daran, dies wäre für Stapelpositionen (z. B. 75k + Standorte). Jede Hilfe wird sehr geschätzt!

+0

Bitte geben Sie einige Beispieldaten für ein reproduzierbares Beispiel. Einige Informationen darüber, wie jedes dieser Codebeispiele fehlschlägt, wären hilfreich (Fehlermeldung, unerwünschte Ausgabe usw.). –

Antwort

0

Siehe Code unten: -

## 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)) 
} 
+0

können Sie dies ein wenig kommentieren? Was sind die Zahlen in den ersten Zeilen? – smb1113

Verwandte Themen