-
Notifications
You must be signed in to change notification settings - Fork 348
/
pgisgeographyinstances.scala
38 lines (30 loc) · 1.7 KB
/
pgisgeographyinstances.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright (c) 2013-2020 Rob Norris and Contributors
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package doobie.postgres
import doobie._
import doobie.util.invariant._
import org.postgis._
import scala.reflect.ClassTag
import org.tpolecat.typename._
// Implicit conversions for postgis geography types
trait PgisGeographyInstances {
// PostGIS outer types
implicit val PGgeographyType: Meta[PGgeography] = Meta.Advanced.other[PGgeography]("geography")
// Constructor for geometry types via the `Geometry` member of PGgeography
@SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf", "org.wartremover.warts.Throw"))
private def geometryType[A >: Null <: Geometry: TypeName](implicit A: ClassTag[A]): Meta[A] =
PGgeographyType.timap[A](g =>
try A.runtimeClass.cast(g.getGeometry).asInstanceOf[A]
catch {
case _: ClassCastException => throw InvalidObjectMapping(A.runtimeClass, g.getGeometry.getClass)
})(new PGgeography(_))
// PostGIS Geometry Types
implicit val MultiLineStringType: Meta[MultiLineString] = geometryType[MultiLineString]
implicit val MultiPolygonType: Meta[MultiPolygon] = geometryType[MultiPolygon]
implicit val PointComposedGeomType: Meta[PointComposedGeom] = geometryType[PointComposedGeom]
implicit val LineStringType: Meta[LineString] = geometryType[LineString]
implicit val MultiPointType: Meta[MultiPoint] = geometryType[MultiPoint]
implicit val PolygonType: Meta[Polygon] = geometryType[Polygon]
implicit val PointType: Meta[Point] = geometryType[Point]
}