Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

class methods specs

  • Loading branch information...
commit 07fe3ce40cd8529cf691f0d880a01179fc8ffa5a 1 parent 309a0a6
Kristian Mandrup authored
23 lib/geo_calc/geo_point.rb
View
@@ -1,6 +1,7 @@
require 'sugar-high/arguments'
require 'geo_calc/calc'
require 'geo_calc/extensions'
+require 'geo_calc/geo_point/shared'
# Sample usage:
# p1 = GeoPoint.new(51.5136, -0.0983)
@@ -9,26 +10,24 @@
# brng = p1.bearing_to(p2) # in degrees clockwise from north
# ... etc
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- #
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Note that minimal error checking is performed in this example code!
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class GeoPoint
include GeoCalc::Calc::All
- # Creates a point on the earth's surface at the supplied latitude / longitude
- #
- # Constructor
- # - Numeric lat: latitude in numeric degrees
- # - Numeric lon: longitude in numeric degrees
- # - Numeric [rad=6371]: radius of earth if different value is required from standard 6,371km
- autoload :ClassMethods, 'geo_calc/geo_point/class_methods'
autoload :Shared, 'geo_calc/geo_point/shared'
+ autoload :ClassMethods, 'geo_calc/geo_point/class_methods'
autoload :CoreExtension, 'geo_calc/geo_point/core_extension'
attr_reader :lat, :lon
-
+
+ # Creates a point on the earth's surface at the supplied latitude / longitude
+ #
+ # - Numeric latitude in numeric degrees
+ # - Numeric longitude in numeric degrees
+
+ # Optional options
+ # - :radius - earth radius in km
+ # - :mode - coordinates mode, either :lng_lat or :lat_lng, otherwise uses global setting as per GeoPoint.coord_mode
def initialize *args
options = args.is_a?(GeoPoint) ? {} : args.last_option
earth_radius_km = options[:radius]
2  lib/geo_calc/geo_point/class_methods.rb
View
@@ -1,3 +1,5 @@
+require 'geo_calc/geo_point/shared'
+
class GeoPoint
module ClassMethods
def earth_radius_km
4 lib/geo_calc/geo_point/shared.rb
View
@@ -16,12 +16,14 @@ def coord_mode= mode
protected
+ include GeoCalc::NumericCheckExt
+
def valid_earth_radius? radius_km
is_numeric?(radius_km) && radius_km.is_between?(6350, 6380)
end
def valid_mode? mode
- [:lng_lat, :lat_lng].inlude? mode
+ [:lng_lat, :lat_lng].include? mode
end
end
end
31 spec/geo_calc/geo_point/class_methods_spec.rb
View
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+# - www.movable-type.co.uk/scripts/latlong.html
+describe GeoPoint do
+ describe 'Class methods' do
+ describe '#coord_mode' do
+ it 'should change global coordinates mode' do
+ GeoPoint.coord_mode = :lng_lat
+ GeoPoint.coord_mode.should == :lng_lat
+
+ GeoPoint.coord_mode = :lat_lng
+ GeoPoint.coord_mode.should == :lat_lng
+ end
+
+ it 'shoould not allow setting invalid coord mode' do
+ lambda { GeoPoint.coord_mode = :blip }.should raise_error
+ end
+ end
+
+ describe '#earth_radius_km' do
+ it 'should change global earth_radius_km' do
+ GeoPoint.earth_radius_km = 6360
+ GeoPoint.earth_radius_km.should == 6360
+ end
+
+ it 'shoould not allow setting invalid earth radius' do
+ lambda { GeoPoint.earth_radius_km = 6100 }.should raise_error
+ end
+ end
+ end
+end
148 spec/geo_calc/geo_point/initializer_spec.rb
View
@@ -0,0 +1,148 @@
+require 'spec_helper'
+
+# - www.movable-type.co.uk/scripts/latlong.html
+describe GeoPoint do
+ describe '#initializer' do
+ describe '1 argument' do
+ describe 'single String' do
+ describe '50.1, 5.0 ' do
+ it 'should create a GeoPoint' do
+ p1 = GeoPoint.new "50.1, 5.0"
+ p1.should be_a(GeoPoint)
+ p1.lat.should == 50.1
+ p1.lon.should == 5.0
+ p1.unit.should == :degrees
+ p1.earth_radius_km.should == 6371
+ end
+ end
+
+ describe '(50.1, 5.0)' do
+ it 'should create a GeoPoint' do
+ p1 = GeoPoint.new "(50.1, 5.2)"
+ p1.should be_a(GeoPoint)
+ p1.lat.should == 50.1
+ p1.lon.should == 5.2
+ p1.unit.should == :degrees
+ p1.earth_radius_km.should == 6371
+ end
+ end
+
+ describe '58 38 38N, 003 04 12W' do
+ it 'should create a GeoPoint' do
+ p1 = GeoPoint.new "58 38 38N, 003 04 12W"
+ p1.should be_a(GeoPoint)
+ p1.lat.should be_within(0.5).of(58.38)
+ p1.lon.should be_within(0.5).of(-3)
+ p1.unit.should == :degrees
+ p1.earth_radius_km.should == 6371
+ end
+ end
+
+ describe '(58 38 38N, 003 04 12W)' do
+ it 'should create a GeoPoint' do
+ p1 = GeoPoint.new "(58 38 38N, 003 04 12W)"
+ p1.should be_a(GeoPoint)
+ p1.lat.should be_within(0.5).of(58.38)
+ p1.lon.should be_within(0.5).of(-3) # W is negative, then normalize
+ p1.unit.should == :degrees
+ p1.earth_radius_km.should == 6371
+ end
+ end
+ end
+
+ describe 'single Array' do
+ describe '2 Fixed numbers: 50,5 ' do
+ it 'should create a GeoPoint' do
+ p1 = GeoPoint.new [50, 5]
+ p1.should be_a(GeoPoint)
+ p1.lat.should == 50
+ p1.lon.should == 5
+ p1.unit.should == :degrees
+ p1.earth_radius_km.should == 6371
+ end
+ end
+
+ describe '2 Float numbers: 50.1, 5.0 ' do
+ it 'should create a GeoPoint' do
+ p1 = GeoPoint.new [50.1, 5.0]
+ p1.should be_a(GeoPoint)
+ p1.lat.should == 50.1
+ p1.lon.should == 5.0
+ p1.unit.should == :degrees
+ p1.earth_radius_km.should == 6371
+ end
+ end
+
+ describe 'single Hash' do
+ describe 'with: {:lat => 50.1, :lng => 5.1}' do
+ it 'should create a GeoPoint' do
+ p1 = GeoPoint.new :lat => 50.1, :lng => 5.1
+ p1.should be_a(GeoPoint)
+ p1.lat.should == 50.1
+ p1.lon.should == 5.1
+ p1.unit.should == :degrees
+ p1.earth_radius_km.should == 6371
+ end
+ end
+
+ describe 'with: {:lat => 50.1, :long => 5.1}' do
+ it 'should create a GeoPoint' do
+ p1 = GeoPoint.new :lat => 50.1, :long => 5.1
+ p1.should be_a(GeoPoint)
+ p1.lat.should == 50.1
+ p1.lon.should == 5.1
+ p1.unit.should == :degrees
+ p1.earth_radius_km.should == 6371
+ end
+ end
+
+ describe 'with: {:latitude => 50.1, :longitude => 5.1}' do
+ it 'should create a GeoPoint' do
+ p1 = GeoPoint.new :latitude => 50.1, :longitude => 5.1
+ p1.should be_a(GeoPoint)
+ p1.lat.should == 50.1
+ p1.lon.should == 5.1
+ p1.unit.should == :degrees
+ p1.earth_radius_km.should == 6371
+ end
+ end
+ end
+ end
+ end
+
+ describe 'with 2 arguments' do
+ describe '2 Fixed numbers (Fixnum)' do
+ it 'should create a GeoPoint' do
+ p1 = GeoPoint.new 50, 5
+ p1.should be_a(GeoPoint)
+ p1.lat.should == 50
+ p1.lon.should == 5
+ p1.unit.should == :degrees
+ p1.earth_radius_km.should == 6371
+ end
+ end
+
+ describe '2 Float numbers' do
+ it 'should create a GeoPoint' do
+ p1 = GeoPoint.new 50.1, 5.0
+ p1.should be_a(GeoPoint)
+ p1.lat.should == 50.1
+ p1.lon.should == 5.0
+ p1.unit.should == :degrees
+ p1.earth_radius_km.should == 6371
+ end
+ end
+
+ describe '2 Strings: "58 38 38N", "003 04 12W"' do
+ it 'should create a GeoPoint' do
+ p1 = GeoPoint.new "58 38 38N", "003 04 12W"
+ p1.should be_a(GeoPoint)
+ p1.lat.should be_within(0.5).of(58.38)
+ p1.lon.should be_within(0.5).of(-3)
+ p1.unit.should == :degrees
+ p1.earth_radius_km.should == 6371
+ end
+ end
+ end
+ end # initializer
+end
115 spec/geo_calc/geo_point/lat_lon.rb
View
@@ -0,0 +1,115 @@
+require 'spec_helper'
+
+# - www.movable-type.co.uk/scripts/latlong.html
+describe GeoPoint do
+ describe '#lat' do
+ before :each do
+ @p1 = GeoPoint.new 50, 5
+ end
+
+ it 'should return latitude' do
+ @p1.lat.should == 50
+ end
+
+ describe '#latitude (alias)' do
+ it 'should return latitude' do
+ @p1.latitude.should == 50
+ end
+ end
+
+ describe '#to_lat (alias)' do
+ it 'should return latitude' do
+ @p1.to_lat.should == 50
+ end
+ end
+ end
+
+ describe '#lat=' do
+ before :each do
+ @p1 = GeoPoint.new 50, 5
+ end
+
+ it 'should set new latitude' do
+ @p1.lat = 60
+ @p1.lat.should == 60
+ end
+
+ it 'should set new latitude -2' do
+ @p1.lat = -2
+ @p1.lat.should == -2
+ end
+
+ it 'should convert latitude -182 to -2' do
+ @p1.lat = -2
+ @p1.lat.should == -2
+ end
+
+
+ it 'should set new latitude within allowed range' do
+ @p1.lat = 520
+ @p1.lat.should be_between(0, 360)
+ end
+
+ describe '#latitude (alias)' do
+ it 'should set latitude' do
+ @p1.lat = 72
+ @p1.latitude.should == 72
+ end
+ end
+ end
+
+ describe '#lon' do
+ before :each do
+ @p1 = GeoPoint.new 5, 50
+ end
+
+ it 'should return longitude' do
+ @p1.lon.should == 50
+ end
+
+ it 'should return longitude (via lng)' do
+ @p1.lng.should == 50
+ end
+
+ describe '#longitude (alias)' do
+ it 'should return longitude' do
+ @p1.longitude.should == 50
+ end
+ end
+
+ describe '#to_lng (alias)' do
+ it 'should return latitude' do
+ @p1.to_lng.should == 50
+ end
+ end
+ end
+
+ describe '#lon=' do
+ before :each do
+ @p1 = GeoPoint.new 5, 50
+ end
+
+ it 'should set new longitude' do
+ @p1.lat.should == 5
+ @p1.lon = 60
+ @p1.lon.should == 60
+ end
+
+ it 'should return longitude (via lng)' do
+ @p1.lng = 70
+ @p1.lng.should == 70
+ end
+
+ it 'should set new latitude within allowed range' do
+ @p1.lon = 520
+ @p1.longitude.should be_between(-180, 180)
+ end
+
+ describe '#latitude (alias)' do
+ it 'should set latitude' do
+ @p1.longitude = 72
+ @p1.longitude.should == 72
+ end
+ end
+ end
+end
258 spec/geo_calc/geo_point_spec.rb
View
@@ -1,151 +1,7 @@
require 'spec_helper'
# - www.movable-type.co.uk/scripts/latlong.html
-describe GeoPoint do
- describe '#initializer' do
- describe '1 argument' do
- describe 'single String' do
- describe '50.1, 5.0 ' do
- it 'should create a GeoPoint' do
- p1 = GeoPoint.new "50.1, 5.0"
- p1.should be_a(GeoPoint)
- p1.lat.should == 50.1
- p1.lon.should == 5.0
- p1.unit.should == :degrees
- p1.earth_radius_km.should == 6371
- end
- end
-
- describe '(50.1, 5.0)' do
- it 'should create a GeoPoint' do
- p1 = GeoPoint.new "(50.1, 5.2)"
- p1.should be_a(GeoPoint)
- p1.lat.should == 50.1
- p1.lon.should == 5.2
- p1.unit.should == :degrees
- p1.earth_radius_km.should == 6371
- end
- end
-
- describe '58 38 38N, 003 04 12W' do
- it 'should create a GeoPoint' do
- p1 = GeoPoint.new "58 38 38N, 003 04 12W"
- p1.should be_a(GeoPoint)
- p1.lat.should be_within(0.5).of(58.38)
- p1.lon.should be_within(0.5).of(-3)
- p1.unit.should == :degrees
- p1.earth_radius_km.should == 6371
- end
- end
-
- describe '(58 38 38N, 003 04 12W)' do
- it 'should create a GeoPoint' do
- p1 = GeoPoint.new "(58 38 38N, 003 04 12W)"
- p1.should be_a(GeoPoint)
- p1.lat.should be_within(0.5).of(58.38)
- p1.lon.should be_within(0.5).of(-3) # W is negative, then normalize
- p1.unit.should == :degrees
- p1.earth_radius_km.should == 6371
- end
- end
- end
-
- describe 'single Array' do
- describe '2 Fixed numbers: 50,5 ' do
- it 'should create a GeoPoint' do
- p1 = GeoPoint.new [50, 5]
- p1.should be_a(GeoPoint)
- p1.lat.should == 50
- p1.lon.should == 5
- p1.unit.should == :degrees
- p1.earth_radius_km.should == 6371
- end
- end
-
- describe '2 Float numbers: 50.1, 5.0 ' do
- it 'should create a GeoPoint' do
- p1 = GeoPoint.new [50.1, 5.0]
- p1.should be_a(GeoPoint)
- p1.lat.should == 50.1
- p1.lon.should == 5.0
- p1.unit.should == :degrees
- p1.earth_radius_km.should == 6371
- end
- end
-
- describe 'single Hash' do
- describe 'with: {:lat => 50.1, :lng => 5.1}' do
- it 'should create a GeoPoint' do
- p1 = GeoPoint.new :lat => 50.1, :lng => 5.1
- p1.should be_a(GeoPoint)
- p1.lat.should == 50.1
- p1.lon.should == 5.1
- p1.unit.should == :degrees
- p1.earth_radius_km.should == 6371
- end
- end
-
- describe 'with: {:lat => 50.1, :long => 5.1}' do
- it 'should create a GeoPoint' do
- p1 = GeoPoint.new :lat => 50.1, :long => 5.1
- p1.should be_a(GeoPoint)
- p1.lat.should == 50.1
- p1.lon.should == 5.1
- p1.unit.should == :degrees
- p1.earth_radius_km.should == 6371
- end
- end
-
- describe 'with: {:latitude => 50.1, :longitude => 5.1}' do
- it 'should create a GeoPoint' do
- p1 = GeoPoint.new :latitude => 50.1, :longitude => 5.1
- p1.should be_a(GeoPoint)
- p1.lat.should == 50.1
- p1.lon.should == 5.1
- p1.unit.should == :degrees
- p1.earth_radius_km.should == 6371
- end
- end
- end
- end
- end
-
- describe 'with 2 arguments' do
- describe '2 Fixed numbers (Fixnum)' do
- it 'should create a GeoPoint' do
- p1 = GeoPoint.new 50, 5
- p1.should be_a(GeoPoint)
- p1.lat.should == 50
- p1.lon.should == 5
- p1.unit.should == :degrees
- p1.earth_radius_km.should == 6371
- end
- end
-
- describe '2 Float numbers' do
- it 'should create a GeoPoint' do
- p1 = GeoPoint.new 50.1, 5.0
- p1.should be_a(GeoPoint)
- p1.lat.should == 50.1
- p1.lon.should == 5.0
- p1.unit.should == :degrees
- p1.earth_radius_km.should == 6371
- end
- end
-
- describe '2 Strings: "58 38 38N", "003 04 12W"' do
- it 'should create a GeoPoint' do
- p1 = GeoPoint.new "58 38 38N", "003 04 12W"
- p1.should be_a(GeoPoint)
- p1.lat.should be_within(0.5).of(58.38)
- p1.lon.should be_within(0.5).of(-3)
- p1.unit.should == :degrees
- p1.earth_radius_km.should == 6371
- end
- end
- end
- end # initializer
-
+describe GeoPoint do
describe '#reverse_point' do
before :each do
@p = GeoPoint.new -2, 5
@@ -185,118 +41,6 @@
end
end
- describe '#lat' do
- before :each do
- @p1 = GeoPoint.new 50, 5
- end
-
- it 'should return latitude' do
- @p1.lat.should == 50
- end
-
- describe '#latitude (alias)' do
- it 'should return latitude' do
- @p1.latitude.should == 50
- end
- end
-
- describe '#to_lat (alias)' do
- it 'should return latitude' do
- @p1.to_lat.should == 50
- end
- end
- end
-
- describe '#lat=' do
- before :each do
- @p1 = GeoPoint.new 50, 5
- end
-
- it 'should set new latitude' do
- @p1.lat = 60
- @p1.lat.should == 60
- end
-
- it 'should set new latitude -2' do
- @p1.lat = -2
- @p1.lat.should == -2
- end
-
- it 'should convert latitude -182 to -2' do
- @p1.lat = -2
- @p1.lat.should == -2
- end
-
-
- it 'should set new latitude within allowed range' do
- @p1.lat = 520
- @p1.lat.should be_between(0, 360)
- end
-
- describe '#latitude (alias)' do
- it 'should set latitude' do
- @p1.lat = 72
- @p1.latitude.should == 72
- end
- end
- end
-
- describe '#lon' do
- before :each do
- @p1 = GeoPoint.new 5, 50
- end
-
- it 'should return longitude' do
- @p1.lon.should == 50
- end
-
- it 'should return longitude (via lng)' do
- @p1.lng.should == 50
- end
-
- describe '#longitude (alias)' do
- it 'should return longitude' do
- @p1.longitude.should == 50
- end
- end
-
- describe '#to_lng (alias)' do
- it 'should return latitude' do
- @p1.to_lng.should == 50
- end
- end
- end
-
- describe '#lon=' do
- before :each do
- @p1 = GeoPoint.new 5, 50
- end
-
- it 'should set new longitude' do
- @p1.lat.should == 5
- @p1.lon = 60
- @p1.lon.should == 60
- end
-
- it 'should return longitude (via lng)' do
- @p1.lng = 70
- @p1.lng.should == 70
- end
-
- it 'should set new latitude within allowed range' do
- @p1.lon = 520
- @p1.longitude.should be_between(-180, 180)
- end
-
- describe '#latitude (alias)' do
- it 'should set latitude' do
- @p1.longitude = 72
- @p1.longitude.should == 72
- end
- end
- end
-
-
describe '#[]' do
before :each do
@p1 = GeoPoint.new 50, 5
Please sign in to comment.
Something went wrong with that request. Please try again.