Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions jvm/src/test/scala/net/teralytics/geohex/ZoneSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.teralytics.geohex

import com.vividsolutions.jts.geom.{GeometryFactory, PrecisionModel}
import com.vividsolutions.jts.io.WKTReader
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}

class ZoneSpec extends FlatSpec with PropertyChecks with Matchers {

it should "create a WellKnownText in which the starting point is equal to the ending point" in {
val zones = GeoHex.getZonesWithin(((51d, 21d),(56d, 22d)), 2)
val defaultSRID = 4326
val factory = new GeometryFactory(new PrecisionModel(1e7), defaultSRID)
val zoneCoords = new WKTReader(factory).read(zones.head.toWellKnownText).getCoordinates
zoneCoords.head should equal(zoneCoords.last)
}
}
12 changes: 12 additions & 0 deletions jvm/src/test/scala/net/teralytics/terahex/ZoneSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import org.scalatest.{ Matchers, FlatSpec }
import org.scalatest.prop.PropertyChecks
import scala.math._

import com.vividsolutions.jts.geom.{GeometryFactory, PrecisionModel}
import com.vividsolutions.jts.io.{WKTReader}

class ZoneSpec extends FlatSpec with PropertyChecks with Matchers {

import Generators._
Expand Down Expand Up @@ -46,4 +49,13 @@ class ZoneSpec extends FlatSpec with PropertyChecks with Matchers {
.map(_.code)
codes should contain theSameElementsInOrderAs codes.distinct
}

it should "create a WellKnownText in which the starting point is equal to the ending point" in {
implicit val grid = TeraHex.grid
val defaultSRID = 4326
val zones = Zone.zonesWithin(LatLon(Lon(51), Lat(21)) -> LatLon(Lon(56), Lat(22)), 5)
val factory = new GeometryFactory(new PrecisionModel(1e7), defaultSRID)
val zoneCoords = new WKTReader(factory).read(zones.head.toWellKnownText).getCoordinates
zoneCoords.head should equal(zoneCoords.last)
}
}
9 changes: 6 additions & 3 deletions shared/src/main/scala/net/teralytics/geohex/Zone.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ case class Zone(code: String, lat: Double = 0, lon: Double = 0, x: Long = 0, y:

val size: Double = circumradiusInMetersAtEquator(level)

def toWellKnownText: String = getHexCoords
.map(loc => s"${loc.lon} ${loc.lat}")
.mkString("POLYGON ((", ", ", "))")
def toWellKnownText: String = {
val polygons = getHexCoords
(polygons :+ polygons.head)
.map(loc => s"${loc.lon} ${loc.lat}")
.mkString("POLYGON ((", ", ", "))")
}

def getHexCoords: Array[Loc] = {
val xy = loc2xy(lon, lat)
Expand Down