geo_query 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/geo_query/engine.rb +1 -0
- data/lib/geo_query/geo_queryable.rb +25 -8
- data/lib/geo_query/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4e3a23fa1c89f60d664ce9ee75373be8892286f
|
4
|
+
data.tar.gz: a78743b2e1201b7005cea4ad1370c17f6bff65e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51e64f9a53a33b0d6304ab27545c0f45aff4f0fa93548d2a040a03902772831ddb7c4e844d3d0be8f9020a04132790a19f73a97da764034faee276f2a5591f25
|
7
|
+
data.tar.gz: 1a7dc59f46be27cfa3e221bf3f1737b893b65a91616d3e0d7deb8d2081a2e7e796e168e29be94d53e303d525904560eb9afcfe6bf60885df619ebe88f79abe27
|
data/lib/geo_query/engine.rb
CHANGED
@@ -7,32 +7,49 @@ module GeoQuery
|
|
7
7
|
mattr_accessor :point_column
|
8
8
|
@@point_column = nil
|
9
9
|
|
10
|
-
# Validations
|
11
|
-
before_validation :save_coordinates
|
12
|
-
|
13
10
|
attr_accessor :lat, :lng
|
14
11
|
|
15
|
-
#
|
12
|
+
# Validations
|
13
|
+
validate :save_coordinates
|
14
|
+
|
16
15
|
def save_coordinates
|
17
16
|
if lat.present? && lng.present?
|
18
17
|
self.coordinates = "POINT(#{lng} #{lat})"
|
18
|
+
elsif (lat.present? && lng.blank?) || (lng.present? && lat.blank?)
|
19
|
+
errors.add(self.point_column, "requires both latitude and longitude")
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
23
|
+
# Instance Methods
|
24
|
+
def rgeo_factory
|
25
|
+
RGeo::Geographic.spherical_factory(:srid => 4326)
|
26
|
+
end
|
27
|
+
|
28
|
+
def near(radius=500) #radius in metres
|
29
|
+
# x = longitude && y = latitude
|
30
|
+
return User.near_lat_lng(st_coordinates.y, st_coordinates.x, radius) if st_coordinates
|
31
|
+
self.class.none
|
32
|
+
end
|
33
|
+
|
34
|
+
def st_coordinates
|
35
|
+
send("#{self.point_column}")
|
36
|
+
end
|
37
|
+
|
22
38
|
# Class methods
|
23
39
|
## Returns other objects within the given radius of a point
|
24
40
|
def self.near_lat_lng(lat, lng, radius=500) #radius in metres
|
25
|
-
select("#{self.
|
26
|
-
.where("ST_DWithin(#{self.
|
41
|
+
select("#{self.table_name}.*, ST_Distance(#{self.point_column}, ST_GeographyFromText('POINT(#{lng} #{lat})')) AS distance")
|
42
|
+
.where("ST_DWithin(#{self.point_column} , ST_GeographyFromText('POINT(#{lng} #{lat})'), #{radius})")
|
27
43
|
.order("distance ASC")
|
28
44
|
end
|
29
45
|
|
30
46
|
## Returns all objects within a bounding box
|
31
47
|
def self.within_bounding_box(min_lat, min_lng, max_lat, max_lng)
|
32
|
-
select("#{self.
|
33
|
-
where("#{self.
|
48
|
+
select("#{self.table_name}.*").
|
49
|
+
where("#{self.point_column} && ST_MakeEnvelope(?, ?, ?, ?, 4326)",
|
34
50
|
min_lng, min_lat, max_lng, max_lat)
|
35
51
|
end
|
52
|
+
|
36
53
|
end
|
37
54
|
|
38
55
|
module ClassMethods
|
data/lib/geo_query/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geo_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- William Porter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|