Skip to content

Instantly share code, notes, and snippets.

@WandersonAlves
Last active October 2, 2015 20:49
Show Gist options
  • Save WandersonAlves/f5c18d14e74f580a682f to your computer and use it in GitHub Desktop.
Save WandersonAlves/f5c18d14e74f580a682f to your computer and use it in GitHub Desktop.
Convert decimal to degrees (modified from https://gist.github.com/laurynas/519740)
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