2017-04-26 28 views
0

プログラムRを使用してUTM NAD83ゾーン13に緯度/経度(小数点以下の桁)の位置を変換する方法を教えてもらえますか?UTMへの緯度/経度

私はこのこれまで書いてきた:

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

どちらもこれらの作業:

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) 

を私はまた、別のコードを試してみました。これはバッチの場所(75キロ以上の場所など)のためのものであることに注意してください。どんな助けでも大歓迎です!

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

これに少し注釈を付けることができますか?最初の数行の数字は何ですか? – smb1113