st_makeenvelope

Applies to: check marked yes Databricks Runtime 18.2 and above

Important

This feature is in Public Preview.

Returns a Geometry representing the 2D axis-aligned envelope (minimum bounding box) defined by the two corner coordinates (x1, y1) and (x2, y2).

For the corresponding Databricks SQL function, see st_makeenvelope function.

Syntax

from pyspark.databricks.sql import functions as dbf

dbf.st_makeenvelope(x1=<x1>, y1=<y1>, x2=<x2>, y2=<y2>)

Parameters

Parameter Type Description
x1 pyspark.sql.Column or float The X coordinate of the first corner.
y1 pyspark.sql.Column or float The Y coordinate of the first corner.
x2 pyspark.sql.Column or float The X coordinate of the second corner.
y2 pyspark.sql.Column or float The Y coordinate of the second corner.

Returns

pyspark.sql.Column: A column of Geometry values representing the 2D axis-aligned envelope of the two input corners. The SRID of the returned geometry is 0.

The input corners may be provided in any order; the resulting envelope is the same as if the corners were normalized to (xmin, ymin) and (xmax, ymax).

The type of the returned geometry depends on the input corners:

  • If the box degenerates to a single point (x1 = x2 and y1 = y2), the result is a point.
  • If the box degenerates to a segment (x1 = x2 or y1 = y2, but not both), the result is a linestring with two points.
  • Otherwise, the result is a polygon with five vertices (closed ring).

The function returns None if any of the inputs is None.

Examples

# Returns the polygon envelope defined by two corners.
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(1.0, 2.0, 4.0, 6.0)], ['x1', 'y1', 'x2', 'y2'])
df.select(dbf.st_astext(dbf.st_makeenvelope('x1', 'y1', 'x2', 'y2')).alias('result')).collect()
[Row(result='POLYGON((1 2,1 6,4 6,4 2,1 2))')]
# Corners may be provided in any order.
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(4.0, 6.0, 1.0, 2.0)], ['x1', 'y1', 'x2', 'y2'])
df.select(dbf.st_astext(dbf.st_makeenvelope('x1', 'y1', 'x2', 'y2')).alias('result')).collect()
[Row(result='POLYGON((1 2,1 6,4 6,4 2,1 2))')]
# Returns a point when the box degenerates to a point.
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(3.0, 5.0, 3.0, 5.0)], ['x1', 'y1', 'x2', 'y2'])
df.select(dbf.st_astext(dbf.st_makeenvelope('x1', 'y1', 'x2', 'y2')).alias('result')).collect()
[Row(result='POINT(3 5)')]
# Returns a linestring when the box degenerates to a horizontal segment.
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(1.0, 0.0, 4.0, 0.0)], ['x1', 'y1', 'x2', 'y2'])
df.select(dbf.st_astext(dbf.st_makeenvelope('x1', 'y1', 'x2', 'y2')).alias('result')).collect()
[Row(result='LINESTRING(1 0,4 0)')]
# Returns a linestring when the box degenerates to a vertical segment.
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(0.0, 2.0, 0.0, 7.0)], ['x1', 'y1', 'x2', 'y2'])
df.select(dbf.st_astext(dbf.st_makeenvelope('x1', 'y1', 'x2', 'y2')).alias('result')).collect()
[Row(result='LINESTRING(0 2,0 7)')]
# The SRID of the returned geometry is always 0.
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(0.0, 0.0, 10.0, 10.0)], ['x1', 'y1', 'x2', 'y2'])
df.select(dbf.st_srid(dbf.st_makeenvelope('x1', 'y1', 'x2', 'y2')).alias('result')).collect()
[Row(result=0)]