Reputation: 4278
I'm (trying to) do operations on pairs of geographical points. I have the coordinates of my points in WGS84, and I need to have them in the Lambert 2 Extended CRS (LIIE). I'm trying to do it using rgdal
.
Here's what I'm doing :
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)
The columns of matrix
are as follow : origin_id
, destination_id
, or_lon
, or_lat
, de_lon
, de_lat
. Obviously, only the last 4 columns need to be transformed from WGS84 to LIIE.
I'm able to transform the coordinates by doing this :
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)
However, I therefore lose the origin and destination IDs... (And I don't have anything left that could allow me to merge back together matrix_liie
with the origin/destination ids
in matrix_sp
).
I tried this (it's basically the same code but with destination_id
and oririgin_id
included in the first line), but I couldn't really get to something interesting (I get a Error in .local(obj, ...) : cannot derive coordinates from non-numeric matrix
error).
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)
Any idea on how I could achieve this ?
Thanks.
Sample from 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
Upvotes: 0
Views: 244
Reputation: 13866
Hi it's sp
that does the conversion, and you can do that without use SpatialPoints
, just specify which columns in matrix
are the coordinates with coordinates
, here an example :
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
EDIT : After seeing @Spacedman comment, you can effectively use SpatialPointsDataFrame
instead of 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
Upvotes: 2