Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

all specs pass - new design and structure and now with coords mode

  • Loading branch information...
commit 539c620f07d8dc617c74c07d84864f245230d336 1 parent 7ec43d1
Kristian Mandrup authored
2  VERSION
View
@@ -1 +1 @@
-0.6.1
+0.7.0
6 lib/geo_calc.rb
View
@@ -1,2 +1,6 @@
require 'geo_calc/geo_point'
-require 'geo_units'
+require 'geo_units'
+
+module GeoCalc
+ autoload :Dms, 'geo_calc/dms'
+end
2  lib/geo_calc/calc/destination.rb
View
@@ -14,7 +14,7 @@ def destination_point brng, dist
# Returns GeoPoint: Destination point
def self.destination_point base_point, brng, dist
- dist = dist / base_point.radius # convert dist to angular distance in radians
+ dist = dist / base_point.earth_radius_km # convert dist to angular distance in radians
brng = brng.to_rad
lat1 = base_point.lat.to_rad
lon1 = base_point.lon.to_rad
2  lib/geo_calc/calc/distance.rb
View
@@ -35,7 +35,7 @@ def self.distance_to base_point, point, precision = 4
a = Math.sin(dlat/2) * Math.sin(dlat/2) + Math.cos(lat1) * Math.cos(lat2) * Math.sin(dlon/2) * Math.sin(dlon/2)
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
- d = base_point.radius * c
+ d = base_point.earth_radius_km * c
d.round(precision)
end
end
4 lib/geo_calc/calc/rhumb.rb
View
@@ -29,7 +29,7 @@ def self.rhumb_distance_to base_point, point
# if dlon over 180° take shorter rhumb across 180° meridian:
dlon = 2*Math::PI - dlon if (dlon > Math::PI)
- dist = Math.sqrt(dlat*dlat + q*q*dlon*dlon) * base_point.radius;
+ dist = Math.sqrt(dlat*dlat + q*q*dlon*dlon) * base_point.earth_radius_km;
dist.round(4) # 4 sig figures reflects typical 0.3% accuracy of spherical model
end
@@ -69,7 +69,7 @@ def rhumb_destination_point brng, dist
# @returns {LatLon} Destination point
def self.rhumb_destination_point base_point, brng, dist
- d = dist / base_point.radius # d = angular distance covered on earth's surface
+ d = dist / base_point.earth_radius_km # d = angular distance covered on earth's surface
lat1 = base_point.lat.to_rad
lon1 = base_point.lon.to_rad
brng = brng.to_rad
4 lib/geo_calc/dms/converter.rb
View
@@ -1,6 +1,8 @@
module GeoCalc
module Dms
module Converter
+ include GeoCalc::NumericCheckExt
+
def parse_dms dms_str
# check for signed decimal degrees without NSEW, if so return it directly
return dms_str if is_numeric?(dms_str)
@@ -97,6 +99,8 @@ def to_dms deg, format = :dms, dp = nil
end
return dms
end
+
+ extend self
end
end
end
4 lib/geo_calc/extensions/string.rb
View
@@ -1,4 +1,6 @@
class String
+ include ::GeoPoint::CoreExtension
+
def concat *args
args.inject(self) do |res, arg|
res << arg.to_s
@@ -7,7 +9,7 @@ def concat *args
end
def parse_dms
- Geo.parse_dms self
+ GeoCalc::Dms::Converter.parse_dms self
end
def to_rad
4 lib/geo_calc/geo_point.rb
View
@@ -38,7 +38,7 @@ def initialize *args
when 1
create_from_one args
when 2
- create_from_two args
+ create_from_two *args
else
raise "GeoPoint must be initialized with either one or to arguments defining the (latitude, longitude) coordinate on the map"
end
@@ -124,7 +124,7 @@ def to_coords points
end
def create_from_one args
- args = args.first if args.is_a?(Array) && args.first.is_a?(Array)
+ args = args.first
create_from_two *to_coords(args)
end
4 lib/geo_calc/pretty_print.rb
View
@@ -43,8 +43,8 @@ def to_s format = :dms, dp = 0
return '-,-' if !lat || !lon
- _lat = Geo.to_lat lat, format, dp
- _lon = Geo.to_lon lon, format, dp
+ _lat = GeoUnits::Converter.to_lat lat, format, dp
+ _lon = GeoUnits::Converter.to_lon lon, format, dp
"#{_lat}, #{_lon}"
end
11 lib/geo_units/converter.rb
View
@@ -12,11 +12,10 @@ module Converter
def to_lat deg, format = :dms, dp = 0
deg = deg.normalize_lat
- _lat = to_dms deg, format, dp
+ _lat = GeoCalc::Dms::Converter.to_dms deg, format, dp
_lat == '' ? '' : _lat[1..-1] + (deg<0 ? 'S' : 'N') # knock off initial '0' for lat!
end
-
# Convert numeric degrees to deg/min/sec longitude (suffixed with E/W)
#
# @param {Number} deg: Degrees
@@ -26,7 +25,7 @@ def to_lat deg, format = :dms, dp = 0
def to_lon deg, format = :dms, dp = 0
deg = deg.normalize_lng
- lon = to_dms deg, format, dp
+ lon = GeoCalc::Dms::Converter.to_dms deg, format, dp
lon == '' ? '' : lon + (deg<0 ? 'W' : 'E')
end
@@ -40,7 +39,7 @@ def to_lon deg, format = :dms, dp = 0
def to_brng deg, format = :dms, dp = 0
deg = (deg.to_f + 360) % 360 # normalise -ve values to 180º..360º
- brng = to_dms deg, format, dp
+ brng = GeoCalc::Dms::Converter.to_dms deg, format, dp
brng.gsub /360/, '0' # just in case rounding took us up to 360º!
end
@@ -71,6 +70,8 @@ def to_deg radians
alias_method :as_degrees, :to_deg
alias_method :in_deg, :to_deg
alias_method :in_degrees, :to_deg
+
+ extend self
end
# all degrees between -180 and 180
@@ -117,4 +118,6 @@ def normalize_deg degrees, shift = 0
(degrees + shift) % 360
end
alias_method :normalize_degrees, :normalize_deg
+
+ extend self
end
7 lib/geo_units/numeric_ext.rb
View
@@ -12,16 +12,17 @@ def is_between? lower, upper
(lower..upper).cover? self
end
+
def to_dms format = :dms, dp = nil
- Geo.to_dms self, format, dp
+ GeoCalc::Dms::Converter.to_dms self, format, dp
end
def to_lat_dms format = :dms, dp = nil
- Geo.to_lat self, format, dp
+ GeoUnits::Converter.to_lat self, format, dp
end
def to_lon_dms format = :dms, dp = nil
- Geo.to_lon self, format, dp
+ GeoUnits::Converter.to_lon self, format, dp
end
# Converts numeric degrees to radians
98 spec/geo_calc/core_ext/numeric_geo_ext_spec.rb
View
@@ -3,71 +3,69 @@
# - www.movable-type.co.uk/scripts/latlong.html
describe GeoPoint do
describe 'ruby core Class extensions' do
- describe NumericGeoExt do
- describe '#to_rad' do
- it 'should convert 0 degrees to 0 radians' do
- 0.to_rad.should == 0
- end
+ describe '#to_rad' do
+ it 'should convert 0 degrees to 0 radians' do
+ 0.to_rad.should == 0
+ end
- it 'should convert 180 degrees to PI radians' do
- 180.to_rad.should == Math::PI
- end
+ it 'should convert 180 degrees to PI radians' do
+ 180.to_rad.should == Math::PI
+ end
- it 'should convert 360 degrees to 6.28 radians' do
- 360.to_rad.should == Math::PI*2
- end
+ it 'should convert 360 degrees to 6.28 radians' do
+ 360.to_rad.should == Math::PI*2
end
+ end
- describe '#to_radians' do
- it 'should alias to_rad' do
- 360.to_rad.should == 360.to_radians
- end
+ describe '#to_radians' do
+ it 'should alias to_rad' do
+ 360.to_rad.should == 360.to_radians
end
+ end
- describe '#to_deg' do
- it 'should convert 0 radians to 0 degrees' do
- 0.to_deg.should == 0
- end
+ describe '#to_deg' do
+ it 'should convert 0 radians to 0 degrees' do
+ 0.to_deg.should == 0
+ end
- it 'should convert PI radians to 180 degrees' do
- 180.to_rad.to_deg.should be_within(0.01).of(180)
- end
+ it 'should convert PI radians to 180 degrees' do
+ 180.to_rad.to_deg.should be_within(0.01).of(180)
+ end
- it 'should convert 6.28 radians to 360 degrees' do
- 360.to_rad.to_deg.should be_within(0.01).of(360)
- end
+ it 'should convert 6.28 radians to 360 degrees' do
+ 360.to_rad.to_deg.should be_within(0.01).of(360)
end
-
- describe '#to_degrees' do
- it 'should alias to_deg' do
- 360.to_deg.should == 360.to_degrees
- end
+ end
+
+ describe '#to_degrees' do
+ it 'should alias to_deg' do
+ 360.to_deg.should == 360.to_degrees
end
-
- describe '#to_fixed' do
- it 'should make precision 4' do
- 1.123456.to_fixed(2).should == '1.12'
- end
+ end
+
+ describe '#to_fixed' do
+ it 'should make precision 4' do
+ 1.123456.to_fixed(2).should == '1.12'
end
+ end
- describe '#to_precision' do
- it 'should alis to_fixed' do
- 1.123456.to_precision(4).should == '1.1235'
- end
+ describe '#to_precision' do
+ it 'should alis to_fixed' do
+ 1.123456.to_precision(4).should == '1.1235'
end
+ end
- describe '#normalize' do
- it 'should turn 180 deg and normalize' do
- 361.normalize_deg(180).should == 181
- end
- it 'should normalize deg' do
- 361.normalize_deg.should == 1
- end
+ describe '#normalize' do
+ it 'should turn 180 deg and normalize' do
+ 361.normalize_deg(180).should == 181
+ end
+ it 'should normalize deg' do
+ 361.normalize_deg.should == 1
+ end
- it 'should alias with #normalize_degrees' do
- 362.normalize_degrees.should == 2
- end
+ it 'should alias with #normalize_degrees' do
+ 362.normalize_degrees.should == 2
end
- end # NumericGeoExt
+ end
end
end
60 spec/geo_calc/dms/converter_spec.rb
View
@@ -0,0 +1,60 @@
+require 'spec_helper'
+
+class Parser
+ include GeoCalc::Dms::Converter
+end
+
+def parser
+ Parser.new
+end
+
+# - www.movable-type.co.uk/scripts/latlong.html
+describe GeoCalc::Dms::Converter do
+ # # @param {String|Number} dmsStr: Degrees or deg/min/sec in variety of formats
+ # @returns {Number} Degrees as decimal number
+ describe '#parse_dms' do
+ it 'should convert "58 38 38N" to a Float of degrees (58..59)' do
+ deg = parser.parse_dms("58 38 38N")
+ deg.should be_a(Float)
+ deg.should be_between(58, 59)
+ end
+
+ it 'should convert "01 38 38W" to a Float of degrees (1..2)' do
+ deg = parser.parse_dms("01 38 38W")
+ deg.should be_a(Float)
+ deg.should < 0
+ deg.should > -2
+ end
+
+ it 'should convert "005 38 E" to a Float of degrees (5..6)' do
+ deg = parser.parse_dms("005 38 E")
+ deg.should be_a(Float)
+ deg.should be_between(5, 6)
+ end
+ end
+
+ # deg, format = :dms, dp = 0
+ describe '#to_dms' do
+ it 'should convert 58.3 to a String in DMS format' do
+ dms = parser.to_dms(58.3)
+ dms.should be_a(String)
+ expr = Regexp.escape "058".concat("\u00B0", "18", "\u2032", "00", "\u2033")
+ dms.should match expr
+ end
+
+ it 'should convert 58.3 to a String in DM format' do
+ dm = parser.to_dms(58.3, :dm, 2)
+ dm.should be_a(String)
+ expr = Regexp.escape "058".concat("\u00B0", "18", "\u2032")
+ dm.should match expr
+ end
+
+ it 'should convert 58.3 to a String in D format' do
+ d = parser.to_dms(58.3, :d, 2)
+ d.should be_a(String)
+ m = Regexp.escape "058".concat("\u00B0")
+ d.should match m
+ end
+ end
+end
+
44 spec/geo_calc/geo_point_spec.rb
View
@@ -12,7 +12,7 @@
p1.lat.should == 50.1
p1.lon.should == 5.0
p1.unit.should == :degrees
- p1.radius.should == 6371
+ p1.earth_radius_km.should == 6371
end
end
@@ -23,7 +23,7 @@
p1.lat.should == 50.1
p1.lon.should == 5.2
p1.unit.should == :degrees
- p1.radius.should == 6371
+ p1.earth_radius_km.should == 6371
end
end
@@ -34,7 +34,7 @@
p1.lat.should be_within(0.5).of(58.38)
p1.lon.should be_within(0.5).of(-3)
p1.unit.should == :degrees
- p1.radius.should == 6371
+ p1.earth_radius_km.should == 6371
end
end
@@ -45,7 +45,7 @@
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.radius.should == 6371
+ p1.earth_radius_km.should == 6371
end
end
end
@@ -58,7 +58,7 @@
p1.lat.should == 50
p1.lon.should == 5
p1.unit.should == :degrees
- p1.radius.should == 6371
+ p1.earth_radius_km.should == 6371
end
end
@@ -69,7 +69,7 @@
p1.lat.should == 50.1
p1.lon.should == 5.0
p1.unit.should == :degrees
- p1.radius.should == 6371
+ p1.earth_radius_km.should == 6371
end
end
@@ -81,7 +81,7 @@
p1.lat.should == 50.1
p1.lon.should == 5.1
p1.unit.should == :degrees
- p1.radius.should == 6371
+ p1.earth_radius_km.should == 6371
end
end
@@ -92,7 +92,7 @@
p1.lat.should == 50.1
p1.lon.should == 5.1
p1.unit.should == :degrees
- p1.radius.should == 6371
+ p1.earth_radius_km.should == 6371
end
end
@@ -103,7 +103,7 @@
p1.lat.should == 50.1
p1.lon.should == 5.1
p1.unit.should == :degrees
- p1.radius.should == 6371
+ p1.earth_radius_km.should == 6371
end
end
end
@@ -118,7 +118,7 @@
p1.lat.should == 50
p1.lon.should == 5
p1.unit.should == :degrees
- p1.radius.should == 6371
+ p1.earth_radius_km.should == 6371
end
end
@@ -129,7 +129,7 @@
p1.lat.should == 50.1
p1.lon.should == 5.0
p1.unit.should == :degrees
- p1.radius.should == 6371
+ p1.earth_radius_km.should == 6371
end
end
@@ -140,7 +140,7 @@
p1.lat.should be_within(0.5).of(58.38)
p1.lon.should be_within(0.5).of(-3)
p1.unit.should == :degrees
- p1.radius.should == 6371
+ p1.earth_radius_km.should == 6371
end
end
end
@@ -323,28 +323,14 @@
end
end
- describe '#to_arr' do
+ describe '#to_a' do
before :each do
@p1 = GeoPoint.new 50, 5
end
it 'should return GeoPoint as an array depending on state of reverse_arr' do
- @p1.to_arr.should == [50, 5]
- end
-
- describe '#reverse_arr!' do
- it 'should reverse the array returned by #to_arr to [lng, lat]' do
- @p1.reverse_arr!
- @p1.to_arr.should == [5, 50]
- end
- end
-
- describe '#reverse_arr!' do
- it 'should turn effect of #to_arr back to normal [lat, lng]' do
- @p1.normal_arr!
- @p1.to_arr.should == [50, 5]
- end
- end
+ @p1.to_a.should == [50, 5]
+ end
end
describe '#to_lat_lng' do
99 spec/geo_calc/geo_spec.rb
View
@@ -1,99 +0,0 @@
-require 'spec_helper'
-
-# - www.movable-type.co.uk/scripts/latlong.html
-describe GeoPoint do
- describe Geo do
-
- # # @param {String|Number} dmsStr: Degrees or deg/min/sec in variety of formats
- # @returns {Number} Degrees as decimal number
- describe '#parse_dms' do
- it 'should convert "58 38 38N" to a Float of degrees (58..59)' do
- deg = Geo.parse_dms("58 38 38N")
- deg.should be_a(Float)
- deg.should be_between(58, 59)
- end
-
- it 'should convert "01 38 38W" to a Float of degrees (1..2)' do
- deg = Geo.parse_dms("01 38 38W")
- deg.should be_a(Float)
- deg.should < 0
- deg.should > -2
- end
-
- it 'should convert "005 38 E" to a Float of degrees (5..6)' do
- deg = Geo.parse_dms("005 38 E")
- deg.should be_a(Float)
- deg.should be_between(5, 6)
- end
- end
-
- # deg, format = :dms, dp = 0
- describe '#to_dms' do
- it 'should convert 58.3 to a String in DMS format' do
- dms = Geo.to_dms(58.3)
- dms.should be_a(String)
- expr = Regexp.escape "058".concat("\u00B0", "18", "\u2032", "00", "\u2033")
- dms.should match expr
- end
-
- it 'should convert 58.3 to a String in DM format' do
- dm = Geo.to_dms(58.3, :dm, 2)
- dm.should be_a(String)
- expr = Regexp.escape "058".concat("\u00B0", "18", "\u2032")
- dm.should match expr
- end
-
- it 'should convert 58.3 to a String in D format' do
- d = Geo.to_dms(58.3, :d, 2)
- d.should be_a(String)
- m = Regexp.escape "058".concat("\u00B0")
- d.should match m
- end
- end
-
- # deg, format, dp
- describe '#to_lat' do
- it 'should convert 58.3 to a latitude String in North direction' do
- str_lat = Geo.to_lat(58.3)
- str_lat.should be_a(String)
- expr = Regexp.escape "58".concat("\u00B0", "18", "\u2032", "00", "\u2033", "N")
- str_lat.should match expr
- end
-
- it 'should convert -58.3 to a latitude String in South direction' do
- str_lat = Geo.to_lat(-58.3)
- str_lat.should be_a(String)
- expr = Regexp.escape "58".concat("\u00B0", "18", "\u2032", "00", "\u2033", "S")
- str_lat.should match expr
- end
- end
-
- # deg, format, dp
- describe '#to_lon' do
- it 'should convert 58.3 to a longitude String' do
- str_lat = Geo.to_lon(58.3)
- str_lat.should be_a(String)
- expr = Regexp.escape "58".concat("\u00B0", "18", "\u2032", "00", "\u2033", "E")
- str_lat.should match expr
- end
-
- it 'should convert 58.3 to a longitude String' do
- str_lat = Geo.to_lon(-58.3)
- str_lat.should be_a(String)
- expr = Regexp.escape "58".concat("\u00B0", "18", "\u2032", "00", "\u2033", "W")
- str_lat.should match expr
- end
- end
-
- # Convert numeric degrees to deg/min/sec as a bearing (0º..360º)
- # deg, format, dp
- describe '#to_brng' do
- it 'should convert 58.3 to a longitude String' do
- brng = Geo.to_brng(-58.3)
- brng.to_f.should be_between(0, 360)
- expr = Regexp.escape "301".concat("\u00B0", "42", "\u2032", "00")
- brng.should match expr
- end
- end
- end
-end
57 spec/geo_units/converter_spec.rb
View
@@ -0,0 +1,57 @@
+require 'spec_helper'
+
+class Converter
+ include GeoUnits::Converter
+end
+
+def converter
+ Converter.new
+end
+
+# - www.movable-type.co.uk/scripts/latlong.html
+describe GeoUnits::Converter do
+ # deg, format, dp
+ describe '#to_lat' do
+ it 'should convert 58.3 to a latitude String in North direction' do
+ str_lat = converter.to_lat(58.3)
+ str_lat.should be_a(String)
+ expr = Regexp.escape "58".concat("\u00B0", "18", "\u2032", "00", "\u2033", "N")
+ str_lat.should match expr
+ end
+
+ it 'should convert -58.3 to a latitude String in South direction' do
+ str_lat = converter.to_lat(-58.3)
+ str_lat.should be_a(String)
+ expr = Regexp.escape "58".concat("\u00B0", "18", "\u2032", "00", "\u2033", "S")
+ str_lat.should match expr
+ end
+ end
+
+ # deg, format, dp
+ describe '#to_lon' do
+ it 'should convert 58.3 to a longitude String' do
+ str_lat = converter.to_lon(58.3)
+ str_lat.should be_a(String)
+ expr = Regexp.escape "58".concat("\u00B0", "18", "\u2032", "00", "\u2033", "E")
+ str_lat.should match expr
+ end
+
+ it 'should convert 58.3 to a longitude String' do
+ str_lat = converter.to_lon(-58.3)
+ str_lat.should be_a(String)
+ expr = Regexp.escape "58".concat("\u00B0", "18", "\u2032", "00", "\u2033", "W")
+ str_lat.should match expr
+ end
+ end
+
+ # Convert numeric degrees to deg/min/sec as a bearing (0º..360º)
+ # deg, format, dp
+ describe '#to_brng' do
+ it 'should convert 58.3 to a longitude String' do
+ brng = converter.to_brng(-58.3)
+ brng.to_f.should be_between(0, 360)
+ expr = Regexp.escape "301".concat("\u00B0", "42", "\u2032", "00")
+ brng.should match expr
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.