Skip to content

Commit

Permalink
CASL-390 not null type
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhead-pl committed Oct 2, 2024
1 parent c8b4195 commit 40b0cd0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 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 @@ -213,13 +213,13 @@ object ProxyGeoUtil {
@JvmStatic
fun toJtsGeometry(geometry: SpGeometry): Geometry {
return when (geometry.type) {
Point.TYPENAME_POINT -> toJtsPoint(geometry.asPoint())
Point.TYPENAME_MULTIPOINT -> toJtsMultiPoint(geometry.asMultiPoint())
Point.TYPENAME_LINESTRING -> toJtsLineString(geometry.asLineString())
Point.TYPENAME_MULTILINESTRING -> toJtsMultiLineString(geometry.asMultiLineString())
Point.TYPENAME_POLYGON -> toJtsPolygon(geometry.asPolygon())
Point.TYPENAME_MULTIPOLYGON -> toJtsMultiPolygon(geometry.asMultiPolygon())
Point.TYPENAME_GEOMETRYCOLLECTION -> toJtsGeometryCollection(geometry.asGeometryCollection())
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
16 changes: 16 additions & 0 deletions here-naksha-lib-geo/src/jvmTest/kotlin/naksha/geo/JtsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 40b0cd0

Please sign in to comment.