From 5cea66126f1f558041bd00de50bff5e1ed0252b9 Mon Sep 17 00:00:00 2001 From: sllynn Date: Wed, 25 Sep 2024 15:50:00 +0200 Subject: [PATCH] fixed issue 434: some two digit BNG grid references interpreted as quadrants --- .../databricks/labs/mosaic/core/index/BNGIndexSystem.scala | 4 ++-- .../labs/mosaic/core/index/TestBNGIndexSystem.scala | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/scala/com/databricks/labs/mosaic/core/index/BNGIndexSystem.scala b/src/main/scala/com/databricks/labs/mosaic/core/index/BNGIndexSystem.scala index 3b0d1dbdc..dffca7455 100644 --- a/src/main/scala/com/databricks/labs/mosaic/core/index/BNGIndexSystem.scala +++ b/src/main/scala/com/databricks/labs/mosaic/core/index/BNGIndexSystem.scala @@ -392,7 +392,7 @@ object BNGIndexSystem extends IndexSystem(StringType) with Serializable { encode(eLetter, 0, 0, 0, 0, 1, -1) } else { val suffix = index.slice(index.length - 2, index.length) - val quadrant: Int = if (quadrants.contains(suffix)) quadrants.indexOf(suffix) else 0 + val quadrant: Int = if (quadrants.contains(suffix) && index.length > 2) quadrants.indexOf(suffix) else 0 val binDigits = if (quadrant > 0) index.drop(2).dropRight(2) else index.drop(2) if (binDigits.isEmpty) { encode(eLetter, nLetter, 0, 0, quadrant, 1, -2) @@ -537,7 +537,7 @@ object BNGIndexSystem extends IndexSystem(StringType) with Serializable { math.abs((x1 - x2) / edgeSize) + math.abs((y1 - y2) / edgeSize) } - private def encode(eLetter: Int, nLetter: Int, eBin: Int, nBin: Int, quadrant: Int, nPositions: Int, resolution: Int): Long = { + def encode(eLetter: Int, nLetter: Int, eBin: Int, nBin: Int, quadrant: Int, nPositions: Int, resolution: Int): Long = { val idPlaceholder = math.pow(10, 5 + 2 * nPositions - 2) // 1(##)(##)(#...#)(#...#)(#) val eLetterShift = math.pow(10, 3 + 2 * nPositions - 2) // (##)(##)(#...#)(#...#)(#) val nLetterShift = math.pow(10, 1 + 2 * nPositions - 2) // (##)(#...#)(#...#)(#) diff --git a/src/test/scala/com/databricks/labs/mosaic/core/index/TestBNGIndexSystem.scala b/src/test/scala/com/databricks/labs/mosaic/core/index/TestBNGIndexSystem.scala index f45a8af7d..30d0f53b5 100644 --- a/src/test/scala/com/databricks/labs/mosaic/core/index/TestBNGIndexSystem.scala +++ b/src/test/scala/com/databricks/labs/mosaic/core/index/TestBNGIndexSystem.scala @@ -81,6 +81,9 @@ class TestBNGIndexSystem extends AnyFunSuite { BNGIndexSystem.parse("TQ") shouldBe 105010 BNGIndexSystem.format(105010) shouldBe "TQ" + BNGIndexSystem.parse("NW") shouldBe BNGIndexSystem.encode(1, 5, 0, 0, 0, 1, -2) + BNGIndexSystem.format(105010) shouldBe "TQ" + BNGIndexSystem.parse("TQNW") shouldBe 105012 BNGIndexSystem.format(105012) shouldBe "TQNW"