geo_magic 0.1.0 → 0.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.1.0
1
+ 0.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.1.0"
8
+ s.version = "0.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"]
@@ -30,15 +30,18 @@ Gem::Specification.new do |s|
30
30
  "lib/geo_magic/calculate.rb",
31
31
  "lib/geo_magic/distance.rb",
32
32
  "lib/geo_magic/location.rb",
33
+ "lib/geo_magic/map_point.rb",
33
34
  "lib/geo_magic/meta.rb",
34
35
  "lib/geo_magic/point.rb",
36
+ "lib/geo_magic/radius.rb",
35
37
  "lib/geo_magic/remote.rb",
36
38
  "lib/geo_magic/util.rb",
37
- "spec/geo_magic_calculate_spec.rb",
38
- "spec/geo_magic_include_calc_spec.rb",
39
- "spec/geo_magic_include_remote_spec.rb",
40
- "spec/geo_magic_plane_dist_spec.rb",
41
- "spec/geo_magic_remote_spec.rb",
39
+ "spec/geo_magic/calculate_spec.rb",
40
+ "spec/geo_magic/include_calc_spec.rb",
41
+ "spec/geo_magic/include_remote_spec.rb",
42
+ "spec/geo_magic/plane_dist_spec.rb",
43
+ "spec/geo_magic/remote_spec.rb",
44
+ "spec/geo_magic/select_nearest_spec.rb",
42
45
  "spec/spec_helper.rb"
43
46
  ]
