Permalink
Browse files

specs pass

  • Loading branch information...
1 parent 08785f9 commit 5bd8cb3303b70363f6596d1fe04f5bcdb3c4f27e @kristianmandrup committed Jun 13, 2011
View
@@ -1,8 +1,8 @@
source "http://rubygems.org"
gem "require_all", "~> 1.2.0"
-gem "sugar-high", "~> 0.4.6.2"
-gem "geo_units", :path => '/Users/kristianconsult/ruby-gems/geo_units'
+gem "sugar-high", "~> 0.4.6.3"
+gem "geo_units"
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
View
@@ -3,6 +3,7 @@
module GeoCalc
autoload :Calc, 'geo_calc/calc'
- autoload :Dms, 'geo_calc/dms'
autoload :PrettyPrint, 'geo_calc/pretty_print'
-end
+end
+
+require 'geo_calc/extensions'
@@ -24,7 +24,8 @@ def self.destination_point base_point, brng, dist
lon2 = (lon2 + 3*Math::PI) % (2*Math::PI) - Math::PI # normalise to -180...+180
- GeoPoint.new lat2.to_deg, lon2.to_deg
+ [lat2.to_deg, lon2.to_deg]
+ # GeoPoint.new lat2.to_deg, lon2.to_deg
end
end
end
@@ -1,4 +1,4 @@
-module GeoCalc::Calc
+module GeoCalc
module Intersection
def intersection brng1, p2, brng2
GeoCalc::Calc::Intersection.intersection self, brng1, p2, brng2
@@ -8,11 +8,11 @@ def intersection brng1, p2, brng2
#
# see http:#williams.best.vwh.net/avform.htm#Intersection
#
- # @param {LatLon} p1: First point
- # @param {Number} brng1: Initial bearing from first point
- # @param {LatLon} p2: Second point
- # @param {Number} brng2: Initial bearing from second point
- # @returns {LatLon} Destination point (null if no unique intersection defined)
+ # @param [GeoPoint] p1: First point
+ # @param [Number] brng1: Initial bearing from first point
+ # @param [GeoPoint] p2: Second point
+ # @param [Number] brng2: Initial bearing from second point
+ # @returns [Array] Destination point (null if no unique intersection defined)
def self.intersection p1, brng1, p2, brng2
lat1 = p1.lat.to_rad
@@ -66,7 +66,7 @@ def self.intersection p1, brng1, p2, brng2
lon3 = lon1 + dlon13;
lon3 = (lon3 + Math::PI) % (2*Math::PI) - Math::PI # normalise to -180..180º
- GeoPoint.new lat3.to_deg, lon3.to_deg
- end
+ [lat3.to_deg, lon3.to_deg]
+ end
end
end
@@ -5,11 +5,12 @@ def midpoint_to point
end
# Returns the midpoint between this point and the supplied point.
- # see http:#mathforum.org/library/drmath/view/51822.html for derivation
+ # see(http:#mathforum.org/library/drmath/view/51822.html for derivation)
#
- # - GeoPoint point: Latitude/longitude of destination point
- # Returns GeoPoint: Midpoint between this point and the supplied point
-
+ # @param [GeoPoint] base_point: Starting point (latitude, longitude)
+ # @param [GeoPoint] point: Destination point (latitude, longitude)
+ # @return [Array] Midpoint between this point and the supplied point
+ #
def self.midpoint_to base_point, point
lat1 = base_point.lat.to_rad
lon1 = base_point.lon.to_rad;
@@ -23,7 +24,8 @@ def self.midpoint_to base_point, point
lon3 = lon1 + Math.atan2(by, Math.cos(lat1) + bx)
- GeoPoint.new lat3.to_deg, lon3.to_deg
+ [lat3.to_deg, lon3.to_deg]
+ # GeoPoint.new
end
end
end
View
@@ -1,16 +1,21 @@
module GeoCalc::Calc
module Rhumb
+ # Returns the distance from this point to the supplied point, in km, travelling along a rhumb line
+ #
+ # see(http:#williams.best.vwh.net/avform.htm#Rhumb)
+ #
+ # @param [GeoPoint] Destination point latitude and longitude of
+ # @return [Numeric] Distance in km between start and destination point
def rhumb_distance_to point
GeoCalc::Calc::Rhumb.rhumb_distance_to self, point
end
# Returns the distance from this point to the supplied point, in km, travelling along a rhumb line
- #
- # see http:#williams.best.vwh.net/avform.htm#Rhumb
- #
- # - GeoPoint point: Latitude/longitude of destination point
- # Returns Numeric: Distance in km between this point and destination point
-
+ #
+ # @param [GeoPoint] Start point with (latitude, longitude)
+ # @param [GeoPoint] Destination point (latitude, longitude)
+ # @return [Numeric] Distance in km between start and destination point
+ #
def self.rhumb_distance_to base_point, point
lat1 = base_point.lat.to_rad
lat2 = point.lat.to_rad
@@ -34,14 +39,19 @@ def self.rhumb_distance_to base_point, point
dist.round(4) # 4 sig figures reflects typical 0.3% accuracy of spherical model
end
+ # Returns the bearing from this point to the supplied point along a rhumb line, in degrees
+ #
+ # @param [GeoPoint] Destination point (latitude, longitude)
+ # @return [Numeric] Bearing in degrees from North
+ #
def rhumb_bearing_to point
GeoCalc::Calc::Rhumb.rhumb_bearing_to self, point
end
# Returns the bearing from this point to the supplied point along a rhumb line, in degrees
#
- # - GeoPoint point: Latitude/longitude of destination point
- # Returns Numeric: Bearing in degrees from North
-
+ # @param [GeoPoint] Destination point (latitude, longitude)
+ # @return [Numeric] Bearing in degrees from North
+ #
def self.rhumb_bearing_to base_point, point
lat1 = base_point.lat.to_rad
lat2 = point.lat.to_rad
@@ -58,16 +68,24 @@ def self.rhumb_bearing_to base_point, point
(brng.to_deg+360) % 360
end
+ # Returns the destination point from this point having travelled the given distance (in km) on the
+ # given bearing along a rhumb line
+ #
+ # @param [Number] brng: Bearing in degrees from North
+ # @param [Number] dist: Distance in km
+ # @returns [Array] Destination point as an array [lat, long]
def rhumb_destination_point brng, dist
GeoCalc::Calc::Rhumb.rhumb_destination_point self, brng, dist
end
+
# Returns the destination point from this point having travelled the given distance (in km) on the
# given bearing along a rhumb line
- #
- # @param {Number} brng: Bearing in degrees from North
- # @param {Number} dist: Distance in km
- # @returns {LatLon} Destination point
-
+ #
+ # @param [GeoPoint] Starting point (latitude, longitude)
+ # @param [Number] brng: Bearing in degrees from North
+ # @param [Number] dist: Distance in km
+ # @returns [Array] Destination point as an array [lat, long]
+ #
def self.rhumb_destination_point base_point, brng, dist
d = dist / base_point.earth_radius_km # d = angular distance covered on earth's surface
lat1 = base_point.lat.to_rad
@@ -92,7 +110,8 @@ def self.rhumb_destination_point base_point, brng, dist
end
lon2 = (lon1+dlon+3*Math::PI) % (2*Math::PI) - Math::PI
- GeoPoint.new lat2.to_deg, lon2.to_deg
+ [lat2.to_deg, lon2.to_deg]
+ # GeoPoint.new
end
end
end
View
@@ -1,5 +0,0 @@
-module GeoCalc
- module Dms
- autoload :Converter, 'geo_calc/dms/converter'
- end
-end
@@ -1,108 +0,0 @@
-include 'sugar-high/numeric'
-
-module GeoCalc
- module Dms
- module Converter
- include 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)
-
- # strip off any sign or compass dir'n & split out separate d/m/s
- dms = dms_str.trim.gsub(/^-/,'').gsub(/[NSEW]$/i,'').split(/[^0-9.,]+/).map(&:trim).map(&:to_f)
- return nil if dms.empty?
-
- # and convert to decimal degrees...
- deg = case dms.length
- when 3 # interpret 3-part result as d/m/s
- dms[0]/1 + dms[1]/60 + dms[2]/3600
- when 2 # interpret 2-part result as d/m
- dms[0]/1 + dms[1]/60
- when 1 # just d (possibly decimal) or non-separated dddmmss
- d = dms[0];
- # check for fixed-width unseparated format eg 0033709W
- d = "0#{d}" if (/[NS]/i.match(dms_str)) # - normalise N/S to 3-digit degrees
- d = "#{d.slice(0,3)/1}#{deg.slice(3,5)/60}#{deg.slice(5)/3600}" if (/[0-9]{7}/.match(deg))
- d
- else
- nil
- end
- return nil if !deg
- deg = (deg * -1) if (/^-|[WS]$/i.match(dms_str.trim)) # take '-', west and south as -ve
- deg.to_f
- end
-
- # Convert decimal degrees to deg/min/sec format
- # - degree, prime, double-prime symbols are added, but sign is discarded, though no compass
- # direction is added
- #
- #
- # @param {Number} deg: Degrees
- # @param {String} [format=dms]: Return value as 'd', 'dm', 'dms'
- # @param {Number} [dp=0|2|4]: No of decimal places to use - default 0 for dms, 2 for dm, 4 for d
- # @returns {String} deg formatted as deg/min/secs according to specified format
- # @throws {TypeError} deg is an object, perhaps DOM object without .value?
-
- def to_dms deg, format = :dms, dp = nil
- deg = begin
- deg.to_f
- rescue
- nil
- end
- return nil if !deg # give up here if we can't make a number from deg
-
- # default values
- format ||= :dms
- dp = if dp.nil?
- case format.to_sym
- when :d
- 4
- when :dm
- 2
- else
- 0 # default
- end
- end
- dp ||= 0
-
- deg = deg.abs # (unsigned result ready for appending compass dir'n)
-
- case format
- when :d
- d = deg.round(dp) # round degrees
- ds = "0#{d}" if (d <100) # pad with leading zeros
- ds = "0#{ds}" if (d <10)
- dms = ds.to_s.concat("\u00B0") # add º symbol
- when :dm
- min = (deg*60).round(dp) # convert degrees to minutes & round
- d = d.to_i
- d = (min / 60).floor # get component deg/min
- m = (min % 60).round(dp) # pad with trailing zeros
- ds = d
- ms = m
- ds = "0#{d}" if (d<100) # pad with leading zeros
- ds = "0#{d}" if (d<10)
- ms = "0#{m}" if (m<10)
- dms = ds.to_s.concat("\u00B0", ms, "\u2032") # add º, ' symbols
- when :dms
- sec = (deg * 3600).round # convert degrees to seconds & round
- d = (sec / 3600).floor # get component deg/min/sec
- m = ((sec / 60) % 60).floor
- s = (sec % 60).round(dp) # pad with trailing zeros
- ds = d
- ms = m
- ss = s
- ds = "0#{d}" if (d < 100) # pad with leading zeros
- ds = "0#{ds}" if (d < 10)
- ms = "0#{m}" if (m < 10)
- ss = "0#{s}" if (s < 10)
- dms = ds.to_s.concat("\u00B0", ms, "\u2032", ss, "\u2033") # add º, ', " symbols
- end
- return dms
- end
-
- extend self
- end
- end
-end
@@ -1,15 +1,4 @@
-class String
- def concat *args
- args.inject(self) do |res, arg|
- res << arg.to_s
- res
- end
- end
-
- def parse_dms
- GeoCalc::Dms::Converter.parse_dms self
- end
-
+class String
def to_rad
parse_dms.to_rad
end
@@ -31,12 +20,16 @@ def to_lng_lat
end
def to_lat
- raise "An empty String has no latitude" if empty?
- geo_clean.parse_dms.to_f.to_lat
+ raise "An empty String has no latitude" if self.empty?
+ s = geo_clean
+ 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 empty?
- geo_clean.parse_dms.to_f.to_lng
+ raise "An empty String has no latitude" if self.empty?
+ s = geo_clean
+ s = s.parse_dms if s.respond_to? :parse_dms
+ s.to_f.to_lng
end
end
@@ -1,41 +1,41 @@
+require 'geo_units'
+
module GeoCalc
module PrettyPrint
+
# Returns the latitude of this point; signed numeric degrees if no format, otherwise format & dp
- # as per Geo.to_lat
#
- # - String [format]: Return value as 'd', 'dm', 'dms'
- # - Numeric [dp=0|2|4]: No of decimal places to display
+ # @param [String] format: Return value as 'd', 'dm', 'dms'
+ # @param [Numeric] dp: No of decimal places to display (0|2|4)
#
- # Returns {Numeric|String}: Numeric degrees if no format specified, otherwise deg/min/sec
+ # @return [Numeric|String]: Numeric degrees if no format specified, otherwise deg/min/sec
#
-
def to_lat format = :dms, dp = 0
return lat if !format
- Geo.to_lat lat, format, dp
+ GeoUnits::Converter.to_lat lat, format, dp
end
-
# Returns the longitude of this point; signed numeric degrees if no format, otherwise format & dp
# as per Geo.toLon()
#
- # @param {String} [format]: Return value as 'd', 'dm', 'dms'
- # @param {Number} [dp=0|2|4]: No of decimal places to display
- # @returns {Number|String} Numeric degrees if no format specified, otherwise deg/min/sec
+ # @param [String} [format]: Return value as 'd', 'dm', 'dms'
+ # @param [Number} [dp=0|2|4]: No of decimal places to display
+ # @return [Number|String} Numeric degrees if no format specified, otherwise deg/min/sec
#
# @requires Geo
def to_lon format, dp
return lon if !format
- Geo.to_lon lon, format, dp
+ GeoUnits::Converter.to_lon lon, format, dp
end
# Returns a string representation of this point; format and dp as per lat()/lon()
#
- # @param {String} [format]: Return value as 'd', 'dm', 'dms'
- # @param {Number} [dp=0|2|4]: No of decimal places to display
+ # @param [String] format: Return value as 'd', 'dm', 'dms'
+ # @param [Number] dp: No of decimal places to display (0|2|4)
- # @returns {String} Comma-separated latitude/longitude
+ # @return [String] Comma-separated latitude/longitude
#
def to_s format = :dms, dp = 0
Oops, something went wrong.

0 comments on commit 5bd8cb3

Please sign in to comment.