1

I have a bunch of different types of coordinates in database:

x$lat = c("41°39'","41°; 42°; 43°", "41°39'-43°39'", '42°', "36.458", "42°-44°","36.452 - 37.254", "42°39', 43°39'")

I need to convert all of them to decimal degrees ( some of them already are) however, I also have a range of coordinates (e.g.41°39'-43°39'), in this case i need first find an average and then convert this average to decimal form. similar where i have a list of coordinates ("41°; 42°; 43°" or 42°39', 43°39')- i also first need to find an average and then put it in decimal form.

I know couple of options: for 41°39':

x$latdec = gsub('°', ' ', x$lat)

for 42° type :

x$latdec = gsub('°', x$lat)

for 36.458 just leave it as it is.

the concern is the ranges ("41°39'-43°39'"), I know that i can split the range into two parts by :

out <- strsplit(as.character(x$lat),'-') 
do.call(rbind, out)

and then convert it to decimal.

But i have no idea with the rest. and how can I incorporate it into the loop ( all of them) cause my data set is 1000 lines. Thank you so much in advance!

yuliaUU
  • 1,581
  • 2
  • 12
  • 33
  • Probably a duplicate of https://stackoverflow.com/questions/14404596/converting-geo-coordinates-from-degree-to-decimal – thelatemail Sep 06 '17 at 22:32
  • Possible duplicate of [Converting geo coordinates from degree to decimal](https://stackoverflow.com/questions/14404596/converting-geo-coordinates-from-degree-to-decimal) – HubertL Sep 06 '17 at 22:34
  • why do you need to find average before converting to decimal? – HubertL Sep 06 '17 at 22:36
  • I need to plot points on a map ( that is also then broken into raster cells, and filled with color). I dont know how to plot a range of coordinates on the raster map ( especially if my latitude is 41°39'-43°39' and longitude is 43°39' – yuliaUU Sep 06 '17 at 22:40
  • You can plot a line or a box if you have both lat and lon as ranges... but it is going to be a bit more complicated than converting to decimal as you'll have to manually handle each case – HubertL Sep 06 '17 at 22:44
  • but I need to convert them to decimals anyway if i want to plot a line or a box on the map? or i am wrong? – yuliaUU Sep 06 '17 at 22:46
  • I'm unclear on what you're asking. Please provide examples of the types you *dont* know how to convert to decimal. The ones you're referring to by "the rest". You're also referring to "the loop" - what loop? – Hack-R Sep 06 '17 at 23:54
  • if you mean you want a magic way to get this mess into a vector of clean latitudes in DD format then i don't think it exists. you can convert minutes into decimals simply by dividing them by 60. for the range you have - i'll do this one for you by hand: average of "41°39'-43°39'" will be 42°39' :) – Lukasz Sep 07 '17 at 00:21
  • yes, just got confused with the loop. i already figured it out. thanks – yuliaUU Sep 07 '17 at 00:39

1 Answers1

0

sorry my code looks very ugly, but this is what I came up with

#CONVER LAT or LON TO DECIMAL

    spec= c(1:7)
    west = c("41°39'","41°; 42°, 43°", "41°39'-43°39'", '42°', "36.458", "42°-44°","36.452 - 37.254")
    id= spec
    dff= data.frame(cbind(id, west))
    df <- dff[!(is.na(dff$west))  ,]

#subset everything that has degree sign in it

    p1 <- '°'
    p2 <- '-'
    p3 <- "'"
    p4 <- '"'
    p5 = ","


# work with coordinates that have ranges
# all that have ranges in it 

    df11 <- subset(df, grepl(p2, df$west) )

 # omit everything with comma (list of ranges)

    df11a = subset(df11, grepl(p5, df11$west)) 

#everything with comma

    df1= df11[!(df11$id %in% df11a$id),] 
    out <- strsplit(as.character(df1$west),'-')

 
# break range into two columns

    out2= data.frame(do.call(rbind, out)) 
    df1$west_lower_limit=out2$X1
    df1$west_upper_limit=out2$X2

#UPPER LIMIT
#if we have seconds

    df0_1 = subset (df1, grepl(p4, df1$west_upper_limit)) 

# were we have range- degrees and -deg+min

    df1_1 <- subset(df1, grepl(p1, df1$west_upper_limit) ) 

# where only range - decimals

    df1_2 <- df1[!(df1$id %in% df1_1$id),] 

# subset df1_1 for only degrees and deg+min
# range- degrees+ min

    df1_1a <- subset(df1_1, grepl(p3, df1_1$west_upper_limit) ) 

# convert range- deg+min into dec

    df1_1a$west_upper_limit = gsub('°', ' ', as.factor(df1_1a$west_upper_limit))
    df1_1a$west_upper_limit = gsub("'", ' ', as.factor(df1_1a$west_upper_limit))
    df1_1a$long_upper_limit = conv_unit(df1_1a$west_upper_limit, from = 'deg_dec_min', to = 'dec_deg')

# range- degrees

    df1_1b <-df1_1[!(df1_1$id %in% df1_1a$id),] 

#convert range- deg

    df1_1b$west_upper_limit = gsub('°', ' ', df1_1b$west_upper_limit)
    df1_1b$long_upper_limit = df1_1b$west_upper_limit

#convert range- decimals

    df1_2$long_upper_limit =df1_2$west_upper_limit
     
# LOWER LIMIT
# were we have range- degrees and -deg+min

    df1_1l <- subset(df1, grepl(p1, df1$west_lower_limit) ) 

# where only range - decimals
# subset df1_1 for only degrees and deg+min

    df1_2l <- df1[!(df1$id %in% df1_1$id),]

 
# range- degrees+ min

    df1_1al <- subset(df1_1l, grepl(p3, df1_1l$west_lower_limit) )

 
# range- degrees
# convert range- deg+min into dec

    df1_1bl <-df1_1[!(df1_1l$id %in% df1_1a$id),] 
    df1_1al$west_lower_limit = gsub('°', ' ', df1_1al$west_lower_limit)
    df1_1al$west_lower_limit = gsub("'", ' ', df1_1al$west_lower_limit)
    df1_1al$long_lower_limit = conv_unit(df1_1al$west_lower_limit, from = 'deg_dec_min', to = 'dec_deg')

#convert range- deg

    df1_1bl$west_lower_limit = gsub('°', ' ', df1_1bl$west_lower_limit)
    df1_1bl$long_lower_limit = df1_1bl$west_lower_limit

#convert range- decimals

    df1_2l$long_lower_limit = df1_2l$west_lower_limit

# combine all with ranges

    df1_1_dec1= rbind(df1_1a,df1_1b,df1_2) 
    df1_1_dec2= rbind(df1_1al,df1_1bl,df1_2l) 
    df1dec1 <- merge(df1_1_dec1,df1_1_dec2,by=c("id"))
    df1dec= df1dec1[,c("id","long_lower_limit","long_upper_limit")]


----------
# NO RANGES IN COORDINATES

# all that dont have range in it
 

     df2=  df[!(df$id %in% df11$id),]  

# were we have no range-  degrees and -deg+min and deg-min-sec
 

     df2_1 <- subset(df2, grepl(p1, df2$west) )

 
# were we have   no range -deg+min
  

    df2_1a <- subset(df2_1, grepl(p3, df2_1$west) )

#were we have   no range -deg+min+sec
  

    df2_1aa<- subset(df2_1a, grepl(p4, df2_1a$west) )
      df2_1aa$west = gsub('°', ' ', df2_1aa$west)
      df2_1aa$west = gsub("'", ' ', df2_1aa$west)
      df2_1aa$west = gsub('"', ' ', df2_1aa$west)
      df2_1aa$lat = conv_unit(df2_1aa$west, from = 'deg_min_sec', to = 'dec_deg')

#where no sec
  

    df2_1ab = df2_1a[!(df2_1a$id %in% df2_1aa$id),]  
      df2_1ab$west = gsub('°', ' ', df2_1ab$west)
      df2_1ab$west = gsub("'", ' ', df2_1ab$west)
      df2_1ab$lat = conv_unit(df2_1ab$west, from = 'deg_dec_min', to = 'dec_deg')

  
 #no range- deg 
  

    df2_1b <- df2_1[!(df2_1$id %in% df2_1a$id),] 
      df2_1b$west = gsub('°', ' ', df2_1b$west)
      df2_1b$lat = conv_unit(df2_1b$west, from = 'dec_deg', to = 'dec_deg')

#where no degrees present  
  

    df2_2=  df2[!(df2$id %in% df2_1$id),] 
      df2_2$lat= df2_2$west

#bind non ranges data
  

    df2_dec1= rbind(df2_1aa,df2_1ab,df2_1b, df2_2) 
      df2_decN =data.frame( "id"= df2_dec1$id, "dec_lat"=df2_dec1$lat)
yuliaUU
  • 1,581
  • 2
  • 12
  • 33