-
-
Notifications
You must be signed in to change notification settings - Fork 20
Open
Description
The PolygonGeometry.contains function already checks the bounding box before checking the exact polygon. This is a good optimization. However, the bounding box is recomputed every time, even if the PolygonGeometry object already has a bbox set. That negates much of the optimization
spatial-k/turf/src/commonMain/kotlin/org/maplibre/spatialk/turf/booleans/contains.kt
Lines 35 to 55 in 53208b0
| public fun PolygonGeometry.contains(pos: Position, ignoreBoundary: Boolean): Boolean { | |
| val bbox = this.computeBbox() | |
| // normalize to multipolygon | |
| val polys = | |
| when (this) { | |
| is Polygon -> listOf(this.coordinates) | |
| is MultiPolygon -> this.coordinates | |
| } | |
| return pointInPolygon(pos, bbox, polys, ignoreBoundary) | |
| } | |
| private fun pointInPolygon( | |
| pos: Position, | |
| bbox: BoundingBox, | |
| polys: List<List<List<Position>>>, | |
| ignoreBoundary: Boolean, | |
| ): Boolean { | |
| // Quick elimination if the point is not inside the bbox | |
| if (!inBBox(pos, bbox)) { | |
| return false | |
| } |
Line 36 should instead be:
val bbox = this.bbox ?: this.computeBbox()Repeated calls to contains will still not remember the computed bounding box (not possible with a data class), but at least users can do
val polygonWithBbox = polygon.withComputedBbox()
polygonWithBbox.contains(point1)
polygonWithBbox.contains(point2)to avoid recomputation.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels