Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ internal fun CoverageGrid.bands(ranges: List<ClosedFloatingPointRange<Double>>):
// Greedily merge horizontal runs of in-band cells into rectangles: far fewer polygons than
// one per cell, and the map renders a handful of rings rather than thousands.
val taken = BooleanArray(width * height)
for (y in 0 until height - 1) {
for (y in 0 until height) {
var x = 0
while (x < width - 1) {
while (x < width) {
if (taken[y * width + x] || !inBand(x, y, range)) {
x++
continue
}
var runEnd = x
while (runEnd + 1 < width - 1 && !taken[y * width + runEnd + 1] && inBand(runEnd + 1, y, range)) {
while (runEnd + 1 < width && !taken[y * width + runEnd + 1] && inBand(runEnd + 1, y, range)) {
runEnd++
}
// Extend downwards while the whole run stays in band.
var runBottom = y
while (
runBottom + 1 < height - 1 &&
runBottom + 1 < height &&
(x..runEnd).all { inBand(it, runBottom + 1, range) } &&
(x..runEnd).none { taken[(runBottom + 1) * width + it] }
) {
Expand All @@ -63,11 +63,11 @@ internal fun CoverageGrid.bands(ranges: List<ClosedFloatingPointRange<Double>>):
}
rings.add(
listOf(
lonAt(x.toDouble()) to latAt(y.toDouble()),
lonAt(runEnd + 1.0) to latAt(y.toDouble()),
lonAt(runEnd + 1.0) to latAt(runBottom + 1.0),
lonAt(x.toDouble()) to latAt(runBottom + 1.0),
lonAt(x.toDouble()) to latAt(y.toDouble()),
lonAt(x - HALF_CELL) to latAt(y - HALF_CELL),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Data Integrity & Integration · Quick win

Reject one-row and one-column grids before generating rings.

A CoverageGrid with width = 1 or height = 1 passes its constructor check. The new full-grid scan now reaches this coordinate conversion. lonAt() or latAt() then divides by zero, and toGeoJson() emits invalid, non-finite coordinates. Require both dimensions to be at least two in CoverageGrid, or handle degenerate grids before building rings. (github.com)

lonAt(runEnd + HALF_CELL) to latAt(y - HALF_CELL),
lonAt(runEnd + HALF_CELL) to latAt(runBottom + HALF_CELL),
lonAt(x - HALF_CELL) to latAt(runBottom + HALF_CELL),
lonAt(x - HALF_CELL) to latAt(y - HALF_CELL),
),
)
x = runEnd + 1
Expand Down Expand Up @@ -142,6 +142,7 @@ $features
"""
}

private const val HALF_CELL = 0.5
private const val DEFAULT_BANDS = 6
private const val EMPTY_FEATURE_COLLECTION =
"""{"type":"FeatureCollection","properties":{"generator":"meshtastic-kp1812"},"features":[]}"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.meshtastic.feature.coverage

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class CoverageContoursTest {
Expand Down Expand Up @@ -48,4 +49,23 @@ class CoverageContoursTest {

assertTrue("\"name\": \"Tom's \\\"Hill\\\" \\\\ relay\"" in geoJson, geoJson.take(300))
}

@Test
fun mergedBandExtendsHalfACellBeyondEverySample() {
val grid =
CoverageGrid(
site = Site("Test", 10.0, 26.0, 915.0, 30.0, -130.0),
width = 2,
height = 2,
north = 12.0,
south = 8.0,
east = 32.0,
west = 20.0,
dbm = DoubleArray(4) { -90.0 },
)

val ring = grid.bands(listOf(-130.0..-80.0)).single().rings.single()

assertEquals(listOf(14.0 to 14.0, 38.0 to 14.0, 38.0 to 6.0, 14.0 to 6.0, 14.0 to 14.0), ring)
}
}
Loading