gotime-postgis_adapter 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,130 @@
1
+ ###
2
+ ##
3
+ #
4
+ # BBox
5
+ #
6
+ #
7
+ module PostgisAdapter
8
+ module Functions
9
+
10
+ #
11
+ # These operators utilize indexes. They compare geometries by bounding boxes.
12
+ #
13
+ # You can use the literal forms or call directly using the 'bbox' method. eg.:
14
+ #
15
+ # @point.bbox(">>", @area)
16
+ # @point.bbox("|&>", @area)
17
+ #
18
+ #
19
+ # Cheatsheet:
20
+ #
21
+ # A &< B => A overlaps or is to the left of B
22
+ # A &> B => A overlaps or is to the right of B
23
+ # A << B => A is strictly to the left of B
24
+ # A >> B => A is strictly to the right of B
25
+ # A &<| B => A overlaps B or is below B
26
+ # A |&> B => A overlaps or is above B
27
+ # A <<| B => A strictly below B
28
+ # A |>> B => A strictly above B
29
+ # A = B => A bbox same as B bbox
30
+ # A @ B => A completely contained by B
31
+ # A ~ B => A completely contains B
32
+ # A && B => A and B bboxes interact
33
+ # A ~= B => A and B geometries are binary equal?
34
+ #
35
+ def bbox(operator, other)
36
+ postgis_calculate(:bbox, [self, other], operator)
37
+ end
38
+
39
+ #
40
+ # bbox literal method.
41
+ #
42
+ def completely_contained_by? other
43
+ bbox("@", other)
44
+ end
45
+
46
+ #
47
+ # bbox literal method.
48
+ #
49
+ def completely_contains? other
50
+ bbox("~", other)
51
+ end
52
+
53
+ #
54
+ # bbox literal method.
55
+ #
56
+ def overlaps_or_above? other
57
+ bbox("|&>", other)
58
+ end
59
+
60
+ #
61
+ # bbox literal method.
62
+ #
63
+ def overlaps_or_below? other
64
+ bbox("&<|", other)
65
+ end
66
+
67
+ #
68
+ # bbox literal method.
69
+ #
70
+ def overlaps_or_left_of? other
71
+ bbox("&<", other)
72
+ end
73
+
74
+ #
75
+ # bbox literal method.
76
+ #
77
+ def overlaps_or_right_of? other
78
+ bbox("&>", other)
79
+ end
80
+
81
+ #
82
+ # bbox literal method.
83
+ #
84
+ def strictly_above? other
85
+ bbox("|>>", other)
86
+ end
87
+
88
+ #
89
+ # bbox literal method.
90
+ #
91
+ def strictly_below? other
92
+ bbox("<<|", other)
93
+ end
94
+
95
+ #
96
+ # bbox literal method.
97
+ #
98
+ def strictly_left_of? other
99
+ bbox("<<", other)
100
+ end
101
+
102
+ #
103
+ # bbox literal method.
104
+ #
105
+ def strictly_right_of? other
106
+ bbox(">>", other)
107
+ end
108
+
109
+ #
110
+ # bbox literal method.
111
+ #
112
+ def interacts_with? other
113
+ bbox("&&", other)
114
+ end
115
+
116
+ #
117
+ # bbox literal method.
118
+ #
119
+ def binary_equal? other
120
+ bbox("~=", other)
121
+ end
122
+
123
+ #
124
+ # bbox literal method.
125
+ #
126
+ def same_as? other
127
+ bbox("=", other)
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,67 @@
1
+ module PostgisAdapter
2
+ module Functions
3
+
4
+ #
5
+ # Class Methods
6
+ #
7
+ module ClassMethods
8
+
9
+ #
10
+ # Returns the closest record
11
+ def closest_to(p, opts = {})
12
+ srid = opts.delete(:srid) || 4326
13
+ opts.merge!(:order => "ST_Distance(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))")
14
+ find(:first, opts)
15
+ end
16
+
17
+ #
18
+ # Order by distance
19
+ def close_to(p, opts = {})
20
+ srid = opts.delete(:srid) || 4326
21
+ opts.merge!(:order => "ST_Distance(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))")
22
+ find(:all, opts)
23
+ end
24
+
25
+ def by_length opts = {}
26
+ sort = opts.delete(:sort) || 'asc'
27
+ opts.merge!(:order => "ST_length(geom) #{sort}")
28
+ find(:all, opts)
29
+ end
30
+
31
+ def longest
32
+ find(:first, :order => "ST_length(geom) DESC")
33
+ end
34
+
35
+ def contains(p, srid=4326)
36
+ find(:all, :conditions => ["ST_Contains(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))"])
37
+ end
38
+
39
+ def contain(p, srid=4326)
40
+ find(:first, :conditions => ["ST_Contains(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))"])
41
+ end
42
+
43
+ def by_area sort='asc'
44
+ find(:all, :order => "ST_Area(geom) #{sort}" )
45
+ end
46
+
47
+ def by_perimeter sort='asc'
48
+ find(:all, :order => "ST_Perimeter(geom) #{sort}" )
49
+ end
50
+
51
+ def all_dwithin(other, margin=1)
52
+ # find(:all, :conditions => "ST_DWithin(geom, ST_GeomFromEWKB(E'#{other.as_ewkt}'), #{margin})")
53
+ find(:all, :conditions => "ST_DWithin(geom, ST_GeomFromEWKT(E'#{other.as_hex_ewkb}'), #{margin})")
54
+ end
55
+
56
+ def all_within(other)
57
+ find(:all, :conditions => "ST_Within(geom, ST_GeomFromEWKT(E'#{other.as_hex_ewkb}'))")
58
+ end
59
+
60
+ def by_boundaries sort='asc'
61
+ find(:all, :order => "ST_Boundary(geom) #{sort}" )
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+ end