gpsutils 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13f60e90df69b5df1eb2e4bc8ae38bbc4a85552d
4
- data.tar.gz: 185af0e83814dbbb3dbd72e25d7d6cc8e1afd601
3
+ metadata.gz: e410d5e1e4435cecaae8722bc66b90d596289587
4
+ data.tar.gz: 439be1ddb2aaf0cc434337df87332f8f770379da
5
5
  SHA512:
6
- metadata.gz: e603405c2410a019078df94458d2c4844e08ad860486ef82c01693c1799e5ba17f4d2356d178374a25fdea7e8b0e047fbc2e9fffa91b93c093f3dfbd366f1538
7
- data.tar.gz: 58463a2addc7e96469fb9a9a056062a3619e2aed678e6ac0ebf8e88c3ff796914544ae07553b0e9ea03664c86eeccf7a452ad44fc228c9783d728b8bd783d916
6
+ metadata.gz: 9cc43650c648ca115506597522fe6360a588c58fb02f63373e724d6a23db521dfbbec713b3b6c101a5948c2cdfbaef141b9b97a0cf0c839d9f984fdfad706047
7
+ data.tar.gz: b8f4e2253be54e8fb2c8d32d4ac4dfd18cb64434af0915570c328bd3ba764b5e8b281f79ff3a62bd5861ae82ec1d0381f0e82ef4b37e0ea9e6315503764c804d
data/README.md CHANGED
@@ -89,5 +89,23 @@ area = GpsUtils::BoundingBox.new(nw, se)
89
89
  area.cover? location
90
90
  ```
91
91
 
92
+ Sort positions (Points) by distance to location (Point):
93
+
94
+ ```ruby
95
+ # Follow instructions in the installation section.
96
+ require 'gpsutils'
97
+
98
+ # Latitude, Longitude.
99
+ location = GpsUtils::Point.new(-22.955776, -43.536438)
100
+
101
+ positions = []
102
+ positions.push(GpsUtils::Point.new(-23.560112, -46.642043))
103
+ positions.push(GpsUtils::Point.new(-12.994417, -38.508040))
104
+
105
+ positions.sort_by! do |position|
106
+ location.distance(position)
107
+ end
108
+ ```
109
+
92
110
  ## License
93
111
  This code is free to use under the terms of the [MIT license](LICENSE).
@@ -1,3 +1,3 @@
1
1
  module GpsUtils
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/gpsutils.rb CHANGED
@@ -1,4 +1,9 @@
1
1
  module GpsUtils
2
+ R = 6371e3
3
+
4
+ def self.to_radians(degrees)
5
+ degrees * Math::PI / 180
6
+ end
2
7
 
3
8
  class Point
4
9
  # @!attribute [r] lat
@@ -35,6 +40,29 @@ module GpsUtils
35
40
  def to_s
36
41
  "#{@lat},#{@lng}"
37
42
  end
43
+
44
+ # Measure the distance between this point and another.
45
+ #
46
+ # Distance is calculated using equirectangular projection.
47
+ # @see https://en.wikipedia.org/wiki/Equirectangular_projection
48
+ #
49
+ # @param other [Point]
50
+ # @return [Float]
51
+ # @raise [ArgumentError] if other is not a Point
52
+ def distance(other)
53
+ unless other.is_a? Point
54
+ raise ArgumentError.new 'other must be a Point.'
55
+ end
56
+
57
+ dlng = GpsUtils::to_radians(other.lng - @lng)
58
+ dlat = GpsUtils::to_radians(other.lat - @lat)
59
+
60
+ x = dlng * Math.cos(dlat / 2)
61
+ y = GpsUtils::to_radians(other.lat - @lat)
62
+
63
+ Math.sqrt(x**2 + y**2) * GpsUtils::R
64
+ end
65
+
38
66
  end
39
67
 
40
68
  class BoundingBox
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gpsutils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronnie Mose
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-12 00:00:00.000000000 Z
11
+ date: 2016-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler