-
Notifications
You must be signed in to change notification settings - Fork 692
/
st_functions.py
2440 lines (1961 loc) · 86 KB
/
st_functions.py
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 inspect
import sys
from functools import partial
from typing import Optional, Union
from pyspark.sql import Column
from sedona.sql.dataframe_api import (
ColumnOrName,
ColumnOrNameOrNumber,
call_sedona_function,
validate_argument_types,
)
_call_st_function = partial(call_sedona_function, "st_functions")
@validate_argument_types
def GeometryType(geometry: ColumnOrName):
"""Return the type of the geometry as a string.
This function also indicates if the geometry is measured, by returning a string of the form 'POINTM'.
:param geometry: Geometry column to calculate the dimension for.
:type geometry: ColumnOrName
:return: Type of geometry as a string column.
:rtype: Column
"""
return _call_st_function("GeometryType", geometry)
@validate_argument_types
def ST_3DDistance(a: ColumnOrName, b: ColumnOrName) -> Column:
"""Calculate the 3-dimensional minimum Cartesian distance between two geometry columns.
:param a: One geometry column to use in the calculation.
:type a: ColumnOrName
:param b: Other geometry column to use in the calculation.
:type b: ColumnOrName
:return: Minimum cartesian distance between a and b as a double column.
:rtype: Column
"""
return _call_st_function("ST_3DDistance", (a, b))
@validate_argument_types
def ST_AddMeasure(
geom: ColumnOrName,
measureStart: Union[ColumnOrName, float],
measureEnd: Union[ColumnOrName, float],
) -> Column:
"""Interpolate measure values with the provided start and end points and return the result geometry.
:param geom: Geometry column to use in the calculation.
:type geom: ColumnOrName
:param measureStart: Start point for the measure.
:type measureStart: ColumnOrName
:param measureEnd: End point for the measure.
:type measureEnd: ColumnOrName
:return: Result geometry column.
:rtype: Column
"""
return _call_st_function("ST_AddMeasure", (geom, measureStart, measureEnd))
@validate_argument_types
def ST_AddPoint(
line_string: ColumnOrName,
point: ColumnOrName,
index: Optional[Union[ColumnOrName, int]] = None,
) -> Column:
"""Add a point to either the end of a linestring or a specified index.
If index is not provided then point will be added to the end of line_string.
:param line_string: Linestring geometry column to add point to.
:type line_string: ColumnOrName
:param point: Point geometry column to add to line_string.
:type point: ColumnOrName
:param index: 0-based index to insert point at in line_string, if None then point is appended to the end of line_string, defaults to None
:type index: Optional[Union[ColumnOrName, int]], optional
:return: Linestring geometry column with point added.
:rtype: Column
"""
args = (line_string, point) if index is None else (line_string, point, index)
return _call_st_function("ST_AddPoint", args)
@validate_argument_types
def ST_Area(geometry: ColumnOrName) -> Column:
"""Calculate the area of a geometry.
:param geometry: Geometry column to calculate the area of.
:type geometry: ColumnOrName
:return: Area of geometry as a double column.
:rtype: Column
"""
return _call_st_function("ST_Area", geometry)
@validate_argument_types
def ST_AreaSpheroid(geometry: ColumnOrName) -> Column:
"""Calculate the area of a geometry using WGS84 spheroid.
:param geometry: Geometry column to calculate the area of.
:type geometry: ColumnOrName
:return: Area of geometry as a double column. Unit is meter.
:rtype: Column
"""
return _call_st_function("ST_AreaSpheroid", geometry)
@validate_argument_types
def ST_AsBinary(geometry: ColumnOrName) -> Column:
"""Generate the Well-Known Binary (WKB) representation of a geometry.
:param geometry: Geometry column to generate WKB for.
:type geometry: ColumnOrName
:return: Well-Known Binary representation of geometry as a binary column.
:rtype: Column
"""
return _call_st_function("ST_AsBinary", geometry)
@validate_argument_types
def ST_AsEWKB(geometry: ColumnOrName) -> Column:
"""Generate the Extended Well-Known Binary representation of a geometry.
As opposed to WKB, EWKB will include the SRID of the geometry.
:param geometry: Geometry to generate EWKB for.
:type geometry: ColumnOrName
:return: Extended Well-Known Binary representation of geometry as a binary column.
:rtype: Column
"""
return _call_st_function("ST_AsEWKB", geometry)
@validate_argument_types
def ST_AsHEXEWKB(
geometry: ColumnOrName, endian: Optional[ColumnOrName] = None
) -> Column:
"""Generate the Extended Well-Known Binary representation of a geometry as Hex string.
:param geometry: Geometry to generate EWKB for.
:type geometry: ColumnOrName
:return: Extended Well-Known Binary representation of geometry as Hex string.
:rtype: Column
"""
args = (geometry) if endian is None else (geometry, endian)
return _call_st_function("ST_AsHEXEWKB", args)
@validate_argument_types
def ST_AsEWKT(geometry: ColumnOrName) -> Column:
"""Generate the Extended Well-Known Text representation of a geometry column.
As opposed to WKT, EWKT will include the SRID of the geometry.
:param geometry: Geometry column to generate EWKT for.
:type geometry: ColumnOrName
:return: Extended Well-Known Text representation of geometry as a string column.
:rtype: Column
"""
return _call_st_function("ST_AsEWKT", geometry)
@validate_argument_types
def ST_AsGeoJSON(
geometry: ColumnOrName, type: Optional[Union[ColumnOrName, str]] = None
) -> Column:
"""Generate the GeoJSON style representation of a geometry column.
:param geometry: Geometry column to generate GeoJSON for.
:type geometry: ColumnOrName
:return: GeoJSON representation of geometry as a string column.
:rtype: Column
"""
args = (geometry) if type is None else (geometry, type)
return _call_st_function("ST_AsGeoJSON", args)
@validate_argument_types
def ST_AsGML(geometry: ColumnOrName) -> Column:
"""Generate the Geography Markup Language (GML) representation of a
geometry column.
:param geometry: Geometry column to generate GML for.
:type geometry: ColumnOrName
:return: GML representation of geometry as a string column.
:rtype: Column
"""
return _call_st_function("ST_AsGML", geometry)
@validate_argument_types
def ST_AsKML(geometry: ColumnOrName) -> Column:
"""Generate the KML representation of a geometry column.
:param geometry: Geometry column to generate KML for.
:type geometry: ColumnOrName
:return: KML representation of geometry as a string column.
:rtype: Column
"""
return _call_st_function("ST_AsKML", geometry)
@validate_argument_types
def ST_AsText(geometry: ColumnOrName) -> Column:
"""Generate the Well-Known Text (WKT) representation of a geometry column.
:param geometry: Geometry column to generate WKT for.
:type geometry: ColumnOrName
:return: WKT representation of geometry as a string column.
:rtype: Column
"""
return _call_st_function("ST_AsText", geometry)
@validate_argument_types
def ST_Azimuth(point_a: ColumnOrName, point_b: ColumnOrName) -> Column:
"""Calculate the azimuth for two point columns in radians.
:param point_a: One point geometry column to use for the calculation.
:type point_a: ColumnOrName
:param point_b: Other point geometry column to use for the calculation.
:type point_b: ColumnOrName
:return: Azimuth for point_a and point_b in radians as a double column.
:rtype: Column
"""
return _call_st_function("ST_Azimuth", (point_a, point_b))
@validate_argument_types
def ST_BestSRID(geometry: ColumnOrName) -> Column:
"""Estimates the best SRID (EPSG code) of the geometry.
:param geometry: Geometry column to calculate the boundary for.
:type geometry: ColumnOrName
:return: SRID as an Integer
:rtype: Column
"""
return _call_st_function("ST_BestSRID", geometry)
@validate_argument_types
def ST_ShiftLongitude(geometry: ColumnOrName) -> Column:
"""Shifts longitudes between -180..0 degrees to 180..360 degrees and vice versa.
:param geometry: Geometry column.
:type geometry: ColumnOrName
:return: Shifted geometry
:rtype: Column
"""
return _call_st_function("ST_ShiftLongitude", geometry)
@validate_argument_types
def ST_Boundary(geometry: ColumnOrName) -> Column:
"""Calculate the closure of the combinatorial boundary of a geometry column.
:param geometry: Geometry column to calculate the boundary for.
:type geometry: ColumnOrName
:return: Boundary of the input geometry as a geometry column.
:rtype: Column
"""
return _call_st_function("ST_Boundary", geometry)
@validate_argument_types
def ST_Buffer(
geometry: ColumnOrName,
buffer: ColumnOrNameOrNumber,
useSpheroid: Optional[Union[ColumnOrName, bool]] = None,
parameters: Optional[Union[ColumnOrName, str]] = None,
) -> Column:
"""Calculate a geometry that represents all points whose distance from the
input geometry column is equal to or less than a given amount.
:param geometry: Input geometry column to buffer.
:type geometry: ColumnOrName
:param buffer: Either a column or value for the amount to buffer the input geometry by.
:type buffer: ColumnOrNameOrNumber
:return: Buffered geometry as a geometry column.
:rtype: Column
"""
if parameters is None and useSpheroid is None:
args = (geometry, buffer)
elif parameters is None:
args = (geometry, buffer, useSpheroid)
else:
args = (geometry, buffer, useSpheroid, parameters)
return _call_st_function("ST_Buffer", args)
@validate_argument_types
def ST_BuildArea(geometry: ColumnOrName) -> Column:
"""Generate a geometry described by the constituent linework of the input
geometry column.
:param geometry: Linestring or multilinestring geometry column to use as input.
:type geometry: ColumnOrName
:return: Area formed by geometry as a geometry column.
:rtype: Column
"""
return _call_st_function("ST_BuildArea", geometry)
@validate_argument_types
def ST_Centroid(geometry: ColumnOrName) -> Column:
"""Calculate the centroid of the given geometry column.
:param geometry: Geometry column to calculate a centroid for.
:type geometry: ColumnOrName
:return: Centroid of geometry as a point geometry column.
:rtype: Column
"""
return _call_st_function("ST_Centroid", geometry)
@validate_argument_types
def ST_Collect(*geometries: ColumnOrName) -> Column:
"""Collect multiple geometry columns or an array of geometries into a single
multi-geometry or geometry collection.
:param geometries: Either a single geometry column that holds an array of geometries or multiple geometry columns.
:return: If the types of geometries are homogeneous then a multi-geometry is returned, otherwise a geometry collection is returned.
:rtype: Column
"""
if len(geometries) == 1:
return _call_st_function("ST_Collect", geometries)
else:
return _call_st_function("ST_Collect", [geometries])
@validate_argument_types
def ST_CollectionExtract(
collection: ColumnOrName, geom_type: Optional[Union[ColumnOrName, int]] = None
) -> Column:
"""Extract a specific type of geometry from a geometry collection column
as a multi-geometry column.
:param collection: Column for the geometry collection.
:type collection: ColumnOrName
:param geom_type: Type of geometry to extract where 1 is point, 2 is linestring, and 3 is polygon, if None then the highest dimension geometry is extracted, defaults to None
:type geom_type: Optional[Union[ColumnOrName, int]], optional
:return: Multi-geometry column containing all geometry from collection of the selected type.
:rtype: Column
"""
args = (collection,) if geom_type is None else (collection, geom_type)
return _call_st_function("ST_CollectionExtract", args)
@validate_argument_types
def ST_ClosestPoint(a: ColumnOrName, b: ColumnOrName) -> Column:
"""Returns the 2-dimensional point on geom1 that is closest to geom2.
This is the first point of the shortest line between the geometries.
:param a: Geometry column to use in the calculation.
:type a: ColumnOrName
:param b: Geometry column to use in the calculation.
:type b: ColumnOrName
:return: the 2-dimensional point on a that is closest to b.
:rtype: Column
"""
return _call_st_function("ST_ClosestPoint", (a, b))
@validate_argument_types
def ST_ConcaveHull(
geometry: ColumnOrName,
pctConvex: Union[ColumnOrName, float],
allowHoles: Optional[Union[ColumnOrName, bool]] = None,
) -> Column:
"""Generate the cancave hull of a geometry column.
:param geometry: Geometry column to generate a cancave hull for.
:type geometry: ColumnOrName
:param pctConvex: value between 0 and 1, controls the concaveness of the computed hull.
:type pctConvex: Union[ColumnOrName, float]
:param allowHoles: The computed hull will not contain holes unless allowHoles is specified as true
:type allowHoles: Optional[Union[ColumnOrName, bool]], optional
:return: Concave hull of geometry as a geometry column.
:rtype: Column
"""
args = (
(geometry, pctConvex)
if allowHoles is None
else (geometry, pctConvex, allowHoles)
)
return _call_st_function("ST_ConcaveHull", args)
@validate_argument_types
def ST_ConvexHull(geometry: ColumnOrName) -> Column:
"""Generate the convex hull of a geometry column.
:param geometry: Geometry column to generate a convex hull for.
:type geometry: ColumnOrName
:return: Convex hull of geometry as a geometry column.
:rtype: Column
"""
return _call_st_function("ST_ConvexHull", geometry)
@validate_argument_types
def ST_CrossesDateLine(a: ColumnOrName) -> Column:
"""Check whether geometry a crosses the International Date Line.
:param a: Geometry to check crossing with.
:type a: ColumnOrName
:return: True if geometry a cross the dateline.
:rtype: Column
"""
return _call_st_function("ST_CrossesDateLine", (a))
@validate_argument_types
def ST_Dimension(geometry: ColumnOrName):
"""Calculate the inherent dimension of a geometry column.
:param geometry: Geometry column to calculate the dimension for.
:type geometry: ColumnOrName
:return: Dimension of geometry as an integer column.
:rtype: Column
"""
return _call_st_function("ST_Dimension", geometry)
@validate_argument_types
def ST_Difference(a: ColumnOrName, b: ColumnOrName) -> Column:
"""Calculate the difference of two geometry columns. This difference
is not symmetric. It only returns the part of geometry a that is not
in b.
:param a: Geometry column to use in the calculation.
:type a: ColumnOrName
:param b: Geometry column to subtract from geometry column a.
:type b: ColumnOrName
:return: Part of geometry a that is not in b as a geometry column.
:rtype: Column
"""
return _call_st_function("ST_Difference", (a, b))
@validate_argument_types
def ST_Distance(a: ColumnOrName, b: ColumnOrName) -> Column:
"""Calculate the minimum cartesian distance between two geometry columns.
:param a: Geometry column to use in the calculation.
:type a: ColumnOrName
:param b: Other geometry column to use in the calculation.
:type b: ColumnOrName
:return: Two-dimensional cartesian distance between a and b as a double column.
:rtype: Column
"""
return _call_st_function("ST_Distance", (a, b))
@validate_argument_types
def ST_DistanceSpheroid(a: ColumnOrName, b: ColumnOrName) -> Column:
"""Calculate the geodesic distance between two geometry columns using WGS84 spheroid.
:param a: Geometry column to use in the calculation.
:type a: ColumnOrName
:param b: Other geometry column to use in the calculation.
:type b: ColumnOrName
:return: Two-dimensional geodesic distance between a and b as a double column. Unit is meter.
:rtype: Column
"""
return _call_st_function("ST_DistanceSpheroid", (a, b))
@validate_argument_types
def ST_DistanceSphere(
a: ColumnOrName,
b: ColumnOrName,
radius: Optional[Union[ColumnOrName, float]] = 6371008.0,
) -> Column:
"""Calculate the haversine/great-circle distance between two geometry columns using a given radius.
:param a: Geometry column to use in the calculation.
:type a: ColumnOrName
:param b: Other geometry column to use in the calculation.
:type b: ColumnOrName
:param radius: Radius of the sphere, defaults to 6371008.0
:type radius: Optional[Union[ColumnOrName, float]], optional
:return: Two-dimensional haversine/great-circle distance between a and b as a double column. Unit is meter.
:rtype: Column
"""
return _call_st_function("ST_DistanceSphere", (a, b, radius))
@validate_argument_types
def ST_Dump(geometry: ColumnOrName) -> Column:
"""Returns an array of geometries that are members of a multi-geometry
or geometry collection column. If the input geometry is a regular geometry
then the geometry is returned inside of a single element array.
:param geometry: Geometry column to dump.
:type geometry: ColumnOrName
:return: Array of geometries column comprised of the members of geometry.
:rtype: Column
"""
return _call_st_function("ST_Dump", geometry)
@validate_argument_types
def ST_DumpPoints(geometry: ColumnOrName) -> Column:
"""Return the list of points of a geometry column. Specifically, return
the vertices of the input geometry as an array.
:param geometry: Geometry column to dump the points of.
:type geometry: ColumnOrName
:return: Array of point geometry column comprised of the vertices of geometry.
:rtype: Column
"""
return _call_st_function("ST_DumpPoints", geometry)
@validate_argument_types
def ST_EndPoint(line_string: ColumnOrName) -> Column:
"""Return the last point of a linestring geometry column.
:param line_string: Linestring geometry column to get the end point of.
:type line_string: ColumnOrName
:return: The last point of the linestring geometry column as a point geometry column.
:rtype: Column
"""
return _call_st_function("ST_EndPoint", line_string)
@validate_argument_types
def ST_Envelope(geometry: ColumnOrName) -> Column:
"""Calculate the envelope boundary of a geometry column.
:param geometry: Geometry column to calculate the envelope of.
:type geometry: ColumnOrName
:return: Envelope of geometry as a geometry column.
:rtype: Column
"""
return _call_st_function("ST_Envelope", geometry)
@validate_argument_types
def ST_Expand(
geometry: ColumnOrName,
deltaX_uniformDelta: Union[ColumnOrName, float],
deltaY: Optional[Union[ColumnOrName, float]] = None,
deltaZ: Optional[Union[ColumnOrName, float]] = None,
) -> Column:
"""Expand the given geometry column by a constant unit in each direction
:param geometry: Geometry column to calculate the envelope of.
:type geometry: ColumnOrName
:param deltaX_uniformDelta: it is either deltaX or uniformDelta depending on the number of arguments provided
:type deltaX_uniformDelta: Union[ColumnOrName, float]
:param deltaY: Constant unit of deltaY
:type deltaY: Union[ColumnOrName, float]
:param deltaZ: Constant unit of deltaZ
:type deltaZ: Union[ColumnOrName, float]
:return: Envelope of geometry as a geometry column.
:rtype: Column
"""
if deltaZ is None:
args = (geometry, deltaX_uniformDelta, deltaY)
if deltaY is None:
args = (geometry, deltaX_uniformDelta)
else:
args = (geometry, deltaX_uniformDelta, deltaY, deltaZ)
return _call_st_function("ST_Expand", args)
@validate_argument_types
def ST_ExteriorRing(polygon: ColumnOrName) -> Column:
"""Get a linestring representing the exterior ring of a polygon geometry
column.
:param polygon: Polygon geometry column to get the exterior ring of.
:type polygon: ColumnOrName
:return: Exterior ring of polygon as a linestring geometry column.
:rtype: Column
"""
return _call_st_function("ST_ExteriorRing", polygon)
@validate_argument_types
def ST_FlipCoordinates(geometry: ColumnOrName) -> Column:
"""Flip the X and Y coordinates of a geometry column.
:param geometry: Geometry column to flip coordinates for.
:type geometry: ColumnOrName
:return: Geometry column identical to geometry except with flipped coordinates.
:rtype: Column
"""
return _call_st_function("ST_FlipCoordinates", geometry)
@validate_argument_types
def ST_Force_2D(geometry: ColumnOrName) -> Column:
"""Force the geometry column to only output two dimensional representations.
:param geometry: Geometry column to force to be 2D.
:type geometry: ColumnOrName
:return: Geometry column identical to geometry except with only X and Y coordinates.
:rtype: Column
"""
return _call_st_function("ST_Force_2D", geometry)
@validate_argument_types
def ST_GeneratePoints(
geometry: ColumnOrName,
numPoints: Union[ColumnOrName, int],
seed: Optional[Union[ColumnOrName, int]] = None,
) -> Column:
"""Generate random points in given geometry.
:param geometry: Geometry column to hash.
:type geometry: ColumnOrName
:param numPoints: Precision level to hash geometry at, given as an integer or an integer column.
:type numPoints: Union[ColumnOrName, int]
:return: Generate random points in given geometry
:rtype: Column
"""
if seed is None:
args = (geometry, numPoints)
else:
args = (geometry, numPoints, seed)
return _call_st_function("ST_GeneratePoints", args)
@validate_argument_types
def ST_GeoHash(geometry: ColumnOrName, precision: Union[ColumnOrName, int]) -> Column:
"""Return the geohash of a geometry column at a given precision level.
:param geometry: Geometry column to hash.
:type geometry: ColumnOrName
:param precision: Precision level to hash geometry at, given as an integer or an integer column.
:type precision: Union[ColumnOrName, int]
:return: Geohash of geometry as a string column.
:rtype: Column
"""
return _call_st_function("ST_GeoHash", (geometry, precision))
@validate_argument_types
def ST_GeometricMedian(
geometry: ColumnOrName,
tolerance: Optional[Union[ColumnOrName, float]] = 1e-6,
max_iter: Optional[Union[ColumnOrName, int]] = 1000,
fail_if_not_converged: Optional[Union[ColumnOrName, bool]] = False,
) -> Column:
"""Computes the approximate geometric median of a MultiPoint geometry using the Weiszfeld algorithm.
The geometric median provides a centrality measure that is less sensitive to outlier points than the centroid.
The algorithm will iterate until the distance change between successive iterations is less than the
supplied `tolerance` parameter. If this condition has not been met after `maxIter` iterations, the function will
produce an error and exit, unless `failIfNotConverged` is set to `false`. If a `tolerance` value is not provided,
a default `tolerance` value is `1e-6`.
:param geometry: MultiPoint or Point geometry.
:type geometry: ColumnOrName
:param tolerance: Distance limit change between successive iterations, defaults to 1e-6.
:type tolerance: Optional[Union[ColumnOrName, float]], optional
:param max_iter: Max number of iterations, defaults to 1000.
:type max_iter: Optional[Union[ColumnOrName, int]], optional
:param fail_if_not_converged: Generate error if not converged within given tolerance and number of iterations, defaults to False
:type fail_if_not_converged: Optional[Union[ColumnOrName, boolean]], optional
:return: Point geometry column.
:rtype: Column
"""
args = (geometry, tolerance, max_iter, fail_if_not_converged)
return _call_st_function("ST_GeometricMedian", args)
@validate_argument_types
def ST_GeometryN(multi_geometry: ColumnOrName, n: Union[ColumnOrName, int]) -> Column:
"""Return the geometry at index n (0-th based) of a multi-geometry column.
:param multi_geometry: Multi-geometry column to get from.
:type multi_geometry: ColumnOrName
:param n: Index to select, given as an integer or integer column, 0-th based index, returns null if index is greater than maximum index.
:type n: Union[ColumnOrName, int]
:return: Geometry located at index n in multi_geometry as a geometry column.
:rtype: Column
:raises ValueError: If
"""
if isinstance(n, int) and n < 0:
raise ValueError(f"Index n for ST_GeometryN must by >= 0: {n} < 0")
return _call_st_function("ST_GeometryN", (multi_geometry, n))
@validate_argument_types
def ST_GeometryType(geometry: ColumnOrName) -> Column:
"""Return the type of geometry in a given geometry column.
:param geometry: Geometry column to find the type for.
:type geometry: ColumnOrName
:return: Type of geometry as a string column.
:rtype: Column
"""
return _call_st_function("ST_GeometryType", geometry)
@validate_argument_types
def ST_H3CellDistance(
cell1: Union[ColumnOrName, int], cell2: Union[ColumnOrName, int]
) -> Column:
"""Cover Geometry with H3 Cells and return a List of Long type cell IDs
:param cell: start cell
:type cell: long
:param k: end cell
:type k: int
:return: distance between cells
:rtype: Long
"""
args = (cell1, cell2)
return _call_st_function("ST_H3CellDistance", args)
@validate_argument_types
def ST_H3CellIDs(
geometry: ColumnOrName,
level: Union[ColumnOrName, int],
full_cover: Union[ColumnOrName, bool],
) -> Column:
"""Cover Geometry with H3 Cells and return a List of Long type cell IDs
:param geometry: Geometry column to generate cell IDs
:type geometry: ColumnOrName
:param level: value between 1 and 15, controls the size of the cells used for coverage. With a bigger level, the cells will be smaller, the coverage will be more accurate, but the result size will be exponentially increasing.
:type level: int
:param full_cover: ColumnOrName
:type full_cover: int
:return: List of cellIDs
:rtype: List[long]
"""
args = (geometry, level, full_cover)
return _call_st_function("ST_H3CellIDs", args)
@validate_argument_types
def ST_H3KRing(
cell: Union[ColumnOrName, int],
k: Union[ColumnOrName, int],
exact_ring: Union[ColumnOrName, bool],
) -> Column:
"""Cover Geometry with H3 Cells and return a List of Long type cell IDs
:param cell: original cell
:type cell: long
:param k: the k number of rings spread from the original cell
:type k: int
:param exact_ring: if exactDistance is true, it will only return the cells on the exact kth ring, else will return all 0 - kth neighbors
:type exact_ring: bool
:return: List of cellIDs
:rtype: List[long]
"""
args = (cell, k, exact_ring)
return _call_st_function("ST_H3KRing", args)
@validate_argument_types
def ST_H3ToGeom(cells: Union[ColumnOrName, list]) -> Column:
"""Cover Geometry with H3 Cells and return a List of Long type cell IDs
:param cells: h3 cells
:return: the reversed multipolygon
:rtype: Geometry
"""
return _call_st_function("ST_H3ToGeom", cells)
@validate_argument_types
def ST_InteriorRingN(polygon: ColumnOrName, n: Union[ColumnOrName, int]) -> Column:
"""Return the index n (0-th based) interior ring of a polygon geometry column.
:param polygon: Polygon geometry column to get an interior ring from.
:type polygon: ColumnOrName
:param n: Index of interior ring to return as either an integer or integer column, 0-th based.
:type n: Union[ColumnOrName, int]
:raises ValueError: If n is an integer and less than 0.
:return: Interior ring at index n as a linestring geometry column or null if n is greater than maximum index
:rtype: Column
"""
if isinstance(n, int) and n < 0:
raise ValueError(f"Index n for ST_InteriorRingN must by >= 0: {n} < 0")
return _call_st_function("ST_InteriorRingN", (polygon, n))
@validate_argument_types
def ST_Intersection(a: ColumnOrName, b: ColumnOrName) -> Column:
"""Calculate the intersection of two geometry columns.
:param a: One geometry column to use in the calculation.
:type a: ColumnOrName
:param b: Other geometry column to use in the calculation.
:type b: ColumnOrName
:return: Intersection of a and b as a geometry column.
:rtype: Column
"""
return _call_st_function("ST_Intersection", (a, b))
@validate_argument_types
def ST_IsClosed(geometry: ColumnOrName) -> Column:
"""Check if the linestring in a geometry column is closed (its end point is equal to its start point).
:param geometry: Linestring geometry column to check.
:type geometry: ColumnOrName
:return: True if geometry is closed and False otherwise as a boolean column.
:rtype: Column
"""
return _call_st_function("ST_IsClosed", geometry)
@validate_argument_types
def ST_IsEmpty(geometry: ColumnOrName) -> Column:
"""Check if the geometry in a geometry column is an empty geometry.
:param geometry: Geometry column to check.
:type geometry: ColumnOrName
:return: True if the geometry is empty and False otherwise as a boolean column.
:rtype: Column
"""
return _call_st_function("ST_IsEmpty", geometry)
@validate_argument_types
def ST_IsPolygonCW(geometry: ColumnOrName) -> Column:
"""Check if the Polygon or MultiPolygon use a clockwise orientation for exterior ring and counter-clockwise
orientation for interior ring.
:param geometry: Geometry column to check.
:type geometry: ColumnOrName
:return: True if the geometry is empty and False otherwise as a boolean column.
:rtype: Column
"""
return _call_st_function("ST_IsPolygonCW", geometry)
@validate_argument_types
def ST_IsRing(line_string: ColumnOrName) -> Column:
"""Check if a linestring geometry is both closed and simple.
:param line_string: Linestring geometry column to check.
:type line_string: ColumnOrName
:return: True if the linestring is both closed and simple and False otherwise as a boolean column.
:rtype: Column
"""
return _call_st_function("ST_IsRing", line_string)
@validate_argument_types
def ST_IsSimple(geometry: ColumnOrName) -> Column:
"""Check if a geometry's only intersections are at boundary points.
:param geometry: Geometry column to check in.
:type geometry: ColumnOrName
:return: True if geometry is simple and False otherwise as a boolean column.
:rtype: Column
"""
return _call_st_function("ST_IsSimple", geometry)
@validate_argument_types
def ST_IsValid(
geometry: ColumnOrName, flag: Optional[Union[ColumnOrName, int]] = None
) -> Column:
"""Check if a geometry is well formed.
:param geometry: Geometry column to check in.
:type geometry: ColumnOrName
:param flag: Optional flag to modify behavior of the validity check.
:type flag: Optional[Union[ColumnOrName, int]]
:return: True if geometry is well formed and False otherwise as a boolean column.
:rtype: Column
"""
args = (geometry,) if flag is None else (geometry, flag)
return _call_st_function("ST_IsValid", args)
@validate_argument_types
def ST_IsValidDetail(
geometry: ColumnOrName, flag: Optional[Union[ColumnOrName, int]] = None
) -> Column:
"""
Return a row of valid, reason and location. valid defines the validity of geometry, reason defines the
reason why it is not valid and location defines the location where it is not valid
If the geometry is valid then it will return null for reason and location
:param geometry: Geometry column to validate.
:type geometry: ColumnOrName
:param flag: Optional flag to modify behavior of the validity check.
:type flag: Optional[Union[ColumnOrName, int]]
:return: Row of valid, reason and location
:rtype: Column
"""
args = (geometry,) if flag is None else (geometry, flag)
return _call_st_function("ST_IsValidDetail", args)
@validate_argument_types
def ST_IsValidTrajectory(geometry: ColumnOrName) -> Column:
"""
Tests if a geometry encodes a valid trajectory. A valid trajectory is represented as a LINESTRING with measures
(M values). The measure values must increase from each vertex to the next.
:param geometry: Geometry column to validate.
:type geometry: ColumnOrName
:return: True if the geometry is valid trajectory and False otherwise as a boolean column.
:rtype: Column
"""
return _call_st_function("ST_IsValidTrajectory", (geometry))
@validate_argument_types
def ST_IsValidReason(
geometry: ColumnOrName, flag: Optional[Union[ColumnOrName, int]] = None
) -> Column:
"""
Provides a text description of why a geometry is not valid or states that it is valid.
An optional flag parameter can be provided for additional options.
:param geometry: Geometry column to validate.
:type geometry: ColumnOrName
:param flag: Optional flag to modify behavior of the validity check.
:type flag: Optional[Union[ColumnOrName, int]]
:return: Description of validity as a string column.
:rtype: Column
"""
args = (geometry,) if flag is None else (geometry, flag)
return _call_st_function("ST_IsValidReason", args)
@validate_argument_types
def ST_Length(geometry: ColumnOrName) -> Column:
"""Calculate the length of a linestring geometry.
:param geometry: Linestring geometry column to calculate length for.
:type geometry: ColumnOrName
:return: Length of geometry as a double column.
:rtype: Column
"""
return _call_st_function("ST_Length", geometry)
@validate_argument_types
def ST_Length2D(geometry: ColumnOrName) -> Column:
"""Calculate the length of a linestring geometry.
:param geometry: Linestring geometry column to calculate length for.
:type geometry: ColumnOrName
:return: Length of geometry as a double column.
:rtype: Column
"""
return _call_st_function("ST_Length2D", geometry)
@validate_argument_types
def ST_LengthSpheroid(geometry: ColumnOrName) -> Column:
"""Calculate the perimeter of a geometry using WGS84 spheroid.
:param geometry: Geometry column to calculate length for.
:type geometry: ColumnOrName
:return: perimeter of geometry as a double column. Unit is meter.
:rtype: Column
"""
return _call_st_function("ST_LengthSpheroid", geometry)
@validate_argument_types
def ST_LineFromMultiPoint(geometry: ColumnOrName) -> Column:
"""Creates a LineString from a MultiPoint geometry.
:param geometry: MultiPoint geometry column to create LineString from.
:type geometry: ColumnOrName
:return: LineString geometry of a MultiPoint geometry column.
:rtype: Column
"""
return _call_st_function("ST_LineFromMultiPoint", geometry)
@validate_argument_types
def ST_LineInterpolatePoint(
geometry: ColumnOrName, fraction: ColumnOrNameOrNumber