Last active
November 21, 2018 10:14
-
-
Save guizmaii/a4bc554f1cec45f43f9628f10f02e0f5 to your computer and use it in GitHub Desktop.
JTS Geometry Play JSON Format
This file contains hidden or 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
| import com.vividsolutions.jts.geom.Geometry | |
| import com.vividsolutions.jts.io.ParseException | |
| import com.vividsolutions.jts.io.geojson.{GeoJsonReader, GeoJsonWriter} | |
| import play.api.libs.json._ | |
| import scala.reflect.ClassTag | |
| import scala.util.{Failure, Success, Try} | |
| object GeometryJson { | |
| implicit final val geometryFormat: Format[Geometry] = { | |
| val geoJsonReader = new GeoJsonReader() | |
| val geoJsonWriter = new GeoJsonWriter() | |
| new Format[Geometry] { | |
| def reads(json: JsValue): JsResult[Geometry] = try { JsSuccess(geoJsonReader.read(json.toString)) } catch { case e: ParseException => JsError(s"Invalid GeoJson: ${e.getMessage}") } | |
| def writes(geometry: Geometry): JsValue = Json.parse(geoJsonWriter.write(geometry)) | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
build.sbt dependencies: