Skip to content

Commit

Permalink
Merge pull request #2 from Free2MoveApp/fix-polyline-on-small-negativ…
Browse files Browse the repository at this point in the history
…e-delta

Fix polyline encoding for cases with very small negative delta
  • Loading branch information
ybasket authored Aug 8, 2019
2 parents ea9f6eb + 397046b commit 62587f4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
12 changes: 12 additions & 0 deletions polyline/src/test/scala/com/free2move/geoscala/PolylineTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.free2move.geoscala

import org.scalacheck._
import org.scalactic.{Equality, TolerantNumerics}
import org.scalatest._
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks

Expand All @@ -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)
Expand Down

0 comments on commit 62587f4

Please sign in to comment.