Spatial predicates
st_contains
- st_contains(geom1, geom2)
Returns true if geom1 ‘spatially’ contains geom2.
- Parameters:
geom1 (Column) – Geometry
geom2 (Column) – Geometry
- Return type:
Column: BooleanType
- Example:
df = spark.createDataFrame([{'point': 'POINT (25 15)', 'poly': 'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))'}])
df.select(st_contains('poly', 'point')).show()
+------------------------+
|st_contains(poly, point)|
+------------------------+
| true|
+------------------------+
val df = List(("POINT (25 15)", "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))")).toDF("point", "poly")
df.select(st_contains($"poly", $"point")).show()
+------------------------+
|st_contains(poly, point)|
+------------------------+
| true|
+------------------------+
SELECT st_contains("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))", "POINT (25 15)")
+------------------------+
|st_contains(poly, point)|
+------------------------+
| true|
+------------------------+
df <- createDataFrame(data.frame(point = c( "POINT (25 15)"), poly = "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"))
showDF(select(df, st_contains(column("poly"), column("point"))))
+------------------------+
|st_contains(poly, point)|
+------------------------+
| true|
+------------------------+
Note
ST_Within is the inverse of ST_Contains, where ST_Contains(a, b)==ST_Within(b,a).
st_intersects
- st_intersects(geom1, geom2)
Returns true if the geometry geom1 intersects geom2.
- Parameters:
geom1 (Column) – Geometry
geom2 (Column) – Geometry
- Return type:
Column: BooleanType
- Example:
df = spark.createDataFrame([{'p1': 'POLYGON ((0 0, 0 3, 3 3, 3 0))', 'p2': 'POLYGON ((2 2, 2 4, 4 4, 4 2))'}])
df.select(st_intersects(col('p1'), col('p2'))).show(1, False)
+---------------------+
|st_intersects(p1, p2)|
+---------------------+
| true|
+---------------------+
val df = List(("POLYGON ((0 0, 0 3, 3 3, 3 0))", "POLYGON ((2 2, 2 4, 4 4, 4 2))")).toDF("p1", "p2")
df.select(st_intersects($"p1", $"p2")).show(false)
+---------------------+
|st_intersects(p1, p2)|
+---------------------+
| true|
+---------------------+
SELECT st_intersects("POLYGON ((0 0, 0 3, 3 3, 3 0))", "POLYGON ((2 2, 2 4, 4 4, 4 2))")
+---------------------+
|st_intersects(p1, p2)|
+---------------------+
| true|
+---------------------+
df <- createDataFrame(data.frame(p1 = "POLYGON ((0 0, 0 3, 3 3, 3 0))", p2 = "POLYGON ((2 2, 2 4, 4 4, 4 2))"))
showDF(select(df, st_intersects(column("p1"), column("p2"))), truncate=F)
+---------------------+
|st_intersects(p1, p2)|
+---------------------+
| true|
+---------------------+
Note
Intersection logic will be dependent on the chosen geometry API (ESRI or JTS). ESRI is only available for mosaic < 0.4.x series, in mosaic >= 0.4.0 JTS is the only geometry API.
st_within
- st_within(geom1, geom2)
Returns true if geom1 ‘spatially’ is within geom2.
- Parameters:
geom1 (Column) – Geometry
geom2 (Column) – Geometry
- Return type:
Column: BooleanType
- Example:
df = spark.createDataFrame([{'point': 'POINT (25 15)', 'poly': 'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))'}])
df.select(st_within('point', 'poly')).show()
+----------------------+
|st_within(point, poly)|
+----------------------+
| true|
+----------------------+
val df = List(("POINT (25 15)", "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))")).toDF("point", "poly")
df.select(st_within($"point", $"poly")).show()
+----------------------+
|st_within(point, poly)|
+----------------------+
| true|
+----------------------+
SELECT st_within("POINT (25 15)", "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))")
+----------------------+
|st_within(point, poly)|
+----------------------+
| true|
+----------------------+
df <- createDataFrame(data.frame(point = c( "POINT (25 15)"), poly = "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"))
showDF(select(df, st_within(column("point"), column("poly"))))
+----------------------+
|st_within(point, poly)|
+----------------------+
| true|
+----------------------+
Note
ST_Within is the inverse of ST_Contains, where ST_Contains(a, b)==ST_Within(b,a).