tags:

views:

72

answers:

2

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.

A: 
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).

Myrddin Emrys
You're a saver!
Eimantas
Just a quibble, but doesn't 123.12.34.56 mean 123d12m34.56s ? Where do the hours come in in WGS coordinates ? Isn't correct solution 123+12/60+34.56/3600 ?
High Performance Mark
It could well be... honestly, I'm unfamiliar with WGS... looking online, it appears you are correct. Fixing the math.
Myrddin Emrys
A: 

Hi

Emboldened by @Myrddin's near-agreement that he made a small mistake, I'm posting an answer in a blatant attempt to gather rep.

I suspect that

123.12.34.56

should be interpreted as 123d12m34.56s and converted to decimal degrees like this:

123+12/60+34.56/3600

Regards

Mark

High Performance Mark