ST_SegmentEnds#

Table of Contents#

  1. What is ST_SegmentEnds?

  2. Using ST_SegmentEnds

Part 1: What is ST_SegmentEnds?#

ST_SegmentEnds is a function that returns the first and last segment of a given multipath geometry.

The direction the segments are facing is controlled by the away parameter. When away is true, the returned segments point away from each other. When away is false, the segments point toward each other.

segment ends visual

Part 2: Using ST_SegmentEnds#

[ ]:
import bdt
bdt.auth("bdt.lic")
from bdt import functions as F
from pyspark.sql.functions import explode
BDT has been successfully authorized!

            Welcome to
             ___    _                ___         __             ______             __   __     _   __
            / _ )  (_)  ___ _       / _ \ ___ _ / /_ ___ _     /_  __/ ___  ___   / /  / /__  (_) / /_
           / _  | / /  / _ `/      / // // _ `// __// _ `/      / /   / _ \/ _ \ / /  /  '_/ / / / __/
          /____/ /_/   \_, /      /____/ \_,_/ \__/ \_,_/      /_/    \___/\___//_/  /_/\_\ /_/  \__/
                      /___/

BDT python version: v3.3.0-v3.3.0
BDT jar version: v3.3.0-v3.3.0

Create the multipath data. This is the same line displayed in the visual above.

[ ]:
wkt = "LINESTRING (0 0, 1 1, 2 2, 3 3)"

df = spark.createDataFrame([
    (wkt,)
], schema="wkt string").select(F.st_fromText("wkt").alias("SHAPE"))

df.show(truncate=False)
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|SHAPE                                                                                                                                                                                                                                                                        |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|{[01 05 00 00 00 01 00 00 00 01 02 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F0 3F 00 00 00 00 00 00 F0 3F 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40], 0.0, 0.0, 3.0, 3.0}|
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Call ST_SegmentEnds with explode to create a new row for each output segment.

[ ]:
segments_df = (
    df.select(
        explode(
            F.st_segment_ends("SHAPE", False)
        ).alias("segment_ends")
    )
)

segments_df.show(truncate=False)
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|segment_ends                                                                                                                                                                 |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|{[01 05 00 00 00 01 00 00 00 01 02 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F0 3F 00 00 00 00 00 00 F0 3F], 0.0, 0.0, 1.0, 1.0}|
|{[01 05 00 00 00 01 00 00 00 01 02 00 00 00 02 00 00 00 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 40], 2.0, 2.0, 3.0, 3.0}|
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+