0
votes

I have a csv file with three columns in the form of value x y. I know that x and y refer to EPSG 25832. I need to transform these coordinates to EPSG 4326 since, when I call summary() on the shapefile where I want to plot the data, I get this line:

proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0]

WGS84 corresponds to EPSG 4326, right?

By reading other answers I understood that I need to use the spTransform function from the rgdal package. However I didn't find any thorough explanation of the function usage. Please help!

The documentation was quite cryptic to me (I am new to R and spatial data), so it didn't help me out.

EDIT: adding the output of dput(head(data))

structure(list(Value = c(10L, 9L, 17L, 13L, 10L, 6L), X = c(687199.0608, 
687199.0608, 687199.0608, 687199.0608, 687199.0608, 687199.0608
), Y = c(4928179.721, 4928179.721, 4928179.721, 4928179.721, 
4928179.721, 4928179.721)), .Names = c("Value", "X", "Y"), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))
1
Can you edit your question to include the first few lines of your csv file (i.e. head(csv)), ideally the output from dput(head(csv))?Phil
@Phil I have added itPigna
Btw, yes, WGS84 corresponds to EPSG4326zwep

1 Answers

3
votes

This should do the trick!

The thing is.. you need to define both coordinate systems with a CRS... and these are indeed defined by their EPSG code. Then you can just transform them with spTransform :) But this is of course only possible when you assign the right coordinate system to the data with coordinates and proj4string.

library(rgdal)
library(data.table)
d <- structure(list(Value = c(10L, 9L, 17L, 13L, 10L, 6L), X = c(687199.0608, 
687199.0608, 687199.0608, 687199.0608, 687199.0608, 687199.0608
), Y = c(4928179.721, 4928179.721, 4928179.721, 4928179.721, 
4928179.721, 4928179.721)), .Names = c("Value", "X", "Y"), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

d = as.data.table(d)
d = d[,.(X,Y)]
coordinates(d) <- c("X","Y")
proj4string(d) <- CRS("+init=epsg:25832") 
CRS.new <- CRS("+init=epsg:4326") # WGS 84
dnew <- spTransform(d, CRS.new)