forked from apache/sedona
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_function.py
More file actions
2660 lines (2307 loc) · 110 KB
/
test_function.py
File metadata and controls
2660 lines (2307 loc) · 110 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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import math
from typing import List
import pytest
from pyspark.sql import DataFrame, Row
from pyspark.sql.functions import col, explode, expr
from pyspark.sql.types import IntegerType, StructField, StructType
from shapely import wkt
from shapely.wkt import loads
from tests import mixed_wkt_geometry_input_location
from tests.sql.resource.sample_data import (
create_sample_lines_df,
create_sample_points,
create_sample_points_df,
create_sample_polygons_df,
create_simple_polygons_df,
)
from tests.test_base import TestBase
from sedona.spark.sql.types import GeometryType
class TestPredicateJoin(TestBase):
geo_schema = StructType([StructField("geom", GeometryType(), False)])
geo_schema_with_index = StructType(
[
StructField("index", IntegerType(), False),
StructField("geom", GeometryType(), False),
]
)
geo_pair_schema = StructType(
[
StructField("geomA", GeometryType(), False),
StructField("geomB", GeometryType(), False),
]
)
def test_ST_LabelPoint(self):
geom_expr = "ST_GeomFromWKT('POLYGON ((-112.637484 33.440546, -112.546852 33.477209, -112.489177 33.550488, -112.41777 33.751684, -111.956371 33.719707, -111.766868 33.616843, -111.775107 33.527595, -111.640533 33.504695, -111.440044 33.463462, -111.415326 33.374055, -111.514197 33.309809, -111.643279 33.222542, -111.893203 33.174278, -111.96461 33.250109, -112.123903 33.261593, -112.252985 33.35341, -112.406784 33.346527, -112.667694 33.316695, -112.637484 33.440546))')"
function_df = self.spark.sql(f"select ST_LabelPoint({geom_expr})")
actual = function_df.take(1)[0][0]
expected = "POINT (-112.04278737349767 33.46420809489905)"
self.assert_geometry_almost_equal(expected, actual, 0.1)
geom_expr = "ST_GeomFromWKT('GEOMETRYCOLLECTION(POLYGON ((-112.840785 33.435962, -112.840785 33.708284, -112.409597 33.708284, -112.409597 33.435962, -112.840785 33.435962)), POLYGON ((-112.309264 33.398167, -112.309264 33.746007, -111.787444 33.746007, -111.787444 33.398167, -112.309264 33.398167)))')"
function_df = self.spark.sql(f"select ST_LabelPoint({geom_expr}, 1)")
actual = function_df.take(1)[0][0]
expected = "POINT (-112.04835399999999 33.57208699999999)"
self.assert_geometry_almost_equal(expected, actual, 0.1)
geom_expr = "ST_GeomFromWKT('POLYGON ((-112.654072 33.114485, -112.313516 33.653431, -111.63515 33.314399, -111.497829 33.874913, -111.692825 33.431378, -112.376684 33.788215, -112.654072 33.114485))')"
function_df = self.spark.sql(f"select ST_LabelPoint({geom_expr}, 1, 0.1)")
actual = function_df.take(1)[0][0]
expected = "POINT (-112.0722602222832 33.53914975012836)"
self.assert_geometry_almost_equal(expected, actual, 0.1)
def test_st_concave_hull(self):
polygon_wkt_df = (
self.spark.read.format("csv")
.option("delimiter", "\t")
.option("header", "false")
.load(mixed_wkt_geometry_input_location)
)
polygon_wkt_df.createOrReplaceTempView("polygontable")
polygon_wkt_df.show()
polygon_df = self.spark.sql(
"select ST_GeomFromWKT(polygontable._c0) as countyshape from polygontable"
)
polygon_df.createOrReplaceTempView("polygondf")
polygon_df.show()
function_df = self.spark.sql(
"select ST_ConcaveHull(polygondf.countyshape, 1.0) from polygondf"
)
function_df.show()
def test_st_convex_hull(self):
polygon_wkt_df = (
self.spark.read.format("csv")
.option("delimiter", "\t")
.option("header", "false")
.load(mixed_wkt_geometry_input_location)
)
polygon_wkt_df.createOrReplaceTempView("polygontable")
polygon_wkt_df.show()
polygon_df = self.spark.sql(
"select ST_GeomFromWKT(polygontable._c0) as countyshape from polygontable"
)
polygon_df.createOrReplaceTempView("polygondf")
polygon_df.show()
function_df = self.spark.sql(
"select ST_ConvexHull(polygondf.countyshape) from polygondf"
)
function_df.show()
def test_st_buffer(self):
geom_expr = "ST_GeomFromWKT('LINESTRING(0 0, 10 10)')"
function_df = self.spark.sql(f"select ST_Buffer({geom_expr}, 1)")
actual = function_df.take(1)[0][0]
expected = "POLYGON ((9.292893218813452 10.707106781186548, 9.444429766980399 10.831469612302545, 9.61731656763491 10.923879532511286, 9.804909677983872 10.98078528040323, 10 11, 10.195090322016128 10.98078528040323, 10.38268343236509 10.923879532511286, 10.555570233019603 10.831469612302545, 10.707106781186548 10.707106781186548, 10.831469612302545 10.555570233019601, 10.923879532511286 10.38268343236509, 10.98078528040323 10.195090322016128, 11 10, 10.98078528040323 9.804909677983872, 10.923879532511286 9.61731656763491, 10.831469612302545 9.444429766980399, 10.707106781186548 9.292893218813452, 0.7071067811865475 -0.7071067811865475, 0.5555702330196023 -0.8314696123025452, 0.3826834323650898 -0.9238795325112867, 0.1950903220161283 -0.9807852804032304, 0 -1, -0.1950903220161282 -0.9807852804032304, -0.3826834323650897 -0.9238795325112867, -0.555570233019602 -0.8314696123025453, -0.7071067811865475 -0.7071067811865476, -0.8314696123025453 -0.5555702330196022, -0.9238795325112867 -0.3826834323650899, -0.9807852804032304 -0.1950903220161286, -1 0, -0.9807852804032304 0.1950903220161284, -0.9238795325112867 0.3826834323650896, -0.8314696123025455 0.555570233019602, -0.7071067811865475 0.7071067811865475, 9.292893218813452 10.707106781186548))"
self.assert_geometry_almost_equal(expected, actual, 0.1)
function_df = self.spark.sql(f"select ST_Buffer({geom_expr}, 1, true)")
actual = function_df.take(1)[0][0]
expected = "POLYGON ((9.999993652864127 10.0000063012992, 9.999995015003332 10.000007407644903, 9.999996568713167 10.00000822931893, 9.999998254285428 10.00000873474483, 10.000000006944521 10.000008904499353, 10.000001759336742 10.000008732058928, 10.000003444118638 10.000008224050353, 10.000004996544984 10.000007399996127, 10.000006356956911 10.000006291564157, 10.000007473074547 10.000004941350873, 10.00000830200612 10.000003401244212, 10.000008811896254 10.000001730429604, 10.000008983150158 9.999999993115512, 10.000008809186637 9.999998256065968, 10.000008296691016 9.999996586034781, 10.000007465358209 9.999995047200317, 10.000006347135875 9.99999369869915, 0.0000063471358746 -0.0000063997976682, 0.0000049849966679 -0.0000075234370537, 0.0000034312868348 -0.0000083579549476, 0.000001745714573 -0.0000088712813465, -0.0000000069445209 -0.000009043689356, -0.0000017593367407 -0.000008868553461, -0.0000034441186362 -0.0000083526040333, -0.000004996544984 -0.0000075156687142, -0.0000063569569102 -0.0000063899104433, -0.0000074730745473 -0.0000050185915196, -0.0000083020061205 -0.0000034544109354, -0.0000088118962544 -0.0000017574792784, -0.0000089831501569 0.0000000069910119, -0.0000088091866368 0.0000017711932577, -0.0000082966910144 0.0000034673292289, -0.0000074653582092 0.000005030217681, -0.0000063471358746 0.000006399797681, 9.999993652864127 10.0000063012992))"
self.assert_geometry_almost_equal(expected, actual, 0.1)
function_df = self.spark.sql(
f"select ST_Buffer({geom_expr}, 10, false, 'endcap=square')"
)
actual = function_df.take(1)[0][0]
expected = "POLYGON ((2.9289321881345254 17.071067811865476, 10 24.14213562373095, 24.14213562373095 10, 7.071067811865475 -7.071067811865475, 0 -14.142135623730951, -14.14213562373095 -0.0000000000000009, 2.9289321881345254 17.071067811865476))"
self.assert_geometry_almost_equal(expected, actual, 0.1)
function_df = self.spark.sql(
f"select ST_Buffer({geom_expr}, 10, true, 'endcap=square')"
)
actual = function_df.take(1)[0][0]
expected = "POLYGON ((9.999936528641255 10.000063012993312, 10.000000098210357 10.00012592862454, 10.000127040927849 9.999999902648744, 0.0000634713587459 -0.0000639979767969, -0.0000000982103557 -0.0001278970813952, -0.0001270409278476 0.0000000988678222, 9.999936528641255 10.000063012993312))"
self.assert_geometry_almost_equal(expected, actual, 0.1)
def test_st_bestsrid(self):
polygon_from_wkt = (
self.spark.read.format("csv")
.option("delimiter", "\t")
.option("header", "false")
.load(mixed_wkt_geometry_input_location)
)
polygon_from_wkt.createOrReplaceTempView("polgontable")
polygon_from_wkt.show()
polygon_df = self.spark.sql(
"select ST_GeomFromWKT(polygontable._c0) as countyshape from polygontable"
)
polygon_df.createOrReplaceTempView("polygondf")
polygon_df.show()
function_df = self.spark.sql(
"select ST_BestSRID(polygondf.countyshape) from polygondf"
)
function_df.show()
actual = function_df.take(1)[0][0]
assert actual == 32614
def test_st_shiftlongitude(self):
function_df = self.spark.sql(
"select ST_ShiftLongitude(ST_GeomFromWKT('POLYGON((179 10, -179 10, -179 20, 179 20, 179 10))'))"
)
actual = function_df.take(1)[0][0].wkt
assert actual == "POLYGON ((179 10, 181 10, 181 20, 179 20, 179 10))"
function_df = self.spark.sql(
"select ST_ShiftLongitude(ST_GeomFromWKT('POINT(-179 10)'))"
)
actual = function_df.take(1)[0][0].wkt
assert actual == "POINT (181 10)"
function_df = self.spark.sql(
"select ST_ShiftLongitude(ST_GeomFromWKT('LINESTRING(179 10, 181 10)'))"
)
actual = function_df.take(1)[0][0].wkt
assert actual == "LINESTRING (179 10, -179 10)"
def test_st_box_2d(self):
df = self.spark.sql("""
SELECT
ST_Box2D(ST_GeomFromText('POLYGON((1 2, 1 5, 4 5, 4 2, 1 2))')) AS bbox,
ST_Box2D(ST_GeomFromText('POINT EMPTY')) AS bbox_empty,
ST_Box2D(ST_GeomFromText(NULL)) AS bbox_null
""")
row = df.first()
bbox = row[0]
assert bbox.xmin == 1.0
assert bbox.ymin == 2.0
assert bbox.xmax == 4.0
assert bbox.ymax == 5.0
assert row[1] is None
assert row[2] is None
def test_st_as_text_box_2d(self):
result = self.spark.sql("""
SELECT ST_AsText(ST_Box2D(ST_GeomFromText('POLYGON((1 2, 1 5, 4 5, 4 2, 1 2))')))
""").first()[0]
assert result == "BOX(1.0 2.0, 4.0 5.0)"
def test_st_envelope(self):
polygon_from_wkt = (
self.spark.read.format("csv")
.option("delimiter", "\t")
.option("header", "false")
.load(mixed_wkt_geometry_input_location)
)
polygon_from_wkt.createOrReplaceTempView("polygontable")
polygon_from_wkt.show()
polygon_df = self.spark.sql(
"select ST_GeomFromWKT(polygontable._c0) as countyshape from polygontable"
)
polygon_df.createOrReplaceTempView("polygondf")
polygon_df.show()
function_df = self.spark.sql(
"select ST_Envelope(polygondf.countyshape) from polygondf"
)
function_df.show()
def test_st_expand(self):
baseDf = self.spark.sql(
"SELECT ST_GeomFromWKT('POLYGON ((50 50 1, 50 80 2, 80 80 3, 80 50 2, 50 50 1))') as geom"
)
actual = baseDf.selectExpr("ST_AsText(ST_Expand(geom, 10))").first()[0]
expected = "POLYGON Z((40 40 -9, 40 90 -9, 90 90 13, 90 40 13, 40 40 -9))"
assert expected == actual
actual = baseDf.selectExpr("ST_AsText(ST_Expand(geom, 5, 6))").first()[0]
expected = "POLYGON Z((45 44 1, 45 86 1, 85 86 3, 85 44 3, 45 44 1))"
assert expected == actual
actual = baseDf.selectExpr("ST_AsText(ST_Expand(geom, 6, 5, -3))").first()[0]
expected = "POLYGON Z((44 45 4, 44 85 4, 86 85 0, 86 45 0, 44 45 4))"
assert expected == actual
def test_st_expand_box_2d(self):
df = self.spark.sql("""
SELECT
ST_Expand(ST_Box2D(ST_GeomFromText('POLYGON((1 2, 1 5, 4 5, 4 2, 1 2))')), 1.0) AS uniform,
ST_Expand(ST_Box2D(ST_GeomFromText('POLYGON((1 2, 1 5, 4 5, 4 2, 1 2))')), 2.0, 0.5) AS per_axis,
ST_Expand(ST_Box2D(ST_GeomFromText(NULL)), 1.0) AS null_uniform,
ST_Expand(ST_Box2D(ST_GeomFromText(NULL)), 1.0, 1.0) AS null_per_axis
""")
row = df.first()
uniform = row[0]
assert uniform.xmin == 0.0
assert uniform.ymin == 1.0
assert uniform.xmax == 5.0
assert uniform.ymax == 6.0
per_axis = row[1]
assert per_axis.xmin == -1.0
assert per_axis.ymin == 1.5
assert per_axis.xmax == 6.0
assert per_axis.ymax == 5.5
# NULL Box2D input deserializes to None for both signatures.
assert row[2] is None
assert row[3] is None
def test_st_centroid(self):
polygon_wkt_df = (
self.spark.read.format("csv")
.option("delimiter", "\t")
.option("header", "false")
.load(mixed_wkt_geometry_input_location)
)
polygon_wkt_df.createOrReplaceTempView("polygontable")
polygon_wkt_df.show()
polygon_df = self.spark.sql(
"select ST_GeomFromWKT(polygontable._c0) as countyshape from polygontable"
)
polygon_df.createOrReplaceTempView("polygondf")
polygon_df.show()
function_df = self.spark.sql(
"select ST_Centroid(polygondf.countyshape) from polygondf"
)
function_df.show()
def test_st_crossesdateline(self):
crosses_test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON((175 10, -175 10, -175 -10, 175 -10, 175 10))') as geom"
)
crosses_test_table.createOrReplaceTempView("crossesTesttable")
crosses = self.spark.sql(
"select(ST_CrossesDateLine(geom)) from crossesTesttable"
)
not_crosses_test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON((1 1, 4 1, 4 4, 1 4, 1 1))') as geom"
)
not_crosses_test_table.createOrReplaceTempView("notCrossesTesttable")
not_crosses = self.spark.sql(
"select(ST_CrossesDateLine(geom)) from notCrossesTesttable"
)
assert crosses.take(1)[0][0]
assert not not_crosses.take(1)[0][0]
def test_st_length(self):
polygon_wkt_df = (
self.spark.read.format("csv")
.option("delimiter", "\t")
.option("header", "false")
.load(mixed_wkt_geometry_input_location)
)
polygon_wkt_df.createOrReplaceTempView("polygontable")
polygon_df = self.spark.sql(
"select ST_GeomFromWKT(polygontable._c0) as countyshape from polygontable"
)
polygon_df.createOrReplaceTempView("polygondf")
function_df = self.spark.sql(
"select ST_Length(polygondf.countyshape) from polygondf"
)
assert function_df.take(1)[0][0] == 0.0
def test_st_length2d(self):
polygon_wkt_df = (
self.spark.read.format("csv")
.option("delimiter", "\t")
.option("header", "false")
.load(mixed_wkt_geometry_input_location)
)
polygon_wkt_df.createOrReplaceTempView("polygontable")
polygon_df = self.spark.sql(
"select ST_GeomFromWKT(polygontable._c0) as countyshape from polygontable"
)
polygon_df.createOrReplaceTempView("polygondf")
function_df = self.spark.sql(
"select ST_Length2D(polygondf.countyshape) from polygondf"
)
assert function_df.take(1)[0][0] == 0.0
def test_st_area(self):
polygon_wkt_df = (
self.spark.read.format("csv")
.option("delimiter", "\t")
.option("header", "false")
.load(mixed_wkt_geometry_input_location)
)
polygon_wkt_df.createOrReplaceTempView("polygontable")
polygon_wkt_df.show()
polygon_df = self.spark.sql(
"select ST_GeomFromWKT(polygontable._c0) as countyshape from polygontable"
)
polygon_df.createOrReplaceTempView("polygondf")
polygon_df.show()
function_df = self.spark.sql(
"select ST_Area(polygondf.countyshape) from polygondf"
)
function_df.show()
def test_st_distance(self):
polygon_wkt_df = (
self.spark.read.format("csv")
.option("delimiter", "\t")
.option("header", "false")
.load(mixed_wkt_geometry_input_location)
)
polygon_wkt_df.createOrReplaceTempView("polygontable")
polygon_wkt_df.show()
polygon_df = self.spark.sql(
"select ST_GeomFromWKT(polygontable._c0) as countyshape from polygontable"
)
polygon_df.createOrReplaceTempView("polygondf")
polygon_df.show()
function_df = self.spark.sql(
"select ST_Distance(polygondf.countyshape, polygondf.countyshape) from polygondf"
)
function_df.show()
def test_st_3ddistance(self):
function_df = self.spark.sql(
"select ST_3DDistance(ST_PointZ(0.0, 0.0, 5.0), ST_PointZ(1.0, 1.0, -6.0))"
)
assert function_df.count() == 1
def test_st_transform(self):
polygon_wkt_df = (
self.spark.read.format("csv")
.option("delimiter", "\t")
.option("header", "false")
.load(mixed_wkt_geometry_input_location)
)
polygon_wkt_df.createOrReplaceTempView("polygontable")
polygon_wkt_df.show()
polygon_df = self.spark.sql(
"select ST_GeomFromWKT(polygontable._c0) as countyshape from polygontable"
)
polygon_df.createOrReplaceTempView("polygondf")
polygon_df.show()
function_df = self.spark.sql(
"select ST_ReducePrecision(ST_Transform(polygondf.countyshape, 'epsg:4326','epsg:3857', false), 2) from polygondf"
)
actual = function_df.take(1)[0][0].wkt
assert (
actual[:300]
== "POLYGON ((-10800163.45 5161718.41, -10800164.34 5162103.12, -10800164.57 5162440.81, -10800164.57 5162443.95, -10800164.57 5162468.37, -10800164.57 5162501.93, -10800165.57 5163066.47, -10800166.9 5163158.61, -10800166.9 5163161.46, -10800167.01 5163167.9, -10800167.01 5163171.5, -10800170.24 516340"
)
function_df = self.spark.sql(
"select ST_ReducePrecision(ST_Transform(ST_SetSRID(polygondf.countyshape, 4326), 'epsg:3857'), 2) from polygondf"
)
actual = function_df.take(1)[0][0].wkt
assert (
actual[:300]
== "POLYGON ((-10800163.45 5161718.41, -10800164.34 5162103.12, -10800164.57 5162440.81, -10800164.57 5162443.95, -10800164.57 5162468.37, -10800164.57 5162501.93, -10800165.57 5163066.47, -10800166.9 5163158.61, -10800166.9 5163161.46, -10800167.01 5163167.9, -10800167.01 5163171.5, -10800170.24 516340"
)
def test_st_intersection_intersects_but_not_contains(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON((1 1, 8 1, 8 8, 1 8, 1 1))') as a,ST_GeomFromWKT('POLYGON((2 2, 9 2, 9 9, 2 9, 2 2))') as b"
)
test_table.createOrReplaceTempView("testtable")
intersect = self.spark.sql("select ST_Intersection(a,b) from testtable")
assert intersect.take(1)[0][0].wkt == "POLYGON ((2 8, 8 8, 8 2, 2 2, 2 8))"
def test_st_intersection_intersects_but_left_contains_right(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON((1 1, 1 5, 5 5, 1 1))') as a,ST_GeomFromWKT('POLYGON((2 2, 2 3, 3 3, 2 2))') as b"
)
test_table.createOrReplaceTempView("testtable")
intersects = self.spark.sql("select ST_Intersection(a,b) from testtable")
assert intersects.take(1)[0][0].wkt == "POLYGON ((2 2, 2 3, 3 3, 2 2))"
def test_st_intersection_intersects_but_right_contains_left(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON((2 2, 2 3, 3 3, 2 2))') as a,ST_GeomFromWKT('POLYGON((1 1, 1 5, 5 5, 1 1))') as b"
)
test_table.createOrReplaceTempView("testtable")
intersects = self.spark.sql("select ST_Intersection(a,b) from testtable")
assert intersects.take(1)[0][0].wkt == "POLYGON ((2 2, 2 3, 3 3, 2 2))"
def test_st_intersection_not_intersects(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON((40 21, 40 22, 40 23, 40 21))') as a,ST_GeomFromWKT('POLYGON((2 2, 9 2, 9 9, 2 9, 2 2))') as b"
)
test_table.createOrReplaceTempView("testtable")
intersects = self.spark.sql("select ST_Intersection(a,b) from testtable")
assert intersects.take(1)[0][0].wkt == "POLYGON EMPTY"
def test_st_maximum_inscribed_circle(self):
baseDf = self.spark.sql(
"SELECT ST_GeomFromWKT('POLYGON ((40 180, 110 160, 180 180, 180 120, 140 90, 160 40, 80 10, 70 40, 20 50, 40 180),(60 140, 50 90, 90 140, 60 140))') AS geom"
)
actual = baseDf.selectExpr("ST_MaximumInscribedCircle(geom)").take(1)[0][0]
self.assert_geometry_almost_equal(
"POINT (96.9287109375 76.3232421875)", actual.center
)
self.assert_geometry_almost_equal(
"POINT (61.64205411585366 104.55256764481707)", actual.nearest
)
assert actual.radius == pytest.approx(45.18896951053177, 1e-6)
def test_st_is_valid_detail(self):
baseDf = self.spark.sql(
"SELECT ST_GeomFromText('POLYGON ((0 0, 2 0, 2 2, 0 2, 1 1, 0 0))') AS geom"
)
actual = baseDf.selectExpr("ST_IsValidDetail(geom)").first()[0]
expected = Row(valid=True, reason=None, location=None)
assert expected == actual
baseDf = self.spark.sql(
"SELECT ST_GeomFromText('POLYGON ((0 0, 2 0, 1 1, 2 2, 0 2, 1 1, 0 0))') AS geom"
)
actual = baseDf.selectExpr("ST_IsValidDetail(geom)").first()[0]
expected = Row(
valid=False,
reason="Ring Self-intersection at or near point (1.0, 1.0)",
location=self.spark.sql("SELECT ST_GeomFromText('POINT (1 1)')").first()[0],
)
assert expected == actual
def test_st_is_valid_trajectory(self):
baseDf = self.spark.sql(
"SELECT ST_GeomFromText('LINESTRING M (0 0 1, 0 1 2)') as geom1, ST_GeomFromText('LINESTRING M (0 0 1, 0 1 1)') as geom2"
)
actual = baseDf.selectExpr("ST_IsValidTrajectory(geom1)").first()[0]
assert actual
actual = baseDf.selectExpr("ST_IsValidTrajectory(geom2)").first()[0]
assert not actual
def test_st_is_valid(self):
test_table = self.spark.sql(
"SELECT ST_IsValid(ST_GeomFromWKT('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (15 15, 15 20, 20 20, 20 15, 15 15))')) AS a, "
+ "ST_IsValid(ST_GeomFromWKT('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')) as b"
)
assert not test_table.take(1)[0][0]
assert test_table.take(1)[0][1]
def test_fixed_null_pointer_exception_in_st_valid(self):
test_table = self.spark.sql("SELECT ST_IsValid(null)")
assert test_table.take(1)[0][0] is None
def test_st_precision_reduce(self):
test_table = self.spark.sql(
"""SELECT ST_ReducePrecision(ST_GeomFromWKT('Point(0.1234567890123456789 0.1234567890123456789)'), 8)"""
)
test_table.show(truncate=False)
assert test_table.take(1)[0][0].x == 0.12345679
test_table = self.spark.sql(
"""SELECT ST_ReducePrecision(ST_GeomFromWKT('Point(0.1234567890123456789 0.1234567890123456789)'), 11)"""
)
test_table.show(truncate=False)
assert test_table.take(1)[0][0].x == 0.12345678901
def test_st_is_simple(self):
test_table = self.spark.sql(
"SELECT ST_IsSimple(ST_GeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))')) AS a, "
+ "ST_IsSimple(ST_GeomFromText('POLYGON((1 1,3 1,3 3,2 0,1 1))')) as b"
)
assert test_table.take(1)[0][0]
assert not test_table.take(1)[0][1]
def test_st_as_text(self):
polygon_wkt_df = (
self.spark.read.format("csv")
.option("delimiter", "\t")
.option("header", "false")
.load(mixed_wkt_geometry_input_location)
)
polygon_wkt_df.createOrReplaceTempView("polygontable")
polygon_df = self.spark.sql(
"select ST_GeomFromWKT(polygontable._c0) as countyshape from polygontable"
)
polygon_df.createOrReplaceTempView("polygondf")
wkt_df = self.spark.sql("select ST_AsText(countyshape) as wkt from polygondf")
assert (
polygon_df.take(1)[0]["countyshape"].wkt
== loads(wkt_df.take(1)[0]["wkt"]).wkt
)
def test_st_astext_3d(self):
input_df = self.spark.createDataFrame(
[
("Point(21 52 87)",),
("Polygon((0 0 1, 0 1 1, 1 1 1, 1 0 1, 0 0 1))",),
("Linestring(0 0 1, 1 1 2, 1 0 3)",),
("MULTIPOINT ((10 40 66), (40 30 77), (20 20 88), (30 10 99))",),
(
"MULTIPOLYGON (((30 20 11, 45 40 11, 10 40 11, 30 20 11)), ((15 5 11, 40 10 11, 10 20 11, 5 10 11, 15 5 11)))",
),
(
"MULTILINESTRING ((10 10 11, 20 20 11, 10 40 11), (40 40 11, 30 30 11, 40 20 11, 30 10 11))",
),
(
"MULTIPOLYGON (((40 40 11, 20 45 11, 45 30 11, 40 40 11)), ((20 35 11, 10 30 11, 10 10 11, 30 5 11, 45 20 11, 20 35 11), (30 20 11, 20 15 11, 20 25 11, 30 20 11)))",
),
(
"POLYGON((0 0 11, 0 5 11, 5 5 11, 5 0 11, 0 0 11), (1 1 11, 2 1 11, 2 2 11, 1 2 11, 1 1 11))",
),
],
["wkt"],
)
input_df.createOrReplaceTempView("input_wkt")
polygon_df = self.spark.sql(
"select ST_AsText(ST_GeomFromWkt(wkt)) as wkt from input_wkt"
)
assert polygon_df.count() == 8
def test_st_as_text_3d(self):
polygon_wkt_df = (
self.spark.read.format("csv")
.option("delimiter", "\t")
.option("header", "false")
.load(mixed_wkt_geometry_input_location)
)
polygon_wkt_df.createOrReplaceTempView("polygontable")
polygon_df = self.spark.sql(
"select ST_GeomFromWKT(polygontable._c0) as countyshape from polygontable"
)
polygon_df.createOrReplaceTempView("polygondf")
wkt_df = self.spark.sql("select ST_AsText(countyshape) as wkt from polygondf")
assert (
polygon_df.take(1)[0]["countyshape"].wkt
== loads(wkt_df.take(1)[0]["wkt"]).wkt
)
def test_st_n_points(self):
test = self.spark.sql(
"SELECT ST_NPoints(ST_GeomFromText('LINESTRING(77.29 29.07,77.42 29.26,77.27 29.31,77.29 29.07)'))"
)
def test_st_geometry_type(self):
test = self.spark.sql(
"SELECT ST_GeometryType(ST_GeomFromText('LINESTRING(77.29 29.07,77.42 29.26,77.27 29.31,77.29 29.07)'))"
)
def test_st_difference_right_overlaps_left(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON ((-3 -3, 3 -3, 3 3, -3 3, -3 -3))') as a,ST_GeomFromWKT('POLYGON ((0 -4, 4 -4, 4 4, 0 4, 0 -4))') as b"
)
test_table.createOrReplaceTempView("test_diff")
diff = self.spark.sql("select ST_Difference(a,b) from test_diff")
assert diff.take(1)[0][0].wkt == "POLYGON ((0 -3, -3 -3, -3 3, 0 3, 0 -3))"
def test_st_difference_right_not_overlaps_left(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON ((-3 -3, 3 -3, 3 3, -3 3, -3 -3))') as a,ST_GeomFromWKT('POLYGON ((5 -3, 7 -3, 7 -1, 5 -1, 5 -3))') as b"
)
test_table.createOrReplaceTempView("test_diff")
diff = self.spark.sql("select ST_Difference(a,b) from test_diff")
assert diff.take(1)[0][0].wkt == "POLYGON ((-3 -3, 3 -3, 3 3, -3 3, -3 -3))"
def test_st_difference_left_contains_right(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON ((-3 -3, 3 -3, 3 3, -3 3, -3 -3))') as a,ST_GeomFromWKT('POLYGON ((-1 -1, 1 -1, 1 1, -1 1, -1 -1))') as b"
)
test_table.createOrReplaceTempView("test_diff")
diff = self.spark.sql("select ST_Difference(a,b) from test_diff")
assert (
diff.take(1)[0][0].wkt
== "POLYGON ((-3 -3, -3 3, 3 3, 3 -3, -3 -3), (-1 -1, 1 -1, 1 1, -1 1, -1 -1))"
)
def test_st_difference_left_is_contained_by_right(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON ((-1 -1, 1 -1, 1 1, -1 1, -1 -1))') as a,ST_GeomFromWKT('POLYGON ((-3 -3, 3 -3, 3 3, -3 3, -3 -3))') as b"
)
test_table.createOrReplaceTempView("test_diff")
diff = self.spark.sql("select ST_Difference(a,b) from test_diff")
assert diff.take(1)[0][0].wkt == "POLYGON EMPTY"
def test_st_delaunay_triangles(self):
baseDf = self.spark.sql(
"SELECT ST_GeomFromWKT('MULTIPOLYGON (((10 10, 10 20, 20 20, 20 10, 10 10)),((25 10, 25 20, 35 20, 35 10, 25 10)))') AS geom"
)
actual = baseDf.selectExpr("ST_DelaunayTriangles(geom)").take(1)[0][0].wkt
expected = "GEOMETRYCOLLECTION (POLYGON ((10 20, 10 10, 20 10, 10 20)), POLYGON ((10 20, 20 10, 20 20, 10 20)), POLYGON ((20 20, 20 10, 25 10, 20 20)), POLYGON ((20 20, 25 10, 25 20, 20 20)), POLYGON ((25 20, 25 10, 35 10, 25 20)), POLYGON ((25 20, 35 10, 35 20, 25 20)))"
assert expected == actual
def test_st_sym_difference_part_of_right_overlaps_left(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON ((-1 -1, 1 -1, 1 1, -1 1, -1 -1))') as a,ST_GeomFromWKT('POLYGON ((0 -2, 2 -2, 2 0, 0 0, 0 -2))') as b"
)
test_table.createOrReplaceTempView("test_sym_diff")
diff = self.spark.sql("select ST_SymDifference(a,b) from test_sym_diff")
assert (
diff.take(1)[0][0].wkt
== "MULTIPOLYGON (((0 -1, -1 -1, -1 1, 1 1, 1 0, 0 0, 0 -1)), ((0 -1, 1 -1, 1 0, 2 0, 2 -2, 0 -2, 0 -1)))"
)
def test_st_sym_difference_not_overlaps_left(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON ((-3 -3, 3 -3, 3 3, -3 3, -3 -3))') as a,ST_GeomFromWKT('POLYGON ((5 -3, 7 -3, 7 -1, 5 -1, 5 -3))') as b"
)
test_table.createOrReplaceTempView("test_sym_diff")
diff = self.spark.sql("select ST_SymDifference(a,b) from test_sym_diff")
assert (
diff.take(1)[0][0].wkt
== "MULTIPOLYGON (((-3 -3, -3 3, 3 3, 3 -3, -3 -3)), ((5 -3, 5 -1, 7 -1, 7 -3, 5 -3)))"
)
def test_st_sym_difference_contains(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON ((-3 -3, 3 -3, 3 3, -3 3, -3 -3))') as a,ST_GeomFromWKT('POLYGON ((-1 -1, 1 -1, 1 1, -1 1, -1 -1))') as b"
)
test_table.createOrReplaceTempView("test_sym_diff")
diff = self.spark.sql("select ST_SymDifference(a,b) from test_sym_diff")
assert (
diff.take(1)[0][0].wkt
== "POLYGON ((-3 -3, -3 3, 3 3, 3 -3, -3 -3), (-1 -1, 1 -1, 1 1, -1 1, -1 -1))"
)
def test_st_union_part_of_right_overlaps_left(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON ((-3 -3, 3 -3, 3 3, -3 3, -3 -3))') as a, ST_GeomFromWKT('POLYGON ((-2 1, 2 1, 2 4, -2 4, -2 1))') as b"
)
test_table.createOrReplaceTempView("test_union")
union = self.spark.sql("select ST_Union(a,b) from test_union")
assert (
union.take(1)[0][0].wkt
== "POLYGON ((2 3, 3 3, 3 -3, -3 -3, -3 3, -2 3, -2 4, 2 4, 2 3))"
)
def test_st_union_not_overlaps_left(self):
test_table = self.spark.sql(
"select ST_GeomFromWKT('POLYGON ((-3 -3, 3 -3, 3 3, -3 3, -3 -3))') as a,ST_GeomFromWKT('POLYGON ((5 -3, 7 -3, 7 -1, 5 -1, 5 -3))') as b"
)
test_table.createOrReplaceTempView("test_union")
union = self.spark.sql("select ST_Union(a,b) from test_union")
assert (
union.take(1)[0][0].wkt
== "MULTIPOLYGON (((-3 -3, -3 3, 3 3, 3 -3, -3 -3)), ((5 -3, 5 -1, 7 -1, 7 -3, 5 -3)))"
)
def test_st_union_array_variant(self):
test_table = self.spark.sql(
"select array(ST_GeomFromWKT('POLYGON ((-3 -3, 3 -3, 3 3, -3 3, -3 -3))'),ST_GeomFromWKT('POLYGON ((5 -3, 7 -3, 7 -1, 5 -1, 5 -3))'), ST_GeomFromWKT('POLYGON((4 4, 4 6, 6 6, 6 4, 4 4))')) as polys"
)
actual = test_table.selectExpr("ST_Union(polys)").take(1)[0][0].wkt
expected = "MULTIPOLYGON (((5 -3, 5 -1, 7 -1, 7 -3, 5 -3)), ((-3 -3, -3 3, 3 3, 3 -3, -3 -3)), ((4 4, 4 6, 6 6, 6 4, 4 4)))"
assert expected == actual
def test_st_unary_union(self):
baseDf = self.spark.sql(
"SELECT ST_GeomFromWKT('MULTIPOLYGON(((0 10,0 30,20 30,20 10,0 10)),((10 0,10 20,30 20,30 0,10 0)))') AS geom"
)
actual = baseDf.selectExpr("ST_UnaryUnion(geom)").take(1)[0][0].wkt
expected = (
"POLYGON ((10 0, 10 10, 0 10, 0 30, 20 30, 20 20, 30 20, 30 0, 10 0))"
)
assert expected == actual
def test_st_azimuth(self):
sample_points = create_sample_points(20)
sample_pair_points = [
[el, sample_points[1]]
for el in sample_points
if not el.equals(sample_points[1])
]
schema = StructType(
[
StructField("geomA", GeometryType(), True),
StructField("geomB", GeometryType(), True),
]
)
df = self.spark.createDataFrame(sample_pair_points, schema)
st_azimuth_result = [
el[0] * 180 / math.pi
for el in df.selectExpr("ST_Azimuth(geomA, geomB)").collect()
]
expected_result = [
240.0133139011053,
270.0,
286.8042682202057,
315.0,
314.9543472191815,
315.0058223408927,
245.14762725688198,
314.84984546897755,
314.8868529256147,
314.9510567053395,
314.95443984912936,
314.89925480835245,
314.60187991438806,
314.6834083423315,
314.80689827870725,
314.90290827689506,
314.90336326341765,
314.7510398533675,
314.73608518601935,
]
assert st_azimuth_result == expected_result
# ST_Azimuth should return null for identical points
identical_result = self.spark.sql(
"SELECT ST_Azimuth(ST_Point(1.0, 1.0), ST_Point(1.0, 1.0))"
).collect()
assert identical_result[0][0] is None
azimuth = self.spark.sql(
"""SELECT ST_Azimuth(ST_Point(25.0, 45.0), ST_Point(75.0, 100.0)) AS degA_B,
ST_Azimuth(ST_Point(75.0, 100.0), ST_Point(25.0, 45.0)) AS degB_A
"""
).collect()
azimuths = [
[azimuth1 * 180 / math.pi, azimuth2 * 180 / math.pi]
for azimuth1, azimuth2 in azimuth
]
assert azimuths[0] == [42.27368900609373, 222.27368900609372]
def test_st_x(self):
point_df = create_sample_points_df(self.spark, 5)
polygon_df = create_sample_polygons_df(self.spark, 5)
linestring_df = create_sample_lines_df(self.spark, 5)
points = point_df.selectExpr("ST_X(geom)").collect()
polygons = polygon_df.selectExpr("ST_X(geom) as x").filter("x IS NOT NULL")
linestrings = linestring_df.selectExpr("ST_X(geom) as x").filter(
"x IS NOT NULL"
)
assert [point[0] for point in points] == [
-71.064544,
-88.331492,
88.331492,
1.0453,
32.324142,
]
assert not linestrings.count()
assert not polygons.count()
def test_st_y(self):
point_df = create_sample_points_df(self.spark, 5)
polygon_df = create_sample_polygons_df(self.spark, 5)
linestring_df = create_sample_lines_df(self.spark, 5)
points = point_df.selectExpr("ST_Y(geom)").collect()
polygons = polygon_df.selectExpr("ST_Y(geom) as y").filter("y IS NOT NULL")
linestrings = linestring_df.selectExpr("ST_Y(geom) as y").filter(
"y IS NOT NULL"
)
assert [point[0] for point in points] == [
42.28787,
32.324142,
32.324142,
5.3324324,
-88.331492,
]
assert not linestrings.count()
assert not polygons.count()
def test_st_z(self):
point_df = self.spark.sql(
"select ST_GeomFromWKT('POINT Z (1.1 2.2 3.3)') as geom"
)
polygon_df = self.spark.sql(
"select ST_GeomFromWKT('POLYGON Z ((0 0 2, 0 1 2, 1 1 2, 1 0 2, 0 0 2))') as geom"
)
linestring_df = self.spark.sql(
"select ST_GeomFromWKT('LINESTRING Z (0 0 1, 0 1 2)') as geom"
)
points = point_df.selectExpr("ST_Z(geom)").collect()
polygons = polygon_df.selectExpr("ST_Z(geom) as z").filter("z IS NOT NULL")
linestrings = linestring_df.selectExpr("ST_Z(geom) as z").filter(
"z IS NOT NULL"
)
assert [point[0] for point in points] == [3.3]
assert not linestrings.count()
assert not polygons.count()
def test_st_zmflag(self):
actual = self.spark.sql("SELECT ST_Zmflag(ST_GeomFromWKT('POINT (1 2)'))").take(
1
)[0][0]
assert actual == 0
actual = self.spark.sql(
"SELECT ST_Zmflag(ST_GeomFromWKT('LINESTRING (1 2 3, 4 5 6)'))"
).take(1)[0][0]
assert actual == 2
actual = self.spark.sql(
"SELECT ST_Zmflag(ST_GeomFromWKT('POLYGON M((1 2 3, 3 4 3, 5 6 3, 3 4 3, 1 2 3))'))"
).take(1)[0][0]
assert actual == 1
actual = self.spark.sql(
"SELECT ST_Zmflag(ST_GeomFromWKT('MULTIPOLYGON ZM (((30 10 5 1, 40 40 10 2, 20 40 15 3, 10 20 20 4, 30 10 5 1)), ((15 5 3 1, 20 10 6 2, 10 10 7 3, 15 5 3 1)))'))"
).take(1)[0][0]
assert actual == 3
def test_st_z_max(self):
linestring_df = self.spark.sql(
"SELECT ST_GeomFromWKT('LINESTRING Z (0 0 1, 0 1 2)') as geom"
)
linestring_row = [
lnstr_row[0]
for lnstr_row in linestring_df.selectExpr("ST_ZMax(geom)").collect()
]
assert linestring_row == [2.0]
def test_st_z_min(self):
linestring_df = self.spark.sql(
"SELECT ST_GeomFromWKT('POLYGON Z ((0 0 2, 0 1 1, 1 1 2, 1 0 2, 0 0 2))') as geom"
)
linestring_row = [
lnstr_row[0]
for lnstr_row in linestring_df.selectExpr("ST_ZMin(geom)").collect()
]
assert linestring_row == [1.0]
def test_st_n_dims(self):
point_df = self.spark.sql("SELECT ST_GeomFromWKT('POINT(1 1 2)') as geom")
point_row = [
pt_row[0] for pt_row in point_df.selectExpr("ST_NDims(geom)").collect()
]
assert point_row == [3]
def test_st_start_point(self):
point_df = create_sample_points_df(self.spark, 5)
polygon_df = create_sample_polygons_df(self.spark, 5)
linestring_df = create_sample_lines_df(self.spark, 5)
expected_points = [
"POINT (-112.506968 45.98186)",
"POINT (-112.519856 45.983586)",
"POINT (-112.504872 45.919281)",
"POINT (-112.574945 45.987772)",
"POINT (-112.520691 42.912313)",
]
points = point_df.selectExpr("ST_StartPoint(geom) as geom").filter(
"geom IS NOT NULL"
)
polygons = polygon_df.selectExpr("ST_StartPoint(geom) as geom").filter(
"geom IS NOT NULL"
)
linestrings = linestring_df.selectExpr("ST_StartPoint(geom) as geom").filter(
"geom IS NOT NULL"
)
assert [line[0] for line in linestrings.collect()] == [
wkt.loads(el) for el in expected_points
]
assert not points.count()
assert not polygons.count()
def test_st_snap(self):
baseDf = self.spark.sql(
"SELECT ST_GeomFromWKT('POLYGON((2.6 12.5, 2.6 20.0, 12.6 20.0, 12.6 12.5, 2.6 12.5 "
"))') AS poly, ST_GeomFromWKT('LINESTRING (0.5 10.7, 5.4 8.4, 10.1 10.0)') AS line"
)
actual = baseDf.selectExpr("ST_AsText(ST_Snap(poly, line, 2.525))").take(1)[0][
0
]
expected = "POLYGON ((2.6 12.5, 2.6 20, 12.6 20, 12.6 12.5, 10.1 10, 2.6 12.5))"
assert expected == actual
actual = baseDf.selectExpr("ST_AsText(ST_Snap(poly, line, 3.125))").take(1)[0][
0
]
expected = "POLYGON ((0.5 10.7, 2.6 20, 12.6 20, 12.6 12.5, 10.1 10, 5.4 8.4, 0.5 10.7))"
assert expected == actual
def test_st_end_point(self):
linestring_dataframe = create_sample_lines_df(self.spark, 5)
other_geometry_dataframe = create_sample_points_df(self.spark, 5).union(
create_sample_points_df(self.spark, 5)
)
point_data_frame = linestring_dataframe.selectExpr(
"ST_EndPoint(geom) as geom"
).filter("geom IS NOT NULL")
expected_ending_points = [
"POINT (-112.504872 45.98186)",
"POINT (-112.506968 45.983586)",
"POINT (-112.41643 45.919281)",
"POINT (-112.519856 45.987772)",
"POINT (-112.442664 42.912313)",
]
empty_dataframe = other_geometry_dataframe.selectExpr(
"ST_EndPoint(geom) as geom"
).filter("geom IS NOT NULL")
assert [
wkt_row[0]
for wkt_row in point_data_frame.selectExpr("ST_AsText(geom)").collect()
] == expected_ending_points
assert empty_dataframe.count() == 0
def test_st_minimum_clearance(self):
baseDf = self.spark.sql(
"SELECT ST_GeomFromWKT('POLYGON ((65 18, 62 16, 64.5 16, 62 14, 65 14, 65 18))') as geom"
)
actual = baseDf.selectExpr("ST_MinimumClearance(geom)").take(1)[0][0]
assert actual == 0.5