Module: GeoCalc::Rhumb

Defined in:
lib/geo_calc/calc/rhumb.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.rhumb_bearing_to(base_point, point) ⇒ Numeric

Returns the bearing from this point to the supplied point along a rhumb line, in degrees

Parameters:

  • Destination (GeoPoint)

    point (latitude, longitude)

Returns:

  • (Numeric)

    Bearing in degrees from North



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/geo_calc/calc/rhumb.rb', line 55

def self.rhumb_bearing_to base_point, point
  lat1 = base_point.lat.to_rad
  lat2 = point.lat.to_rad

  dlon = (point.lon - base_point.lon).to_rad

  dphi = Math.log(Math.tan(lat2/2+Math::PI/4) / Math.tan(lat1/2+Math::PI/4))
  if dlon.abs > Math::PI
    dlon = dlon>0 ? -(2*Math::PI-dlon) : (2*Math::PI+dlon);
  end

  brng = Math.atan2(dlon, dphi);

  (brng.to_deg+360) % 360
end

.rhumb_destination_point(base_point, brng, dist) ⇒ Object

Returns the destination point from this point having travelled the given distance (in km) on the given bearing along a rhumb line

Parameters:

  • Starting (GeoPoint)

    point (latitude, longitude)

  • brng: (Number)

    Bearing in degrees from North

  • dist: (Number)

    Distance in km



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/geo_calc/calc/rhumb.rb', line 89

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
  lon1 = base_point.lon.to_rad
  brng = brng.to_rad

  lat2 = lat1 + d*Math.cos(brng);
  dlat = lat2 - lat1;
  dphi = Math.log(Math.tan(lat2/2+Math::PI/4) / Math.tan(lat1/2+Math::PI/4))

  q = begin
    dlat / dphi
  rescue 
    Math.cos(lat1) # E-W line gives dPhi=0
  end

  dlon = d * Math.sin(brng) / q
  # check for some daft bugger going past the pole

  if lat2.abs > Math::PI/2
    lat2 = lat2>0 ? Math::PI-lat2 : -(Math::PI-lat2)
  end
  lon2 = (lon1+dlon+3*Math::PI) % (2*Math::PI) - Math::PI

  [lat2.to_deg, lon2.to_deg]
  # GeoPoint.new 
end

.rhumb_distance_to(base_point, point) ⇒ Numeric

Returns the distance from this point to the supplied point, in km, travelling along a rhumb line

Parameters:

  • Start (GeoPoint)

    point with (latitude, longitude)

  • Destination (GeoPoint)

    point (latitude, longitude)

Returns:

  • (Numeric)

    Distance in km between start and destination point



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/geo_calc/calc/rhumb.rb', line 19

def self.rhumb_distance_to base_point, point
  lat1 = base_point.lat.to_rad
  lat2 = point.lat.to_rad

  dlat = (point.lat - base_point.lat).to_rad
  dlon = (point.lon - base_point.lon).abs.to_rad

  dphi = Math.log(Math.tan(lat2/2 + Math::PI/4) / Math.tan(lat1/2 + Math::PI/4))

  q = begin
    dlat / dphi
  rescue 
    Math.cos(lat1) # E-W line gives dPhi=0
  end
  
  # 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.earth_radius_km; 

  dist.round(4)  # 4 sig figures reflects typical 0.3% accuracy of spherical model
end

Instance Method Details

#rhumb_bearing_to(point) ⇒ Numeric

Returns the bearing from this point to the supplied point along a rhumb line, in degrees

Parameters:

  • Destination (GeoPoint)

    point (latitude, longitude)

Returns:

  • (Numeric)

    Bearing in degrees from North



47
48
49
# File 'lib/geo_calc/calc/rhumb.rb', line 47

def rhumb_bearing_to point
  GeoCalc::Rhumb.rhumb_bearing_to self, point
end

#rhumb_destination_point(brng, dist) ⇒ Object

Returns the destination point from this point having travelled the given distance (in km) on the given bearing along a rhumb line

Parameters:

  • brng: (Number)

    Bearing in degrees from North

  • dist: (Number)

    Distance in km



77
78
79
# File 'lib/geo_calc/calc/rhumb.rb', line 77

def rhumb_destination_point brng, dist
  GeoCalc::Rhumb.rhumb_destination_point self, brng, dist
end

#rhumb_distance_to(point) ⇒ Numeric

Returns the distance from this point to the supplied point, in km, travelling along a rhumb line

see(#williams.best.vwh.net/avform.htm#Rhumb)

Parameters:

  • Destination (GeoPoint)

    point latitude and longitude of

Returns:

  • (Numeric)

    Distance in km between start and destination point



9
10
11
# File 'lib/geo_calc/calc/rhumb.rb', line 9

def rhumb_distance_to point
  GeoCalc::Rhumb.rhumb_distance_to self, point
end