diff --git a/addons/jts/src/test/scala/com/github/tminglei/slickpg/PgPostGISSupportSuite.scala b/addons/jts/src/test/scala/com/github/tminglei/slickpg/PgPostGISSupportSuite.scala index e39acef5..afc892ea 100644 --- a/addons/jts/src/test/scala/com/github/tminglei/slickpg/PgPostGISSupportSuite.scala +++ b/addons/jts/src/test/scala/com/github/tminglei/slickpg/PgPostGISSupportSuite.scala @@ -63,6 +63,8 @@ class PgPostGISSupportSuite extends FunSuite { val line = wktReader.read("LINESTRING(-10 0, 50 50, -100 -100, 10 -70, -10 0)") val bean = GeometryBean(101L, point) + val line1 = wktReader.read("LINESTRING(-120.2 38.5,-120.95 40.7,-126.453 43.252)") + line1.setSRID(4326) Await.result(db.run( DBIO.seq( @@ -105,6 +107,10 @@ class PgPostGISSupportSuite extends FunSuite { // r => assert(bean === r) // ) // )), Duration.Inf) + // line_from_encoded_polyline + GeomTests.filter(_.id === bean.id.bind).map(r => lineFromEncodedPolyline("_p~iF~ps|U_ulLnnqC_mqNvxq`@".bind)).result.head.map( + r => assert(line1 === r) + ), // make_box GeomTests.filter(_.geom @&& makeBox(point1.bind, point2.bind)).result.head.map( r => assert(bean === r) @@ -382,15 +388,24 @@ class PgPostGISSupportSuite extends FunSuite { val point = wktReader.read(POINT) val POINT_LatLon = """2°19'29.928"S 3°14'3.243"W""" + val LINE_STRING = "LINESTRING(-120.2 38.5,-120.95 40.7,-126.453 43.252)" + val lineString = wktReader.read(LINE_STRING) + lineString.setSRID(4326) + val bean = GeometryBean(141L, polygon) val bean1 = GeometryBean(142L, point) + val bean2 = GeometryBean(143L, lineString) Await.result(db.run( DBIO.seq( (GeomTests.schema ++ PointTests.schema) create, - GeomTests forceInsertAll List(bean, bean1) + GeomTests forceInsertAll List(bean, bean1, bean2) ).andThen( DBIO.seq( + // as_encoded_polyline + GeomTests.filter(_.id === bean2.id.bind).map(_.geom.asEncodedPolyline()).result.head.map( + r => assert("_p~iF~ps|U_ulLnnqC_mqNvxq`@" === r) + ), // as_text GeomTests.filter(_.id === bean.id.bind).map(_.geom.asText).result.head.map( r => assert(POLYGON === r) diff --git a/core/src/main/scala/com/github/tminglei/slickpg/geom/PgPostGISExtensions.scala b/core/src/main/scala/com/github/tminglei/slickpg/geom/PgPostGISExtensions.scala index 0ca11427..9da07f3b 100644 --- a/core/src/main/scala/com/github/tminglei/slickpg/geom/PgPostGISExtensions.scala +++ b/core/src/main/scala/com/github/tminglei/slickpg/geom/PgPostGISExtensions.scala @@ -46,6 +46,12 @@ trait PgPostGISExtensions extends JdbcTypesComponent { driver: PostgresProfile = implicit tm: JdbcType[GEOMETRY], om: OptionMapperDSL.arg[String, P]#to[GEOMETRY, R]) = { om.column(GeomLibrary.GeomFromGeoJSON, json.toNode) } + def lineFromEncodedPolyline[P, R](encodedPolyline: Rep[P], precision: Option[Int] = None)( + implicit tm: JdbcType[GEOMETRY], om: OptionMapperDSL.arg[String, P]#to[GEOMETRY, R]) = + precision match { + case Some(p) => om.column(GeomLibrary.LineFromEncodedPolyline, encodedPolyline.toNode, LiteralNode(p)) + case None => om.column(GeomLibrary.LineFromEncodedPolyline, encodedPolyline.toNode) + } def makeBox[G1 <: GEOMETRY, P1, G2 <: GEOMETRY, P2, R](lowLeftPoint: Rep[P1], upRightPoint: Rep[P2])( implicit tm: JdbcType[GEOMETRY], om: OptionMapperDSL.arg[G1, P1]#arg[G2, P2]#to[GEOMETRY, R]) = { om.column(GeomLibrary.MakeBox, lowLeftPoint.toNode, upRightPoint.toNode) @@ -107,6 +113,7 @@ trait PgPostGISExtensions extends JdbcTypesComponent { driver: PostgresProfile = val GeomFromGML = new SqlFunction("ST_GeomFromGML") val GeomFromKML = new SqlFunction("ST_GeomFromKML") val GeomFromGeoJSON = new SqlFunction("ST_GeomFromGeoJSON") + val LineFromEncodedPolyline = new SqlFunction("ST_LineFromEncodedPolyline") val MakeBox = new SqlFunction("ST_MakeBox2D") val MakeBox3D = new SqlFunction("ST_3DMakeBox") val MakeEnvelope = new SqlFunction("ST_MakeEnvelope") @@ -145,6 +152,7 @@ trait PgPostGISExtensions extends JdbcTypesComponent { driver: PostgresProfile = /** Geometry Outputs */ val AsBinary = new SqlFunction("ST_AsBinary") + val AsEncodedPolyline = new SqlFunction("ST_AsEncodedPolyline") val AsText = new SqlFunction("ST_AsText") val AsLatLonText = new SqlFunction("ST_AsLatLonText") val AsEWKB = new SqlFunction("ST_AsEWKB") @@ -358,6 +366,11 @@ trait PgPostGISExtensions extends JdbcTypesComponent { driver: PostgresProfile = case Some(endian) => om.column(GeomLibrary.AsBinary, n, LiteralNode(endian)) case None => om.column(GeomLibrary.AsBinary, n) } + def asEncodedPolyline[R](precision: Option[Int] = None)(implicit om: o#to[String, R]) = + precision match { + case Some(p) => om.column(GeomLibrary.AsEncodedPolyline, n, LiteralNode(p)) + case None => om.column(GeomLibrary.AsEncodedPolyline, n) + } def asText[R](implicit om: o#to[String, R]) = { om.column(GeomLibrary.AsText, n) } diff --git a/core/src/main/scala/com/github/tminglei/slickpg/geom/README.md b/core/src/main/scala/com/github/tminglei/slickpg/geom/README.md index ae6e082d..c1824970 100644 --- a/core/src/main/scala/com/github/tminglei/slickpg/geom/README.md +++ b/core/src/main/scala/com/github/tminglei/slickpg/geom/README.md @@ -11,6 +11,7 @@ Supported Geometry Oper/Functions | geomFromGML | ST_GeomFromGML | create a geometry from input GML | ST_GeomFromGML(gml[, srid]) | | geomFromKML | ST_GeomFromKML | create a geometry from input KML | ST_GeomFromKML(kml) | | geomFromGeoJSON | ST_GeomFromGeoJSON | create a geometry from input geojson | ST_GeomFromGeoJSON( json) | +| lineFromEncodedPolyline | ST_LineFromEncodedPolyline | create a LineString from an encoded polyline | ST_LineFromEncodedPolyline(text polyline, integer precision=5) | | makeBox | ST_MakeBox2D | Creates a BOX2D defined by the given point geometries | ST_MakeBox2D( pointLowLeft, pointUpRight) | | makeBox3d | ST_3DMakeBox | Creates a BOX3D defined by the given 3d point geometries | ST_3DMakeBox( point3dLowLeft, point3dUpRight) | | makeEnvelope | ST_MakeEnvelope | Creates a rectangular Polygon formed from the given minimums and maximums | ST_MakeEnvelope(xmin, ymin, xmax, ymax, srid=unknown) | @@ -69,7 +70,8 @@ Supported Geometry Oper/Functions #### Geometry Outputs | Slick Oper/Function | PostGIS Oper/Function | Description | Example | | ------------------- | --------------------- | ------------------------------------------------------ | ----------------------------- | -| asBinary | ST_AsBinary | Well-Known Binary of the geometry without SRID | ST_AsBinary(geom[, NDRorXDR] | +| asBinary | ST_AsBinary | Well-Known Binary of the geometry without SRID | ST_AsBinary(geom[, NDRorXDR]) | +| asEncodedPolyline | ST_AsEncodedPolyline | Encoded Polyline from a LineString geometry | ST_AsEncodedPolyline(geom, integer precision=5) | | asText | ST_AsText | Well-Known Text of the geometry without SRID | ST_AsText(geom) | | asLatLonText | ST_AsLatLonText | Degrees, Minutes, Seconds representation of the point | ST_AsLatLonText(geom[, format]) | | asEWKB | ST_AsEWKB | Well-Known Binary of the geometry with SRID | ST_AsEWKB(geom[, NDRorXDR]) |