44
47
  s.homepage = %q{http://github.com/kristianmandrup/geo_magic}
@@ -47,11 +50,12 @@ Gem::Specification.new do |s|
47
50
  s.rubygems_version = %q{1.3.7}
48
51
  s.summary = %q{Get your IP and location data and calculate distances on the globe}
49
52
  s.test_files = [
50
- "spec/geo_magic_calculate_spec.rb",
51
- "spec/geo_magic_include_calc_spec.rb",
52
- "spec/geo_magic_include_remote_spec.rb",
53
- "spec/geo_magic_plane_dist_spec.rb",
54
- "spec/geo_magic_remote_spec.rb",
53
+ "spec/geo_magic/calculate_spec.rb",
54
+ "spec/geo_magic/include_calc_spec.rb",
55
+ "spec/geo_magic/include_remote_spec.rb",
56
+ "spec/geo_magic/plane_dist_spec.rb",
57
+ "spec/geo_magic/remote_spec.rb",
58
+ "spec/geo_magic/select_nearest_spec.rb",
55
59
  "spec/spec_helper.rb"
56
60
  ]
57
61
 
@@ -1,4 +1,5 @@
1
1
  require 'geo-distance'
2
2
  require 'geo_magic/remote'
3
3
  require 'geo_magic/calculate'
4
+ require 'geo_magic/distance'
4
5
  require 'geo_magic/meta'
@@ -12,9 +12,7 @@ module GeoMagic
12
12
  module ClassMethods
13
13
  def distance from_point, to_point, options = { :unit => :meters }
14
14
  points = extract_points from_point, to_point
15
- puts "points: #{points.inspect}"
16
15
  dist = ::GeoDistance.distance( *points )[options[:unit]]
17
- puts "the distance from (#{points[0]}, #{points[1]}) to (#{points[2]}, #{points[3]}) is: #{dist}"
18
16
  dist.number
19
17
  end
20
18
 
@@ -1,26 +1,26 @@
1
1
  module GeoDistance
2
2
  class Haversine < DistanceFormula
3
- def self.point_distance( point_a, point_b, units = :meters )
3
+ def self.point_distance point_a, point_b
4
4
  points = GeoMagic::Util.extract_points point_a, point_b
5
- distance *points, units
5
+ distance *points
6
6
  end
7
7
  end
8
8
  end
9
9
 
10
10
  module GeoDistance
11
11
  class Spherical < DistanceFormula
12
- def self.point_distance( point_a, point_b, units = :meters )
12
+ def self.point_distance point_a, point_b
13
13
  points = GeoMagic::Util.extract_points point_a, point_b
14
- distance *points, units
14
+ distance *points
15
15
  end
16
16
  end
17
17
  end
18
18
 
19
19
  module GeoDistance
20
20
  class Vincenty < DistanceFormula
21
- def self.point_distance( point_a, point_b, units = :meters )
21
+ def self.point_distance point_a, point_b
22
22
  points = GeoMagic::Util.extract_points point_a, point_b
23
- distance *points, units
23
+ distance *points
24
24
  end
25
25
  end
26
26
  end
@@ -1,7 +1,9 @@
1
+ require 'geo_magic/map_point'
2
+
1
3
  module GeoMagic
2
- class Location #:nodoc:
3
- attr_accessor :latitude, :longitude, :ip
4
- attr_writer :city, :region, :country
4
+ class Location < MapPoint
5
+ attr_accessor :ip
6
+ attr_writer :city, :region, :country
5
7
 
6
8
  def initialize raw_location
7
9
  raw_location.each_pair do |key, value|
@@ -13,7 +15,7 @@ module GeoMagic
13
15
  def [] key
14
16
  send key
15
17
  end
16
-
18
+
17
19
  class City
18
20
  attr_accessor :name, :metrocode, :zipcode
19
21
 
@@ -0,0 +1,16 @@
1
+ require 'geo_magic/radius'
2
+
3
+ module GeoMagic
4
+ class MapPoint
5
+ attr_accessor :latitude, :longitude, :dist
6
+
7
+ def initialize latitude, longitude
8
+ @latitude = latitude
9
+ @longitude = longitude
10
+ end
11
+
12
+ def within(distance)
13
+ GeoMagic::Radius.new self, distance
14
+ end
15
+ end
16
+ end
@@ -1,12 +1,12 @@
1
+ require 'geo_magic/map_point'
2
+
1
3
  module GeoMagic
2
- class Point #:nodoc:
3
- attr_accessor :latitude, :longitude
4
+ class Point < MapPoint
4
5
 
5
6
  def initialize latitude, longitude
6
- @latitude = latitude
7
- @longitude = longitude
7
+ super
8
8
  end
9
-
9
+
10
10
  def to_hash mode= :long
11
11
  case mode
12
12
  when :short
@@ -15,9 +15,12 @@ module GeoMagic
15
15
  {:latitude => latitude, :longitude => longitude}
16
16
  end
17
17
  end
18
+
19
+ def to_location
20
+ end
18
21
 
19
22
  def to_s
20
- "(lat: #{latitude}, long: #{longitude})"
23
+ "(lat: #{latitude}, long: #{longitude}, dist: #{dist})"
21
24
  end
22
25
  end
23
26
  end
@@ -0,0 +1,43 @@
1
+ module GeoMagic
2
+ class Radius
3
+ attr_accessor :center, :distance
4
+
5
+ RAD_PER_DEG = 0.017453293
6
+
7
+ # radius of the great circle in miles
8
+ # radius in kilometers...some algorithms use 6367
9
+ def rad
10
+ {:km => 6371, :miles => 3956, :feet => 20895592, :meters => 6371000}
11
+ end
12
+
13
+ def initialize center, distance
14
+ @center = center
15
+ @distance = distance
16
+ end
17
+
18
+ def create_points number
19
+ conversion = (RAD_PER_DEG * rad[distance.unit])
20
+
21
+ max_radius_rad = distance.distance / conversion
22
+
23
+ range = (max_radius_rad * normalize).to_i
24
+
25
+ number.times.inject([]) do |res, n|
26
+ dlong = (get_randowm_radiant(range) / normalize) * conversion
27
+ dlat = (get_randowm_radiant(range) / normalize) * conversion
28
+
29
+ point = GeoMagic::Point.new @center.latitude + dlat, @center.longitude + dlong
30
+ res << point
31
+ res
32
+ end
33
+ end
34
+
35
+ def get_randowm_radiant range
36
+ rand(range) - (range / 2)
37
+ end
38
+
39
+ def normalize
40
+ 1000000.0
41
+ end
42
+ end
43
+ end
@@ -10,7 +10,84 @@ module GeoMagic
10
10
  [point.latitude, point.longitude]
11
11
  when Array
12
12
  [point[0], point[1]]
13
+ end
14
+ end
15
+
16
+ def self.extract_points from_point, to_point
17
+ [extract_point(from_point), extract_point(to_point)].flatten.map(&:to_f)
18
+ end
19
+ end
20
+ end
21
+
22
+ class Array
23
+ def as_locations
24
+ self.extend GeoMagic::LocationsList
25
+ end
26
+
27
+ def sort_by_distance
28
+ self.sort_by { |item| item.dist }
29
+ end
30
+ end
31
+
32
+ module GeoMagic
33
+ module LocationsList
34
+ RAD_PER_DEG = 0.017453293
35
+
36
+ def rad
37
+ {:km => 6371, :miles => 3956, :feet => 20895592, :meters => 6371000}
38
+ end
39
+
40
+ def get_within dist_obj, options = {:precision => :lowest}
41
+ calc_method = get_proc(options[:precision] || :normal)
42
+ from_loc = get_location options[:from]
43
+
44
+ dist = dist_obj.distance / (RAD_PER_DEG * rad[dist_obj.unit])
45
+
46
+ res = []
47
+ spots = populate_distance(calc_method, from_loc)
48
+ spots.sort_by_distance.select do |item|
49
+ if item.dist <= dist
50
+ res << item
51
+ else
52
+ break
53
+ end
54
+ end
55
+ res
56
+ end
57
+
58
+ def get_closest number, options = {}
59
+ calc_method = get_proc(options[:precision] || :normal)
60
+ from_loc = get_location options[:from]
61
+ populate_distance(calc_method, from_loc).sort_by_distance[0..number]
62
+ end
63
+
64
+ protected
65
+
66
+ def populate_distance calc_method, from_loc
67
+ self.map! do |item|
68
+ dist_obj = calc_method.call(from_loc, item)
69
+ item.dist = dist_obj.distance
70
+ item
71
+ end
72
+ end
73
+
74
+ def get_location location
75
+ raise ArgumentError, "You must specify a :from location" if !location
76
+ raise ArgumentError, "You must specify a :from location that is a kind of GeoMagic::MapPoint" if !location.kind_of?(GeoMagic::MapPoint)
77
+ location
78
+ end
79
+
80
+ def get_proc precision
81
+ case precision
82
+ when :low
83
+ Proc.new {|point_a, point_b| ::GeoMagic::Calculate.plane_distance point_a, point_b }
84
+ when :normal
85
+ Proc.new {|point_a, point_b| ::GeoDistance::Haversine.point_distance point_a, point_b }
86
+ when :high
87
+ Proc.new {|point_a, point_b| ::GeoDistance::Vincenty.point_distance point_a, point_b }
88
+ else
89
+ raise ArgumentError, "Unknown precision: #{precision}"
13
90
  end
14
91
  end
15
92
  end
16
- end
93
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+ require 'geo_magic'
3
+ require 'geo_magic/remote'
4
+
5
+ describe "GeoMagic closest" do
6
+ before do
7
+ @long1 = -104.88544
8
+ @lat1 = 39.06546
9
+
10
+ @center_point = GeoMagic::Point.new @long1, @lat1
11
+ @radius = @center_point.within(10.km)
12
+ @points = @radius.create_points 10
13
+ end
14
+
15
+
16
+ it "should select the closest 3 points" do
17
+ # puts "radius: #{@radius.inspect}"
18
+ # puts "points: #{@points.inspect}"
19
+ closest = @points.as_locations.get_closest 3, :from => @center_point
20
+ puts "3 closest points: #{closest.inspect}"
21
+ end
22
+
23
+ it "should select all points within 4 km" do
24
+ # puts "radius: #{@radius.inspect}"
25
+ # puts "points: #{@points.inspect}"
26
+ closest = @points.as_locations.get_within 4.km, :from => @center_point
27
+ puts "points within 4 km: #{closest.inspect}"
28
+ end
29
+
30
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -158,15 +158,18 @@ files:
158
158
  - lib/geo_magic/calculate.rb
159
159
  - lib/geo_magic/distance.rb
160
160
  - lib/geo_magic/location.rb
161
+ - lib/geo_magic/map_point.rb
161
162
  - lib/geo_magic/meta.rb
162
163
  - lib/geo_magic/point.rb
164
+ - lib/geo_magic/radius.rb
163
165
  - lib/geo_magic/remote.rb
164
166
  - lib/geo_magic/util.rb
165
- - spec/geo_magic_calculate_spec.rb
166
- - spec/geo_magic_include_calc_spec.rb
167
- - spec/geo_magic_include_remote_spec.rb
168
- - spec/geo_magic_plane_dist_spec.rb
169
- - spec/geo_magic_remote_spec.rb
167
+ - spec/geo_magic/calculate_spec.rb
168
+ - spec/geo_magic/include_calc_spec.rb
169
+ - spec/geo_magic/include_remote_spec.rb
170
+ - spec/geo_magic/plane_dist_spec.rb
171
+ - spec/geo_magic/remote_spec.rb
172
+ - spec/geo_magic/select_nearest_spec.rb
170
173
  - spec/spec_helper.rb
171
174
  has_rdoc: true
172
175
  homepage: http://github.com/kristianmandrup/geo_magic
@@ -182,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
182
185
  requirements:
183
186
  - - ">="
184
187
  - !ruby/object:Gem::Version
185
- hash: 2192296416920126926
188
+ hash: 4556321129170548695
186
189
  segments:
187
190
  - 0
188
191
  version: "0"
@@ -202,9 +205,10 @@ signing_key:
202
205
  specification_version: 3
203
206
  summary: Get your IP and location data and calculate distances on the globe
204
207
  test_files:
205
- - spec/geo_magic_calculate_spec.rb
206
- - spec/geo_magic_include_calc_spec.rb
207
- - spec/geo_magic_include_remote_spec.rb
208
- - spec/geo_magic_plane_dist_spec.rb
209
- - spec/geo_magic_remote_spec.rb
208
+ - spec/geo_magic/calculate_spec.rb
209
+ - spec/geo_magic/include_calc_spec.rb
210
+ - spec/geo_magic/include_remote_spec.rb
211
+ - spec/geo_magic/plane_dist_spec.rb
212
+ - spec/geo_magic/remote_spec.rb
213
+ - spec/geo_magic/select_nearest_spec.rb
210
214
  - spec/spec_helper.rb