Skip to content

Commit

Permalink
fixed issue 434: some two digit BNG grid references interpreted as qu…
Browse files Browse the repository at this point in the history
…adrants
  • Loading branch information
sllynn committed Sep 25, 2024
1 parent 0a9d8bd commit 5cea661
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) // (##)(#...#)(#...#)(#)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 5cea661

Please sign in to comment.