geo_query 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 067870f16295abd5335d979eaff90afe4fc43193
4
- data.tar.gz: f0b5a9c5ae41d5fc49b9cc548968b0356d652257
3
+ metadata.gz: f4e3a23fa1c89f60d664ce9ee75373be8892286f
4
+ data.tar.gz: a78743b2e1201b7005cea4ad1370c17f6bff65e4
5
5
  SHA512:
6
- metadata.gz: 8d0efcf1edb11f5276814a205e8787592bfc04d2ffb5b677c3afbaa53a554ca0a58c6fd69d480580021e1b8de584bfd45f862230bded6bcf9da469135b85a600
7
- data.tar.gz: 440a3a9ab8c43cd7ff3270af33f9f7899d9166a173b7ec9bc1bd33600dc2c7bac9e0988a7e3748023dc01479d8db50a5ff618403bada00442d29817627dc0cac
6
+ metadata.gz: 51e64f9a53a33b0d6304ab27545c0f45aff4f0fa93548d2a040a03902772831ddb7c4e844d3d0be8f9020a04132790a19f73a97da764034faee276f2a5591f25
7
+ data.tar.gz: 1a7dc59f46be27cfa3e221bf3f1737b893b65a91616d3e0d7deb8d2081a2e7e796e168e29be94d53e303d525904560eb9afcfe6bf60885df619ebe88f79abe27
@@ -1,4 +1,5 @@
1
1
  require 'activerecord-postgis-adapter'
2
+ require 'rgeo-activerecord'
2
3
 
3
4
  module GeoQuery
4
5
  class Engine < ::Rails::Engine
@@ -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
- # Instance Methods
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.base_class.table_name}.*, ST_Distance(#{self.base_class.point_column}, ST_GeographyFromText('POINT(#{lng} #{lat})')) AS distance")
26
- .where("ST_DWithin(#{self.base_class.point_column} , ST_GeographyFromText('POINT(#{lng} #{lat})'), #{radius})")
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.base_class.table_name}.*").
33
- where("#{self.base_class.point_column} && ST_MakeEnvelope(?, ?, ?, ?, 4326)",
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
@@ -1,3 +1,3 @@
1
1
  module GeoQuery
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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.1
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-25 00:00:00.000000000 Z
11
+ date: 2015-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails