Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed more lat_lng conversion bugs

  • Loading branch information...
commit b7f7bc5b312665d3ef7834dde731805d6c7105b9 1 parent b6c77b1
@kristianmandrup authored
View
2  Gemfile
@@ -9,7 +9,7 @@ gem 'activesupport', '>= 3.0.1'
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
- gem "geo_point", ">= 0.2.2"
+ gem "geo_point", "~> 0.2.3"
gem "rspec", ">= 2.5.0"
gem "bundler", ">= 1"
gem "jeweler", ">= 1.5.2"
View
15 lib/geo_calc/extensions/array.rb
@@ -8,14 +8,25 @@ def to_lng_lat
to_lat_lng.reverse
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?
- first.to_lat
+ if defined?(GeoPoint) && GeoPoint.respond_to?(:coord_mode) && GeoPoint.coord_mode == :lng_lat
+ self[1].to_lat
+ else
+ first.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]
- self[1].to_lng
+ if defined?(GeoPoint) && GeoPoint.respond_to?(:coord_mode) && GeoPoint.coord_mode == :lng_lat
+ first.to_lng
+ else
+ self[1].to_lng
+ end
end
def trim
View
8 lib/geo_calc/extensions/string.rb
@@ -12,11 +12,15 @@ def geo_clean
end
def to_lat_lng
- geo_clean.split(',').to_lat_lng
+ a = geo_clean.split(',').map(&:strip)
+ a = (a.last.is_a?(String) && a.last =~ /[N|S]$/) ? a.reverse : a
+ a.to_lat_lng
end
def to_lng_lat
- geo_clean.split(',').to_lng_lat
+ a = geo_clean.split(',')
+ a = (a.last.is_a?(String) && a.last =~ /[N|S]$/) ? a.reverse : a
+ a.to_lng_lat
end
def to_lat
View
68 spec/geo_calc/core_ext/array_ext_spec.rb
@@ -1,39 +1,75 @@
require 'spec_helper'
# - www.movable-type.co.uk/scripts/latlong.html
-describe GeoPoint do
+describe GeoCalc do
describe 'ruby core Class extensions' do
describe 'Array extension' do
describe '#to_lat' do
- it 'should return latitude as first element' do
- @arr = [4, 27]
- @arr.to_lat.should == 4
+ it 'should return latitude as first element' do
+ [4, 27].to_lat.should == 4
end
it 'should return latitude as #to_lng of first element' do
- @arr = ["4.1", 27]
- @arr.to_lat.should == 4.1
+ ["4.1", 27].to_lat.should == 4.1
end
end
describe '#to_lng' do
it 'should return latitude degree value for 360' do
- @arr = [4, 27]
- @arr.to_lng.should == 27
+ [4, 27].to_lng.should == 27
end
-
- it 'should return latitude as #to_lng of first element' do
- @arr = [4, "27.2"]
- @arr.to_lng.should == 27.2
+
+ it 'should return latitude as #to_lng of first element' do
+ [4, "27.2"].to_lng.should == 27.2
end
end
-
+
describe '#to_lat_lng' do
it 'should return Array with lat, lng' do
- @arr = ["3", {:lng => "2"}]
- @arr.to_lat_lng.should == [3, 2]
+ ["3", {:lng => "2"}].to_lat_lng.should == [3, 2]
+ end
+ end
+
+ describe '#to_lng_lat' do
+ it 'should return Array with lat, lng' do
+ ["3", {:lng => "2"}].to_lng_lat.should == [2, 3]
end
end
end # Array
end
-end
+end
+
+class GeoPoint
+ mattr_accessor :coord_mode
+end
+
+describe 'coord mode' do
+ context 'coord_mode = :lng_lat' do
+ before do
+ GeoPoint.coord_mode = :lng_lat
+ end
+
+ it 'should not reverse array' do
+ [2, 3].to_lng_lat.should == [2, 3]
+ end
+
+ it 'should reverse array' do
+ [2, 3].to_lat_lng.should == [3, 2]
+ end
+ end
+
+ context 'coord_mode = :lat_lng' do
+ before do
+ GeoPoint.coord_mode = :lat_lng
+ end
+
+ it 'should not reverse array' do
+ [2, 3].to_lat_lng.should == [2, 3]
+ end
+
+ it 'should reverse array' do
+ [2, 3].to_lng_lat.should == [3, 2]
+ end
+ end
+end
+
View
60 spec/geo_calc/core_ext/string_ext_spec.rb
@@ -44,6 +44,22 @@
@str.to_lat_lng.should == [4, 3]
end
+ it 'should return Array with lat, lng' do
+ @str = "04 38 08 W, 58 38 08 N"
+ arr = @str.to_lat_lng
+ # W = west is longitude and should be last!
+ arr.last.should < 0
+ arr.first.should > 58
+ end
+
+ it 'should return Array with lng, lat' do
+ @str = "58 38 08 N, 04 38 08 W"
+ arr = @str.to_lat_lng
+ # W = west is longitude and should be last!
+ arr.last.should < 0
+ arr.first.should > 58
+ end
+
it 'should raise error if only latitude' do
@str = "4"
lambda { @str.to_lat_lng}.should raise_error
@@ -64,6 +80,50 @@
lambda { @str.to_lat_lng}.should raise_error
end
end
+
+ describe '#to_lng_lat' do
+ it 'should return Array with lng, lat' do
+ @str = "4, 3"
+ @str.to_lng_lat.should == [3, 4]
+ end
+
+ it 'should return Array with lng, lat' do
+ @str = "04 38 08 W, 58 38 08 N"
+ arr = @str.to_lng_lat
+ # W = west is longitude and should be first!
+ arr.first.should < 5
+ arr.last.should > 58
+ end
+
+ it 'should return Array with lng, lat' do
+ @str = "58 38 08 N, 04 38 08 W"
+ arr = @str.to_lng_lat
+ # W = west is longitude and should be first!
+ arr.first.should < 5
+ arr.last.should > 58
+ end
+
+ it 'should raise error if only latitude' do
+ @str = "4"
+ lambda { @str.to_lng_lat}.should raise_error
+ end
+
+ it 'should raise error if "," but only latitude' do
+ @str = "4,"
+ lambda { @str.to_lng_lat}.should raise_error
+ end
+
+ it 'should raise error if "," in bad position' do
+ @str = ", 4 3"
+ lambda { @str.to_lng_lat}.should raise_error
+ end
+
+ it 'should raise error if not using "," as seperator' do
+ @str = "4; 3"
+ lambda { @str.to_lng_lat}.should raise_error
+ end
+ end
+
end # String
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.