How to represent a GeoJSON point in a ReactiveMongo model?

To execute geospatial queries in MongoDB, a document with a location (with a geospatial index 2dor 2dsphere) should look something like this:

{
    _id: …,
    loc: {
        type: "Point",
        coordinates: [ <longitude>, <latitude> ]
    }
}

I am very new to Scala, ReactiveMongo, and the Play Framework, but in my opinion, the case class is an obvious way to use this place, for example:

case class Point(lon: Double, lat: Double)

And the JSON representation that the website API is working with should look something like this:

{
    _id: …
    loc: [ <longitude>, <latitude> ]
}

Now I can’t understand how to tell my ReactiveMongo model about serialization / deserialization between these formats.

My controller is as follows:

package controllers

import play.api._
import play.api.mvc._
import play.api.libs.json._
import scala.concurrent.Future

// Reactive Mongo imports
import reactivemongo.api._
import scala.concurrent.ExecutionContext.Implicits.global

// Reactive Mongo plugin
import play.modules.reactivemongo.MongoController
import play.modules.reactivemongo.json.collection.JSONCollection

object Application extends Controller with MongoController {
    def collection: JSONCollection = db.collection[JSONCollection]("test")

    import play.api.data.Form
    import models._
    import models.JsonFormats._

    def createCC = Action.async {
        val user = User("John", "Smith", Point(-0.0015, 51.0015))
        val futureResult = collection.insert(user)
        futureResult.map(_ => Ok("Done!"))
    }
}

I tried using PointWriter and PointReader. These are my models. Scala:

package models

import reactivemongo.bson._
import play.modules.reactivemongo.json.BSONFormats._

case class User(
    // _id: Option[BSONObjectID],
    firstName: String,
    lastName: String,
    loc: Point)

case class Point(lon: Double, lat: Double)

object Point {
    implicit object PointWriter extends BSONDocumentWriter[Point] {
        def write(point: Point): BSONDocument = BSONDocument(
            "type" -> "Point",
            "coordinates" -> Seq(point.lat, point.lon)
        )
    }

    implicit object PointReader extends BSONReader[BSONDocument, Point] {
        def read(doc: BSONDocument): Point = Point(88, 88)
    }
}

object JsonFormats {
    import play.api.libs.json.Json
    import play.api.data._
    import play.api.data.Forms._

    import play.api.libs.json._
    import play.api.libs.functional.syntax._

    implicit val pointFormat = Json.format[Point]
}

createCC, , Point , - :

{
    "_id": ObjectId("52ac76dd1454bbf6d96ad1f1"),
    "loc": {
        "lon": -0.0015,
        "lat": 51.0015 
    }
}

, PointWriter PointReader, ReactiveMongo, Point , .

- , ?

( PHP Scala...)

Update: tmbo :

val pointWrites = Writes[Point]( p =>
    Json.obj(
        "type" -> JsString("Point"),
        "coordinates" -> Json.arr(JsNumber(p.lon), JsNumber(p.lat))
    )
)
+4
1

, , JSONCollection BSONCollection.

BSONCollection - reactivemongo. BSONDocumentWriter BSONReader case, (-) .

JSONCollection , , . JSONCollection db.collection[JSONCollection]("test"), json-.

json, ,

implicit val pointFormat = Json.format[Point]

{
    "lon": -0.0015,
    "lat": 51.0015 
}

Point , pointFormat:

import play.api.libs.json._
import play.api.libs.json.Reads._

case class Point(lng: Double, lat: Double)

object Point {

  val pointWrites = Writes[Point]( p => Json.toJson(List(p.lng, p.lat)))

  val pointReads = minLength[List[Double]](2).map(l => Point(l(0), l(1)))

  implicit val pointFormat = Format(pointReads, pointWrites)
}

BSONReader BSONDocumentWriter.

Edit: , type :

val pointReads =
  (__ \ 'type).read[String](constraints.verifying[String](_ == "Point")) andKeep
    (__ \ 'coordinates).read[Point](minLength[List[Double]](2).map(l => Point(l(0), l(1))))
+5

All Articles