Class: GeoPosition::Conversion::Dms

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_position/conversion/dms.rb

Overview

This is the main class that will perform the conversion from Degrees, Minutes, and Seconds

Examples:

conversion = GeoPosition::Conversion::Dms.new(12,3,42.2,'w')

conversion.to_s
=> 12 deg 3' 42.2" W

conversion.to_f
=> -12.061783333333333

Constant Summary collapse

ALLOWED_SECONDS =
(0.0..60.0)
ALLOWED_DEGREES =
(0.0..180.0)
ALLOWED_DIRECTIONS =
%w( N n E e S s W w )
MINUTES_CONVERSION =
60
SECONDS_CONVERSION =
3600

Instance Method Summary collapse

Constructor Details

#initialize(degrees, minutes, seconds, direction) ⇒ void

Creates a new instance of the DMS conversion object

Parameters:

  • degrees (String, Integer)
  • minutes (String, Integer)
  • seconds (String, Integer)
  • direction (String)

    The direction

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/geo_position/conversion/dms.rb', line 30

def initialize(degrees, minutes, seconds, direction)
  raise GeoPosition::Error::InvalidDirectionError.new("Please provided a direction of N, S, E, or W") unless valid_direction?(direction)
  raise GeoPosition::Error::InvalidFloatError.new("Arguments could not be coerced to a float") unless valid_floats?([degrees, minutes, seconds])
  raise GeoPosition::Error::InvalidDegreesError.new("Degrees must be between 0 and 180. %s was provided" % [degrees]) unless valid_degrees?(degrees)
  raise GeoPosition::Error::InvalidMinutesError.new("Minutes must be between 0 and 60. %s was provided" % [minutes]) unless valid_minutes?(minutes)
  raise GeoPosition::Error::InvalidSecondsError.new("Seconds must be between 0 and 60. %s was provided" % [seconds]) unless valid_seconds?(seconds)

  @degrees = degrees
  @minutes = minutes
  @seconds = seconds

  @direction = direction
end

Instance Method Details

#degreesFloat

Returns the coerced degrees

Returns:

  • (Float)

    Positive float of the provided degrees



47
48
49
# File 'lib/geo_position/conversion/dms.rb', line 47

def degrees
  @degrees.to_f.abs
end

#directionString

Returns the uppercased direction

Returns:

  • (String)

    Uppercase direction



68
69
70
# File 'lib/geo_position/conversion/dms.rb', line 68

def direction
  @direction.to_s[0,1].upcase
end

#minutesFloat

Returns the coerced minutes

Returns:

  • (Float)


54
55
56
# File 'lib/geo_position/conversion/dms.rb', line 54

def minutes
  @minutes.to_f
end

#secondsFloat

Returns the coerced seconds

Returns:

  • (Float)


61
62
63
# File 'lib/geo_position/conversion/dms.rb', line 61

def seconds
  @seconds.to_f
end

#to_fFloat

Returns the converted DMS to a float for use with latitude/longitude

Returns:

  • (Float)


82
83
84
# File 'lib/geo_position/conversion/dms.rb', line 82

def to_f
  convert!
end

#to_sString

Returns the formatted string

Returns:

  • (String)


75
76
77
# File 'lib/geo_position/conversion/dms.rb', line 75

def to_s
  "%s deg %s' %s\" %s" % [self.degrees.to_i, self.minutes.to_i, self.seconds, self.direction]
end