Skip to content

PolygonGeometry.contains not using existing bounding box #290

@kodebach

Description

@kodebach

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

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions