2016-01-22 68 views
0

我(试图)在一对地理点上执行操作。我在WGS84中有我的坐标,我需要让他们参加Lambert 2 Extended CRS(LIIE)。我正在尝试使用rgdal在R中的两个CRS之间切换(使用rgdal)

下面是我在做什么:

library("rgdal") 
library("sp") 

# Loading CRS 
WGS84<-"+proj=longlat +ellps=WGS84 +towgs84=0,0,0,-0,-0,-0,0 +no_defs" 
LIIE<-"+proj=lcc +lat_1=46.8 +lat_0=46.8 +lon_0=0 +k_0=0.99987742 +x_0=600000 +y_0=2200000 +a=6378249.2 +b=6356515 +towgs84=-168,-60,320,0,0,0,0 +pm=paris +units=m +no_defs" 

# Loading the pairs of points 
matrix<-read.table(file="file_with_coordinates.csv", header=TRUE, sep=";", stringsAsFactors = F) 

matrix列如下:origin_iddestination_idor_lonor_latde_londe_lat。显然,只有最后4列需要从WGS84转换为LIIE。

我能够通过这样变换坐标:

matrix_sp<-SpatialPoints(coords = od_matrix[,c("de_lon","de_lat","or_lon","or_lat")],proj4string = CRS(WGS84)) 
matrix_sp_liie<-spTransform(od_matrix_sp, CRSobj = CRS(LIIE)) 
matrix_liie<-data.frame(matrix_sp_liie) 

但是,我因此失去了出发地和目的地的ID ...(而且什么事,可以让我去,我没有合并在一起matrix_liieorigin/destination idsmatrix_sp)。我试过了(它基本上是相同的代码,但destination_idoririgin_id包含在第一行),但我真的无法得到一些有趣的东西(我得到一个Error in .local(obj, ...) : cannot derive coordinates from non-numeric matrix错误)。

od_matrix_sp<-SpatialPoints(coords = od_matrix[,c("destination_id","oririgin_id","de_lon","de_lat","or_lon","or_lat")],proj4string = CRS(WGS84)) 
matrix_sp_liie<-spTransform(od_matrix_sp, CRSobj = CRS(LIIE)) 
matrix_liie<-data.frame(matrix_sp_liie) 

有关我如何实现这一点的任何想法?

谢谢。从CSV


样品:

origin_id destination_id or_lon or_lat de_lon  de_lat 
123_a  005    3.88  45.6  1.56  46.7 
123_b  006    5.10  41.1  2.4  42.6 
+1

你可以给你的csv文件中的几行代码示例吗?因为我怀疑答案只是创建一个'SpatialLinesDataFrame'或者一个'SpatialPointsDataFrame'作为行数的两倍的特征,并且使用ID值来链接它们...... – Spacedman

+1

啊,是的,你是对的我没有,我想这样做,我编辑了我的答案。如果你想发布这个作为答案,我会撤回我的 – Victorp

+0

谢谢你的评论。我从CSV中添加了一个示例。我不确定你的意思是“行数为特征数的两倍,并使用ID值来链接它们”? –

回答

1

嗨它sp执行转换,你可以做,没有使用SpatialPoints,只是指定列matrix的坐标与coordinates,这里的例子:

library("sp") 
# Some coordinates 
latlong <- data.frame(
    ID = 1:8, 
    LETTERS = LETTERS[1:8], 
    lon = runif(n = 8, min = 2.0798, max = 2.9931), 
    lat = runif(n = 8, min = 48.6823, max = 49.0698) 
) 

# Loading CRS 
WGS84<-"+proj=longlat +ellps=WGS84 +towgs84=0,0,0,-0,-0,-0,0 +no_defs" 
LIIE<-"+proj=lcc +lat_1=46.8 +lat_0=46.8 +lon_0=0 +k_0=0.99987742 +x_0=600000 +y_0=2200000 +a=6378249.2 +b=6356515 +towgs84=-168,-60,320,0,0,0,0 +pm=paris +units=m +no_defs" 

# Set which var are the coord 
coordinates(latlong) <- c("lon", "lat") 
# Set the proj 
proj4string(latlong) <- CRS(WGS84) 
# Convert 
latlon_conv <- spTransform(x = latlong, CRSobj = CRS(LIIE)) 
# Back to data.frame 
latlon_conv <- as.data.frame(latlon_conv) 
latlon_conv # maybe change columns names... 
# ID LETTERS  lon  lat 
# 1 1  A 632441.1 2440172 
# 2 2  B 633736.7 2434332 
# 3 3  C 586298.5 2411320 
# 4 4  D 645107.6 2410351 
# 5 5  E 642454.6 2443052 
# 6 6  F 628371.7 2448833 
# 7 7  G 625445.7 2436324 
# 8 8  H 624509.7 2443864 

编辑:看到@Spacedman评论后,你可以有效地使用SpatialPointsDataFrame而不是SpatialPoints

latlong.sp <- SpatialPointsDataFrame(
    coords = latlong[, c("lon", "lat")], 
    data = latlong[, c("ID", "LETTERS")], 
    proj4string = CRS(WGS84) 
) 

latlon_conv <- spTransform(x = latlong.sp, CRSobj = CRS(LIIE)) 
latlon_conv.df <- as.data.frame(latlon_conv) 
latlon_conv.df 
+0

我尝试过'SpatialPointsDataFrame'解决方案,它部分工作:它转换两个点中的一个的坐标,但另一个保持在WGS84中。 –

相关问题