-
Notifications
You must be signed in to change notification settings - Fork 757
[GH-2926] Add ST_BoxIntersects and ST_BoxContains for Box2D #2932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| */ | ||
| package org.apache.sedona.common; | ||
|
|
||
| import org.apache.sedona.common.geometryObjects.Box2D; | ||
| import org.apache.sedona.common.sphere.Spheroid; | ||
| import org.locationtech.jts.geom.*; | ||
| import org.locationtech.jts.operation.relate.RelateOp; | ||
|
|
@@ -27,6 +28,30 @@ public static boolean contains(Geometry leftGeometry, Geometry rightGeometry) { | |
| return leftGeometry.contains(rightGeometry); | ||
| } | ||
|
|
||
| /** | ||
| * Closed-interval bbox intersection: true if {@code a} and {@code b} share any point on either | ||
| * axis (matches PostGIS {@code &&} on box2d). Either argument being null returns null at the SQL | ||
| * layer; this Java entry point throws {@link NullPointerException} on null input. | ||
| */ | ||
| public static boolean boxIntersects(Box2D a, Box2D b) { | ||
| return !(a.getXMax() < b.getXMin() | ||
| || a.getXMin() > b.getXMax() | ||
| || a.getYMax() < b.getYMin() | ||
| || a.getYMin() > b.getYMax()); | ||
|
Comment on lines
+43
to
+49
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 6726b1b — went with option (a). |
||
| } | ||
|
|
||
| /** | ||
| * True if {@code a} fully contains {@code b} (closed intervals; matches PostGIS {@code ~} on | ||
| * box2d). Either argument being null returns null at the SQL layer; this Java entry point throws | ||
| * {@link NullPointerException} on null input. | ||
| */ | ||
| public static boolean boxContains(Box2D a, Box2D b) { | ||
| return a.getXMin() <= b.getXMin() | ||
| && a.getYMin() <= b.getYMin() | ||
| && a.getXMax() >= b.getXMax() | ||
| && a.getYMax() >= b.getYMax(); | ||
|
Comment on lines
+60
to
+66
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same fix in 6726b1b — |
||
| } | ||
|
|
||
| public static boolean intersects(Geometry leftGeometry, Geometry rightGeometry) { | ||
| return leftGeometry.intersects(rightGeometry); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import static org.apache.sedona.common.Functions.crossesDateLine; | ||
| import static org.junit.Assert.*; | ||
|
|
||
| import org.apache.sedona.common.geometryObjects.Box2D; | ||
| import org.junit.Test; | ||
| import org.locationtech.jts.geom.Coordinate; | ||
| import org.locationtech.jts.geom.Geometry; | ||
|
|
@@ -32,6 +33,38 @@ public class PredicatesTest extends TestBase { | |
|
|
||
| private static final GeometryFactory GEOMETRY_FACTORY = new GeometryFactory(); | ||
|
|
||
| @Test | ||
| public void testBoxIntersects() { | ||
| Box2D a = new Box2D(0.0, 0.0, 5.0, 5.0); | ||
|
|
||
| // Full overlap | ||
| assertTrue(Predicates.boxIntersects(a, new Box2D(1.0, 1.0, 2.0, 2.0))); | ||
| // Partial overlap | ||
| assertTrue(Predicates.boxIntersects(a, new Box2D(3.0, 3.0, 7.0, 7.0))); | ||
| // Edge-touching (closed intervals) | ||
| assertTrue(Predicates.boxIntersects(a, new Box2D(5.0, 0.0, 10.0, 5.0))); | ||
| // Corner-touching (closed intervals) | ||
| assertTrue(Predicates.boxIntersects(a, new Box2D(5.0, 5.0, 10.0, 10.0))); | ||
| // Disjoint on X | ||
| assertFalse(Predicates.boxIntersects(a, new Box2D(6.0, 0.0, 10.0, 5.0))); | ||
| // Disjoint on Y | ||
| assertFalse(Predicates.boxIntersects(a, new Box2D(0.0, 6.0, 5.0, 10.0))); | ||
| } | ||
|
Comment on lines
+36
to
+52
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
|
|
||
| @Test | ||
| public void testBoxContains() { | ||
| Box2D outer = new Box2D(0.0, 0.0, 10.0, 10.0); | ||
|
|
||
| assertTrue(Predicates.boxContains(outer, new Box2D(2.0, 2.0, 5.0, 5.0))); | ||
| // Boundaries are inclusive | ||
| assertTrue(Predicates.boxContains(outer, new Box2D(0.0, 0.0, 10.0, 10.0))); | ||
| assertTrue(Predicates.boxContains(outer, new Box2D(0.0, 0.0, 1.0, 1.0))); | ||
| // Outside on X | ||
| assertFalse(Predicates.boxContains(outer, new Box2D(-1.0, 0.0, 5.0, 5.0))); | ||
| // Crosses boundary on X | ||
| assertFalse(Predicates.boxContains(outer, new Box2D(5.0, 0.0, 11.0, 5.0))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDWithinSuccess() { | ||
| Geometry point1 = GEOMETRY_FACTORY.createPoint(new Coordinate(1, 1)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| package org.apache.spark.sql.sedona_sql.expressions | ||
|
|
||
| import org.apache.sedona.common.Predicates | ||
| import org.apache.sedona.common.geometryObjects.Box2D | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed in 6726b1b — |
||
| import org.apache.sedona.sql.utils.GeometrySerializer | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback | ||
|
|
@@ -95,6 +96,22 @@ private[apache] case class ST_Contains(inputExpressions: Seq[Expression]) | |
| * | ||
| * @param inputExpressions | ||
| */ | ||
| private[apache] case class ST_BoxIntersects(inputExpressions: Seq[Expression]) | ||
| extends InferredExpression(Predicates.boxIntersects _) { | ||
|
|
||
|
Comment on lines
92
to
+103
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 6726b1b — replaced the bleed-through Scaladoc above |
||
| protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = { | ||
| copy(inputExpressions = newChildren) | ||
| } | ||
| } | ||
|
|
||
| private[apache] case class ST_BoxContains(inputExpressions: Seq[Expression]) | ||
| extends InferredExpression(Predicates.boxContains _) { | ||
|
|
||
| protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = { | ||
| copy(inputExpressions = newChildren) | ||
| } | ||
| } | ||
|
|
||
| private[apache] case class ST_Intersects(inputExpressions: Seq[Expression]) | ||
| extends InferredExpression( | ||
| inferrableFunction2(Predicates.intersects), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reworded in 6726b1b: now says overlap on both the X and Y axes.