ST_Extend#
Table of Contents
Part 0: Setup BDT#
[ ]:
import bdt
bdt.auth("bdt.lic")
from bdt.processors import *
from bdt.functions import *
from pyspark.sql.functions import *
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
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
Part 1: Generate Sample Data#
Part 1.1: Sample Data for Simple Line (2 points)#
[ ]:
line_wkt = "LINESTRING (0.0 0.0, 4.0 2.0)"
line_schema = StructType([StructField("LINE_ID", IntegerType()),
StructField("LINE_WKT", StringType())])
line_data = [(1, line_wkt),]
line_df = (
spark
.createDataFrame(data = line_data, schema = line_schema)
.select(col("LINE_ID"), st_fromText("LINE_WKT").alias("SHAPE"))
)
line_df.show(truncate = True)
+-------+--------------------+
|LINE_ID| SHAPE|
+-------+--------------------+
| 1|{[01 05 00 00 00 ...|
+-------+--------------------+
Part 1.2: Sample Data for Multipath (2 lines)#
[ ]:
multipath_wkt = "MULTILINESTRING ((0.0 0.0, 4.0 2.0), (3.0 4.0, 7.0 2.0))"
multipath_schema = StructType([StructField("MULTIPATH_ID", IntegerType()),
StructField("MULTIPATH_WKT", StringType())])
multipath_data = [(1, multipath_wkt),]
multipath_df = (
spark
.createDataFrame(data = multipath_data, schema = multipath_schema)
.select(col("MULTIPATH_ID"), st_fromText("MULTIPATH_WKT").alias("SHAPE"))
)
multipath_df.show(truncate = True)
+------------+--------------------+
|MULTIPATH_ID| SHAPE|
+------------+--------------------+
| 1|{[01 05 00 00 00 ...|
+------------+--------------------+
Part 2: ST_Extend#
ST_Extend supports lines with one or more segments as an argument, but not multipaths.
If ST_Extend needs to be used on multipaths, first use ST_Dump to extract each of the component lines into their own row in the DataFrame. Then, ST_Extend can be used on each of the component lines. Below, the two cases are illustrated (line and multipath).
Part 2.1: ST_Extend on Line#
Call ST_Extend on the simple line with two points.
ST_Extend takes the following arguments in order:
The line SHAPE.
The distance to extend in the origin direction.
The distance to extend in the destination direction.
[ ]:
result_df = (
line_df
.select(st_extend("SHAPE", 2.0, 4.0).alias("SHAPE"))
.select(st_asText("SHAPE").alias("WKT"))
)
result_df.show(truncate=False)
+-------------------------------------------------------------------------------------------------+
|WKT |
+-------------------------------------------------------------------------------------------------+
|MULTILINESTRING ((-1.7888543819998317 -0.8944271909999159, 7.577708763999663 3.7888543819998315))|
+-------------------------------------------------------------------------------------------------+
The red parts of the line are the parts added by ST_Extend.
Part 2.2: ST_Extend on Multipath#
ST_Extend does not support Multipaths. To use ST_Extend on a Multipath, first use ST_Dump to divide the multpath into its component lines.
[ ]:
line_df_2 = multipath_df.select(explode(st_dump("SHAPE")).alias("SHAPE"))
line_df_2_wkt = line_df_2.select(st_asText("SHAPE")).alias("WKT")
line_df_2_wkt.show(truncate=False)
+----------------------------+
|ST_AsText(SHAPE) |
+----------------------------+
|MULTILINESTRING ((0 0, 4 2))|
|MULTILINESTRING ((3 4, 7 2))|
+----------------------------+
ST_Extend can be used now that each part (line) of the Multipath has been dumped into its own row.
Call ST_Extend on each of the two lines that composed the Multipath.
[ ]:
result_df = (
line_df_2
.select(st_extend("SHAPE", 2.0, 4.0).alias("SHAPE"))
.select(st_asText("SHAPE").alias("WKT"))
)
result_df.show(truncate=False)
+-------------------------------------------------------------------------------------------------+
|WKT |
+-------------------------------------------------------------------------------------------------+
|MULTILINESTRING ((-1.7888543819998317 -0.8944271909999159, 7.577708763999663 3.7888543819998315))|
|MULTILINESTRING ((1.2111456180001683 4.894427190999916, 10.577708763999663 0.2111456180001683)) |
+-------------------------------------------------------------------------------------------------+
Each line of the Multpath is extended.