geo_magic 0.2.1 → 0.2.1.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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.1.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{geo_magic}
8
- s.version = "0.2.1"
8
+ s.version = "0.2.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
@@ -36,6 +36,7 @@ Gem::Specification.new do |s|
36
36
  "lib/geo_magic/meta.rb",
37
37
  "lib/geo_magic/point.rb",
38
38
  "lib/geo_magic/radius.rb",
39
+ "lib/geo_magic/rectangle.rb",
39
40
  "lib/geo_magic/remote.rb",
40
41
  "lib/geo_magic/util.rb",
41
42
  "lib/rails/config.rb",
@@ -47,6 +48,7 @@ Gem::Specification.new do |s|
47
48
  "spec/geo_magic/plane_dist_spec.rb",
48
49
  "spec/geo_magic/remote_spec.rb",
49
50
  "spec/geo_magic/select_nearest_spec.rb",
51
+ "spec/geo_magic/within_rect_spec.rb",
50
52
  "spec/spec_helper.rb"
51
53
  ]
52
54
  s.homepage = %q{http://github.com/kristianmandrup/geo_magic}
@@ -62,6 +64,7 @@ Gem::Specification.new do |s|
62
64
  "spec/geo_magic/plane_dist_spec.rb",
63
65
  "spec/geo_magic/remote_spec.rb",
64
66
  "spec/geo_magic/select_nearest_spec.rb",
67
+ "spec/geo_magic/within_rect_spec.rb",
65
68
  "spec/spec_helper.rb"
66
69
  ]
67
70
 
@@ -0,0 +1,52 @@
1
+ module GeoMagic
2
+ class Rectangle
3
+ attr_accessor :top_left_point, :bottom_right_point
4
+
5
+ def initialize point_a, point_b
6
+ @top_left_point = GeoMagic::Point.new left_lat(point_a, point_b), bot_long(point_a, point_b)
7
+ @bottom_right_point = GeoMagic::Point.new right_lat(point_a, point_b), top_long(point_a, point_b)
8
+ end
9
+
10
+ def overlaps? point
11
+ # puts "inside_top_left?: #{point} -> #{inside_top_left?(point)}"
12
+ # puts "inside_bottom_right?: #{point} -> #{inside_bottom_right?(point)}"
13
+ inside_top_left?(point) && inside_bottom_right?(point)
14
+ end
15
+
16
+ def to_s
17
+ "#{top_left_point} - #{bottom_right_point}"
18
+ end
19
+
20
+ protected
21
+
22
+ def top_long point_a, point_b
23
+ return point_a.longitude if point_a.longitude > point_b.longitude
24
+ point_b.longitude
25
+ end
26
+
27
+ def bot_long point_a, point_b
28
+ return point_a.longitude if point_a.longitude < point_b.longitude
29
+ point_b.longitude
30
+ end
31
+
32
+ def left_lat point_a, point_b
33
+ return point_a.latitude if point_a.latitude < point_b.latitude
34
+ point_b.latitude
35
+ end
36
+
37
+ def right_lat point_a, point_b
38
+ return point_a.latitude if point_a.latitude > point_b.latitude
39
+ point_b.latitude
40
+ end
41
+
42
+
43
+ def inside_top_left? point
44
+ top_left_point.latitude < point.latitude && top_left_point.longitude < point.longitude
45
+ end
46
+
47
+ def inside_bottom_right? point
48
+ bottom_right_point.latitude > point.latitude && bottom_right_point.longitude > point.longitude
49
+ end
50
+ end
51
+ end
52
+
@@ -1,3 +1,5 @@
1
+ require 'geo_magic/rectangle'
2
+
1
3
  module GeoMagic
2
4
  module Util #:nodoc:
3
5
  def self.extract_point point
@@ -37,9 +39,9 @@ module GeoMagic
37
39
  {:km => 6371, :miles => 3956, :feet => 20895592, :meters => 6371000}
38
40
  end
39
41
 
40
- def get_within dist_obj, options = {:precision => :lowest}
42
+ def get_within dist_obj, options = {:precision => :lowest}
41
43
  calc_method = get_proc(options[:precision] || :normal)
42
- from_loc = get_location options[:from]
44
+ from_loc = get_location get_dist_obj(options[:from])
43
45
 
44
46
  dist = dist_obj.distance / (RAD_PER_DEG * rad[dist_obj.unit])
45
47
 
@@ -56,6 +58,12 @@ module GeoMagic
56
58
  res
57
59
  end
