networklocation: validate geocoder generator end-to-end with synthetic GeoJSONSeq self-test
- Commit
833170984e6076717fc66d762c0b243236145b2b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-09 22:03 UTC (3 months ago)
- Parents
bb85a5a
Diff
networklocation/src/test/java/com/vayunmathur/networklocation/geocoder/GeocoderGenerator.kt
diff --git a/networklocation/src/test/java/com/vayunmathur/networklocation/geocoder/GeocoderGenerator.kt b/networklocation/src/test/java/com/vayunmathur/networklocation/geocoder/GeocoderGenerator.ktindex 8e835ce281..fbe922a366 100644--- a/networklocation/src/test/java/com/vayunmathur/networklocation/geocoder/GeocoderGenerator.kt+++ b/networklocation/src/test/java/com/vayunmathur/networklocation/geocoder/GeocoderGenerator.kt@@ -8,7 +8,10 @@ import kotlinx.serialization.json.doubleOrNull import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonObject import java.io.File+import java.nio.file.Files import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue /** * Real geocoder-DB generator. Not a unit test — it reuses the JVM test classpath (GeoDb* are@@ -40,7 +43,15 @@ class GeocoderGenerator { val inFile = File(input) require(inFile.exists()) { "input not found: $input" } val outFile = File(output).apply { parentFile?.mkdirs() }+ buildDb(inFile, outFile)+ } + /**+ * Parse a GeoJSONSeq file (one Feature per line, optional RS separators) into a GeoDb.+ * Returns the number of addresses kept. Shared by [generate] and the self-test so both+ * exercise exactly the same parse -> write path.+ */+ internal fun buildDb(inFile: File, outFile: File): Int { val json = Json { ignoreUnknownKeys = true; isLenient = true } val addresses = ArrayList<GeoAddress>(1 shl 20) var lines = 0L@@ -63,6 +74,83 @@ class GeocoderGenerator { GeoDbWriter().write(outFile, addresses) val mb = outFile.length() / (1024.0 * 1024.0) println("Wrote ${outFile.absolutePath} — %.1f MB for ${addresses.size} addresses.".format(mb))+ return addresses.size+ }++ /**+ * Self-contained end-to-end check: synthesise a tiny GeoJSONSeq sample (Point + LineString ++ * Polygon features spread across two grid cells, plus RS separators and a junk line that must+ * be skipped), run the real parse -> write pipeline, then read it back and assert both reverse+ * and forward lookups. Uses the same [buildDb] path as the planet build, so it protects the+ * parser and the on-disk format in ordinary unit-test runs.+ */+ @Test+ fun selfTestSyntheticSample() {+ val dir = Files.createTempDirectory("geocoder-selftest").toFile()+ val seq = File(dir, "addr.geojsonseq")+ val out = File(dir, "geocoder.geodb")+ try {+ // Cell A ~ (39.78, -89.65); Cell B ~ (40.10, -89.00). CELL is 0.05deg so these differ.+ fun feature(+ house: String, street: String, city: String, state: String,+ country: String, postcode: String, geometry: String,+ ): String = """{"type":"Feature","properties":{""" ++ """"addr:housenumber":"$house","addr:street":"$street",""" ++ """"addr:city":"$city","addr:state":"$state",""" ++ """"addr:country":"$country","addr:postcode":"$postcode"},""" ++ """"geometry":$geometry}"""++ val rs = "\u001e" // GeoJSONSeq record separator; parser must tolerate it.+ val lines = listOf(+ rs + feature("10", "Main St", "Springfield", "IL", "US", "62704",+ """{"type":"Point","coordinates":[-89.6500,39.7800]}"""),+ feature("12", "Main St", "Springfield", "IL", "US", "62704",+ """{"type":"Point","coordinates":[-89.6510,39.7810]}"""),+ // A way (LineString): centroid is the average of the two endpoints.+ feature("20", "Oak Ave", "Springfield", "IL", "US", "62704",+ """{"type":"LineString","coordinates":[[-89.6400,39.7700],[-89.6420,39.7720]]}"""),+ // A building (Polygon) in a different grid cell; centroid ~ (40.10, -89.00).+ rs + feature("5", "Elm St", "Lincoln", "IL", "US", "62656",+ """{"type":"Polygon","coordinates":[[[-89.0010,40.0990],[-88.9990,40.0990],""" ++ """[-88.9990,40.1010],[-89.0010,40.1010],[-89.0010,40.0990]]]}"""),+ // No street/house -> must be skipped.+ """{"type":"Feature","properties":{"addr:city":"Nowhere"},""" ++ """"geometry":{"type":"Point","coordinates":[0,0]}}""",+ "not-json-garbage-line",+ )+ seq.writeText(lines.joinToString("\n"))++ val kept = buildDb(seq, out)+ assertEquals(4, kept, "expected 4 kept addresses (1 no-addr + 1 junk skipped)")++ GeoDbReader(out).use { db ->+ assertEquals(4, db.size)++ // Reverse: query the exact coord of "10 Main St" -> nearest must be it.+ val r = db.reverse(39.7800, -89.6500)!!+ assertEquals("10", r.house, "reverse house")+ assertEquals("Main St", r.street, "reverse street")+ assertEquals(39.7800, r.lat, 1e-4, "reverse lat")+ assertEquals(-89.6500, r.lon, 1e-4, "reverse lon")++ // Reverse in the other cell -> Elm St polygon centroid.+ val r2 = db.reverse(40.1000, -89.0000)!!+ assertEquals("Elm St", r2.street, "reverse cell B street")+ assertEquals(40.1000, r2.lat, 1e-3, "polygon centroid lat")+ assertEquals(-89.0000, r2.lon, 1e-3, "polygon centroid lon")++ // Forward: both Main St houses come back, none from Oak Ave.+ val hits = db.forward("US", "IL", "Springfield", "Main St")+ assertEquals(2, hits.size, "forward Main St count")+ assertTrue(hits.all { it.street == "Main St" && it.city == "Springfield" })+ assertTrue(hits.map { it.house }.toSet() == setOf("10", "12"), "forward houses")++ // Unknown street -> empty.+ assertTrue(db.forward("US", "IL", "Springfield", "No Such St").isEmpty())+ }+ } finally {+ dir.deleteRecursively()+ } } private fun parseFeature(json: Json, line: String): GeoAddress? {