6 min read

Geographical coordinates conversion made easy with parzer package in R

Geographic coordinates are often in “degrees” but they can also include “minutes” and “seconds”. This gives us a variety of different formats for reporting the coordinates and you’ll see that different groups will use different formats. Computers do not really work well with DMS coordinates and we really prefer the coordinates to be in Decimal Degrees or DD.

This format allows us to perform standard decimal math operations on the coordinates. Even if you see DMS coordinates in a software package, the software will convert the coordinates to DD for calculations. Unfortunately, many people are unfamiliar with this format and you will always get data from a a colleague, and the coordinates are in DMS format that you’d like to clean up before you import them into your PC.

R has some tools that handle of coordinates formats. For instance function char2dms from sp package (Bivand, Pebesma, and Gomez-Rubio 2013) is widely used to convert coordinates stored in character to decimal degrees. However, the char2dms demands you specify the termination character for each of degree, minutes and seconds. The other package in R is biogeo (Robertson 2016), which has a dms2dd function. However, this function requires specific variable in the data frame that you ought to pass into function. Some of the variable required in the data frame include degrees, minutes, seconds and direction.

Chamberlain (2020) from ropensci team developed a parzer package that has nifty functions to handle data sets with messy geographic coordinates. In this post, I highlight the necessary step you ought to take when you receive from a friend coordinates that are in mess condition.

We need to install the package into the machine. The stable version is available in CRAN and you can simply intall the version with the code in the chunk below;

install.packages("parzer")

Once the package is installed, we need to call it into the session using the require function. we load other packages that we will use for data manipulation, mapping and plotting

require(parzer)
require(tidyverse)
require(sf)
require(leaflet)

Data

For illustration I use the dataset of coordinates recorded in Mwanza Gulf. First we load the data into the session. This dataset can also be accessed in R using the from github. Because the data is stored in Excel sheet, the function read_excel from readxl package (Wickham and Bryan 2018) is used. You must specify the sheet to read that you want to import its data. Either a string (the name of a sheet), or an integer (the position of the sheet). Ignored if the sheet is specified via range. If neither argument specifies the sheet, defaults to the first sheet. For this dataset, we need data from sheet 1, then we specify sheet = 1 in the function. The chunk below show the path of the Excell file to read and the sheet number.

coords = readxl::read_excel("./content/post/coordinates.xlsx", sheet = 1)

Table 1 is an dataset, showing the coordinates. A bird-eye view help us notice that the coordinates are stored as degree–minutes–seconds format.

Table 1: Geographical coordinates stored in degree minutes and seconds
Geographical Coordinates
Station Latitude Longitude
E1 02°34’10.0 S 32°53’49.4 E
E2 NA 32°53’48.5 E
E3 02°34’18.7 S 32°53’50.8 E
F2 02°34’09.0 S 32°52’91.6 E
F5 02°34’15.3 S 32°53’11.3 E
E-F2 02°34’23.4 S 32°53’04.8 E
G1 02°33’48.8 S 32°52’50.8 E
G2 02°33’54.6 S 32°52’55.1 E
G-F1 02°33’82.9 S 32°52’82.8 E
I1 02°33’03.5 S 32°51’22.2 E
J1 02°32’98.1 S 32°50’37.5 E
J2 02°33’04.3 S 32°50’43.8 E
J4 02°33’17.7 S 32°50’49.6 E
K2 02°33’22.7 S 32°50’23.8 E
E1 02°34’23.8 S 32°53’51.9 E
E2 02°34’20.1 S 32°53’50.8 E
EF1 02°34’13.0 S 32°53’32.3 E
F1 02°34’17.8 S 32°52’98.1 E
E1 02°34’22.9 S 32°53’41.1 E
F1 02°33’97.3 S 32°53’07.4 E
J1 02°33’14.1 S 32°50’47.9 E
K1 02°33’38.8 S 32°50’38.5 E
BB2 02°34’18.7 S 32°53’55.0 E
E1 02°34’21.2 S 32°53’47.5 E
F1 02°33’97.6 S 32°53’0.15 E
* Courtesy of TAFIRI

Degrees, Minutes, Seconds (DMS)

Degrees for DMS are divided into 60 minutes and then each minute is divided into 60 seconds. This is just like the hours on our clocks and goes back to the Babylonians who worked with a base 60 number system!

  • Each degree contains 60 minutes
  • Each minute contains 60 seconds

DMS Formats

One of the issues with DMS is that you will see them expressed in a variety of ways. This includes:

  • 02° 31’ 21" North by 32° 5’ 39" East
  • 02 31 21 N, 32 5 39 E
  • 023121N, 320539E
  • 023121, 320539

Convert coordinates to decimal degrees

