Skip to content

Commit

Permalink
[SEDONA-610] Add ST_IsValidTrajectory (Python code) (apache#1489)
Browse files Browse the repository at this point in the history
* feat: add ST_IsValidTrajectory

* remove print statements and add example

* feat: add python code and tests
  • Loading branch information
furqaankhan authored and Kontinuation committed Jul 10, 2024
1 parent b306678 commit 2d26d9a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions python/sedona/sql/st_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,19 @@ def ST_IsValidDetail(geometry: ColumnOrName, flag: Optional[Union[ColumnOrName,
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:
"""
Expand Down
2 changes: 2 additions & 0 deletions python/tests/sql/test_dataframe_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
(stf.ST_IsPolygonCCW, ("geom",), "geom_with_hole", "", True),
(stf.ST_IsRing, ("line",), "linestring_geom", "", False),
(stf.ST_IsSimple, ("geom",), "triangle_geom", "", True),
(stf.ST_IsValidTrajectory, ("line",), "4D_line", "", False),
(stf.ST_IsValid, ("geom",), "triangle_geom", "", True),
(stf.ST_IsValid, ("geom", 1), "triangle_geom", "", True),
(stf.ST_IsValid, ("geom", 0), "triangle_geom", "", True),
Expand Down Expand Up @@ -364,6 +365,7 @@
(stf.ST_IsPolygonCCW, (None,)),
(stf.ST_IsRing, (None,)),
(stf.ST_IsSimple, (None,)),
(stf.ST_IsValidTrajectory, (None,)),
(stf.ST_IsValidDetail, (None,)),
(stf.ST_IsValid, (None,)),
(stf.ST_IsValidReason, (None,)),
Expand Down
8 changes: 8 additions & 0 deletions python/tests/sql/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,14 @@ def test_st_is_valid_detail(self):
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, " +
Expand Down

0 comments on commit 2d26d9a

Please sign in to comment.