1import re
2
3from sqlalchemy.sql import func
4
5#
6# Importing geoalchemy2 actually registers the GeoAlchemy generic
7# functions in SQLAlchemy's function registry.
8#
9
10import geoalchemy2.functions  # NOQA
11from geoalchemy2.types import Raster  # NOQA
12
13
14def eq_sql(a, b):
15    a = re.sub(r'[\n\t]', '', str(a))
16    assert a == b
17
18
19def _test_simple_func(name):
20    eq_sql(getattr(func, name)(1).select(),
21           'SELECT %(name)s(:%(name)s_2) AS "%(name)s_1"' %
22           dict(name=name))
23
24
25def _test_geometry_returning_func(name):
26    eq_sql(getattr(func, name)(1).select(),
27           'SELECT ST_AsEWKB(%(name)s(:%(name)s_2)) AS "%(name)s_1"' %
28           dict(name=name))
29
30
31def _test_geography_returning_func(name):
32    eq_sql(getattr(func, name)(1).select(),
33           'SELECT ST_AsBinary(%(name)s(:%(name)s_2)) AS "%(name)s_1"' %
34           dict(name=name))
35
36
37def _test_raster_returning_func(name, *args, **kwargs):
38    eq_sql(getattr(func, name)(1, *args, **kwargs).select(),
39           'SELECT raster(%(name)s(:%(name)s_2)) AS "%(name)s_1"' %
40           dict(name=name))
41
42
43#
44# Geometry Constructors
45#
46def test_ST_Collect():
47    _test_geometry_returning_func('ST_Collect')
48
49
50def test_ST_BdPolyFromText():
51    _test_geometry_returning_func('ST_BdPolyFromText')
52
53
54def test_ST_BdMPolyFromText():
55    _test_geometry_returning_func('ST_BdMPolyFromText')
56
57
58def test_ST_Box2dFromGeoHash():
59    _test_geometry_returning_func('ST_Box2dFromGeoHash')
60
61
62def test_ST_GeogFromText():
63    _test_geography_returning_func('ST_GeogFromText')
64
65
66def test_ST_GeographyFromText():
67    _test_geography_returning_func('ST_GeographyFromText')
68
69
70def test_ST_GeogFromWKB():
71    _test_geography_returning_func('ST_GeogFromWKB')
72
73
74def test_ST_GeomFromTWKB():
75    _test_geometry_returning_func('ST_GeomFromTWKB')
76
77
78def test_ST_GeomCollFromText():
79    _test_geometry_returning_func('ST_GeomCollFromText')
80
81
82def test_ST_GeomFromEWKB():
83    _test_geometry_returning_func('ST_GeomFromEWKB')
84
85
86def test_ST_GeomFromEWKT():
87    _test_geometry_returning_func('ST_GeomFromEWKT')
88
89
90def test_ST_GeometryFromText():
91    _test_geometry_returning_func('ST_GeometryFromText')
92
93
94def test_ST_GeomFromGeoHash():
95    _test_geometry_returning_func('ST_GeomFromGeoHash')
96
97
98def test_ST_GeomFromGML():
99    _test_geometry_returning_func('ST_GeomFromGML')
100
101
102def test_ST_GeomFromGeoJSON():
103    _test_geometry_returning_func('ST_GeomFromGeoJSON')
104
105
106def test_ST_GeomFromKML():
107    _test_geometry_returning_func('ST_GeomFromKML')
108
109
110def test_ST_GMLToSQL():
111    _test_geometry_returning_func('ST_GMLToSQL')
112
113
114def test_ST_GeomFromText():
115    _test_geometry_returning_func('ST_GeomFromText')
116
117
118def test_ST_GeomFromWKB():
119    _test_geometry_returning_func('ST_GeomFromWKB')
120
121
122def test_ST_LineFromEncodedPolyline():
123    _test_geometry_returning_func('ST_LineFromEncodedPolyline')
124
125
126def test_ST_LineFromMultiPoint():
127    _test_geometry_returning_func('ST_LineFromMultiPoint')
128
129
130def test_ST_LineFromText():
131    _test_geometry_returning_func('ST_LineFromText')
132
133
134def test_ST_LineFromWKB():
135    _test_geometry_returning_func('ST_LineFromWKB')
136
137
138def test_ST_LinestringFromWKB():
139    _test_geometry_returning_func('ST_LinestringFromWKB')
140
141
142def test_ST_MakeBox2D():
143    _test_geometry_returning_func('ST_MakeBox2D')
144
145
146def test_ST_3DMakeBox():
147    _test_geometry_returning_func('ST_3DMakeBox')
148
149
150def test_ST_MakeLine():
151    _test_geometry_returning_func('ST_MakeLine')
152
153
154def test_ST_MakeEnvelope():
155    _test_geometry_returning_func('ST_MakeEnvelope')
156
157
158def test_ST_MakePolygon():
159    _test_geometry_returning_func('ST_MakePolygon')
160
161
162def test_ST_MakePoint():
163    _test_geometry_returning_func('ST_MakePoint')
164
165
166def test_ST_MakePointM():
167    _test_geometry_returning_func('ST_MakePointM')
168
169
170def test_ST_MLineFromText():
171    _test_geometry_returning_func('ST_MLineFromText')
172
173
174def test_ST_MPointFromText():
175    _test_geometry_returning_func('ST_MPointFromText')
176
177
178def test_ST_MPolyFromText():
179    _test_geometry_returning_func('ST_MPolyFromText')
180
181
182def test_ST_Point():
183    _test_geometry_returning_func('ST_Point')
184
185
186def test_ST_PointFromGeoHash():
187    _test_geometry_returning_func('ST_PointFromGeoHash')
188
189
190def test_ST_PointFromText():
191    _test_geometry_returning_func('ST_PointFromText')
192
193
194def test_ST_PointFromWKB():
195    _test_geometry_returning_func('ST_PointFromWKB')
196
197
198def test_ST_Polygon():
199    _test_geometry_returning_func('ST_Polygon')
200
201
202def test_ST_PolygonFromText():
203    _test_geometry_returning_func('ST_PolygonFromText')
204
205
206def test_ST_TileEnvelope():
207    _test_geometry_returning_func('ST_TileEnvelope')
208
209
210def test_ST_WKBToSQL():
211    _test_geometry_returning_func('ST_WKBToSQL')
212
213
214def test_ST_WKTToSQL():
215    _test_geometry_returning_func('ST_WKTToSQL')
216
217
218#
219# Geometry Accessors
220#
221def test_ST_Boundary():
222    _test_geometry_returning_func('ST_Boundary')
223
224
225def test_ST_BoundingDiagonal():
226    _test_geometry_returning_func('ST_BoundingDiagonal')
227
228
229def test_ST_EndPoint():
230    _test_geometry_returning_func('ST_EndPoint')
231
232
233def test_ST_Envelope():
234    _test_geometry_returning_func('ST_Envelope')
235
236
237def test_ST_GeometryN():
238    _test_geometry_returning_func('ST_GeometryN')
239
240
241def test_ST_GeometryType():
242    _test_simple_func('ST_GeometryType')
243
244
245def test_ST_InteriorRingN():
246    _test_geometry_returning_func('ST_InteriorRingN')
247
248
249def test_ST_IsValid():
250    _test_simple_func('ST_IsValid')
251
252
253def test_ST_NPoints():
254    _test_simple_func('ST_NPoints')
255
256
257def test_ST_PatchN():
258    _test_geometry_returning_func('ST_PatchN')
259
260
261def test_ST_PointN():
262    _test_geometry_returning_func('ST_PointN')
263
264
265def test_ST_Points():
266    _test_geometry_returning_func('ST_Points')
267
268
269def test_ST_SRID():
270    _test_simple_func('ST_SRID')
271
272
273def test_ST_StartPoint():
274    _test_geometry_returning_func('ST_StartPoint')
275
276
277def test_ST_X():
278    _test_simple_func('ST_X')
279
280
281def test_ST_Y():
282    _test_simple_func('ST_Y')
283
284
285def test_ST_Z():
286    _test_simple_func('ST_Z')
287
288
289#
290# Geometry Editors
291#
292def test_ST_AddPoint():
293    _test_geometry_returning_func('ST_AddPoint')
294
295
296def test_ST_Affine():
297    _test_geometry_returning_func('ST_Affine')
298
299
300def test_ST_CollectionExtract():
301    _test_geometry_returning_func('ST_CollectionExtract')
302
303
304def test_ST_CollectionHomogenize():
305    _test_geometry_returning_func('ST_CollectionHomogenize')
306
307
308def test_ST_ExteriorRing():
309    _test_geometry_returning_func('ST_ExteriorRing')
310
311
312def test_ST_Force2D():
313    _test_geometry_returning_func('ST_Force2D')
314
315
316def test_ST_Force3D():
317    _test_geometry_returning_func('ST_Force3D')
318
319
320def test_ST_Force3DM():
321    _test_geometry_returning_func('ST_Force3DM')
322
323
324def test_ST_Force3DZ():
325    _test_geometry_returning_func('ST_Force3DZ')
326
327
328def test_ST_Force4D():
329    _test_geometry_returning_func('ST_Force4D')
330
331
332def test_ST_ForceCollection():
333    _test_geometry_returning_func('ST_ForceCollection')
334
335
336def test_ST_ForceCurve():
337    _test_geometry_returning_func('ST_ForceCurve')
338
339
340def test_ST_ForcePolygonCCW():
341    _test_geometry_returning_func('ST_ForcePolygonCCW')
342
343
344def test_ST_ForcePolygonCW():
345    _test_geometry_returning_func('ST_ForcePolygonCW')
346
347
348def test_ST_ForceRHR():
349    _test_geometry_returning_func('ST_ForceRHR')
350
351
352def test_ST_ForceSFS():
353    _test_geometry_returning_func('ST_ForceSFS')
354
355
356def test_ST_M():
357    _test_simple_func('ST_M')
358
359
360def test_ST_Multi():
361    _test_geometry_returning_func('ST_Multi')
362
363
364def test_ST_Normalize():
365    _test_geometry_returning_func('ST_Normalize')
366
367
368def test_ST_QuantizeCoordinates():
369    _test_geometry_returning_func('ST_QuantizeCoordinates')
370
371
372def test_ST_RemovePoint():
373    _test_geometry_returning_func('ST_RemovePoint')
374
375
376def test_ST_Reverse():
377    _test_geometry_returning_func('ST_Reverse')
378
379
380def test_ST_Rotate():
381    _test_geometry_returning_func('ST_Rotate')
382
383
384def test_ST_RotateX():
385    _test_geometry_returning_func('ST_RotateX')
386
387
388def test_ST_RotateY():
389    _test_geometry_returning_func('ST_RotateY')
390
391
392def test_ST_RotateZ():
393    _test_geometry_returning_func('ST_RotateZ')
394
395
396def test_ST_Scale():
397    _test_geometry_returning_func('ST_Scale')
398
399
400def test_ST_Segmentize():
401    _test_geometry_returning_func('ST_Segmentize')
402
403
404def test_ST_SetPoint():
405    _test_geometry_returning_func('ST_SetPoint')
406
407
408def test_ST_SetSRID():
409    _test_geometry_returning_func('ST_SetSRID')
410
411
412def test_ST_Snap():
413    _test_geometry_returning_func('ST_Snap')
414
415
416def test_ST_SnapToGrid():
417    _test_geometry_returning_func('ST_SnapToGrid')
418
419
420def test_ST_SwapOrdinates():
421    _test_geometry_returning_func('ST_SwapOrdinates')
422
423
424def test_ST_Transform():
425    _test_geometry_returning_func('ST_Transform')
426
427
428def test_ST_Translate():
429    _test_geometry_returning_func('ST_Translate')
430
431
432def test_ST_TransScale():
433    _test_geometry_returning_func('ST_TransScale')
434
435
436#
437# Geometry Outputs
438#
439def test_ST_AsBinary():
440    _test_simple_func('ST_AsBinary')
441
442
443def test_ST_AsEWKB():
444    _test_simple_func('ST_AsEWKB')
445
446
447def test_ST_AsTWKB():
448    _test_simple_func('ST_AsTWKB')
449
450
451def test_ST_AsGeoJSON():
452    _test_simple_func('ST_AsGeoJSON')
453
454
455def test_ST_AsGML():
456    _test_simple_func('ST_AsGML')
457
458
459def test_ST_AsKML():
460    _test_simple_func('ST_AsKML')
461
462
463def test_ST_AsSVG():
464    _test_simple_func('ST_AsSVG')
465
466
467def test_ST_AsText():
468    _test_simple_func('ST_AsText')
469
470
471def test_ST_AsEWKT():
472    _test_simple_func('ST_AsEWKT')
473
474
475def test_ST_AsMVTGeom():
476    _test_geometry_returning_func('ST_AsMVTGeom')
477
478
479#
480# Spatial Relationships and Measurements
481#
482def test_ST_Area():
483    _test_simple_func('ST_Area')
484
485
486def test_ST_Azimuth():
487    _test_simple_func('ST_Azimuth')
488
489
490def test_ST_Centroid():
491    _test_geometry_returning_func('ST_Centroid')
492
493
494def test_ST_ClosestPoint():
495    _test_geometry_returning_func('ST_ClosestPoint')
496
497
498def test_ST_3DClosestPoint():
499    _test_geometry_returning_func('ST_3DClosestPoint')
500
501
502def test_ST_Contains():
503    _test_simple_func('ST_Contains')
504
505
506def test_ST_ContainsProperly():
507    _test_simple_func('ST_ContainsProperly')
508
509
510def test_ST_Covers():
511    _test_simple_func('ST_Covers')
512
513
514def test_ST_CoveredBy():
515    _test_simple_func('ST_CoveredBy')
516
517
518def test_ST_Crosses():
519    _test_simple_func('ST_Crosses')
520
521
522def test_ST_Disjoint():
523    _test_simple_func('ST_Disjoint')
524
525
526def test_ST_Distance():
527    _test_simple_func('ST_Distance')
528
529
530def test_ST_Distance_Sphere():
531    _test_simple_func('ST_Distance_Sphere')
532
533
534def test_ST_DistanceSphere():
535    _test_simple_func('ST_DistanceSphere')
536
537
538def test_ST_DFullyWithin():
539    _test_simple_func('ST_DFullyWithin')
540
541
542def test_ST_DWithin():
543    _test_simple_func('ST_DWithin')
544
545
546def test_ST_Equals():
547    _test_simple_func('ST_Equals')
548
549
550def test_ST_Intersects():
551    _test_simple_func('ST_Intersects')
552
553
554def test_ST_Length():
555    _test_simple_func('ST_Length')
556
557
558def test_ST_LineLocatePoint():
559    _test_simple_func('ST_LineLocatePoint')
560
561
562def test_ST_LongestLine():
563    _test_geometry_returning_func('ST_LongestLine')
564
565
566def test_ST_3DLongestLine():
567    _test_geometry_returning_func('ST_3DLongestLine')
568
569
570def test_ST_MinimumClearanceLine():
571    _test_geometry_returning_func('ST_MinimumClearanceLine')
572
573
574def test_ST_OrderingEquals():
575    _test_simple_func('ST_OrderingEquals')
576
577
578def test_ST_Overlaps():
579    _test_simple_func('ST_Overlaps')
580
581
582def test_ST_Perimeter():
583    _test_simple_func('ST_Perimeter')
584
585
586def test_ST_Project():
587    _test_geography_returning_func('ST_Project')
588
589
590def test_ST_Relate():
591    _test_simple_func('ST_Relate')
592
593
594def test_ST_ShortestLine():
595    _test_geometry_returning_func('ST_ShortestLine')
596
597
598def test_ST_3DShortestLine():
599    _test_geometry_returning_func('ST_3DShortestLine')
600
601
602def test_ST_Touches():
603    _test_simple_func('ST_Touches')
604
605
606def test_ST_Within():
607    _test_simple_func('ST_Within')
608
609
610#
611# Geometry Processing
612#
613def test_ST_Buffer():
614    _test_geometry_returning_func('ST_Buffer')
615
616
617def test_ST_BuildArea():
618    _test_geometry_returning_func('ST_BuildArea')
619
620
621def test_ST_ClipByBox2D():
622    _test_geometry_returning_func('ST_ClipByBox2D')
623
624
625def test_ST_ChaikinSmoothing():
626    _test_geometry_returning_func('ST_ChaikinSmoothing')
627
628
629def test_ST_ConcaveHull():
630    _test_geometry_returning_func('ST_ConcaveHull')
631
632
633def test_ST_ConvexHull():
634    _test_geometry_returning_func('ST_ConvexHull')
635
636
637def test_ST_CurveToLine():
638    _test_geometry_returning_func('ST_CurveToLine')
639
640
641def test_ST_DelaunayTriangles():
642    _test_geometry_returning_func('ST_DelaunayTriangles')
643
644
645def test_ST_Difference():
646    _test_geometry_returning_func('ST_Difference')
647
648
649def test_ST_Dump():
650    _test_simple_func('ST_Dump')
651
652
653def test_ST_DumpPoints():
654    _test_simple_func('ST_DumpPoints')
655
656
657def test_ST_FilterByM():
658    _test_geometry_returning_func('ST_FilterByM')
659
660
661def test_ST_FlipCoordinates():
662    _test_geometry_returning_func('ST_FlipCoordinates')
663
664
665def test_ST_GeneratePoints():
666    _test_geometry_returning_func('ST_GeneratePoints')
667
668
669def test_ST_GeometricMedian():
670    _test_geometry_returning_func('ST_GeometricMedian')
671
672
673def test_ST_Intersection():
674    _test_geometry_returning_func('ST_Intersection')
675
676
677def test_ST_LineToCurve():
678    _test_geometry_returning_func('ST_LineToCurve')
679
680
681def test_ST_LineMerge():
682    _test_geometry_returning_func('ST_LineMerge')
683
684
685def test_ST_LineSubstring():
686    _test_geometry_returning_func('ST_LineSubstring')
687
688
689def test_ST_MakeValid():
690    _test_geometry_returning_func('ST_MakeValid')
691
692
693def test_ST_MemUnion():
694    _test_geometry_returning_func('ST_MemUnion')
695
696
697def test_ST_MinimumBoundingCircle():
698    _test_geometry_returning_func('ST_MinimumBoundingCircle')
699
700
701def test_ST_Node():
702    _test_geometry_returning_func('ST_Node')
703
704
705def test_ST_OffsetCurve():
706    _test_geometry_returning_func('ST_OffsetCurve')
707
708
709def test_ST_OrientedEnvelope():
710    _test_geometry_returning_func('ST_OrientedEnvelope')
711
712
713def test_ST_PointOnSurface():
714    _test_geometry_returning_func('ST_PointOnSurface')
715
716
717def test_ST_Polygonize():
718    _test_geometry_returning_func('ST_Polygonize')
719
720
721def test_ST_RemoveRepeatedPoints():
722    _test_geometry_returning_func('ST_RemoveRepeatedPoints')
723
724
725def test_ST_SetEffectiveArea():
726    _test_geometry_returning_func('ST_SetEffectiveArea')
727
728
729def test_ST_SharedPaths():
730    _test_geometry_returning_func('ST_SharedPaths')
731
732
733def test_ST_ShiftLongitude():
734    _test_geometry_returning_func('ST_ShiftLongitude')
735
736
737def test_ST_Simplify():
738    _test_geometry_returning_func('ST_Simplify')
739
740
741def test_ST_SimplifyPreserveTopology():
742    _test_geometry_returning_func('ST_SimplifyPreserveTopology')
743
744
745def test_ST_SimplifyVW():
746    _test_geometry_returning_func('ST_SimplifyVW')
747
748
749def test_ST_Split():
750    _test_geometry_returning_func('ST_Split')
751
752
753def test_ST_Subdivide():
754    _test_geometry_returning_func('ST_Subdivide')
755
756
757def test_ST_SymDifference():
758    _test_geometry_returning_func('ST_SymDifference')
759
760
761def test_ST_Union():
762    _test_geometry_returning_func('ST_Union')
763
764
765def test_ST_UnaryUnion():
766    _test_geometry_returning_func('ST_UnaryUnion')
767
768
769def test_ST_VoronoiLines():
770    _test_geometry_returning_func('ST_VoronoiLines')
771
772
773def test_ST_VoronoiPolygons():
774    _test_geometry_returning_func('ST_VoronoiPolygons')
775
776
777def test_ST_WrapX():
778    _test_geometry_returning_func('ST_WrapX')
779
780
781#
782# Bounding Box Functions
783#
784def test_ST_Expand():
785    _test_geometry_returning_func('ST_Expand')
786
787
788#
789# Linear Referencing
790#
791def test_ST_AddMeasure():
792    _test_geometry_returning_func('ST_AddMeasure')
793
794
795def test_ST_LineInterpolatePoint():
796    _test_geometry_returning_func('ST_LineInterpolatePoint')
797
798
799def test_ST_LineInterpolatePoints():
800    _test_geometry_returning_func('ST_LineInterpolatePoints')
801
802
803def test_ST_LocateAlong():
804    _test_geometry_returning_func('ST_LocateAlong')
805
806
807def test_ST_LocateBetween():
808    _test_geometry_returning_func('ST_LocateBetween')
809
810
811def test_ST_LocateBetweenElevations():
812    _test_geometry_returning_func('ST_LocateBetweenElevations')
813
814
815def test_ST_3DLineInterpolatePoint():
816    _test_geometry_returning_func('ST_3DLineInterpolatePoint')
817
818
819#
820# Raster Constructors
821#
822def test_ST_AddBand():
823    _test_raster_returning_func('ST_AddBand')
824
825
826def test_ST_AsRaster():
827    _test_raster_returning_func('ST_AsRaster')
828
829
830#
831# Raster Editors
832#
833def test_ST_Resample():
834    _test_raster_returning_func('ST_Resample')
835
836
837def test_ST_Rescale():
838    _test_raster_returning_func('ST_Rescale')
839
840
841def test_ST_Reskew():
842    _test_raster_returning_func('ST_Reskew')
843
844
845# ST_SnapToGrid already exists for Geometry type so it can not be duplicated
846def test_ST_SnapToGrid_raster():
847    _test_raster_returning_func('ST_SnapToGrid', type_=Raster)
848
849
850def test_ST_Resize():
851    _test_raster_returning_func('ST_Resize')
852
853
854#
855# Raster Accessors
856#
857def test_ST_Height():
858    _test_simple_func('ST_Height')
859
860
861def test_ST_Width():
862    _test_simple_func('ST_Width')
863
864
865#
866# Raster Pixel Accessors and Setters
867#
868def test_ST_Value():
869    _test_simple_func('ST_Value')
870
871
872#
873# Raster Band Statistics and Analytics
874#
875def test_ST_ValueCount():
876    _test_simple_func('ST_ValueCount')
877
878
879#
880# DEM (Elevation)
881#
882def test_ST_HillShade():
883    _test_raster_returning_func('ST_HillShade')
884