kookjr-geodesic 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING.LESSER +503 -504
- data/Changelog +6 -0
- data/README.rdoc +30 -5
- data/lib/geodesic.rb +23 -19
- metadata +3 -3
data/Changelog
CHANGED
data/README.rdoc
CHANGED
@@ -7,13 +7,19 @@ Geodesic contains the following features:
|
|
7
7
|
|
8
8
|
* A Position class to encapsulate an Earth position (latitude and longitude)
|
9
9
|
|
10
|
-
* A method to calculate the distance between two positions using Haversine
|
10
|
+
* A method to calculate the distance between two positions using Haversine formula
|
11
11
|
|
12
12
|
* A method to calculate the distance between two positions using the Law of Cosines
|
13
13
|
|
14
|
-
* A method to calculate the initial bearing between two
|
14
|
+
* A method to calculate the initial bearing between two positions
|
15
15
|
|
16
|
-
* A method to calculate a
|
16
|
+
* A method to calculate a position a given distance from another position along a bearing
|
17
|
+
|
18
|
+
All input parameters to these functions are Floats. Longitudes
|
19
|
+
are expected to be in the range of -180.0 to 180.0 decimal degrees,
|
20
|
+
latitudes in the range of -90.0 to 90.0 decimal degrees and bearings in
|
21
|
+
the range of 0.0 to 360.0 (where 0.0 is North). Distances are measured in
|
22
|
+
kilometers.
|
17
23
|
|
18
24
|
== Installation
|
19
25
|
|
@@ -28,7 +34,26 @@ Then, install.
|
|
28
34
|
|
29
35
|
== Sample Usage
|
30
36
|
|
31
|
-
|
37
|
+
require 'rubygems'
|
38
|
+
require 'geodesic'
|
39
|
+
|
40
|
+
fremont = Geodesic::Position.new(37.549531, -121.998711)
|
41
|
+
farmington = Geodesic::Position.new(37.923482, -121.015263)
|
42
|
+
|
43
|
+
d = Geodesic::dist_haversine(fremont.lat, fremont.lon, farmington.lat, farmington.lon)
|
44
|
+
print "distance from Fremont to Farmington is ", d, " kilometers\n"
|
45
|
+
|
46
|
+
b = Geodesic::bearing(fremont.lat, fremont.lon, farmington.lat, farmington.lon)
|
47
|
+
print "bearing is ", b, " degrees\n"
|
48
|
+
|
49
|
+
p = Geodesic::dest_position(fremont.lat, fremont.lon, b, d + 10)
|
50
|
+
print "10 kilometers beyond farmington is ", p.lat, ", ", p.lon, "\n"
|
51
|
+
|
52
|
+
The output is:
|
53
|
+
|
54
|
+
distance from Fremont to Farmington is 95.957632116596 kilometers
|
55
|
+
bearing is 64.0206814526114 degrees
|
56
|
+
10 kilometers beyond farmington is 37.9619800970259, -120.912203460738
|
32
57
|
|
33
58
|
== Development
|
34
59
|
=== Source Repository
|
@@ -62,4 +87,4 @@ can be found at:
|
|
62
87
|
== License
|
63
88
|
|
64
89
|
GNU LGPL, Lesser General Public License version 2.1. For details,
|
65
|
-
see file "COPYING.LESSER".
|
90
|
+
see file "COPYING.LESSER".
|
data/lib/geodesic.rb
CHANGED
@@ -19,33 +19,34 @@
|
|
19
19
|
|
20
20
|
# Additions to Float for conversion between various units.
|
21
21
|
class Float
|
22
|
-
#
|
22
|
+
# Convert decimal degrees to radians. Input range is either -180.0 to 180.0
|
23
|
+
# for longitudes, or -90.0 to 90.0 for latitudes.
|
23
24
|
def to_radians
|
24
|
-
self * Math::PI / 180
|
25
|
+
self * Math::PI / 180.0
|
25
26
|
end
|
26
27
|
|
27
|
-
#
|
28
|
+
# Convert radians to degrees. Return range is -180.0 to 180.0.
|
28
29
|
def to_degrees
|
29
30
|
self * 180 / Math::PI
|
30
31
|
end
|
31
32
|
|
32
|
-
#
|
33
|
+
# Convert radians to bearing. Return degrees range 0.0 to 360.0.
|
33
34
|
def to_bearing
|
34
|
-
(self.to_degrees()+360) % 360
|
35
|
+
(self.to_degrees()+360.0) % 360.0
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
38
39
|
# Geodesic is a Ruby library for calculating distance and bearing on
|
39
|
-
# the Earth's surface.
|
40
|
+
# the Earth's surface. Positions are defined by longitude and
|
40
41
|
# latitude. The API uses Float values for all input and returned
|
41
42
|
# types. Distances are memasured in kilometers (km).
|
42
43
|
module Geodesic
|
43
44
|
|
44
|
-
# Earth's radius in
|
45
|
-
EARTH_RADIUS = 6371
|
45
|
+
# Earth's radius in kilometers
|
46
|
+
EARTH_RADIUS = 6371.0
|
46
47
|
|
47
48
|
|
48
|
-
# Class to hold the latitude and longitude of a
|
49
|
+
# Class to hold the latitude and longitude of a position.
|
49
50
|
class Position
|
50
51
|
attr_accessor :lat
|
51
52
|
attr_accessor :lon
|
@@ -57,8 +58,8 @@ module Geodesic
|
|
57
58
|
end
|
58
59
|
end
|
59
60
|
|
60
|
-
# Use Haversine formula to Calculate distance (in
|
61
|
-
# latitude/longitude
|
61
|
+
# Use Haversine formula to Calculate distance (in kilometers) between two positions specified by
|
62
|
+
# latitude/longitude in numeric degrees of type Float.
|
62
63
|
def Geodesic.dist_haversine(lat1, lon1, lat2, lon2)
|
63
64
|
dLat = (lat2-lat1).to_radians()
|
64
65
|
dLon = (lon2-lon1).to_radians()
|
@@ -73,8 +74,8 @@ module Geodesic
|
|
73
74
|
return d
|
74
75
|
end
|
75
76
|
|
76
|
-
# Use Law of Cosines formula to
|
77
|
-
# latitude/longitude
|
77
|
+
# Use Law of Cosines formula to calculate distance (in kilometers) between two positions specified by
|
78
|
+
# latitude/longitude in numeric degrees of type Float.
|
78
79
|
def Geodesic.dist_cosine_law(lat1, lon1, lat2, lon2)
|
79
80
|
d = Math::acos(Math::sin(lat1.to_radians())*Math::sin(lat2.to_radians()) +
|
80
81
|
Math::cos(lat1.to_radians())*Math::cos(lat2.to_radians()) *
|
@@ -82,8 +83,9 @@ module Geodesic
|
|
82
83
|
return d
|
83
84
|
end
|
84
85
|
|
85
|
-
# Calculate (initial) bearing between two
|
86
|
-
#
|
86
|
+
# Calculate (initial) bearing in degrees between two positions. Return
|
87
|
+
# range is 0.0 to 360.0. Latitudes and longitudes are of type Float.
|
88
|
+
# see http://williams.best.vwh.net/avform.htm#Crs
|
87
89
|
def Geodesic.bearing(lat1, lon1, lat2, lon2)
|
88
90
|
lat1 = lat1.to_radians()
|
89
91
|
lat2 = lat2.to_radians()
|
@@ -95,9 +97,11 @@ module Geodesic
|
|
95
97
|
return Math::atan2(y, x).to_bearing()
|
96
98
|
end
|
97
99
|
|
98
|
-
# Calculate destination
|
99
|
-
#
|
100
|
-
|
100
|
+
# Calculate destination position given start position, initial bearing (degrees 0.0 to
|
101
|
+
# 360.0) and distance (kilometers).
|
102
|
+
# All values of type Float. Return a Position class or nil.
|
103
|
+
# see http://williams.best.vwh.net/avform.htm#LL
|
104
|
+
def Geodesic.dest_position(lat1, lon1, brng, d)
|
101
105
|
lat1 = lat1.to_radians()
|
102
106
|
lon1 = lon1.to_radians()
|
103
107
|
brng = brng.to_radians()
|
@@ -106,7 +110,7 @@ module Geodesic
|
|
106
110
|
Math::cos(lat1)*Math::sin(d/EARTH_RADIUS)*Math::cos(brng) )
|
107
111
|
lon2 = lon1 + Math::atan2(Math::sin(brng)*Math::sin(d/EARTH_RADIUS)*Math::cos(lat1),
|
108
112
|
Math::cos(d/EARTH_RADIUS)-Math::sin(lat1)*Math::sin(lat2))
|
109
|
-
lon2 = (lon2+Math::PI)%(2*Math::PI) - Math::PI # normalise to
|
113
|
+
lon2 = (lon2+Math::PI)%(2*Math::PI) - Math::PI # normalise to +/-radians
|
110
114
|
|
111
115
|
return nil if lat2.nan? || lon2.nan?
|
112
116
|
return Position.new(lat2.to_degrees(), lon2.to_degrees())
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kookjr-geodesic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mathew Cucuzella
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description: Geodesic is a Ruby library for calculating distance and bearing on the Earth's surface.
|
16
|
+
description: Geodesic is a Ruby library for calculating distance and bearing on the Earth's surface. Positions are defined by longitude and latitude. The API uses Float values for all input and returned types. Distances are memasured in kilometers (km).
|
17
17
|
email: kookjr@gmail.com
|
18
18
|
executables: []
|
19
19
|
|