2016-01-22 69 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中。 –

相關問題