# Convert shapefile to LonLat (R)

Just a small disclaimer: I don't have any background in cartography, hence I might have missed the answer to my question in search just because I didn't even know the keywords to look for.

So, I am using R to plot statistical data on maps. It was all fine and understandable when all of the materials came in same units of measurments (lon/lat in form of decimal degrees or degrees/minutes/seconds which have obvious and easy conversion).

So what I have encountered - doing routine `fortify(shapeMap)` to plot data with ggplot2 yields dataframe with values:

``````    long    lat order   hole    piece   group   id
1   563656.6    373826.0    1   FALSE   1   0.1 0
2   563864.6    373664.3    2   FALSE   1   0.1 0
3   564272.9    373895.4    3   FALSE   1   0.1 0
4   564491.6    373805.5    4   FALSE   1   0.1 0
5   565151.1    372709.0    5   FALSE   1   0.1 0
6   565328.3    372347.0    6   FALSE   1   0.1 0

...
``````

As far as I understand that this is UTM coordinate system. And hence the question - is there an easy way to convert these coordinates to decimal degrees? I have dug through quite a few materials and online convertors but I can't really seem to get the expected result.

Just for reference - shapefile coordinates above should be somewhere in Latvia. And Latvia is split among 3 UTM grid cells: 34V; 35V and 35U (if that is relevant at all).

Update: I have found out that coordinates are in LKS92 system. Hence now there is a clear objective. Convert LKS92 to WGS84. Guess now it is up to figuring out `spTransform()` function from `rgdal` package. Unfortunately it is not an obvious function with "from" and "to" arguments. Or at least at first glance.

Update2: Reference for those that will encounter the same problem:

1. You have to be sure what coordinate system you want to conver from/into. In my case I found out it was LKS92.

2. Find specific parameters for the coordinate system on spatialreference.org. In my case parameters look like "+proj=tmerc +lat_0=0 +lon_0=24 +k=0.9996 +x_0=500000 +y_0=-6000000 +ellps=GRS80 +units=m +no_defs". Not that these are parameters for coordinate system FROM which I want to convert. If this is not WGS84 then you will want another parameter string for target coordinate system.

3. R package proj4 has function project(). An example of LKS92 -> WGS84 conversion:

```xy <- cbind(c(508148.442), c(319423.294)) project(xy, "+proj=tmerc +lat_0=0 +lon_0=24 +k=0.9996 +x_0=500000 +y_0=-6000000 +ellps=GRS80 +units=m +no_defs", inverse = T)``` ``` [,1] [,2] [1,] 24.1342 57.01823 ```

Note the `inverse=T` which means convert to WGS84. Making `inverse = F` suggests that input coordinates are in WGS84 format.

Another small update:

To convert whole shapefile you would still need `rgdal` package:

``````proj4string(shapeMap) <- CRS("+proj=tmerc +lat_0=0 +lon_0=24 +k=0.9996 +x_0=500000 +y_0=-6000000 +ellps=GRS80 +units=m +no_defs")
shapeMap <- spTransform(shapeMap, CRS("+proj=longlat"))
``````

First line defines the CRS of shapefile, second line tells what CRS to transform into.

• – BradHards Feb 12 '15 at 8:59
• I have come across this doc while searching. It requires thorough knowledge of the subject... understanding arguments of `spTransform()` from `rgdal` package alone would be quite challenging. – statespace Feb 12 '15 at 9:13

Use proj4 package for R. There is a function `project()`:
``````project(xy, proj, inverse = FALSE, ellps.default="sphere")
You need to use it with option `inverse = TRUE` in order to project from other projection to lat/long. And notice that documentation for this package might be ambiguous: seems that you should pass long/lat coordinates not lat/long.