Col*_*lin 13 r coordinates rgdal
我找到了一个如何做到这一点的相当简单的例子,但我无法让它为我工作.我是R的新手
library(rgdal)
xy <- cbind(c(118, 119), c(10, 50))
project(xy, "+proj=utm +zone=51 ellps=WGS84")
[,1] [,2]
[1,] -48636.65 1109577
[2,] 213372.05 5546301
Run Code Online (Sandbox Code Playgroud)
但这是示例数字.我有成千上万的坐标需要转换,我无法弄清楚如何将它们从我的表中转移到这个脚本中
我的数据集有3列,ID,X和Y.如何使用这个等式转换它们?我已经坚持了几个星期
Jos*_*ien 24
为了确保在与坐标相关的每个步骤中都有适当的投影元数据,我建议SpatialPointsDataFrame尽快将这些点转换为对象.
有关?"SpatialPointsDataFrame-class"如何将简单data.frames或矩阵转换为SpatialPointsDataFrame对象的更多信息,请参阅参考资料.
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
Run Code Online (Sandbox Code Playgroud)
Sta*_*lav 13
将纬度和经度点转换为UTM
library(sp)
library(rgdal)
#Function
LongLatToUTM<-function(x,y,zone){
xy <- data.frame(ID = 1:length(x), X = x, Y = y)
coordinates(xy) <- c("X", "Y")
proj4string(xy) <- CRS("+proj=longlat +datum=WGS84") ## for example
res <- spTransform(xy, CRS(paste("+proj=utm +zone=",zone," ellps=WGS84",sep='')))
return(as.data.frame(res))
}
# Example
x<-c( -94.99729,-94.99726,-94.99457,-94.99458,-94.99729)
y<-c( 29.17112, 29.17107, 29.17273, 29.17278, 29.17112)
LongLatToUTM(x,y,15)
Run Code Online (Sandbox Code Playgroud)
在您的问题中,您不清楚您是否已经将数据集读入 data.frame 或矩阵。所以我假设在下面你有一个文本文件中的数据集:
# read in data
dataset = read.table("dataset.txt", header=T)
# ... or use example data
dataset = read.table(text="ID X Y
1 118 10
2 119 50
3 100 12
4 101 12", header=T, sep=" ")
# create a matrix from columns X & Y and use project as in the question
project(as.matrix(dataset[,c("X","Y")]), "+proj=utm +zone=51 ellps=WGS84")
# [,1] [,2]
# [1,] -48636.65 1109577
# [2,] 213372.05 5546301
# ...
Run Code Online (Sandbox Code Playgroud)
更新:
评论表明问题来自应用project()到data.frame。project()不适用于 data.frames,因为它会检查is.numeric(). 因此,您需要像上面的示例一样将数据转换为矩阵。如果您想坚持使用您的代码,cbind()您必须执行以下操作:
X <- dd[,"X"]
Y <- dd[,"Y"]
xy <- cbind(X,Y)
Run Code Online (Sandbox Code Playgroud)
dd["X"]和之间的区别在于dd[,"X"]后者不会返回 data.frame,因此cbind()将产生矩阵而不是 data.frame。
| 归档时间: |
|
| 查看次数: |
42910 次 |
| 最近记录: |