58
60
 
61
+ def get_within_rect rectangle
62
+ self.select do |point|
63
+ rectangle.overlaps? point
64
+ end
65
+ end
66
+
59
67
  def get_closest number, options = {}
60
68
  calc_method = get_proc(options[:precision] || :normal)
61
69
  from_loc = get_location options[:from]
@@ -60,8 +60,10 @@ describe "GeoMagic closest" do
60
60
  it "should select all points within 4 km" do
61
61
  points = @radius.create_points_in_square 4
62
62
  persons = Person.random_at points
63
+
64
+ center_person = Person.new 'Man in the center', @center_point
63
65
 
64
- closest = persons.as_map_points.get_within 5.km, :from => @center_point
65
- puts "Persons within 4 km: #{closest.inspect}"
66
+ people_within = persons.as_map_points.get_within 5.km, :from => center_person
67
+ puts "Persons within 4 km of Man in the center: #{people_within.inspect}"
66
68
  end
67
69
  end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'geo_magic'
3
+
4
+ describe "GeoMagic within rectanlge" do
5
+ before do
6
+ @lat1 = 104.88544
7
+ @long1 = 39.06546
8
+
9
+ @center_point = GeoMagic::Point.new @lat1, @long1
10
+ @radius = @center_point.within(10.km)
11
+ @points = @radius.create_points_in_square 10
12
+ end
13
+
14
+ it "should select all points in rectangle" do
15
+ rectangle = GeoMagic::Rectangle.new(GeoMagic::Point.new(115, 50), GeoMagic::Point.new(100, 20))
16
+ points_in_rectangle = @points.as_map_points.get_within_rect rectangle
17
+ puts "points: #{@points}"
18
+
19
+ puts "---"
20
+
21
+ puts "points within rectangle #{rectangle} :"
22
+ puts points_in_rectangle.inspect
23
+ end
24
+ end
25
+
26
+ describe "GeoMagic within rectangle - negative longitude" do
27
+ before do
28
+ @lat1 = -104.88544
29
+ @long1 = 39.06546
30
+
31
+ @center_point = GeoMagic::Point.new @lat1, @long1
32
+ @radius = @center_point.within(10.km)
33
+ @points = @radius.create_points_in_square 10
34
+ end
35
+
36
+ it "should select all points in rectangle" do
37
+ rectangle = GeoMagic::Rectangle.new(GeoMagic::Point.new(-115, 50), GeoMagic::Point.new(-100, 20))
38
+ points_in_rectangle = @points.as_map_points.get_within_rect rectangle
39
+ puts "points: #{@points}"
40
+
41
+ puts "---"
42
+
43
+ puts "points within rectangle #{rectangle} :"
44
+ puts points_in_rectangle.inspect
45
+ end
46
+ end
47
+
metadata CHANGED
@@ -6,7 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 0
7
7
  - 2
8
8
  - 1
9
- version: 0.2.1
9
+ - 1
10
+ version: 0.2.1.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Kristian Mandrup
@@ -164,6 +165,7 @@ files:
164
165
  - lib/geo_magic/meta.rb
165
166
  - lib/geo_magic/point.rb
166
167
  - lib/geo_magic/radius.rb
168
+ - lib/geo_magic/rectangle.rb
167
169
  - lib/geo_magic/remote.rb
168
170
  - lib/geo_magic/util.rb
169
171
  - lib/rails/config.rb
@@ -175,6 +177,7 @@ files:
175
177
  - spec/geo_magic/plane_dist_spec.rb
176
178
  - spec/geo_magic/remote_spec.rb
177
179
  - spec/geo_magic/select_nearest_spec.rb
180
+ - spec/geo_magic/within_rect_spec.rb
178
181
  - spec/spec_helper.rb
179
182
  has_rdoc: true
180
183
  homepage: http://github.com/kristianmandrup/geo_magic
@@ -190,7 +193,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
190
193
  requirements:
191
194
  - - ">="
192
195
  - !ruby/object:Gem::Version
193
- hash: 27646408712887287
196
+ hash: -1898968996463192661
194
197
  segments:
195
198
  - 0
196
199
  version: "0"
@@ -217,4 +220,5 @@ test_files:
217
220
  - spec/geo_magic/plane_dist_spec.rb
218
221
  - spec/geo_magic/remote_spec.rb
219
222
  - spec/geo_magic/select_nearest_spec.rb
223
+ - spec/geo_magic/within_rect_spec.rb
220
224
  - spec/spec_helper.rb