From 397046bfa7b7d35bb2158466da81d2e8310c3c7a Mon Sep 17 00:00:00 2001 From: Yannick Heiber Date: Wed, 7 Aug 2019 21:34:52 +0200 Subject: [PATCH] Fix polyline encoding for cases with very small negative delta --- .../main/scala/com/free2move/geoscala/polyline.scala | 2 +- .../scala/com/free2move/geoscala/PolylineTest.scala | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/polyline/src/main/scala/com/free2move/geoscala/polyline.scala b/polyline/src/main/scala/com/free2move/geoscala/polyline.scala index 4b81818..0c69dfd 100644 --- a/polyline/src/main/scala/com/free2move/geoscala/polyline.scala +++ b/polyline/src/main/scala/com/free2move/geoscala/polyline.scala @@ -48,7 +48,7 @@ object polyline { private def num2Poly(num: Double): String = { val scaled = Math.round(num * 1e5).toInt - val bits = if (num < 0.0) ~(scaled << 1) else scaled << 1 + val bits = if (scaled < 0.0) ~(scaled << 1) else scaled << 1 chunkMasks .map { case (mask, idx) => diff --git a/polyline/src/test/scala/com/free2move/geoscala/PolylineTest.scala b/polyline/src/test/scala/com/free2move/geoscala/PolylineTest.scala index 20511e4..728debd 100644 --- a/polyline/src/test/scala/com/free2move/geoscala/PolylineTest.scala +++ b/polyline/src/test/scala/com/free2move/geoscala/PolylineTest.scala @@ -16,6 +16,7 @@ package com.free2move.geoscala import org.scalacheck._ +import org.scalactic.{Equality, TolerantNumerics} import org.scalatest._ import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks @@ -41,6 +42,17 @@ class PolylineTest extends FlatSpec with Matchers with OptionValues with TryValu polyline.encode(decoded) shouldBe poly } + it should "handle cases where coordinate delta is slightly negative (< 10^-5)" in { + val ls = LineString(List(Coordinate(13.39336395263672,52.52311483252328), Coordinate(13.39332,52.52311))) + val encoded = polyline.encode(ls) + val decoded = polyline.decode(encoded).success.value + implicit val doubleEquality: Equality[Double] = TolerantNumerics.tolerantDoubleEquality(0.00001) // 10^-5 + decoded.coordinates.head.latitude should ===(ls.coordinates.head.latitude) + decoded.coordinates.head.longitude should ===(ls.coordinates.head.longitude) + decoded.coordinates.tail.head.latitude should ===(ls.coordinates.tail.head.latitude) + decoded.coordinates.tail.head.longitude should ===(ls.coordinates.tail.head.longitude) + } + implicit val coordGen: Gen[Coordinate] = for { lat <- Gen.chooseNum[Double](-90.0, +90.0) lng <- Gen.chooseNum[Double](-180.0, +180.0)