Computer software will convert coordinates from DMS to DD on a regular basis without us even knowing it is happening. However, it’s good to know the equation and I routinely see spreadsheets of data in DMS and I can quickly convert the data to DD because I know the following information. First, recognize that:

  • 60 minutes = one degree
  • 60 seconds = one minute
  • 3600 seconds = one degree

Coordinates can be converted into decimal degrees in a spreadsheet using an appropriate formula shown in equation below;

\[ Decimal.Degree= Degrees + \frac{Minutes}{60} + \frac{Seconds}{3600} \]

Unfortunately, this approach is tedious particularly when you have coordinate data stored in several different formats. The parse_lon and parse_lat function remove the tedious task of converting the data into columns and then compute using the formula. These function convert the degree–minutes–seconds into decimal degree straight. The chunk below highlight the steps used to convert the coordinates into the decimal degree (Table 2) and the locations in figure 1 were mapped with leaflet package (Cheng, Karambelkar, and Xie 2018)

coords.dd = coords %>%
  mutate(Lon = parzer::parse_lon(Longitude),
         Lat = parzer::parse_lat(Latitude))
Table 2: Geographical coordinates stored in degree minutes and seconds and decimal degree
Geographical Coordinates
Degree Minutes Seconds
Decimal Degree
Station Latitude Longitude Latitude Longitude
E1 02°34’10.0 S 32°53’49.4 E 32.89706 -2.569444
E2 NA 32°53’48.5 E 32.89680 NaN
E3 02°34’18.7 S 32°53’50.8 E 32.89745 -2.571861
F2 02°34’09.0 S 32°52’91.6 E 32.89211 -2.569167
F5 02°34’15.3 S 32°53’11.3 E 32.88647 -2.570917
E-F2 02°34’23.4 S 32°53’04.8 E 32.88467 -2.573167
G1 02°33’48.8 S 32°52’50.8 E 32.88078 -2.563556
G2 02°33’54.6 S 32°52’55.1 E 32.88197 -2.565167
G-F1 02°33’82.9 S 32°52’82.8 E 32.88967 -2.573028
I1 02°33’03.5 S 32°51’22.2 E 32.85617 -2.550972
J1 02°32’98.1 S 32°50’37.5 E 32.84375 -2.560583
J2 02°33’04.3 S 32°50’43.8 E 32.84550 -2.551194
J4 02°33’17.7 S 32°50’49.6 E 32.84711 -2.554917
K2 02°33’22.7 S 32°50’23.8 E 32.83994 -2.556306
E1 02°34’23.8 S 32°53’51.9 E 32.89775 -2.573278
E2 02°34’20.1 S 32°53’50.8 E 32.89745 -2.572250
EF1 02°34’13.0 S 32°53’32.3 E 32.89231 -2.570278
F1 02°34’17.8 S 32°52’98.1 E 32.89392 -2.571611
E1 02°34’22.9 S 32°53’41.1 E 32.89475 -2.573028
F1 02°33’97.3 S 32°53’07.4 E 32.88539 -2.577028
J1 02°33’14.1 S 32°50’47.9 E 32.84664 -2.553917
K1 02°33’38.8 S 32°50’38.5 E 32.84403 -2.560778
BB2 02°34’18.7 S 32°53’55.0 E 32.89861 -2.571861
E1 02°34’21.2 S 32°53’47.5 E 32.89653 -2.572555
F1 02°33’97.6 S 32°53’0.15 E 32.88337 -2.577111
* Courtesy of TAFIRI
coords.dd %>%
  leaflet() %>%
  addTiles() %>%
  # addMarkers(lng = ~longitude.start, lat = ~latitude.start, popup = ~`Trawling number`)%>%
  addMarkers(lng = ~Lon, lat = ~Lat, popup = ~Station)

Figure 1: The map showing the positions of the sampled locations across the Mwanza Gulf recorded in 2014. To view the name of the marks on the map just click the center of the mark

References

Bivand, Roger S., Edzer Pebesma, and Virgilio Gomez-Rubio. 2013. Applied Spatial Data Analysis with R, Second Edition. Springer, NY. https://asdar-book.org/.

Chamberlain, Scott. 2020. Parzer: Parse Messy Geographic Coordinates. https://CRAN.R-project.org/package=parzer.

Cheng, Joe, Bhaskar Karambelkar, and Yihui Xie. 2018. Leaflet: Create Interactive Web Maps with the Javascript ’Leaflet’ Library. https://CRAN.R-project.org/package=leaflet.

Robertson, Mark. 2016. Biogeo: Point Data Quality Assessment and Coordinate Conversion. https://CRAN.R-project.org/package=biogeo.

Wickham, Hadley, and Jennifer Bryan. 2018. Readxl: Read Excel Files. https://CRAN.R-project.org/package=readxl.