7

I have a vector of DMS coordinates similar in format to the following:

coords <- c("43°46′50″N 79°24′53″W", "43°46′06″N 79°24′46″W")

and I would like to convert them into two numeric decimal vectors for latitude and longitude to eventually mark them in leaflet.

coords_lat <- c(43.780556, 43.768333)
coords_lon <- c(-79.414722, -79.412778)

My question is: Is there a function available out there in R that does this conversion automatically, or would I need to rely on regex and some outside tool to convert them myself?

Phil
  • 207
  • 1
  • 3
  • 7

3 Answers3

6

There's the char2dms function in the sp package that will convert this format, and you'll have to give it the right separator characters. Your example has some interesting separators that I don't have on my keyboard:

> coords
[1] "43°46′50″N 79°24′53″W" "43°46′06″N 79°24′46″W"

You could cut and paste the separators, or extract the separators from the string, eg:

> chd = substr(coords, 3, 3)[1]
> chm = substr(coords, 6, 6)[1]
> chs = substr(coords, 9, 9)[1]

Then convert to "DMS" objects:

> cd = char2dms(coords,chd=chd,chm=chm,chs=chs)
[1] 43d46'50"W 43d46'6"W 

Once you've got those, you can convert to numeric with:

> as.numeric(cd)
[1] -43.78056 -43.76833

To get the lat and the long you'll probably have to split your strings and then use char2dms, unless there's something else in the sp package....

Note that you can only run char2dms on a vector of longs or lats - if you mix them up it fails, interpreting them as whatever the first one is, it seems:

This is a longitude, West:

> char2dms(c("23d12'8\"W"))
[1] 23d12'8"W

Run it with a vector with a latitude and then a longitude:

> char2dms(c("23d12'8\"N","23d12'8\"W"))
[1] 23d12'8"N 23d12'8"S

returns two latitude.

Spacedman
  • 63,755
  • 5
  • 81
  • 115
3

I'm not sure if I'm doing something the wrong way but when I used char2dms I found the numeric converted value is not correct. Instead I prefer to use the measurements::convert function.

This is an example of my results:

my_coordinate_1 <- "-99d 36' 15.7\""
my_coordinate_2 <- "-99 36 15.7"
as.numeric(sp::char2dms(my_coordinate_1)) # -98.4 -- WRONG!
measurements:conv_unit(my_coordinate_2, from = "deg_min_sec", to = "dec_deg") # -99.6043611111111 -- RIGHT VALUE!

Update

Checking on other posts, my problem with the function sp::char2dms is related to the missing easting in the coordinates. The function works properly when using

my_coordinate <- "99d 36' 15.7\" W" # --99.60436 -- NOW OK!
Cris Silva
  • 31
  • 3
1

The package measurements also provides conversion between decimal degrees and DMS.

Aaron
  • 51,658
  • 28
  • 154
  • 317
  • 2
    It is always best to include an example rather than a link-only answer. – Aaron Sep 29 '20 at 17:28
  • Welcome to GIS.se! Please click 'edit' and provide information about the measurements package - links can change or stop working in the future, and best to have some actual descriptions and examples for a good answer here. – Simbamangu Oct 01 '20 at 14:26