ST_Within

Within

ST_Within takes two geometry columns and returns a boolean column. The function returns True if the first geometry is completely inside the second geometry; otherwise, it returns False.

FunctionSyntax
Pythonwithin(geometry1, geometry2)
SQLST_Within(geometry1, geometry2)
Scalawithin(geometry1, geometry2)

For more details, go to the GeoAnalytics Engine API reference for within.

This function implements the OpenGIS Simple Features Implementation Specification for SQL 1.2.1.

Examples

PythonPythonSQLScala
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from geoanalytics.sql import functions as ST

data = [
    ("POLYGON ((0 0, 0 10, 10 10, 10 0))", "LINESTRING (2 2, 4 4)" ),
    ("POLYGON ((0 0, 0 10, 10 10, 10 0))", "LINESTRING (0 10, 5 15, 10 20)")
]

df = spark.createDataFrame(data, ["poly_wkt", "line_wkt"])\
          .withColumn("linestring", ST.line_from_text("line_wkt"))\
          .withColumn("polygon", ST.poly_from_text("poly_wkt"))\
          .withColumn("within", ST.within("linestring", "polygon"))\
          .drop("poly_wkt", "line_wkt")

df.show(truncate=False)
Result
Use dark colors for code blocksCopy
1
2
3
4
5
6
+-----------------------------------+-----------------------------------------------+------+
|linestring                         |polygon                                        |within|
+-----------------------------------+-----------------------------------------------+------+
|{"paths":[[[2,2],[4,4]]]}          |{"rings":[[[0,0],[0,10],[10,10],[10,0],[0,0]]]}|true  |
|{"paths":[[[0,10],[5,15],[10,20]]]}|{"rings":[[[0,0],[0,10],[10,10],[10,0],[0,0]]]}|false |
+-----------------------------------+-----------------------------------------------+------+

Version table

ReleaseNotes

1.0.0

Python and SQL functions introduced

1.5.0

Scala function introduced

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.