Permalink
Browse files

fixed remaining issues with DMS and coord_mode

  • Loading branch information...
1 parent 526173f commit 3ea542854805b7d11ffaddc53e46dd49b4b53c1c @kristianmandrup committed Jun 21, 2011
View
@@ -1 +1 @@
-0.7.5
+0.7.6
@@ -5,27 +5,30 @@ def to_lat_lng
end
def to_lng_lat
- to_lat_lng.reverse
+ [to_lng, to_lat]
end
# Assumes by default that the order is lat, lng
# If GeoPoint is defined, can reverse this order depending on #coord_mode class variable
def to_lat
raise "Array must contain at least one element to return the latitude" if empty?
- if defined?(GeoPoint) && GeoPoint.respond_to?(:coord_mode) && GeoPoint.coord_mode == :lng_lat
- self[1].to_lat
- else
- first.to_lat
- end
+ subject, other = (defined?(GeoPoint) && GeoPoint.respond_to?(:coord_mode) && GeoPoint.coord_mode == :lng_lat) ? [self[1], first] : [first, self[1]]
+
+ begin
+ subject.to_lat
+ rescue GeoDirectionMisMatch
+ other.to_lat
+ end
end
# see(#to_lat)
def to_lng
raise "Array must contain at least two elements to return the longitude" if !self[1]
- if defined?(GeoPoint) && GeoPoint.respond_to?(:coord_mode) && GeoPoint.coord_mode == :lng_lat
- first.to_lng
- else
- self[1].to_lng
+ subject, other = (defined?(GeoPoint) && GeoPoint.respond_to?(:coord_mode) && GeoPoint.coord_mode == :lng_lat) ? [first, self[1]] : [self[1], first]
+ begin
+ subject.to_lng
+ rescue GeoDirectionMisMatch
+ other.to_lng
end
end
@@ -1,3 +1,5 @@
+class GeoDirectionMisMatch < StandardError; end;
+
class String
def to_rad
parse_dms.to_rad
@@ -19,20 +21,22 @@ def to_lat_lng
def to_lng_lat
a = geo_clean.split(',')
- a = (a.last.is_a?(String) && a.last =~ /[N|S]$/) ? a.reverse : a
+ a = (a.last.is_a?(String) && a.last =~ /[N|S]$/) ? a.reverse : a
a.to_lng_lat
end
def to_lat
raise "An empty String has no latitude" if self.empty?
s = geo_clean
+ raise GeoDirectionMisMatch, "Direction E and W signify longitude and thus can't be converted to latitude, was: #{self}" if s =~ /[W|E]$/
s = s.parse_dms if s.respond_to? :parse_dms
s.to_f.to_lat
end
def to_lng
raise "An empty String has no latitude" if self.empty?
- s = geo_clean
+ s = geo_clean
+ raise GeoDirectionMisMatch, "Direction N and S signify latitude and thus can't be converted to longitude, was: #{self}" if s =~ /[N|S]$/
s = s.parse_dms if s.respond_to? :parse_dms
s.to_f.to_lng
end
@@ -1,17 +1,10 @@
require 'spec_helper'
-# require 'geo_point'
class GeoPoint
mattr_accessor :coord_mode
end
describe 'GeoPoint.coord_mode' do
- context 'coord_mode is nil (default is :lat_lng)' do
- before do
- GeoPoint.coord_mode = nil
- end
- end
-
# make shared example for default mode!
context 'coord_mode is :lat_lng' do
before do
@@ -43,15 +36,13 @@ class GeoPoint
describe 'String' do
describe '#to_lat_lng' do
it 'should return Array in same order' do
- @str = "4, 3"
- @str.to_lat_lng.should == [4, 3]
+ "4, 3".to_lat_lng.should == [4, 3]
end
end
describe '#to_lng_lat' do
it 'should return reversed Array' do
- @str = "4, 3"
- @str.to_lng_lat.should == [3,4]
+ "4, 3".to_lng_lat.should == [3,4]
end
end
end # String
@@ -87,15 +78,13 @@ class GeoPoint
describe 'String' do
describe '#to_lng_lat' do
it 'should return Array in same order' do
- @str = "4, 3"
- @str.to_lng_lat.should == [4, 3]
+ "4, 3".to_lng_lat.should == [4, 3]
end
end
describe '#to_lat_lng' do
it 'should return reversed Array' do
- @str = "4, 3"
- @str.to_lat_lng.should == [3,4]
+ "4, 3".to_lat_lng.should == [3,4]
end
end
end # String
@@ -0,0 +1,54 @@
+require 'spec_helper'
+# require 'geo_point'
+
+class GeoPoint
+ mattr_accessor :coord_mode
+end
+
+describe 'GeoPoint.coord_mode' do
+ # make shared example for default mode!
+ context 'coord_mode is :lng_lat' do
+ before do
+ GeoPoint.coord_mode = :lng_lat
+ end
+
+ after do
+ GeoPoint.coord_mode = :lat_lng
+ end
+
+ describe 'DMS Array' do
+ it 'should convert to (lng, lat) floats' do
+ arr = ["58 38 38N", "003 04 12W"].to_lng_lat
+ arr.first.should < 4
+ arr.last.should > 58
+ end
+
+ it 'should convert to (lng, lat) floats' do
+ arr = ["003 04 12W", "58 38 38N"].to_lng_lat
+ arr.first.should < 4
+ arr.last.should > 58
+ end
+ end
+ end
+
+ context 'coord_mode is :lat_lng' do
+ before do
+ GeoPoint.coord_mode = :lat_lng
+ end
+
+ describe 'DMS Array' do
+ it 'should convert to (lat, lng) floats' do
+ arr = ["58 38 38N", "003 04 12W"].to_lng_lat
+ arr.first.should < 4
+ arr.last.should > 58
+ end
+
+ it 'should convert to (lng, lat) floats' do
+ arr = ["003 04 12W", "58 38 38N"].to_lng_lat
+ arr.first.should < 4
+ arr.last.should > 58
+ end
+ end
+ end
+end
+
@@ -19,6 +19,11 @@
@str = "50 03 59N"
@str.to_lat.should be_within(0.4).of(50)
end
+
+ it 'should not allow conversion of longitude format to latitude' do
+ str = "50 03 59E"
+ lambda { str.to_lat}.should raise_error GeoDirectionMisMatch
+ end
end
describe '#to_lng' do
@@ -29,12 +34,17 @@
it 'should return latitude' do
@str = "4"
- @str.to_lat.should == 4
+ @str.to_lng.should == 4
end
it 'should convert to latitude' do
@str = "50 03 59E"
- @str.to_lat.should be_within(0.4).of(50)
+ @str.to_lng.should be_within(0.4).of(50)
+ end
+
+ it 'should not allow conversion of latitude format to longitude' do
+ str = "50 03 59N"
+ lambda { str.to_lng}.should raise_error GeoDirectionMisMatch
end
end

0 comments on commit 3ea5428

Please sign in to comment.