package de.fu_berlin.agdb.crepe.json.serialize;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.PrecisionModel;
import de.fu_berlin.agdb.crepe.json.util.SimplePoint;

import java.io.IOException;

/**
 * Class to deserialize {@link com.vividsolutions.jts.geom.Point} objects from JSON.
 * Adapted from <a href="http://stackoverflow.com/questions/27624940/map-a-postgis-geometry-point-field-with-hibernate-on-spring-boot">Stackoverflow</a>.
 *
 * @author Simon Kalt
 */
public class PointDeserializer extends JsonDeserializer<Point> {

    /**
     * SRID 26910 is used here
     * that means:
     * PROJCS["NAD83 / UTM zone 10N",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-123],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],AUTHORITY["EPSG","26910"],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
     * TODO: externalize value?!
     */
    private static final int SRID = 26910;
    private static final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), SRID);

    @Override
    public Point deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        try {
            SimplePoint jsonPoint = jp.readValueAs(SimplePoint.class);
            return geometryFactory.createPoint(
                    new Coordinate(jsonPoint.getLatitude(), jsonPoint.getLongitude())
            );
        } catch (Exception e) {
            return null;
        }
    }
}

@@ -903,725 +903,12 @@
 RID 
+43
 26
-910 is used here%0A     * that means:%0A     * PROJCS%5B%22NAD83 / UTM zone 10N%22,GEOGCS%5B%22NAD83%22,DATUM%5B%22North_American_Datum_1983%22,SPHEROID%5B%22GRS 1980%22,6378137,298.257222101,AUTHORITY%5B%22EPSG%22,%227019%22%5D%5D,TOWGS84%5B0,0,0,0,0,0,0%5D,AUTHORITY%5B%22EPSG%22,%226269%22%5D%5D,PRIMEM%5B%22Greenwich%22,0,AUTHORITY%5B%22EPSG%22,%228901%22%5D%5D,UNIT%5B%22degree%22,0.0174532925199433,AUTHORITY%5B%22EPSG%22,%229122%22%5D%5D,AUTHORITY%5B%22EPSG%22,%224269%22%5D%5D,UNIT%5B%22metre%22,1,AUTHORITY%5B%22EPSG%22,%229001%22%5D%5D,PROJECTION%5B%22Transverse_Mercator%22%5D,PARAMETER%5B%22latitude_of_origin%22,0%5D,PARAMETER%5B%22central_meridian%22,-123%5D,PARAMETER%5B%22scale_factor%22,0.9996%5D,PARAMETER%5B%22false_easting%22,500000%5D,PARAMETER%5B%22false_northing%22,0%5D,AUTHORITY%5B%22EPSG%22,%2226910%22%5D,AXIS%5B%22Easting%22,EAST%5D,AXIS%5B%22Northing%22,NORTH%5D%5D%0A     * TODO: externalize value?!
 %0A   
@@ -952,13 +952,12 @@
 D = 
+43
 26
-910
 ;%0A  

Update Spatial Reference System to 4326