Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I convert coordinates keeping all the info from the dataframe

Tags:

r

I have a list of coordinates in Lambert78 coordinate system which I want to convert to WGS84. I want these converted coordinates added as 2 additional columns to my dataframe so I can keep labelnames. This is the dataset:

label   Ycoord    Xcoord    
AB_01   227426.9  199559.0  
AB_02   227426.9  199559.0
...

What I would like is a dataframe like this:

label   Ycoord    Xcoord    Ycoord_wgs   Xcoord_wgs
AB_01   227426.9  199559.0  5.92          58.56
AB_02   227316.9  199859.0  5.74          57.68

Converting the coordinates is no problem with something like spTransform but then I loose the connection to the labelnames. Does someone have a suggestion?

like image 604
Anneleen Rutten Avatar asked Dec 05 '25 15:12

Anneleen Rutten


1 Answers

This returns a data.frame with the desired columns by creating a SpatialPoints object and then binding a transformation of the object's coordinates to the original data.frame. However, the wgs84 coordinates are slightly off compared to what you posted, so please provide more information about the "Lambert 78" coordinate system to match the crs correctly.

library(sp)
coord_df <- data.frame(label=c('AB_01', 'AB_02'), Ycoord=c(227426.9, 227316.9), Xcoord=c(199559.0, 199859.0), stringsAsFactors = FALSE)
proj4_lambert <- '+proj=lcc +lat_1=51.16666723333334 +lat_2=49.83333389999999 +lat_0=90 +lon_0=4.367486666666666 +x_0=150000.013 +y_0=5400088.438 +ellps=intl +towgs84=-99.1,53.3,-112.5,0.419,-0.83,1.885,-1.0 +units=m +no_defs'
#see https://spatialreference.org/ref/sr-org/56/
coord_sp <- SpatialPoints(coords=coord_df[,c('Xcoord', 'Ycoord')],  proj4string = CRS(proj4_lambert))
coord_df_final <- cbind(coord_df, coordinates(spTransform(coord_sp, CRS('+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0')))[,2:1])
colnames(coord_df_final)[4:5] <- c('Ycoord_wgs', 'Xcoord_wgs')
coord_df_final
#  label   Ycoord Xcoord Ycoord_wgs Xcoord_wgs
#1 AB_01 227426.9 199559   51.35447   5.080160
#2 AB_02 227316.9 199859   51.35346   5.084451
like image 77
ThetaFC Avatar answered Dec 07 '25 05:12

ThetaFC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!