Skip to content

Commit

Permalink
Utilities::Geo.degrees_minutes_seconds_to_decimal_degrees('88.302630s…
Browse files Browse the repository at this point in the history
…') w/rspec.
  • Loading branch information
TuckerJD committed Nov 26, 2014
1 parent c022a21 commit 7ecce20
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
26 changes: 13 additions & 13 deletions lib/utilities/geo.rb
Expand Up @@ -24,19 +24,21 @@ def initialize(coordinate)

# no limit test, unless there is a letter included
def self.degrees_minutes_seconds_to_decimal_degrees(dms)
degrees = 0.0 ; minutes = 0.0 ; seconds = 0.0
dms =~ /[nsew]/i
dir = $~.to_s.upcase
# return "#{dms}: Too many letters (#{dir})" if dir.length > 1
return nil if dir.length > 1
dms.gsub!(dir, '')
degrees = 0.0; minutes = 0.0; seconds = 0.0
dms =~ /[NSEW]/i
cardinal = $~.to_s.upcase
# return "#{dms}: Too many letters (#{cardinal})" if cardinal.length > 1
return nil if cardinal.length > 1
dms.gsub!(cardinal, '')

/(?<degrees>-*\d+):(?<minutes>\d+):(?<seconds>\d+\.*\d*)/ =~ dms if dms.include? ':'
/(?<degrees>-*\d+\.\d+)/ =~ dms unless dms.include? ':' if dms.include? '.'

/(?<degrees>-*\d+):(?<minutes>\d+\.*\d*)(:(?<seconds>\d+\.*\d*))*/ =~ dms if dms.include? ':'

/(?<degrees>-*\d+)º\s*(?<minutes>\d+)'\s*(?<seconds>\d+\.*\d*)"/ =~ dms if dms.include? 'º'

degrees = degrees.to_f
case dir
case cardinal
when 'W', 'S'
sign = -1.0
else
Expand All @@ -46,9 +48,9 @@ def self.degrees_minutes_seconds_to_decimal_degrees(dms)
sign *= -1
degrees *= -1.0
end
frac = ((minutes.to_f * 60.0) + seconds.to_f) / 3600.0
dd = (degrees + frac) * sign
case dir
frac = ((minutes.to_f * 60.0) + seconds.to_f) / 3600.0
dd = (degrees + frac) * sign
case cardinal
when 'N', 'S'
limit = 90.0
else
Expand All @@ -58,6 +60,4 @@ def self.degrees_minutes_seconds_to_decimal_degrees(dms)
return nil if dd.abs > limit
dd.to_s
end


end
33 changes: 33 additions & 0 deletions spec/lib/utilities/geo_spec.rb
Expand Up @@ -32,6 +32,10 @@
specify 'with no letter' do
expect(Utilities::Geo.degrees_minutes_seconds_to_decimal_degrees('42:5:18.1')).to eq('42.08836111111111')
end

specify 'only degrees and decimal minutes' do
expect(Utilities::Geo.degrees_minutes_seconds_to_decimal_degrees('42:30.1')).to eq('42.501666666666665')
end
end

context 'a Southern latitude' do
Expand Down Expand Up @@ -102,4 +106,33 @@
end
end
end

context 'NN.NNNA or ANN.NNN' do

context 'a Northern latitude' do

specify 'with uppercase letter front' do
# pending 'fixing for degree symbol, tick and doubletick'
expect(Utilities::Geo.degrees_minutes_seconds_to_decimal_degrees('N42.18')).to eq('42.18')
end

specify 'with no letter' do
# pending 'fixing for degree symbol, tick and doubletick'
expect(Utilities::Geo.degrees_minutes_seconds_to_decimal_degrees('42.18')).to eq('42.18')
end
end

context 'a Southern latitude' do

specify 'with lowercase letter' do
# pending 'fixing for degree symbol, tick and doubletick'
expect(Utilities::Geo.degrees_minutes_seconds_to_decimal_degrees('s42.18')).to eq('-42.18')
end

specify 'with no letter' do
# pending 'fixing for degree symbol, tick and doubletick'
expect(Utilities::Geo.degrees_minutes_seconds_to_decimal_degrees('-42.18')).to eq('-42.18')
end
end
end
end

0 comments on commit 7ecce20

Please sign in to comment.