easy_postgis 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b65ba19ced7e796800213e32ddb5f02efb3fdfbc
4
- data.tar.gz: 4d95e9569baa4700a1d27dcc31a82e5024397341
3
+ metadata.gz: 16cb8dd649020b96b5673cbbc75a15d31bb81e95
4
+ data.tar.gz: 99c08a8ea53b3ae5f0ca417f7884b3cf0b4c8c24
5
5
  SHA512:
6
- metadata.gz: b898a4b807458e2caa02e3b3da3584820e0ee9961401b35284e7f382a6b01c2df5aa68866ebb90760b48c8caf853e21ebd7b0666785f7ca0fe62244a2ccb99b6
7
- data.tar.gz: 20c22c099fcdc1a2453eea974db9663be24818a91a35aa767e667f99a0d81926a588c94e3f41bb9370ab3d1facfb90fdfa43453ce06c3a2308a5ce7edbad242b
6
+ metadata.gz: a71995dd7de2c90891791714794133c86f79b8415a4171e4ed2363e22feb50d4c6bfa906e07cab2e41c9a9bc229eabf36d5b6701d0cbfbfabcb62ebed934675a
7
+ data.tar.gz: 6840cdb099997d71bf179712dbbc9c64eaf314526ddc9c8bcd252afa64e2ff78df4756a8a01037bc65b56719abf398a5030b8bf8e3b6d4ec266de944a987f8b6
data/README.md CHANGED
@@ -5,7 +5,7 @@ EasyPostgis is a gemification of the code described in Nick Gauthier's 2013 blog
5
5
 
6
6
  [1]: http://ngauthier.com/2013/08/postgis-and-rails-a-simple-approach.html
7
7
 
8
- It adds 2 scopes to your model:
8
+ It creates 2 scopes for use by your model:
9
9
 
10
10
  * near(point, distance_in_meters)
11
11
  * with_distance(point)
@@ -13,6 +13,16 @@ It adds 2 scopes to your model:
13
13
  *near* filters the rows based on distance from the point.
14
14
  *with_distance* adds a distance column to the result.
15
15
 
16
+ ```ruby
17
+ # find the node closest to a lat lon
18
+ def self.nearest_node(lat, lng)
19
+ point = OpenStruct.new(:lat => lat, :lng => lng)
20
+ Node.with_distance(point).order("distance").first # use quoted string rather than symbol for
21
+ # distance so that rails does not append
22
+ # a table name
23
+ end
24
+ ```
25
+
16
26
  ## Installation
17
27
 
18
28
  Add this line to your application's Gemfile:
@@ -31,6 +41,29 @@ Or install it yourself as:
31
41
 
32
42
  ## Usage
33
43
 
44
+ Add the postgis extension and create the index for your table in a migration:
45
+
46
+ ```ruby
47
+ class AddPostgis < ActiveRecord::Migration
48
+   def up
49
+     execute 'create extension postgis'
50
+     execute %{
51
+       create index index_on_node_location on nodes using gist (
52
+         ST_GeographyFromText(
53
+           'SRID=4326;POINT(' || nodes.lng || ' ' || nodes.lat || ')'
54
+         )
55
+       )
56
+     }
57
+   end
58
+
59
+   def down
60
+     execute %{drop index index_on_node_location}
61
+   end
62
+ end
63
+ ```
64
+
65
+ Include EasyPostgis in your model:
66
+
34
67
  ```ruby
35
68
  class Address
36
69
  # has lat and lng attributes
@@ -45,8 +78,10 @@ Or install it yourself as:
45
78
 
46
79
  TODO:
47
80
 
48
- * you must create the test database: psql> create database easy_postgis_test;
81
+ * you must create the test database to run the tests: psql> create database easy_postgis_test;
49
82
  * you must add gis: psql> create extension postgis;
83
+ * we should allow for customization of the lat/lng column names
84
+ * we should provide a generator to create a migration that adds the index
50
85
 
51
86
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
87
 
@@ -10,19 +10,19 @@ module EasyPostgis
10
10
 
11
11
  class_methods do
12
12
  def near_sql(other, distance_in_meters)
13
- "ST_DWithin(#{geography_from_table}, #{geography_from_point(other.lng, other.lat)}, %d )" % [distance_in_meters]
13
+ "ST_DWithin(#{geography_from_table},#{geography_from_point(other.lng,other.lat)},%d)" % [distance_in_meters]
14
14
  end
15
15
 
16
16
  def distance_select_sql(other)
17
- "ST_Distance(#{geography_from_table}, #{geography_from_point(other.lng, other.lat)}) as distance"
17
+ "ST_Distance(#{geography_from_table},#{geography_from_point(other.lng,other.lat)}) as distance"
18
18
  end
19
19
 
20
20
  def geography_from_table
21
- "ST_GeographyFromText(' SRID=4326; POINT(' || #{table_name}.lng || ' ' || #{table_name}.lat || ') ')"
21
+ "ST_GeographyFromText('SRID=4326;POINT(' || #{table_name}.lng || ' ' || #{table_name}.lat || ')')"
22
22
  end
23
23
 
24
24
  def geography_from_point(lon, lat)
25
- "ST_GeographyFromText( 'SRID=4326; POINT(%f %f)' )" % [lon, lat]
25
+ "ST_GeographyFromText('SRID=4326;POINT(%f %f)')" % [lon, lat]
26
26
  end
27
27
  end
28
28
  end
@@ -1,3 +1,3 @@
1
1
  module EasyPostgis
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_postgis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Felkins
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-10 00:00:00.000000000 Z
11
+ date: 2017-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -146,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  version: '0'
147
147
  requirements: []
148
148
  rubyforge_project:
149
- rubygems_version: 2.4.8
149
+ rubygems_version: 2.6.12
150
150
  signing_key:
151
151
  specification_version: 4
152
152
  summary: Adds simple distance scopes that utilize PostGIS indexes to your Rails models.