asari 0.9.3 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/asari.rb +1 -0
- data/lib/asari/geography.rb +112 -0
- data/lib/asari/version.rb +1 -1
- data/spec/geography_spec.rb +31 -0
- metadata +15 -17
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7ee219d6feb2cf54eeaddd1fd259618b2e877884
|
4
|
+
data.tar.gz: 2554d88f618b327ca3f1a7fc1dcf0e73b4c1bfa1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 644ee6278143ee097db1f5a85b994de8c076e264eb1a135c355c93631efe80d69b0147699266f88ea5d785c67af24970ba52edcb71ddac7b65fdb08f89f56149
|
7
|
+
data.tar.gz: d9f6b562b44862d6d66fbee0e911cc0ab81ea8e0278b0641eeddfe0885cd03d49c8804254375723388e72cd06b4f59209ff0467d192a1a35a20538c745aab96c
|
data/lib/asari.rb
CHANGED
@@ -0,0 +1,112 @@
|
|
1
|
+
class Asari
|
2
|
+
# Public: This module contains helper methods that serialize and deserialize
|
3
|
+
# latitudes and longitudes to store on Cloudsearch. For more information, see:
|
4
|
+
#
|
5
|
+
# http://docs.aws.amazon.com/cloudsearch/latest/developerguide/geosearch.html
|
6
|
+
#
|
7
|
+
module Geography
|
8
|
+
EARTH_RADIUS = 6367444
|
9
|
+
METERS_PER_DEGREE_OF_LATITUDE = 111133
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
# Public: Converts coordinates to unsigned integers that store up to three
|
14
|
+
# place values.
|
15
|
+
#
|
16
|
+
# options - the options hash requires:
|
17
|
+
# lat: a Float
|
18
|
+
# lng: a Float
|
19
|
+
#
|
20
|
+
# Examples:
|
21
|
+
#
|
22
|
+
# Asari::GeographyConversion.degrees_to_int(45.52, 122.6819)
|
23
|
+
# #=> {:lat=>25062714160, :lng=>1112993466}
|
24
|
+
#
|
25
|
+
# Returns: a Hash containing :lat and :lng keys with Integer values
|
26
|
+
#
|
27
|
+
def degrees_to_int(options)
|
28
|
+
latitude = latitude_to_int(options[:lat])
|
29
|
+
longitude = longitude_to_int(options[:lng], options[:lat])
|
30
|
+
{ lat: latitude, lng: longitude }
|
31
|
+
end
|
32
|
+
|
33
|
+
# Public: Converts unsigned integers created with `degrees_to_int` back to
|
34
|
+
# the standard Geographic Coordinate System.
|
35
|
+
#
|
36
|
+
# options - the options hash requires:
|
37
|
+
# lat: an Integer
|
38
|
+
# lng: an Integer
|
39
|
+
#
|
40
|
+
# Examples:
|
41
|
+
#
|
42
|
+
# Asari::GeographyConversion.int_to_degrees(lat: 25062714160, lng: 1112993466)
|
43
|
+
# #=> {:lat=>45.52, :lng=>-122.682}
|
44
|
+
#
|
45
|
+
# Returns: a Hash containing :lat and :lng keys with Float values
|
46
|
+
#
|
47
|
+
def int_to_degrees(options)
|
48
|
+
latitude = latitude_to_degrees(options[:lat])
|
49
|
+
longitude = longitude_to_degrees(options[:lng], latitude)
|
50
|
+
{ lat: latitude, lng: longitude }
|
51
|
+
end
|
52
|
+
|
53
|
+
# Public: Calculates a range of integers to search within from a point
|
54
|
+
# and a distance in meters. This is used to search a certain distance from
|
55
|
+
# a point in Cloudsearch.
|
56
|
+
#
|
57
|
+
# options - the options hash requires:
|
58
|
+
# meters: an Integer
|
59
|
+
# lat: a Float
|
60
|
+
# lng: a Float
|
61
|
+
#
|
62
|
+
# Examples:
|
63
|
+
#
|
64
|
+
# Asari::GeographyConversion.coordinate_box(lat: 25062714160, lng: 1112993466, miles: 5)
|
65
|
+
# #=> {:lat=>25062714160, :lng=>1112993466}
|
66
|
+
#
|
67
|
+
# Returns: a Hash containing :lat and :lng keys with Range values
|
68
|
+
#
|
69
|
+
def coordinate_box(options)
|
70
|
+
latitude = options[:lat]
|
71
|
+
longitude = options[:lng]
|
72
|
+
|
73
|
+
earth_radius_at_latitude = EARTH_RADIUS * Math.cos(latitude * ( Math::PI / 180 ))
|
74
|
+
|
75
|
+
change_in_latitude = ( options[:meters].to_f / EARTH_RADIUS ) * ( 180 / Math::PI )
|
76
|
+
change_in_longitude = ( options[:meters].to_f / earth_radius_at_latitude ) * ( 180 / Math::PI )
|
77
|
+
|
78
|
+
bottom = latitude_to_int(latitude - change_in_latitude)
|
79
|
+
top = latitude_to_int(latitude + change_in_latitude)
|
80
|
+
|
81
|
+
left = longitude_to_int(longitude - change_in_longitude, latitude)
|
82
|
+
right = longitude_to_int(longitude + change_in_longitude, latitude)
|
83
|
+
|
84
|
+
{ lat: (bottom.round..top.round), lng: (left.round..right.round) }
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
private
|
89
|
+
def latitude_to_int(degrees)
|
90
|
+
((degrees + 180) * METERS_PER_DEGREE_OF_LATITUDE * 1000).round
|
91
|
+
end
|
92
|
+
|
93
|
+
def latitude_to_degrees(int)
|
94
|
+
((int / METERS_PER_DEGREE_OF_LATITUDE / 1000.0) - 180).round(3)
|
95
|
+
end
|
96
|
+
|
97
|
+
def longitude_to_int(degrees, latitude)
|
98
|
+
meters = meters_per_degree_of_longitude(latitude)
|
99
|
+
((degrees + 180) * meters * 1000).round
|
100
|
+
end
|
101
|
+
|
102
|
+
def longitude_to_degrees(int, latitude_in_degrees)
|
103
|
+
meters = meters_per_degree_of_longitude(latitude_in_degrees)
|
104
|
+
((int / meters / 1000.0) - 180).round(3)
|
105
|
+
end
|
106
|
+
|
107
|
+
def meters_per_degree_of_longitude(latitude)
|
108
|
+
METERS_PER_DEGREE_OF_LATITUDE * Math.cos(latitude)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
data/lib/asari/version.rb
CHANGED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
describe Asari::Geography do
|
4
|
+
|
5
|
+
let(:convert) { Asari::Geography }
|
6
|
+
|
7
|
+
describe "#degrees_to_int" do
|
8
|
+
it "converts standard lat and lng to integers" do
|
9
|
+
result = convert.degrees_to_int(lat: 45.52, lng: 122.682)
|
10
|
+
expect(result).to eq({ lat: 25062714160, lng: 1112993834 })
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#int_to_degrees" do
|
15
|
+
it "converts back successfully" do
|
16
|
+
integers = convert.degrees_to_int(lat: -45.52, lng: 122.682)
|
17
|
+
result = convert.int_to_degrees(integers)
|
18
|
+
expect(result).to eq({ lat: -45.52, lng: 122.682 })
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#coordinate_box" do
|
23
|
+
it "creates a range from a coordinate and a distance in miles" do
|
24
|
+
result = convert.coordinate_box(lat: 45.52, lng: 122.682, meters: 5000)
|
25
|
+
expect(result).to eq({
|
26
|
+
lat: 25057714154..25067714166,
|
27
|
+
lng: 1112757718..1113229951
|
28
|
+
})
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
CHANGED
@@ -1,46 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: asari
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.10.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Tommy Morgan
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
11
|
+
date: 2013-09-14 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: httparty
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rspec
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
description: Asari s a Ruby interface for AWS CloudSearch
|
@@ -59,37 +54,38 @@ files:
|
|
59
54
|
- lib/asari/active_record.rb
|
60
55
|
- lib/asari/collection.rb
|
61
56
|
- lib/asari/exceptions.rb
|
57
|
+
- lib/asari/geography.rb
|
62
58
|
- lib/asari/version.rb
|
63
59
|
- spec/active_record_spec.rb
|
64
60
|
- spec/asari_spec.rb
|
65
61
|
- spec/collection_spec.rb
|
66
62
|
- spec/conditionals_spec.rb
|
67
63
|
- spec/documents_spec.rb
|
64
|
+
- spec/geography_spec.rb
|
68
65
|
- spec/search_spec.rb
|
69
66
|
- spec_helper.rb
|
70
67
|
homepage: http://github.com/duwanis/asari
|
71
68
|
licenses: []
|
69
|
+
metadata: {}
|
72
70
|
post_install_message:
|
73
71
|
rdoc_options: []
|
74
72
|
require_paths:
|
75
73
|
- lib
|
76
74
|
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
-
none: false
|
78
75
|
requirements:
|
79
|
-
- -
|
76
|
+
- - '>='
|
80
77
|
- !ruby/object:Gem::Version
|
81
78
|
version: '0'
|
82
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
-
none: false
|
84
80
|
requirements:
|
85
|
-
- -
|
81
|
+
- - '>='
|
86
82
|
- !ruby/object:Gem::Version
|
87
83
|
version: '0'
|
88
84
|
requirements: []
|
89
85
|
rubyforge_project: asari
|
90
|
-
rubygems_version:
|
86
|
+
rubygems_version: 2.0.3
|
91
87
|
signing_key:
|
92
|
-
specification_version:
|
88
|
+
specification_version: 4
|
93
89
|
summary: Asari is a Ruby interface for AWS CloudSearch.
|
94
90
|
test_files:
|
95
91
|
- spec/active_record_spec.rb
|
@@ -97,4 +93,6 @@ test_files:
|
|
97
93
|
- spec/collection_spec.rb
|
98
94
|
- spec/conditionals_spec.rb
|
99
95
|
- spec/documents_spec.rb
|
96
|
+
- spec/geography_spec.rb
|
100
97
|
- spec/search_spec.rb
|
98
|
+
has_rdoc:
|