Elisa Seyboth
Elisa Seyboth

Reputation: 1

Convert geographical coordinates using measurements package

I am trying to convert some data with the indicated measurements package, but I'm not succeeding on it.

My data:

Long        Lat
62ᵒ36.080   58ᵒ52.940
61ᵒ28.020   54ᵒ59.940
62ᵒ07.571   56ᵒ48.873
62ᵒ04.929   57ᵒ33.605
63ᵒ01.419   60ᵒ30.349
63ᵒ09.555   61ᵒ29.199
63ᵒ43.499   61ᵒ23.590
64ᵒ34.175   62ᵒ30.304
63ᵒ16.342   59ᵒ16.437
60ᵒ55.090   54ᵒ49.269
61ᵒ28.013   54ᵒ59.928
62ᵒ07.868   56ᵒ48.040
62ᵒ04.719   57ᵒ32.120
62ᵒ36.083   58ᵒ51.766
63ᵒ01.644   60ᵒ30.714
64ᵒ33.897   62ᵒ30.772
63ᵒ43.604   61ᵒ23.426
63ᵒ09.288   61ᵒ29.888
63ᵒ16.722   59ᵒ16.204

What I'm trying:

library(measurements)

library(readxl)

coord = read.table('coord_converter.txt', header = T, stringsAsFactors = F) 

# change the degree symbol to a space
lat = gsub('°','', coord$Lat)
long = gsub('°','', coord$Long)

# convert from decimal minutes to decimal degrees
lat = measurements::conv_unit(lat, from = 'deg_dec_min', to = 'dec_deg')
long = measurements::conv_unit(long, from = 'deg_dec_min', to = 'dec_deg')

What I'm getting with this penultimate line:

Warning messages:

  1. In split(as.numeric(unlist(strsplit(x, " "))) * c(3600, 60), f = rep(1:length(x), : NAs introduced by coercion
  2. In as.numeric(unlist(strsplit(x, " "))) * c(3600, 60) : longer object length is not a multiple of shorter object length
  3. In split.default(as.numeric(unlist(strsplit(x, " "))) * c(3600, : data length is not a multiple of split variable

Can someone point my mistake or make a suggestion of how to proceed?

Thank you!

Upvotes: 0

Views: 550

Answers (1)

Chris Holbrook
Chris Holbrook

Reputation: 2636

I think the issue here was that after gsub call, degrees and minutes were not space delimited, as required by measurements::conv_unit.

For example, this works fine (for this reproducible example I also changed "ᵒ" to "°"):

library(measurements)

#read your data
txt <- 
"Long        Lat
62°36.080   58°52.940
61°28.020   54°59.940
62°07.571   56°48.873
62°04.929   57°33.605
63°01.419   60°30.349
63°09.555   61°29.199
63°43.499   61°23.590
64°34.175   62°30.304
63°16.342   59°16.437
60°55.090   54°49.269
61°28.013   54°59.928
62°07.868   56°48.040
62°04.719   57°32.120
62°36.083   58°51.766
63°01.644   60°30.714
64°33.897   62°30.772
63°43.604   61°23.426
63°09.288   61°29.888
63°16.722   59°16.204"

coord <- read.table(text = foo, header = TRUE, stringsAsFactors = F)

# change the degree symbol to a space
lat = gsub('°',' ', coord$Lat)
long = gsub('°',' ', coord$Long)

# convert from decimal minutes to decimal degrees
lat = measurements::conv_unit(lat, from = 'deg_dec_min', to = 'dec_deg')
long = measurements::conv_unit(long, from = 'deg_dec_min', to = 'dec_deg')

yields...

> cbind(long, lat)
      long               lat               
 [1,] "62.6013333333333" "58.8823333333333"
 [2,] "61.467"           "54.999"          
 [3,] "62.1261833333333" "56.81455"        
 [4,] "62.08215"         "57.5600833333333"
 [5,] "63.02365"         "60.5058166666667"
 [6,] "63.15925"         "61.48665"        
 [7,] "63.7249833333333" "61.3931666666667"
 [8,] "64.5695833333333" "62.5050666666667"
 [9,] "63.2723666666667" "59.27395"        
[10,] "60.9181666666667" "54.82115"        
[11,] "61.4668833333333" "54.9988"         
[12,] "62.1311333333333" "56.8006666666667"
[13,] "62.07865"         "57.5353333333333"
[14,] "62.6013833333333" "58.8627666666667"
[15,] "63.0274"          "60.5119"         
[16,] "64.56495"         "62.5128666666667"
[17,] "63.7267333333333" "61.3904333333333"
[18,] "63.1548"          "61.4981333333333"
[19,] "63.2787"          "59.2700666666667"

Upvotes: 1

Related Questions