# Converting DMS to coordinates with +/- signs

I am looking to convert Degrees/Minutes/Seconds coordinate data into decimals for plotting. However, I am unfamiliar with the science behind the +/- signs given by the longitudinal coordinates.

I have tried the package `parzer` that converts the DMS into decimals for coordinate plotting, however they return nothing when vectorized into a Shapefile. I am guessing that there is a different way of calculating the coordinates for this?

example data:

``````     Latitude  Longitude
1   +52:25:12 -001:49:48
2   +54:21:00 -006:39:00
3   +55:19:00 -003:12:00
4   +51:45:40 -001:15:50
5   +53:45:21 -000:21:23
6   +58:27:00 -003:05:00
7   +60:08:00 -001:11:00
8   +58:57:00 -002:54:00
9   +58:12:49 -006:19:03
10  +53:15:00 -004:32:00
11  +53:09:58 -000:31:26
12  +50:13:00 -005:19:00
13  +57:39:00 -003:34:12
14  +57:10:48 -002:12:00
15  +56:27:00 -003:04:12

structure(list(Latitude = c("+52:25:12", "+54:21:00", "+55:19:00",
"+51:45:40", "+53:45:21", "+58:27:00", "+60:08:00", "+58:57:00",
"+58:12:49", "+53:15:00", "+53:09:58", "+50:13:00", "+57:39:00",
"+57:10:48", "+56:27:00"), Longitude = c("-001:49:48", "-006:39:00",
"-003:12:00", "-001:15:50", "-000:21:23", "-003:05:00", "-001:11:00",
"-002:54:00", "-006:19:03", "-004:32:00", "-000:31:26", "-005:19:00",
"-003:34:12", "-002:12:00", "-003:04:12")), row.names = c(NA,
15L), class = "data.frame")

``````

Wow, that is a terrible DMS format. We will need to perform some string manipulation before we can get to DD.

``````dms <- data.frame(Latitude = c("+52:25:12", "+54:21:00", "+55:19:00",
"+51:45:40", "+53:45:21", "+58:27:00", "+60:08:00", "+58:57:00",
"+58:12:49", "+53:15:00", "+53:09:58", "+50:13:00", "+57:39:00",
"+57:10:48", "+56:27:00"), Longitude = c("-001:49:48", "-006:39:00",
"-003:12:00", "-001:15:50", "-000:21:23", "-003:05:00", "-001:11:00",
"-002:54:00", "-006:19:03", "-004:32:00", "-000:31:26", "-005:19:00",
"-003:34:12", "-002:12:00", "-003:04:12"))
``````

Now, let's write a function that will convert DMS to DD.

``````dms2dd <- function(dd, mm, ss) {
return( (as.numeric(dd) + ((as.numeric(mm) * 60) +
(as.numeric(ss)))/3600) )
}
``````

In this case, since it cannot be parsed as numeric, we need to get rid of the "+" character. However, in cases where the Latitude value is negative, or worse, mixed with "+", you would need to write an if statement to not remove the character. Let's take a quick look at how to manipulate the strings.

First, we can split by ":". The result is a list containing vectors of the split strings.

``````( x <- strsplit(dms\$Latitude, ":") )
``````

Further, we can manipulate each vector. In this case we will take the first observation and first value and subset out the "+" character. For this data, this does not need to be done for

``````x[[1]][1]
substr(x[[1]][1], 2, 3)
``````

We can apply this syntax in a iteration, using `lapply`, to split the vectors and check for the "+". We create two list with the new values.

``````( lat <- lapply(strsplit(dms\$Latitude, ":"), FUN=function(x) {
if(substr(x[1], 1,1) == "+") {
c(substr(x[1], 2,3), x[2:3])
} else { x } } ) )
( long <- lapply(strsplit(dms\$Longitude, ":"), FUN=function(x) {
if(substr(x[1], 1,1) == "+") {
c(substr(x[1], 2,3), x[2:3])
} else { x } } ) )

``````

Finally, we pass the manipulated character vectors to our `dms2dd` function to return the DD values. The use of `unlist` simply coerces the list to a vector.

``````( lat <- unlist(lapply(lat, FUN=function(x) {
dms2dd(x[1], x[2], x[3]) } )) )
( long <- unlist(lapply(long, FUN=function(x) {
dms2dd(x[1], x[2], x[3]) } )) )

plot(long, lat, pch=20)
``````
• I still get the problem of having no points when rasterising the following `xy` coordinates onto a UK map, given that these coordinates are within the UK. Why might this be? I had thought it could be a problem based on the size of points but even at `pch = 1000000` nothing appears. Jun 25, 2021 at 15:21
• @Stackbeans you are asking two different questions here and the second is not at all clear. I have addressed the issue of converting this forma into DD. Your `pch` value is not the problem because that controls symbol type and not size, which is `cex`. Have you converted the data to a spatial object, what do you mean by "rasterizing", which has nothing to do with point overlay? I encourage you to open a second question regarding your overlay issue and clarify what you are after and what you have already tried (with code). Jun 25, 2021 at 16:22
• I have uploaded my question as requested here, perhaps I have approached it the wrong way? Jun 25, 2021 at 16:49

ArcMap has a tool for converting between different notations called unsurprisingly Convert Coordinate Notation. It has a useful subsection in its help file explaining the differences between the various notations have a read of that?