geo_query 0.0.3 → 0.0.4
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/geo_queryable.rb +22 -15
- data/lib/geo_query/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b42919fce03a439c5b52569ce78142cdb180140
|
4
|
+
data.tar.gz: 1d2bbc101cd898b836ceda5f428e7ea0f7b3baf5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b9c13306bc6eb17e946bd613d7bba06def653289c99d2f52f0b99bee465692a40cc49d6ff49bff10b5d51ada7e60fd5c3e9592986fd65e8b0f1633aba15fcbb
|
7
|
+
data.tar.gz: d60f6037fe67d02b8b8f81553da38fbf70622003fbe1b6abf8ea32da4689b24f4754a1fa4b32198a8d18d5bc511e6bbd5361487f3b5cb13c0145d65a537191be
|
@@ -7,10 +7,13 @@ module GeoQuery
|
|
7
7
|
mattr_accessor :point_column
|
8
8
|
@@point_column = :coordinates
|
9
9
|
|
10
|
+
mattr_accessor :track_updates
|
11
|
+
@@track_updates = false
|
12
|
+
|
10
13
|
# Accessors
|
11
|
-
attr_accessor :lat, :
|
14
|
+
attr_accessor :lat, :lon
|
12
15
|
|
13
|
-
def
|
16
|
+
def location_did_change?
|
14
17
|
method("#{self.point_column}_changed?").call
|
15
18
|
end
|
16
19
|
|
@@ -19,9 +22,9 @@ module GeoQuery
|
|
19
22
|
end
|
20
23
|
|
21
24
|
def save_coordinates
|
22
|
-
if lat.present? &&
|
23
|
-
self.coordinates = "POINT(#{
|
24
|
-
elsif (lat.present? &&
|
25
|
+
if lat.present? && lon.present?
|
26
|
+
self.coordinates = "POINT(#{lon} #{lat})"
|
27
|
+
elsif (lat.present? && lon.blank?) || (lon.present? && lat.blank?)
|
25
28
|
errors.add(self.point_column, "requires both latitude and longitude")
|
26
29
|
end
|
27
30
|
end
|
@@ -33,7 +36,7 @@ module GeoQuery
|
|
33
36
|
|
34
37
|
def near(radius=500) #radius in metres
|
35
38
|
# x = longitude && y = latitude
|
36
|
-
return self.class.
|
39
|
+
return self.class.near_lat_lon(st_coordinates.y, st_coordinates.x, radius) if st_coordinates
|
37
40
|
self.class.none
|
38
41
|
end
|
39
42
|
|
@@ -43,17 +46,17 @@ module GeoQuery
|
|
43
46
|
|
44
47
|
# Class methods
|
45
48
|
## Returns other objects within the given radius of a point
|
46
|
-
def self.
|
47
|
-
select("#{self.table_name}.*, ST_Distance(#{self.point_column}, ST_GeographyFromText('POINT(#{
|
48
|
-
.where("ST_DWithin(#{self.point_column} , ST_GeographyFromText('POINT(#{
|
49
|
+
def self.near_lat_lon(lat, lon, radius=500) #radius in metres
|
50
|
+
select("#{self.table_name}.*, ST_Distance(#{self.point_column}, ST_GeographyFromText('POINT(#{lon} #{lat})')) AS distance")
|
51
|
+
.where("ST_DWithin(#{self.table_name}.#{self.point_column} , ST_GeographyFromText('POINT(#{lon} #{lat})'), #{radius})")
|
49
52
|
.order("distance ASC")
|
50
53
|
end
|
51
54
|
|
52
55
|
## Returns all objects within a bounding box
|
53
|
-
def self.within_bounding_box(min_lat,
|
56
|
+
def self.within_bounding_box(min_lat, min_lon, max_lat, max_lon)
|
54
57
|
select("#{self.table_name}.*").
|
55
|
-
where("#{self.point_column} && ST_MakeEnvelope(?, ?, ?, ?, 4326)",
|
56
|
-
|
58
|
+
where("#{self.table_name}.#{self.point_column} && ST_MakeEnvelope(?, ?, ?, ?, 4326)",
|
59
|
+
min_lon, min_lat, max_lon, max_lat)
|
57
60
|
end
|
58
61
|
|
59
62
|
end
|
@@ -61,13 +64,17 @@ module GeoQuery
|
|
61
64
|
module ClassMethods
|
62
65
|
# Init method
|
63
66
|
def geo_queryable(options = {})
|
64
|
-
self.base_class.point_column
|
67
|
+
self.base_class.point_column = options[:column] || :coordinates
|
68
|
+
self.base_class.track_updates = options[:track_updates] || false
|
65
69
|
|
66
70
|
# Validations
|
67
71
|
validate :save_coordinates
|
68
72
|
|
69
|
-
|
70
|
-
|
73
|
+
if self.base_class.track_updates
|
74
|
+
|
75
|
+
# Callbacks
|
76
|
+
before_save :set_location_updated_at, if: :location_did_change?
|
77
|
+
end
|
71
78
|
end
|
72
79
|
end
|
73
80
|
end
|
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.4
|
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-
|
11
|
+
date: 2015-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - '='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 3.
|
33
|
+
version: 3.1.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - '='
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 3.
|
40
|
+
version: 3.1.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: pg
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|