Share via


st_geometryn

Important

This feature is in Public Preview.

Returns the 1-based n-th element of the input multi geometry, or None if it doesn't exist.

For the corresponding Databricks SQL function, see st_geometryn function.

Syntax

from pyspark.databricks.sql import functions as dbf

dbf.st_geometryn(col1=<col1>, col2=<col2>)

Parameters

Parameter Type Description
col1 pyspark.sql.Column or str A Geometry value.
col2 pyspark.sql.Column or int The 1-based index of the geometry to return.

Examples

from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('GEOMETRYCOLLECTION(POINT(4 5),LINESTRING(10 3,24 37,44 85))',)], ['wkt'])
df.select(dbf.st_asewkt(dbf.st_geometryn(dbf.st_geomfromtext('wkt', 4326), 2)).alias('result')).collect()
[Row(result='SRID=4326;LINESTRING(10 3,24 37,44 85)')]
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('MULTIPOLYGON(EMPTY,((0 0,10 0,0 10,0 0),(1 1,9 1,1 9,1 1)))',)], ['wkt'])
df.select(dbf.st_asewkt(dbf.st_geometryn(dbf.st_geomfromtext('wkt'), 2)).alias('result')).collect()
[Row(result='POLYGON((0 0,10 0,0 10,0 0),(1 1,9 1,1 9,1 1))')]
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('POINT(1 2)',)], ['wkt'])
df.select(dbf.st_asewkt(dbf.st_geometryn(dbf.st_geomfromtext('wkt'), 1)).alias('result')).collect()
[Row(result='POINT(1 2)')]