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 +4 -4
- data/README.md +37 -2
- data/lib/easy_postgis.rb +4 -4
- data/lib/easy_postgis/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16cb8dd649020b96b5673cbbc75a15d31bb81e95
|
4
|
+
data.tar.gz: 99c08a8ea53b3ae5f0ca417f7884b3cf0b4c8c24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
|
data/lib/easy_postgis.rb
CHANGED
@@ -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}
|
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}
|
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('
|
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(
|
25
|
+
"ST_GeographyFromText('SRID=4326;POINT(%f %f)')" % [lon, lat]
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
data/lib/easy_postgis/version.rb
CHANGED
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.
|
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:
|
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.
|
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.
|