-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathgeo_postgis.ex
More file actions
633 lines (500 loc) · 18.9 KB
/
geo_postgis.ex
File metadata and controls
633 lines (500 loc) · 18.9 KB
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
defmodule Geo.PostGIS do
@moduledoc """
PostGIS functions that can used in ecto queries
[PostGIS Function Documentation](https://postgis.net/docs/reference.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 """
Geography form of `ST_DWithin/4`: distance is in meters; `use_spheroid` is forwarded to PostGIS.
See [ST_DWithin](https://postgis.net/docs/ST_DWithin.html).
"""
defmacro st_dwithin(geometryA, geometryB, float, use_spheroid) do
quote do:
fragment(
"ST_DWithin(?,?,?,?)",
unquote(geometryA),
unquote(geometryB),
unquote(float),
unquote(use_spheroid)
)
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
@doc """
Like `st_dwithin_in_meters/3` but passes `use_spheroid` to PostGIS.
See [ST_DWithin](https://postgis.net/docs/ST_DWithin.html).
"""
defmacro st_dwithin_in_meters(geometryA, geometryB, float, use_spheroid) do
quote do:
fragment(
"ST_DWithin(?::geography, ?::geography, ?, ?)",
unquote(geometryA),
unquote(geometryB),
unquote(float),
unquote(use_spheroid)
)
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_Crosses(?,?)", 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_geometric_median(geometry) do
quote do: fragment("ST_GeometricMedian(?)", 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_expand(geometry, units_to_expand) do
quote do: fragment("ST_Expand(?, ?)", unquote(geometry), unquote(units_to_expand))
end
defmacro st_convex_hull(geometry) do
quote do: fragment("ST_ConvexHull(?)", unquote(geometry))
end
defmacro st_concave_hull(geometry, pctconvex, allow_holes \\ false) do
quote do:
fragment(
"ST_ConcaveHull(?, ?, ?)",
unquote(geometry),
unquote(pctconvex),
unquote(allow_holes)
)
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_collection_extract(geometry) do
quote do: fragment("ST_CollectionExtract(?)", unquote(geometry))
end
defmacro st_collection_extract(geometry, type) do
quote do: fragment("ST_CollectionExtract(?, ?)", unquote(geometry), unquote(type))
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_as_geojson(table) do
quote do: fragment("ST_AsGeoJSON(?.*)", unquote(table))
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_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_collection(geometry) do
quote do: fragment("ST_IsCollection(?)", unquote(geometry))
end
defmacro st_is_empty(geometry) do
quote do: fragment("ST_IsEmpty(?)", 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_n_points(geometry) do
quote do: fragment("ST_NPoints(?)", 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_points(geometry) do
quote do: fragment("ST_Points(?)", 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
defmacro st_extent(geometry) do
quote do: fragment("ST_EXTENT(?)::geometry", unquote(geometry))
end
defmacro st_build_area(geometryA) do
quote do: fragment("ST_BuildArea(?)", unquote(geometryA))
end
defmacro st_is_valid(geometry) do
quote do: fragment("ST_IsValid(?)", unquote(geometry))
end
defmacro st_make_valid(geometry) do
quote do: fragment("ST_MakeValid(?)", unquote(geometry))
end
defmacro st_make_valid(geometry, params) do
quote do: fragment("ST_MakeValid(?, ?)", unquote(geometry), unquote(params))
end
defmacro st_force_2d(geometry) do
quote do: fragment("ST_Force2D(?)", unquote(geometry))
end
defmacro st_multi(geometry) do
quote do: fragment("ST_Multi(?)", unquote(geometry))
end
defmacro st_make_point(x, y) do
quote do: fragment("ST_MakePoint(?, ?)", unquote(x), unquote(y))
end
defmacro st_make_point(x, y, z) do
quote do: fragment("ST_MakePoint(?, ?, ?)", unquote(x), unquote(y), unquote(z))
end
defmacro st_make_point(x, y, z, m) do
quote do: fragment("ST_MakePoint(?, ?, ?, ?)", unquote(x), unquote(y), unquote(z), unquote(m))
end
defmacro st_make_envelope(xMin, yMin, xMax, yMax) do
quote do:
fragment(
"ST_MakeEnvelope(?, ?, ?, ?)",
unquote(xMin),
unquote(yMin),
unquote(xMax),
unquote(yMax)
)
end
defmacro st_make_envelope(xMin, yMin, xMax, yMax, srid) do
quote do:
fragment(
"ST_MakeEnvelope(?, ?, ?, ?, ?)",
unquote(xMin),
unquote(yMin),
unquote(xMax),
unquote(yMax),
unquote(srid)
)
end
defmacro st_node(geometry) do
quote do: fragment("ST_Node(?)", unquote(geometry))
end
defmacro st_line_merge(geometry) do
quote do: fragment("ST_LineMerge(?)", unquote(geometry))
end
@spec st_line_merge(Geo.MultiLineString.t(), boolean()) :: term()
defmacro st_line_merge(geometry, directed) do
quote do: fragment("ST_LineMerge(?, ?)", unquote(geometry), unquote(directed))
end
defmacro st_line_interpolate_point(geometry, fraction) do
quote do: fragment("ST_LineInterpolatePoint(?, ?)", unquote(geometry), unquote(fraction))
end
defmacro st_line_interpolate_points(geometry, fraction, repeat) do
quote do:
fragment(
"ST_LineInterpolatePoints(?, ?, ?)",
unquote(geometry),
unquote(fraction),
unquote(repeat)
)
end
defmacro st_line_locate_point(lineGeometry, pointGeometry) do
quote do: fragment("ST_LineLocatePoint(?, ?)", unquote(lineGeometry), unquote(pointGeometry))
end
defmacro st_line_substring(geometry, startFraction, endFraction) do
quote do:
fragment(
"ST_LineSubstring(?, ?, ?)",
unquote(geometry),
unquote(startFraction),
unquote(endFraction)
)
end
@doc """
A set-returning function (SRF) that extracts the components of a geometry. It returns a set of
geometry_dump rows, each containing a geometry (geom field) and an array of integers (path field).
"""
defmacro st_dump(geometry) do
quote do: fragment("ST_Dump(?)", unquote(geometry))
end
@doc """
An alternative form of "st_dump" that allows directly extracting out just the dumped geometry for
easier use with sub-queries and within nested calls involving other PostGIS macros.
"""
defmacro st_dump_geometry(geometry) do
quote do: fragment("(ST_Dump(?)).geom", unquote(geometry))
end
defmacro st_mem_size(geometry) do
quote do: fragment("ST_MemSize(?)", unquote(geometry))
end
defmacro st_simplify_preserve_topology(geometryA, geometryB) do
quote do
fragment("ST_SimplifyPreserveTopology(?, ?)", unquote(geometryA), unquote(geometryB))
end
end
defmacro st_reduce_precision(geometry, grid_size) do
quote do: fragment("ST_ReducePrecision(?, ?)", unquote(geometry), unquote(grid_size))
end
defmacro st_snap_to_grid(geometry, grid_size) do
quote do: fragment("ST_SnapToGrid(?, ?)", unquote(geometry), unquote(grid_size))
end
defmacro st_cluster_k_means(geometry, number_of_clusters) do
quote do: fragment("ST_ClusterKMeans(?, ?)", unquote(geometry), unquote(number_of_clusters))
end
defmacro st_cluster_k_means(geometry, number_of_clusters, max_radius) do
quote do:
fragment(
"ST_ClusterKMeans(?, ?, ?)",
unquote(geometry),
unquote(number_of_clusters),
unquote(max_radius)
)
end
end