cacheable_kdtree 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZjBjMDhmMThmNWNhODE5MWYxOTVjM2FmYThkZDE0MzU4M2NmMTAyNw==
4
+ ODNiNGI4ZTY3NmQ2ZDNkZTNlYTlmNzBlY2Y2ZWE5ZjI4ZmIzOWZiMg==
5
5
  data.tar.gz: !binary |-
6
- ZDU2YjQwOTU5NTRhOTI4MDYzZTRmMjAwMTY2YTM4N2VjNjYzZTk4NA==
6
+ M2JhZGZkODZiYzMwZDk2ZjVlMDNhMmIxYzFhMzc3Yzc3MjBhNmMwYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZGNkNTA5MzFmMWFhZWQwZjEyYmZjMmVmOTk4MWMwOGZlMmRmMjE1MWRiNzI2
10
- NjZjM2UyNDgxMDdhMjE4ZjU1MzcwYTI1MTlmMDhjZmFhYjAxYjlhZDIyODZm
11
- OWUzMGFjYjIwZDNmZWIwOTZkZjJmZWFkNDA0NjM3NjQ2MDhhZGY=
9
+ MTllMDlkYzJiNjU4NzNjNzQxMzNiMWU0ZmQxZDQ3YzYwMWU1MzMwNzAyMDlk
10
+ YmU0YmVlODE2YTJhODc3Y2RlYzAxOTk3NDNkY2Q2YWZjMjhmOTk1NTQxZjcz
11
+ NjM1NDFiOTE5NmEzNmQ4ZDczMDJkOWRmNzRlZmNkYzlmODU4YTA=
12
12
  data.tar.gz: !binary |-
13
- ZTk2OGRkYTdmZTJmZTdiNjcwZDU4YTRhNTA4NjVlYjgzYTc1YjkxNWY5YmY2
14
- MDU4MmI2YWU2MWZhOWUwYjE1ZjYxNzcxMWNmZTEwOTM1NDhmZTM5NzBkMGI1
15
- YzAwZDI1MmMxNmJjMTllNWY4YWRhNzBkMzQ3YjgwYmVmYzFmNTk=
13
+ YTFmOTAxNTc0NjQyNDYyZjUxOTk1ZGM2MmFhZTNhNDVhYzlkYmU3ODEyODEz
14
+ ZmQ3YzVkMWRhMmE2OWM0YmY5MGZhNmM5MzJmMWRlOWU1ODc1MTJkMGU4MTJm
15
+ NThhMzI5Y2JkNWE5Zjg3MDMzYTdkNzc2NzEyNzhkMmY0YWZjMzU=
data/README.md CHANGED
@@ -32,6 +32,10 @@ You can query your tree and return the closest nodes:
32
32
  ```ruby
33
33
  # The 4th parameter may be :miles or :kilometers
34
34
  all_my_nodes = my_tree.closest(my_lat, my_long, distance, :kilometers)
35
+ # You may then sort the nodes by their distance to a point (using the Law of Cosines)
36
+ sorted_nodes = CacheableKdtree::LatitudeLongitudeNode.sort_by_distance_between(my_lat, my_long, all_my_nodes)
37
+ # To get your filtered, sorted data, you may run:
38
+ my_data_list = sorted_nodes.map(:&data)
35
39
  ```
36
40
 
37
41
  ## Contributing
@@ -11,6 +11,12 @@ class CacheableKdtree::LatitudeLongitudeNode
11
11
  "region: #{region ? region.to_s : region}"
12
12
  end
13
13
 
14
+ def self.sort_by_distance_between(lat, long, node_list)
15
+ node_list.sort_by do |node|
16
+ CacheableKdtree::Util.distance_miles(lat, long, node.latitude, node.longitude)
17
+ end
18
+ end
19
+
14
20
  def self.create_or_merge_regions(n1, n2)
15
21
  return CacheableKdtree::LatitudeLongitudeRegion.new(n1.latitude, n1.longitude, n2.latitude, n2.longitude) if n1.region.nil? && n2.region.nil?
