# spTransform produces unexpected results

I am struggling to plot points using WGS84 co-ordinates on a map of the UK using the OSGB 1936 / British National Grid CRS. I'm using the rgdal package in R.

Here is my reproducible example, using the co-ordinates of Buckingham Palace...

``````library(rgdal)
foo <- data.frame(lat = 51.500829, long = -0.142994)
bar <- SpatialPoints(foo)
proj4string(bar) <- CRS("+init=epsg:4326")
bar <- spTransform(bar, CRS("+init=epsg:27700"))
coordinates(bar)
``````

This outputs the following transformed co-ordinates:

``````         lat     long
[1,] 7464126 -5554500
``````

Where I should be getting latitude of 179622 and longitude of 528993.

Can anyone tell me what I'm doing wrong? The only similar problem I could find was here and I'm now pretty confident I am choosing the right CRSs. I'm trying to do something similar to this post but again, I don't think the solutions there can help me.

P.S. I'm using this site to check what co-ordinates I should be getting.

• technically they are eastings and northings after the transform not lat/lon May 31, 2018 at 14:40

Short answer: x is longitude and y is latitude. (For geographic coordinates. East and north for projected coordinates)

Solution:

``````library(rgdal)
foo <- data.frame(y = 51.500829, x = -0.142994) # lat == y, long == x
coordinates(foo) <- ~x+y
proj4string(foo) <- CRS("+init=epsg:4326")
bar <- spTransform(foo, CRS("+init=epsg:27700"))
coordinates(bar)
##          x        y
## 1 528992.9 179622.5
``````
• The core problem is that `SpatialPoints(foo)` ignores the names on the data frame and takes the first column as the "X" coordinate (longitude) and the second column as "Y" (latitude). May 31, 2018 at 18:20
• @Spacedman yes, indeed isn't explicit the order of input coordinates May 31, 2018 at 18:39