forked from felt/geo_postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo_postgis.ex
407 lines (314 loc) · 12.3 KB
/
geo_postgis.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
defmodule Geo.PostGIS do
@moduledoc """
PostGIS functions that can used in ecto queries
[PostGIS Function Documentation](http://postgis.net/docs/manual-1.3/ch06.html).
Currently only the OpenGIS functions are implemented.
## Examples
defmodule Example do
import Ecto.Query
import Geo.PostGIS
def example_query(geom) do
from location in Location, limit: 5, select: st_distance(location.geom, ^geom)
end
end
"""
defmacro st_transform(wkt, srid) do
quote do: fragment("ST_Transform(?, ?)", unquote(wkt), unquote(srid))
end
defmacro st_distance(geometryA, geometryB) do
quote do: fragment("ST_Distance(?,?)", unquote(geometryA), unquote(geometryB))
end
@doc """
Casts the 2 geometries given to geographies in order to return distance in meters.
"""
defmacro st_distance_in_meters(geometryA, geometryB) do
quote do:
fragment(
"ST_Distance(?::geography, ?::geography)",
unquote(geometryA),
unquote(geometryB)
)
end
defmacro st_distancesphere(geometryA, geometryB) do
quote do: fragment("ST_DistanceSphere(?,?)", unquote(geometryA), unquote(geometryB))
end
@doc """
Please note that ST_Distance_Sphere has been deprecated as of Postgis 2.2.
Postgis 2.1 is no longer supported on PostgreSQL >= 9.5.
This macro is still in place to support users of PostgreSQL <= 9.4.x.
"""
defmacro st_distance_sphere(geometryA, geometryB) do
quote do: fragment("ST_Distance_Sphere(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_dwithin(geometryA, geometryB, float) do
quote do:
fragment("ST_DWithin(?,?,?)", unquote(geometryA), unquote(geometryB), unquote(float))
end
@doc """
Casts the 2 geometries given to geographies in order to check for distance in meters.
"""
defmacro st_dwithin_in_meters(geometryA, geometryB, float) do
quote do:
fragment(
"ST_DWithin(?::geography, ?::geography, ?)",
unquote(geometryA),
unquote(geometryB),
unquote(float)
)
end
defmacro st_equals(geometryA, geometryB) do
quote do: fragment("ST_Equals(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_disjoint(geometryA, geometryB) do
quote do: fragment("ST_Disjoint(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_intersects(geometryA, geometryB) do
quote do: fragment("ST_Intersects(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_touches(geometryA, geometryB) do
quote do: fragment("ST_Touches(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_crosses(geometryA, geometryB) do
quote do: fragment("ST_Crosess(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_within(geometryA, geometryB) do
quote do: fragment("ST_Within(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_overlaps(geometryA, geometryB) do
quote do: fragment("ST_Overlaps(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_contains(geometryA, geometryB) do
quote do: fragment("ST_Contains(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_covers(geometryA, geometryB) do
quote do: fragment("ST_Covers(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_covered_by(geometryA, geometryB) do
quote do: fragment("ST_CoveredBy(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_relate(geometryA, geometryB, intersectionPatternMatrix) do
quote do:
fragment(
"ST_Relate(?,?,?)",
unquote(geometryA),
unquote(geometryB),
unquote(intersectionPatternMatrix)
)
end
defmacro st_relate(geometryA, geometryB) do
quote do: fragment("ST_Relate(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_centroid(geometry) do
quote do: fragment("ST_Centroid(?)", unquote(geometry))
end
defmacro st_area(geometry) do
quote do: fragment("ST_Area(?)", unquote(geometry))
end
defmacro st_length(geometry) do
quote do: fragment("ST_Length(?)", unquote(geometry))
end
defmacro st_point_on_surface(geometry) do
quote do: fragment("ST_PointOnSurface(?)", unquote(geometry))
end
defmacro st_boundary(geometry) do
quote do: fragment("ST_Boundary(?)", unquote(geometry))
end
defmacro st_buffer(geometry, double) do
quote do: fragment("ST_Buffer(?, ?)", unquote(geometry), unquote(double))
end
defmacro st_buffer(geometry, double, integer) do
quote do: fragment("ST_Buffer(?, ?, ?)", unquote(geometry), unquote(double), unquote(integer))
end
defmacro st_convex_hull(geometry) do
quote do: fragment("ST_ConvexHull(?)", unquote(geometry))
end
defmacro st_intersection(geometryA, geometryB) do
quote do: fragment("ST_Intersection(?, ?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_shift_longitude(geometry) do
quote do: fragment("ST_Shift_Longitude(?)", unquote(geometry))
end
defmacro st_sym_difference(geometryA, geometryB) do
quote do: fragment("ST_SymDifference(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_difference(geometryA, geometryB) do
quote do: fragment("ST_Difference(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_collect(geometryList) do
quote do: fragment("ST_Collect(?)", unquote(geometryList))
end
defmacro st_collect(geometryA, geometryB) do
quote do: fragment("ST_Collect(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_union(geometryList) do
quote do: fragment("ST_Union(?)", unquote(geometryList))
end
defmacro st_union(geometryA, geometryB) do
quote do: fragment("ST_Union(?,?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_mem_union(geometryList) do
quote do: fragment("ST_MemUnion(?)", unquote(geometryList))
end
defmacro st_as_text(geometry) do
quote do: fragment("ST_AsText(?)", unquote(geometry))
end
defmacro st_as_binary(geometry) do
quote do: fragment("ST_AsBinary(?)", unquote(geometry))
end
defmacro st_srid(geometry) do
quote do: fragment("ST_SRID(?)", unquote(geometry))
end
defmacro st_set_srid(geometry, srid) do
quote do: fragment("ST_SetSRID(?, ?)", unquote(geometry), unquote(srid))
end
defmacro st_make_box_2d(geometryA, geometryB) do
quote do: fragment("ST_MakeBox2D(?, ?)", unquote(geometryA), unquote(geometryB))
end
defmacro st_dimension(geometry) do
quote do: fragment("ST_Dimension(?)", unquote(geometry))
end
defmacro st_envelope(geometry) do
quote do: fragment("ST_Envelope(?)", unquote(geometry))
end
defmacro st_make_envelope(coords, srid) do
quote do: fragment("ST_MakeEnvelope(?, ?)", unquote(coords), unquote(srid))
end
defmacro st_is_simple(geometry) do
quote do: fragment("ST_IsSimple(?)", unquote(geometry))
end
defmacro st_is_closed(geometry) do
quote do: fragment("ST_IsClosed(?)", unquote(geometry))
end
defmacro st_is_ring(geometry) do
quote do: fragment("ST_IsRing(?)", unquote(geometry))
end
defmacro st_num_geometries(geometry) do
quote do: fragment("ST_NumGeometries(?)", unquote(geometry))
end
defmacro st_geometry_n(geometry, int) do
quote do: fragment("ST_GeometryN(?, ?)", unquote(geometry), unquote(int))
end
defmacro st_num_points(geometry) do
quote do: fragment("ST_NumPoints(?)", unquote(geometry))
end
defmacro st_point_n(geometry, int) do
quote do: fragment("ST_PointN(?, ?)", unquote(geometry), unquote(int))
end
defmacro st_point(x, y) do
quote do: fragment("ST_Point(?, ?)", unquote(x), unquote(y))
end
defmacro st_exterior_ring(geometry) do
quote do: fragment("ST_ExteriorRing(?)", unquote(geometry))
end
defmacro st_num_interior_rings(geometry) do
quote do: fragment("ST_NumInteriorRings(?)", unquote(geometry))
end
defmacro st_num_interior_ring(geometry) do
quote do: fragment("ST_NumInteriorRing(?)", unquote(geometry))
end
defmacro st_interior_ring_n(geometry, int) do
quote do: fragment("ST_InteriorRingN(?, ?)", unquote(geometry), unquote(int))
end
defmacro st_end_point(geometry) do
quote do: fragment("ST_EndPoint(?)", unquote(geometry))
end
defmacro st_start_point(geometry) do
quote do: fragment("ST_StartPoint(?)", unquote(geometry))
end
defmacro st_geometry_type(geometry) do
quote do: fragment("ST_GeometryType(?)", unquote(geometry))
end
defmacro st_x(geometry) do
quote do: fragment("ST_X(?)", unquote(geometry))
end
defmacro st_y(geometry) do
quote do: fragment("ST_Y(?)", unquote(geometry))
end
defmacro st_z(geometry) do
quote do: fragment("ST_Z(?)", unquote(geometry))
end
defmacro st_m(geometry) do
quote do: fragment("ST_M(?)", unquote(geometry))
end
defmacro st_geom_from_text(text, srid \\ -1) do
quote do: fragment("ST_GeomFromText(?, ?)", unquote(text), unquote(srid))
end
defmacro st_point_from_text(text, srid \\ -1) do
quote do: fragment("ST_PointFromText(?, ?)", unquote(text), unquote(srid))
end
defmacro st_line_from_text(text, srid \\ -1) do
quote do: fragment("ST_LineFromText(?, ?)", unquote(text), unquote(srid))
end
defmacro st_linestring_from_text(text, srid \\ -1) do
quote do: fragment("ST_LinestringFromText(?, ?)", unquote(text), unquote(srid))
end
defmacro st_polygon_from_text(text, srid \\ -1) do
quote do: fragment("ST_PolygonFromText(?, ?)", unquote(text), unquote(srid))
end
defmacro st_m_point_from_text(text, srid \\ -1) do
quote do: fragment("ST_MPointFromText(?, ?)", unquote(text), unquote(srid))
end
defmacro st_m_line_from_text(text, srid \\ -1) do
quote do: fragment("ST_MLineFromText(?, ?)", unquote(text), unquote(srid))
end
defmacro st_m_poly_from_text(text, srid \\ -1) do
quote do: fragment("ST_MPolyFromText(?, ?)", unquote(text), unquote(srid))
end
defmacro st_m_geom_coll_from_text(text, srid \\ -1) do
quote do: fragment("ST_GeomCollFromText(?, ?)", unquote(text), unquote(srid))
end
defmacro st_m_geom_from_wkb(bytea, srid \\ -1) do
quote do: fragment("ST_GeomFromWKB(?, ?)", unquote(bytea), unquote(srid))
end
defmacro st_m_geometry_from_wkb(bytea, srid \\ -1) do
quote do: fragment("ST_GeometryFromWKB(?, ?)", unquote(bytea), unquote(srid))
end
defmacro st_point_from_wkb(bytea, srid \\ -1) do
quote do: fragment("ST_PointFromWKB(?, ?)", unquote(bytea), unquote(srid))
end
defmacro st_line_from_wkb(bytea, srid \\ -1) do
quote do: fragment("ST_LineFromWKB(?, ?)", unquote(bytea), unquote(srid))
end
defmacro st_linestring_from_wkb(bytea, srid \\ -1) do
quote do: fragment("ST_LinestringFromWKB(?, ?)", unquote(bytea), unquote(srid))
end
defmacro st_poly_from_wkb(bytea, srid \\ -1) do
quote do: fragment("ST_PolyFromWKB(?, ?)", unquote(bytea), unquote(srid))
end
defmacro st_polygon_from_wkb(bytea, srid \\ -1) do
quote do: fragment("ST_PolygonFromWKB(?, ?)", unquote(bytea), unquote(srid))
end
defmacro st_m_point_from_wkb(bytea, srid \\ -1) do
quote do: fragment("ST_MPointFromWKB(?, ?)", unquote(bytea), unquote(srid))
end
defmacro st_m_line_from_wkb(bytea, srid \\ -1) do
quote do: fragment("ST_MLineFromWKB(?, ?)", unquote(bytea), unquote(srid))
end
defmacro st_m_poly_from_wkb(bytea, srid \\ -1) do
quote do: fragment("ST_MPolyFromWKB(?, ?)", unquote(bytea), unquote(srid))
end
defmacro st_geom_coll_from_wkb(bytea, srid \\ -1) do
quote do: fragment("ST_GeomCollFromWKB(?, ?)", unquote(bytea), unquote(srid))
end
defmacro st_bd_poly_from_text(wkt, srid) do
quote do: fragment("ST_BdPolyFromText(?, ?)", unquote(wkt), unquote(srid))
end
defmacro st_bd_m_poly_from_text(wkt, srid) do
quote do: fragment("ST_BdMPolyFromText(?, ?)", unquote(wkt), unquote(srid))
end
defmacro st_flip_coordinates(geometryA) do
quote do: fragment("ST_FlipCoordinates(?)", unquote(geometryA))
end
defmacro st_generate_points(geometryA, npoints) do
quote do: fragment("ST_GeneratePoints(?,?)", unquote(geometryA), unquote(npoints))
end
defmacro st_generate_points(geometryA, npoints, seed) do
quote do:
fragment(
"ST_GeneratePoints(?,?,?)",
unquote(geometryA),
unquote(npoints),
unquote(seed)
)
end
end