geo_magic 0.2.0 → 0.2.1
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.
- data/README.textile +4 -0
- data/VERSION +1 -1
- data/geo_magic.gemspec +1 -1
- data/lib/geo_magic/util.rb +15 -5
- data/spec/geo_magic/select_nearest_spec.rb +46 -9
- metadata +3 -3
data/README.textile
CHANGED
@@ -79,6 +79,10 @@ Lots of extra features has been added recently. For now, please check out the sp
|
|
79
79
|
Most recently I have added support to filter a list of points for those who are within a radius of another point (see _select_neares_spec.rb_).
|
80
80
|
I have also just created a Geocode adapter for _Geocode_ and _Graticule_ (see: _geocoder_spec.rb_) - This can also easily be configured for Rails.
|
81
81
|
|
82
|
+
h3. Create random points in Radius
|
83
|
+
|
84
|
+
@create_points_in_square@ works, but @create_points_in_circle@ is currently broken. Please help fix this ASAP ;) Thanks!
|
85
|
+
|
82
86
|
h2. Meta magic
|
83
87
|
|
84
88
|
You can also include the functionality directly into any class like this
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/geo_magic.gemspec
CHANGED
data/lib/geo_magic/util.rb
CHANGED
@@ -25,7 +25,7 @@ class Array
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def sort_by_distance
|
28
|
-
self.sort_by { |item| item.dist }
|
28
|
+
self.sort_by { |item| get_dist_obj(item).dist }
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -37,7 +37,7 @@ module GeoMagic
|
|
37
37
|
{:km => 6371, :miles => 3956, :feet => 20895592, :meters => 6371000}
|
38
38
|
end
|
39
39
|
|
40
|
-
def get_within dist_obj, options = {:precision => :lowest}
|
40
|
+
def get_within dist_obj, options = {:precision => :lowest}
|
41
41
|
calc_method = get_proc(options[:precision] || :normal)
|
42
42
|
from_loc = get_location options[:from]
|
43
43
|
|
@@ -46,7 +46,8 @@ module GeoMagic
|
|
46
46
|
res = []
|
47
47
|
spots = populate_distance(calc_method, from_loc)
|
48
48
|
spots.sort_by_distance.select do |item|
|
49
|
-
|
49
|
+
it = get_dist_obj(item)
|
50
|
+
if it.dist <= dist
|
50
51
|
res << item
|
51
52
|
else
|
52
53
|
break
|
@@ -63,10 +64,19 @@ module GeoMagic
|
|
63
64
|
|
64
65
|
protected
|
65
66
|
|
67
|
+
def get_dist_obj dist_obj
|
68
|
+
return dist_obj if dist_obj.respond_to? :dist
|
69
|
+
[:point, :to_point, :location].each do |loc|
|
70
|
+
return dist_obj.send(loc) if dist_obj.respond_to? loc
|
71
|
+
end
|
72
|
+
raise "Invalid Point object: a valid Point object must either be a sublclass of MapPoint or have a #point #to_point or #location method that returns a MapPoint from which can be calculated a distance"
|
73
|
+
end
|
74
|
+
|
66
75
|
def populate_distance calc_method, from_loc
|
67
76
|
self.map! do |item|
|
68
|
-
|
69
|
-
|
77
|
+
dist_item = get_dist_obj(item)
|
78
|
+
dist_obj = calc_method.call(from_loc, dist_item)
|
79
|
+
dist_item.dist = dist_obj.distance
|
70
80
|
item
|
71
81
|
end
|
72
82
|
end
|
@@ -2,6 +2,36 @@ require 'spec_helper'
|
|
2
2
|
require 'geo_magic'
|
3
3
|
require 'geo_magic/remote'
|
4
4
|
|
5
|
+
class Person
|
6
|
+
attr_accessor :location, :name
|
7
|
+
|
8
|
+
def initialize name, location
|
9
|
+
@name = name
|
10
|
+
@location = location
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_point
|
14
|
+
location
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def names
|
19
|
+
['Hansi', 'Michel', 'Sandy', 'Sam', 'Thomas']
|
20
|
+
end
|
21
|
+
|
22
|
+
def random_at points
|
23
|
+
points.inject([]) do |res, point|
|
24
|
+
res << Person.new(names[rand_index], point)
|
25
|
+
res
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def rand_index
|
30
|
+
rand(names.size)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
5
35
|
describe "GeoMagic closest" do
|
6
36
|
before do
|
7
37
|
@long1 = -104.88544
|
@@ -9,22 +39,29 @@ describe "GeoMagic closest" do
|
|
9
39
|
|
10
40
|
@center_point = GeoMagic::Point.new @long1, @lat1
|
11
41
|
@radius = @center_point.within(10.km)
|
12
|
-
@points = @radius.
|
42
|
+
@points = @radius.create_points_in_square 10
|
13
43
|
end
|
14
44
|
|
15
45
|
|
16
|
-
it "should select the closest 3 points" do
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
|
46
|
+
# it "should select the closest 3 points" do
|
47
|
+
# # puts "radius: #{@radius.inspect}"
|
48
|
+
# # puts "points: #{@points.inspect}"
|
49
|
+
# closest = @points.as_map_points.get_closest 3, :from => @center_point
|
50
|
+
# puts "3 closest points: #{closest.inspect}"
|
51
|
+
# end
|
52
|
+
#
|
23
53
|
it "should select all points within 4 km" do
|
24
54
|
# puts "radius: #{@radius.inspect}"
|
25
55
|
# puts "points: #{@points.inspect}"
|
26
56
|
closest = @points.as_map_points.get_within 4.km, :from => @center_point
|
27
57
|
puts "points within 4 km: #{closest.inspect}"
|
28
58
|
end
|
29
|
-
|
59
|
+
|
60
|
+
it "should select all points within 4 km" do
|
61
|
+
points = @radius.create_points_in_square 4
|
62
|
+
persons = Person.random_at points
|
63
|
+
|
64
|
+
closest = persons.as_map_points.get_within 5.km, :from => @center_point
|
65
|
+
puts "Persons within 4 km: #{closest.inspect}"
|
66
|
+
end
|
30
67
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Kristian Mandrup
|
@@ -190,7 +190,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
190
190
|
requirements:
|
191
191
|
- - ">="
|
192
192
|
- !ruby/object:Gem::Version
|
193
|
-
hash:
|
193
|
+
hash: 27646408712887287
|
194
194
|
segments:
|
195
195
|
- 0
|
196
196
|
version: "0"
|