Last active
October 2, 2015 20:49
-
-
Save WandersonAlves/f5c18d14e74f580a682f to your computer and use it in GitHub Desktop.
Convert decimal to degrees (modified from https://gist.github.com/laurynas/519740)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Float | |
# Convert decimal to degrees, minutes, seconds sexagesimal | |
# (used for GPS coordinates) | |
def to_sexagesimal type | |
direction = nil | |
if type == 'lat' | |
direction = self.positive? ? 'N' : 'S' | |
elsif type == 'long' | |
direction = self.positive? ? 'W' : 'E' | |
end | |
degrees = abs.floor | |
x = (abs - degrees) * 60 | |
minutes = x.floor | |
seconds = (((x - minutes) * 60) * 100).round.to_f / 100 | |
"#{degrees}°#{minutes}'#{seconds.trim}\" #{direction}" | |
end | |
def positive? | |
self >= 0 | |
end | |
def trim | |
i, f = self.to_i, self.to_f | |
i == f ? i : f | |
end | |
end | |
x = -59.9939027777778 | |
puts -3.12.to_sexagesimal 'long' | |
puts -3.109055556.to_sexagesimal 'lat' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment