spatial-k
spatial-k copied to clipboard
PolygonGeometry.contains not using existing bounding box
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
https://github.com/maplibre/spatial-k/blob/53208b07bcf473a698a5dd61a2f930c30476cb17/turf/src/commonMain/kotlin/org/maplibre/spatialk/turf/booleans/contains.kt#L35-L55
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.