16
22
  return n1.region.merge_point(n2.latitude, n2.longitude) if n1.region && n2.region.nil?
@@ -21,7 +21,7 @@ class CacheableKdtree::LatitudeLongitudeTree
21
21
 
22
22
  def nearest_nodes(bounding_box, node = @root, result = [])
23
23
  return result if node.nil?
24
- result << node.data if bounding_box.point_in_region?(node.latitude, node.longitude)
24
+ result << node if bounding_box.point_in_region?(node.latitude, node.longitude)
25
25
  nearest_nodes(bounding_box, node.left, result) if search_child?(node.left, bounding_box)
26
26
  nearest_nodes(bounding_box, node.right, result) if search_child?(node.right, bounding_box)
27
27
  result
@@ -1,16 +1,16 @@
1
1
  class CacheableKdtree::Util
2
- EARTH_MILES = 3959.0
2
+ KILOMETERS_IN_A_MILE = 0.621371192
3
3
  EARTH_KILOMETERS = 6371.0
4
4
 
5
5
  # I pulled the algorithm for the bounding_box calculation from here:
6
6
  # http://stackoverflow.com/questions/1689096/calculating-bounding-box-a-certain-distance-away-from-a-lat-long-coordinate-in-j
7
7
 
8
8
  def self.bounding_box_miles(lat, long, distance_in_miles)
9
- bounding_box(lat, long, distance_in_miles, EARTH_MILES)
9
+ bounding_box(lat, long, distance_in_miles, earth_miles)
10
10
  end
11
11
 
12
- def self.bounding_box_kilometers(lat, long, distance_in_miles)
13
- bounding_box(lat, long, distance_in_miles, EARTH_MILES)
12
+ def self.bounding_box_kilometers(lat, long, distance_in_kilometers)
13
+ bounding_box(lat, long, distance_in_kilometers, EARTH_KILOMETERS)
14
14
  end
15
15
 
16
16
  def self.bounding_box(lat, long, radius_distance, sphere_radius)
@@ -23,11 +23,33 @@ class CacheableKdtree::Util
23
23
  CacheableKdtree::LatitudeLongitudeRegion.new(lat1, long1, lat2, long2)
24
24
  end
25
25
 
26
+ def self.distance_kilometers(p1_lat, p1_long, p2_lat, p2_long)
27
+ distance_law_of_cosines(p1_lat, p1_long, p2_lat, p2_long, EARTH_KILOMETERS)
28
+ end
29
+
30
+ def self.distance_miles(p1_lat, p1_long, p2_lat, p2_long)
31
+ distance_law_of_cosines(p1_lat, p1_long, p2_lat, p2_long, earth_miles)
32
+ end
33
+
34
+ # I am using the law of cosines because it is faster than Haversine...
35
+ def self.distance_law_of_cosines(p1_lat, p1_long, p2_lat, p2_long, sphere_radius)
36
+ p1_lat = degrees_to_radians(p1_lat)
37
+ p1_long = degrees_to_radians(p1_long)
38
+ p2_lat = degrees_to_radians(p2_lat)
39
+ p2_long = degrees_to_radians(p2_long)
40
+ Math.acos(Math.sin(p1_lat) * Math.sin(p2_lat) +
41
+ Math.cos(p1_lat) * Math.cos(p2_lat) * Math.cos(p2_long - p1_long)) * sphere_radius
42
+ end
43
+
26
44
  def self.degrees_to_radians(degrees)
27
- degrees * 3.1416 / 180
45
+ degrees * Math::PI / 180
28
46
  end
29
47
 
30
48
  def self.radians_to_degrees(radians)
31
- radians * 180 / 3.1416
49
+ radians * 180 / Math::PI
50
+ end
51
+
52
+ def self.earth_miles
53
+ KILOMETERS_IN_A_MILE * EARTH_KILOMETERS
32
54
  end
33
55
  end
@@ -1,3 +1,3 @@
1
1
  module CacheableKdtree
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '2.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cacheable_kdtree
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Franssell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-02 00:00:00.000000000 Z
11
+ date: 2016-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler