Skip to main content

st_force2d

Applies to: check marked yes Databricks Runtime 18.1 and above

Preview

This feature is in Public Preview.

Returns the 2D projection of the input Geography or Geometry value.

The SRID value of the output Geography or Geometry value is equal to that of the input value.

The function returns None if the input is None.

For the corresponding Databricks SQL function, see st_force2d function.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.st_force2d(col=<col>)

Parameters

Parameter

Type

Description

col

pyspark.sql.Column or str

A Geography or Geometry value.

Notes

If the input has Z and/or M coordinates, these are dropped in the output.

If the input is already 2D, it is returned as is.

Examples

Drop the M coordinate from a point geography:

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('POINT M (1 2 3)',)], ['wkt'])
df.select(dbf.st_asewkt(dbf.st_force2d(dbf.st_geogfromtext('wkt'))).alias('result')).collect()
Output
[Row(result='SRID=4326;POINT(1 2)')]

Drop Z and M coordinates from a multipoint geography:

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('MULTIPOINT ZM (EMPTY,0 0 10 20, 1 1 11 21)',)], ['wkt'])
df.select(dbf.st_asewkt(dbf.st_force2d(dbf.st_geogfromtext('wkt'))).alias('result')).collect()
Output
[Row(result='SRID=4326;MULTIPOINT(EMPTY,(0 0),(1 1))')]

Drop the Z coordinate from a polygon geography:

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('POLYGON Z ((0 0 2,1 0 3,0 1 4,0 0 5))',)], ['wkt'])
df.select(dbf.st_asewkt(dbf.st_force2d(dbf.st_geogfromtext('wkt'))).alias('result')).collect()
Output
[Row(result='SRID=4326;POLYGON((0 0,1 0,0 1,0 0))')]

Drop the Z coordinate from a point geometry:

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('POINT Z (1 2 3)',)], ['wkt'])
df.select(dbf.st_asewkt(dbf.st_force2d(dbf.st_geomfromtext('wkt', 4326))).alias('result')).collect()
Output
[Row(result='SRID=4326;POINT(1 2)')]

Drop Z and M coordinates from a linestring geometry:

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('LINESTRING ZM (0 0 10 20, 1 1 11 21)',)], ['wkt'])
df.select(dbf.st_asewkt(dbf.st_force2d(dbf.st_geomfromtext('wkt', 4326))).alias('result')).collect()
Output
[Row(result='SRID=4326;LINESTRING(0 0,1 1)')]

Return the input 2D geometry as is:

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('POINT(1 2)',)], ['wkt'])
df.select(dbf.st_asewkt(dbf.st_force2d(dbf.st_geomfromtext('wkt', 4326))).alias('result')).collect()
Output
[Row(result='SRID=4326;POINT(1 2)')]

Preserve the SRID of the input geography:

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('POINT(1 2)',)], ['wkt'])
df.select(dbf.st_srid(dbf.st_force2d(dbf.st_geogfromtext('wkt'))).alias('result')).collect()
Output
[Row(result=4326)]

Preserve the SRID of the input geometry:

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('POINT(1 2)',)], ['wkt'])
df.select(dbf.st_srid(dbf.st_force2d(dbf.st_geomfromtext('wkt', 4326))).alias('result')).collect()
Output
[Row(result=4326)]

Return None with an input of None:

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(None,)], ['wkt'])
df.select(dbf.st_force2d(dbf.st_geogfromtext('wkt')).alias('result')).collect()
Output
[Row(result=None)]