Search code examples
rubywgs84

Wrong coordinate format (wgs84) or am I missing something?


Dummy question here. I'm developing an app that fetches location-aware info. I'm getting location coordinates in following manner: lat=+aaa.bb.cc.dd&lon=+aaa.bb.cc.dd&datum=wgs84

How do I convert those coordinates into standard latitude longitude with single dot. Now it looks as if client is requesting an ip address lookup.


Solution

  • wgs84 = '123.12.34.56'
    deg, minute, second, fraction = wgs84.split(/\./).map(&:to_i)      # Ruby 1.9
    deg, minute, second, fraction = wgs84.split(/\./).map {|x| x.to_i} # Ruby 1.8
    deg += minute / 60.0 + second / 3600.0 + fraction / 360000.0
    puts deg    # => 123.20970370370371
    

    Takes the string with the wgs84 coordinate in it, splits it on the periods, converts the results from text '34' to numeric 34, and then does the division necessary to convert the minutes, seconds, and fractions of seconds to floating point adjustments added to the latitude (or longitude).