-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathGeoJson.scala
450 lines (386 loc) · 15.7 KB
/
GeoJson.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package au.id.jazzy.play.geojson
import scala.collection.immutable.Seq
import play.api.libs.json._
import play.api.libs.functional._
import play.api.libs.functional.syntax._
import scala.language.{higherKinds, implicitConversions}
/**
* A GeoJSON object.
*
* @tparam C The object used to model the CRS that this GeoJSON object uses.
*/
sealed trait GeoJson[C] {
val bbox: Option[(C, C)]
}
object GeoJson {
implicit def geoJsonWrites[C](implicit crs: CrsFormat[C]): Writes[GeoJson[C]] =
GeoFormats.writesWithCrs(GeoFormats.geoJsonFormat[C](crs.format))
implicit def geoJsonReads[C](implicit crs: CrsFormat[C]): Reads[GeoJson[C]] =
GeoFormats.geoJsonFormat(crs.format)
}
/**
* A GeoJSON Geometry object.
*
* @tparam C The object used to model the CRS that this GeoJSON object uses.
*/
sealed trait Geometry[C] extends GeoJson[C]
object Geometry {
implicit def geometryReads[C](implicit crs: CrsFormat[C]): Reads[Geometry[C]] =
GeoFormats.geometryFormat(crs.format)
implicit def geometryWrites[C](implicit crs: CrsFormat[C]): Writes[Geometry[C]] =
GeoFormats.writesWithCrs(GeoFormats.geometryFormat[C](crs.format))
}
/**
* A GeoJSON Point object.
*
* @param coordinates The coordinates of this point.
* @param bbox The bounding box of the point, if any.
* @tparam C The object used to model the CRS that this GeoJSON object uses.
*/
case class Point[C](coordinates: C, bbox: Option[(C, C)] = None) extends Geometry[C]
object Point {
implicit def pointReads[C](implicit crs: CrsFormat[C]): Reads[Point[C]] =
GeoFormats.pointFormat(crs.format)
implicit def pointWrites[C](implicit crs: CrsFormat[C]): Writes[Point[C]] =
GeoFormats.writesWithCrs(GeoFormats.pointFormat[C](crs.format))
}
/**
* A GeoJSON MultiPoint object.
*
* @param coordinates The sequence coordinates for the points.
* @param bbox The bounding box for the points, if any.
* @tparam C The object used to model the CRS that this GeoJSON object uses.
*/
case class MultiPoint[C](coordinates: Seq[C], bbox: Option[(C, C)] = None) extends Geometry[C]
object MultiPoint {
implicit def multiPointReads[C](implicit crs: CrsFormat[C]): Reads[MultiPoint[C]] =
GeoFormats.multiPointFormat(crs.format)
implicit def multiPointWrites[C](implicit crs: CrsFormat[C]): Writes[MultiPoint[C]] =
GeoFormats.writesWithCrs(GeoFormats.multiPointFormat[C](crs.format))
}
/**
* A GeoJSON LineString object.
*
* @param coordinates The sequence of coordinates for the line.
* @param bbox The bounding box for the line, if any.
* @tparam C The object used to model the CRS that this GeoJSON object uses.
*/
case class LineString[C](coordinates: Seq[C], bbox: Option[(C, C)] = None) extends Geometry[C]
object LineString {
implicit def lineStringReads[C](implicit crs: CrsFormat[C]): Reads[LineString[C]] =
GeoFormats.lineStringFormat(crs.format)
implicit def lineStringWrites[C](implicit crs: CrsFormat[C]): Writes[LineString[C]] =
GeoFormats.writesWithCrs(GeoFormats.lineStringFormat[C](crs.format))
}
/**
* A GeoJSON MultiLineString object.
*
* @param coordinates The sequence of lines.
* @param bbox The bounding box for the lines, if any.
* @tparam C The object used to model the CRS that this GeoJSON object uses.
*/
case class MultiLineString[C](coordinates: Seq[Seq[C]], bbox: Option[(C, C)] = None) extends Geometry[C]
object MultiLineString {
implicit def multiLineStringReads[C](implicit crs: CrsFormat[C]): Reads[MultiLineString[C]] =
GeoFormats.multiLineStringFormat(crs.format)
implicit def multiLineStringWrites[C](implicit crs: CrsFormat[C]): Writes[MultiLineString[C]] =
GeoFormats.writesWithCrs(GeoFormats.multiLineStringFormat[C](crs.format))
}
/**
* A GeoJSON Polygon object.
*
* @param coordinates A sequence of sequences of corners in the polygon.
* @param bbox The bounding box for the polygon, if any.
* @tparam C The object used to model the CRS that this GeoJSON object uses.
*/
case class Polygon[C](coordinates: Seq[Seq[C]], bbox: Option[(C, C)] = None) extends Geometry[C]
object Polygon {
implicit def polygonReads[C](implicit crs: CrsFormat[C]): Reads[Polygon[C]] =
GeoFormats.polygonFormat(crs.format)
implicit def polygonWrites[C](implicit crs: CrsFormat[C]): Writes[Polygon[C]] =
GeoFormats.writesWithCrs(GeoFormats.polygonFormat[C](crs.format))
}
/**
* A GeoJSON MultiPolygon object.
*
* @param coordinates The sequence of polygons.
* @param bbox The bounding box for the polygons, if any.
* @tparam C The object used to model the CRS that this GeoJSON object uses.
*/
case class MultiPolygon[C](coordinates: Seq[Seq[Seq[C]]], bbox: Option[(C, C)] = None) extends Geometry[C]
object MultiPolygon {
implicit def multiPolygonReads[C](implicit crs: CrsFormat[C]): Reads[MultiPolygon[C]] =
GeoFormats.multiPolygonFormat(crs.format)
implicit def multiPolygonWrites[C](implicit crs: CrsFormat[C]): Writes[MultiPolygon[C]] =
GeoFormats.writesWithCrs(GeoFormats.multiPolygonFormat[C](crs.format))
}
/**
* A GeoJSON GeometryCollection object.
*
* @param geometries The sequence of geometries.
* @param bbox The bounding box for the geometries, if any.
* @tparam C The object used to model the CRS that this GeoJSON object uses.
*/
case class GeometryCollection[C](geometries: Seq[Geometry[C]], bbox: Option[(C, C)] = None) extends Geometry[C]
object GeometryCollection {
implicit def geometryCollectionReads[C](implicit crs: CrsFormat[C]): Reads[GeometryCollection[C]] =
GeoFormats.geometryCollectionFormat(crs.format)
implicit def geometryCollectionWrites[C](implicit crs: CrsFormat[C]): Writes[GeometryCollection[C]] =
GeoFormats.writesWithCrs(GeoFormats.geometryCollectionFormat[C](crs.format))
}
/**
* A GeoJSON FeatureCollection object.
*
* @param features The sequence of features.
* @param bbox The bounding box for the sequence of features, if any.
* @tparam C The object used to model the CRS that this GeoJSON object uses.
*/
case class FeatureCollection[C](features: Seq[Feature[C]], bbox: Option[(C, C)] = None) extends GeoJson[C]
object FeatureCollection {
implicit def featureCollectionReads[C](implicit crs: CrsFormat[C]): Reads[FeatureCollection[C]] =
GeoFormats.featureCollectionFormat(crs.format)
implicit def featureCollectionWrites[C](implicit crs: CrsFormat[C]): Writes[FeatureCollection[C]] =
GeoFormats.writesWithCrs(GeoFormats.featureCollectionFormat[C](crs.format))
}
/**
* A GeoJSON Feature object.
*
* @param geometry The geometry for the feature.
* @param properties The properties for the feature, if any.
* @param id The id of the feature, if any.
* @param bbox The bounding box for the feature, if any.
* @tparam C The object used to model the CRS that this GeoJSON object uses.
*/
case class Feature[C](geometry: Geometry[C],
properties: Option[JsObject] = None,
id: Option[JsValue] = None,
bbox: Option[(C, C)] = None) extends GeoJson[C]
object Feature {
implicit def featureReads[C](implicit crs: CrsFormat[C]): Reads[Feature[C]] =
GeoFormats.featureFormat(crs.format)
implicit def featureWrites[C](implicit crs: CrsFormat[C]): Writes[Feature[C]] =
GeoFormats.writesWithCrs(GeoFormats.featureFormat[C](crs.format))
}
/**
* A GeoJSON coordinate reference system (CRS).
*/
sealed trait Crs
object Crs {
import GeoFormats._
implicit val crsFormat: Format[Crs] = Format(
readType.flatMap {
case "name" => NamedCrs.namedCrsFormat
case "link" => LinkedCrs.linkedCrsFormat
case unknown => errorReads("Unknown CRS descriptor type: " + unknown)
},
Writes {
case named: NamedCrs => NamedCrs.namedCrsFormat.writes(named)
case linked: LinkedCrs => LinkedCrs.linkedCrsFormat.writes(linked)
}
)
}
/**
* A GeoJSON named CRS.
*
* @param name The name of the CRS.
*/
case class NamedCrs(name: String) extends Crs
object NamedCrs {
implicit val namedCrsFormat: Format[NamedCrs] =
GeoFormats.geoJsonFormatFor("name", (__ \ "properties").format(Json.format[NamedCrs]))
}
/**
* A GeoJSON linked CRS.
*
* @param href The href for the CRS.
* @param type The type of the link, if any.
*/
case class LinkedCrs(href: String, `type`: Option[String]) extends Crs
object LinkedCrs {
implicit val linkedCrsFormat: Format[LinkedCrs] =
GeoFormats.geoJsonFormatFor("link", (__ \ "properties").format(Json.format[LinkedCrs]))
}
/**
* A CRS format
*/
trait CrsFormat[C] {
/**
* The CRS for the CRS format
*/
def crs: Crs
/**
* The format to use to write the CRS.
*/
def format: Format[C]
/**
* Whether this is the default CRS format. If so, no CRS information will be added to the GeoJSON object when
* serialised.
*/
def isDefault = false
}
/**
* These are the raw "internal" formats. They do not add the CRS parameter when serialising.
*/
private object GeoFormats {
/**
* Adds contramap ops to functional builders
*/
object ExtendedWrites {
// adds pathWrite function to JsPath creates a JsObject from a path and value
implicit class PathWrites(val path: JsPath) extends AnyVal {
def pathWrite[A : Writes](a: A): OWrites[Any] = OWrites[Any](_ => JsPath.createObj(path -> implicitly[Writes[A]].writes(a)))
}
implicit class FunctionalBuilderWithContraOps[M[_] : ContravariantFunctor : FunctionalCanBuild, A](val ma: M[A]) {
def ~~> [B <: A](mb: M[B]): M[B] = implicitly[ContravariantFunctor[M]].contramap(
implicitly[FunctionalCanBuild[M]].apply(ma,mb)
, (b:B) => play.api.libs.functional.~(b:A, b:B)
)
def <~~ [B >: A](mb: M[B]): M[A] = implicitly[ContravariantFunctor[M]].contramap(
implicitly[FunctionalCanBuild[M]].apply(ma,mb),
(a:A) => play.api.libs.functional.~(a:A, a:B)
)
}
}
import ExtendedWrites._
/**
* Reads the GeoJSON type property.
*/
def readType: Reads[String] = (__ \ "type").read[String]
/**
* Reads a GeoJSON type property with the given type.
*
* If the type is not the given name, a validation error is thrown.
*/
def filterType(geoJsonType: String): Reads[String] =
readType.filter(JsonValidationError("Geometry is not a " + geoJsonType))(_ == geoJsonType)
/**
* Writes for the GeoJSON type property for the given type.
*/
def writeType(geoJsonType: String): OWrites[Any] = (__ \ "type").pathWrite(geoJsonType)
/**
* Format for the bbox property.
*/
def formatBbox[C : Format]: OFormat[Option[(C, C)]] = (__ \ "bbox").formatNullable[(C, C)]
implicit def crsBoxFormat[C](implicit cFormat: Format[C]): Format[(C, C)] = Format(
Reads[(C, C)] {
case JsArray(seq) =>
val (first, second) = seq.splitAt(seq.size / 2)
for {
f <- cFormat.reads(JsArray(first))
s <- cFormat.reads(JsArray(second))
} yield (f, s)
case _ => JsError("bbox must be an array")
}, Writes { (bbox: (C, C)) =>
(cFormat.writes(bbox._1), cFormat.writes(bbox._2)) match {
case (a: JsArray, b: JsArray) => a ++ b
case _ => throw new RuntimeException("CRS format writes must produce a JsArray")
}
}
)
/**
* Create a GeoJSON format with the given type name.
*/
def geoJsonFormatFor[G](geoJsonType: String, format: OFormat[G]): Format[G] = Format[G](
filterType(geoJsonType) ~> format,
writeType(geoJsonType) ~~> format
)
/**
* Create a Geometry format with the given type name.
*/
def geometryFormatFor[G, T : Format, C : Format](geoJsonType: String,
read: (T, Option[(C, C)]) => G,
write: G => Option[(T, Option[(C, C)])]): Format[G] =
geoJsonFormatFor(geoJsonType,
((__ \ "coordinates").format[T] ~ formatBbox[C]).apply(read, unlift(write))
)
def errorReads[T](message: String) = Reads[T](_ => JsError(message))
/**
* Reads is invariant in its type parameter. This function widens it.
*/
implicit def widenReads[A, B >: A](reads: Reads[A]): Reads[B] = Reads[B](_.validate(reads))
/*
* Formats for each of the different GeoJSON types. These are internal, they do not add a CRS property to the
* output.
*/
def pointFormat[C : Format]: Format[Point[C]] =
geometryFormatFor("Point", Point.apply, Point.unapply)
def multiPointFormat[C : Format]: Format[MultiPoint[C]] =
geometryFormatFor("MultiPoint", MultiPoint.apply, MultiPoint.unapply)
def lineStringFormat[C : Format]: Format[LineString[C]] =
geometryFormatFor("LineString", LineString.apply, LineString.unapply)
def multiLineStringFormat[C : Format]: Format[MultiLineString[C]] =
geometryFormatFor("MultiLineString", MultiLineString.apply, MultiLineString.unapply)
def polygonFormat[C : Format]: Format[Polygon[C]] =
geometryFormatFor("Polygon", Polygon.apply, Polygon.unapply)
def multiPolygonFormat[C : Format]: Format[MultiPolygon[C]] =
geometryFormatFor("MultiPolygon", MultiPolygon.apply, MultiPolygon.unapply)
def geometryCollectionFormat[C : Format]: Format[GeometryCollection[C]] = {
implicit val gf = geometryFormat[C]
geoJsonFormatFor("GeometryCollection",
((__ \ "geometries").format[Seq[Geometry[C]]] ~ formatBbox[C])
.apply(GeometryCollection.apply, unlift(GeometryCollection.unapply))
)
}
def featureFormat[C : Format]: Format[Feature[C]] = {
geoJsonFormatFor("Feature", (
(__ \ "geometry").format(geometryFormat[C]) ~
(__ \ "properties").formatNullable[JsObject] ~
// The spec isn't clear on what the id can be
(__ \ "id").formatNullable[JsValue] ~
formatBbox[C]
).apply(Feature.apply, unlift(Feature.unapply))
)
}
def featureCollectionFormat[C : Format]: Format[FeatureCollection[C]] = {
implicit val ff = featureFormat[C]
geoJsonFormatFor("FeatureCollection",
((__ \ "features").format[Seq[Feature[C]]] ~ formatBbox[C])
.apply(FeatureCollection.apply, unlift(FeatureCollection.unapply))
)
}
def geometryFormat[C : Format]: Format[Geometry[C]] = Format(
readType.flatMap {
case "Point" => pointFormat[C]
case "MultiPoint" => multiPointFormat[C]
case "LineString" => lineStringFormat[C]
case "MultiLineString" => multiLineStringFormat[C]
case "Polygon" => polygonFormat[C]
case "MultiPolygon" => multiPolygonFormat[C]
case "GeometryCollection" => geometryCollectionFormat[C]
case unknown => errorReads("Unknown GeoJSON Geometry type: " + unknown)
},
Writes {
case point: Point[C] => pointFormat[C].writes(point)
case multiPoint: MultiPoint[C] => multiPointFormat[C].writes(multiPoint)
case lineString: LineString[C] => lineStringFormat[C].writes(lineString)
case multiLineString: MultiLineString[C] => multiLineStringFormat[C].writes(multiLineString)
case polygon: Polygon[C] => polygonFormat[C].writes(polygon)
case multiPolygon: MultiPolygon[C] => multiPolygonFormat[C].writes(multiPolygon)
case geometryCollection: GeometryCollection[C] => geometryCollectionFormat[C].writes(geometryCollection)
}
)
def geoJsonFormat[C: Format]: Format[GeoJson[C]] = Format(
(geometryFormat[C]: Reads[Geometry[C]]).or(
readType.flatMap {
case "Feature" => featureFormat[C]
case "FeatureCollection" => featureCollectionFormat[C]
case unknown => errorReads("Unknown GeoJSON type: " + unknown)
}
),
Writes {
case geometry: Geometry[C] => geometryFormat[C].writes(geometry)
case feature: Feature[C] => featureFormat[C].writes(feature)
case featureCollection: FeatureCollection[C] => featureCollectionFormat[C].writes(featureCollection)
}
)
def writesWithCrs[C, G](writes: Writes[G])(implicit crs: CrsFormat[C]) = writes.transform { json =>
if (crs.isDefault) {
json
} else {
json match {
case obj: JsObject => obj ++ Json.obj("crs" -> crs.crs)
case other => other
}
}
}
}