Skip to content

Commit

Permalink
CASL-390 fix geo to jts conversion (#356)
Browse files Browse the repository at this point in the history
* CASL-390 fix geo to jts conversion

* CASL-390 not null type
  • Loading branch information
cyberhead-pl authored Oct 2, 2024
1 parent ef7e77b commit 3fc73d0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
21 changes: 17 additions & 4 deletions here-naksha-lib-geo/src/commonMain/kotlin/naksha/geo/SpGeometry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

package naksha.geo

import naksha.base.*
import naksha.base.AnyObject
import naksha.base.NotNullProperty
import naksha.base.PlatformList
import kotlin.js.JsExport
import kotlin.js.JsName

Expand All @@ -15,7 +17,18 @@ open class SpGeometry() : AnyObject() {
}

companion object GeometryProxyCompanion {
private val TYPE = NullableProperty<SpGeometry, String>(String::class)
private val TYPE = NotNullProperty<SpGeometry, String>(String::class) { self, name ->
when (self) {
is SpPoint -> SpType.Point
is SpMultiPoint -> SpType.MultiPoint
is SpLineString -> SpType.LineString
is SpMultiLineString -> SpType.MultiLineString
is SpPolygon -> SpType.Polygon
is SpMultiPolygon -> SpType.MultiPolygon
is SpGeometryCollection -> SpType.GeometryCollection
else -> throw IllegalArgumentException("Unknown proxy type ${self::class.simpleName}")
}.toString()
}
}

/**
Expand Down Expand Up @@ -66,7 +79,7 @@ open class SpGeometry() : AnyObject() {
* @param coordinates the coordinates to set.
*/
fun setCoordinates(coordinates: ICoordinates) {
val type = when(coordinates) {
val type = when (coordinates) {
is PointCoord -> SpType.Point
is MultiPointCoord -> SpType.MultiPoint
is LineStringCoord -> SpType.LineString
Expand All @@ -85,7 +98,7 @@ open class SpGeometry() : AnyObject() {
* @return the centroid (center of mass) of the geometry.
*/
fun calculateCentroid(): SpPoint // TODO: Improve this implementation!
= SpBoundingBox(getCoordinates()).center()
= SpBoundingBox(getCoordinates()).center()

fun asPoint(): SpPoint = proxy(SpPoint::class)
fun asMultiPoint(): SpMultiPoint = proxy(SpMultiPoint::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@ object ProxyGeoUtil {
*/
@JvmStatic
fun toJtsGeometry(geometry: SpGeometry): Geometry {
return when (geometry) {
is SpPoint -> toJtsPoint(geometry)
is SpMultiPoint -> toJtsMultiPoint(geometry)
is SpLineString -> toJtsLineString(geometry)
is SpMultiLineString -> toJtsMultiLineString(geometry)
is SpPolygon -> toJtsPolygon(geometry)
is SpMultiPolygon -> toJtsMultiPolygon(geometry)
is SpGeometryCollection -> toJtsGeometryCollection(geometry)
return when (geometry.type) {
SpType.Point.toString() -> toJtsPoint(geometry.asPoint())
SpType.MultiPoint.toString() -> toJtsMultiPoint(geometry.asMultiPoint())
SpType.LineString.toString() -> toJtsLineString(geometry.asLineString())
SpType.MultiLineString.toString() -> toJtsMultiLineString(geometry.asMultiLineString())
SpType.Polygon.toString() -> toJtsPolygon(geometry.asPolygon())
SpType.MultiPolygon.toString() -> toJtsMultiPolygon(geometry.asMultiPolygon())
SpType.GeometryCollection.toString() -> toJtsGeometryCollection(geometry.asGeometryCollection())
else -> throw IllegalArgumentException("Unknown proxy type ${geometry::class.simpleName}")
}
}
Expand Down
30 changes: 23 additions & 7 deletions here-naksha-lib-geo/src/jvmTest/kotlin/naksha/geo/JtsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class JtsTest {
}
""".trimIndent()

val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpPoint::class)
val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpGeometry::class)
val jtsFromJson = jtsJsonReader.read(json)

// when
Expand All @@ -83,7 +83,7 @@ class JtsTest {
}
""".trimIndent()

val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpMultiPoint::class)
val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpGeometry::class)
val jtsFromJson = jtsJsonReader.read(json)

// when
Expand All @@ -109,7 +109,7 @@ class JtsTest {
}
""".trimIndent()

val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpLineString::class)
val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpGeometry::class)
val jtsFromJson = jtsJsonReader.read(json)

// when
Expand Down Expand Up @@ -141,7 +141,7 @@ class JtsTest {
}
""".trimIndent()

val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpMultiLineString::class)
val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpGeometry::class)
val jtsFromJson = jtsJsonReader.read(json)

// when
Expand Down Expand Up @@ -183,7 +183,7 @@ class JtsTest {
}
""".trimIndent()

val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpPolygon::class)
val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpGeometry::class)
val jtsFromJson = jtsJsonReader.read(json)

// when
Expand Down Expand Up @@ -243,7 +243,7 @@ class JtsTest {
}
""".trimIndent()

val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpPolygon::class)
val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpGeometry::class)
val jtsFromJson = jtsJsonReader.read(json)

// when
Expand Down Expand Up @@ -307,7 +307,7 @@ class JtsTest {
}
""".trimIndent()

val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpMultiPolygon::class)
val proxyGeometryFromJson = (Platform.fromJSON(json) as JvmMap).proxy(SpGeometry::class)
val jtsFromJson = jtsJsonReader.read(json)

// when
Expand All @@ -319,4 +319,20 @@ class JtsTest {
assertEquals(jtsFromJson, jtsFromProxy)
assertEquals(Platform.toJSON(proxyFromJts), Platform.toJSON(proxyGeometryFromJson))
}

@Test
fun testUnknownType(){
// given
val json = """{"coordinates":[1.0, 2.0]}"""

// when
val proxyPoint = (Platform.fromJSON(json) as JvmMap).proxy(SpPoint::class)
val jtsFromProxy = ProxyGeoUtil.toJtsGeometry(proxyPoint)

// then
assertEquals(SpType.Point.toString(), proxyPoint.type)
assertEquals(Point.TYPENAME_POINT, jtsFromProxy.geometryType)
assertEquals(1.0, jtsFromProxy.coordinates.get(0).x)
assertEquals(2.0, jtsFromProxy.coordinates.get(0).y)
}
}

0 comments on commit 3fc73d0

Please sign in to comment.