-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored GeoJSON to better support underlying type usage
- Loading branch information
1 parent
28a5104
commit cbdb29e
Showing
2 changed files
with
66 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |