Skip to content

Commit

Permalink
Better JSON representation of GeoJSON and updated Fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Nov 28, 2023
1 parent cbdb29e commit 181a7bd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
41 changes: 28 additions & 13 deletions core/src/main/scala/com/outr/arango/geo/GeoJSON.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ object GeoJSON {
}
private[geo] def pointsFromCoords(json: Json): List[GeoPoint] = json.asArr.value.toList.map(pointFromCoords)
private[geo] def multiPointsFromCoords(json: Json): List[List[GeoPoint]] = json.asArr.value.toList.map(pointsFromCoords)
private[geo] def createRW[T <: GeoJSON](toCoordinates: T => Json, fromCoordinates: Json => T): RW[T] = RW.from(
r = t => obj(
"coordinates" -> toCoordinates(t)
),
w = j => fromCoordinates(j("coordinates")),
d = DefType.Json
)
private[geo] def createRW[T <: GeoJSON](toCoordinates: T => Json,
fromCoordinates: Json => T,
dimensions: Int): RW[T] = {
def d(dim: Int): DefType = if (dim == 0) {
DefType.Dec
} else {
DefType.Arr(d(dim - 1))
}
RW.from(
r = t => obj(
"coordinates" -> toCoordinates(t)
),
w = j => fromCoordinates(j("coordinates")),
d = DefType.Obj(None, "coordinates" -> d(dimensions))
)
}

implicit lazy val rw: RW[GeoJSON] = RW.poly[GeoJSON](getType = _.getClass.getSimpleName.replace("$", ""))(
"GeoPoint" -> GeoPoint.rw,
Expand All @@ -44,7 +53,8 @@ case class GeoPoint(latitude: Double, longitude: Double) extends GeoJSON
object GeoPoint {
implicit val rw: RW[GeoPoint] = createRW[GeoPoint](
point => pointArray(point),
pointFromCoords
pointFromCoords,
dimensions = 1
).withPostRead(addType("GeoPoint"))
}

Expand All @@ -53,7 +63,8 @@ case class GeoMultiPoint(points: List[GeoPoint]) extends GeoJSON
object GeoMultiPoint {
implicit val rw: RW[GeoMultiPoint] = createRW[GeoMultiPoint](
mp => mp.points.map(pointArray).json,
json => GeoMultiPoint(pointsFromCoords(json))
json => GeoMultiPoint(pointsFromCoords(json)),
dimensions = 2
).withPostRead(addType("GeoMultiPoint"))
}

Expand All @@ -62,7 +73,8 @@ case class GeoLineString(points: List[GeoPoint]) extends GeoJSON
object GeoLineString {
implicit val rw: RW[GeoLineString] = createRW[GeoLineString](
ls => ls.points.map(pointArray).json,
json => GeoLineString(pointsFromCoords(json))
json => GeoLineString(pointsFromCoords(json)),
dimensions = 2
).withPostRead(addType("GeoLineString"))
}

Expand All @@ -71,7 +83,8 @@ case class GeoMultiLineString(lines: List[List[GeoPoint]]) extends GeoJSON
object GeoMultiLineString {
implicit val rw: RW[GeoMultiLineString] = createRW[GeoMultiLineString](
mls => mls.lines.map(_.map(pointArray)).json,
json => GeoMultiLineString(multiPointsFromCoords(json))
json => GeoMultiLineString(multiPointsFromCoords(json)),
dimensions = 3
).withPostRead(addType("GeoMultiLineString"))
}

Expand All @@ -80,7 +93,8 @@ case class GeoPolygon(points: List[GeoPoint]) extends GeoJSON
object GeoPolygon {
implicit val rw: RW[GeoPolygon] = createRW[GeoPolygon](
p => p.points.map(pointArray).json,
json => GeoPolygon(pointsFromCoords(json))
json => GeoPolygon(pointsFromCoords(json)),
dimensions = 3
).withPostRead(addType("GeoPolygon"))
}

Expand All @@ -89,6 +103,7 @@ case class GeoMultiPolygon(polygons: List[List[GeoPoint]]) extends GeoJSON
object GeoMultiPolygon {
implicit val rw: RW[GeoMultiPolygon] = createRW[GeoMultiPolygon](
mp => mp.polygons.map(_.map(pointArray)).json,
json => GeoMultiPolygon(multiPointsFromCoords(json))
json => GeoMultiPolygon(multiPointsFromCoords(json)),
dimensions = 4
).withPostRead(addType("GeoMultiPolygon"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ trait JsonDocumentModel extends DocumentModel[JsonDocument] {
r = jd => jd.json.merge(obj("_id" -> jd._id)),
w = j => JsonDocument(j, j("_id").as[Id[JsonDocument]]),
d = DefType.Obj(
"json" -> DefType.Obj(),
None,
"json" -> DefType.Obj(None),
"_id" -> Id.rw.definition
)
)
Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sbt.*

object Dependencies {
object version {
val fabric: String = "1.12.6"
val fabric: String = "1.12.8"

val profig: String = "3.4.11"

Expand Down

0 comments on commit 181a7bd

Please sign in to comment.