Skip to content

Commit

Permalink
Refactored GeoJSON to better support underlying type usage
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Nov 11, 2023
1 parent 28a5104 commit cbdb29e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ val scala3 = "3.3.1"

name := "scarango"
ThisBuild / organization := "com.outr"
ThisBuild / version := "3.17.1-SNAPSHOT"
ThisBuild / version := "3.18.0-SNAPSHOT"
ThisBuild / scalaVersion := scala213
ThisBuild / crossScalaVersions := List(scala3, scala213)
ThisBuild / scalacOptions ++= Seq("-unchecked", "-deprecation")
Expand Down
103 changes: 65 additions & 38 deletions core/src/main/scala/com/outr/arango/geo/GeoJSON.scala
Original file line number Diff line number Diff line change
@@ -1,67 +1,94 @@
package com.outr.arango.geo

import com.outr.arango.geo.GeoJSON._
import fabric._
import fabric.define.DefType
import fabric.rw._

sealed trait GeoJSON

object GeoJSON {
private def pointArray(p: Point): Json = arr(p.longitude, p.latitude)
private def pointFromCoords(json: Json): Point = {
private[geo] def addType[T <: GeoJSON](name: String)(t: T, json: Json): Json = json.merge(obj(
"type" -> name
))
private[geo] def pointArray(p: GeoPoint): Json = arr(p.longitude, p.latitude)
private[geo] def pointFromCoords(json: Json): GeoPoint = {
val arr = json.asArr
Point(
GeoPoint(
latitude = arr.value(1).asDouble,
longitude = arr.value(0).asDouble
)
}
private def pointsFromCoords(json: Json): List[Point] = json.asArr.value.toList.map(pointFromCoords)
private def multiPointsFromCoords(json: Json): List[List[Point]] = json.asArr.value.toList.map(pointsFromCoords)
private def createRW[T <: GeoJSON](toCoordinates: T => Json, fromCoordinates: Json => T): RW[T] = RW.from(
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 lazy val pointRW: RW[Point] = createRW[Point](

implicit lazy val rw: RW[GeoJSON] = RW.poly[GeoJSON](getType = _.getClass.getSimpleName.replace("$", ""))(
"GeoPoint" -> GeoPoint.rw,
"GeoMultiPoint" -> GeoMultiPoint.rw,
"GeoLineString" -> GeoLineString.rw,
"GeoMultiLineString" -> GeoMultiLineString.rw,
"GeoPolygon" -> GeoPolygon.rw,
"GeoMultiPolygon" -> GeoMultiPolygon.rw
)
}

case class GeoPoint(latitude: Double, longitude: Double) extends GeoJSON

object GeoPoint {
implicit val rw: RW[GeoPoint] = createRW[GeoPoint](
point => pointArray(point),
pointFromCoords
)
private lazy val multiPointRW: RW[MultiPoint] = createRW[MultiPoint](
).withPostRead(addType("GeoPoint"))
}

case class GeoMultiPoint(points: List[GeoPoint]) extends GeoJSON

object GeoMultiPoint {
implicit val rw: RW[GeoMultiPoint] = createRW[GeoMultiPoint](
mp => mp.points.map(pointArray).json,
json => MultiPoint(pointsFromCoords(json))
)
private lazy val lineStringRW: RW[LineString] = createRW[LineString](
json => GeoMultiPoint(pointsFromCoords(json))
).withPostRead(addType("GeoMultiPoint"))
}

case class GeoLineString(points: List[GeoPoint]) extends GeoJSON

object GeoLineString {
implicit val rw: RW[GeoLineString] = createRW[GeoLineString](
ls => ls.points.map(pointArray).json,
json => LineString(pointsFromCoords(json))
)
private lazy val multiLineStringRW: RW[MultiLineString] = createRW[MultiLineString](
json => GeoLineString(pointsFromCoords(json))
).withPostRead(addType("GeoLineString"))
}

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 => MultiLineString(multiPointsFromCoords(json))
)
private lazy val polygonRW: RW[Polygon] = createRW[Polygon](
json => GeoMultiLineString(multiPointsFromCoords(json))
).withPostRead(addType("GeoMultiLineString"))
}

case class GeoPolygon(points: List[GeoPoint]) extends GeoJSON

object GeoPolygon {
implicit val rw: RW[GeoPolygon] = createRW[GeoPolygon](
p => p.points.map(pointArray).json,
json => Polygon(pointsFromCoords(json))
)
private lazy val multiPolygonRW: RW[MultiPolygon] = createRW[MultiPolygon](
mp => mp.polygons.map(_.map(pointArray)).json,
json => MultiPolygon(multiPointsFromCoords(json))
)
json => GeoPolygon(pointsFromCoords(json))
).withPostRead(addType("GeoPolygon"))
}

implicit lazy val rw: RW[GeoJSON] = RW.poly[GeoJSON](getType = _.getClass.getSimpleName.replace("$", ""))(
"Point" -> pointRW,
"MultiPoint" -> multiPointRW,
"LineString" -> lineStringRW,
"MultiLineString" -> multiLineStringRW,
"Polygon" -> polygonRW,
"MultiPolygon" -> multiPolygonRW
)
case class GeoMultiPolygon(polygons: List[List[GeoPoint]]) extends GeoJSON

case class Point(latitude: Double, longitude: Double) extends GeoJSON
case class MultiPoint(points: List[Point]) extends GeoJSON
case class LineString(points: List[Point]) extends GeoJSON
case class MultiLineString(lines: List[List[Point]]) extends GeoJSON
case class Polygon(points: List[Point]) extends GeoJSON
case class MultiPolygon(polygons: List[List[Point]]) extends GeoJSON
object GeoMultiPolygon {
implicit val rw: RW[GeoMultiPolygon] = createRW[GeoMultiPolygon](
mp => mp.polygons.map(_.map(pointArray)).json,
json => GeoMultiPolygon(multiPointsFromCoords(json))
).withPostRead(addType("GeoMultiPolygon"))
}

0 comments on commit cbdb29e

Please sign in to comment.