From 5eb17a3da486a9e7b0a2bee64257944a80ce99bb Mon Sep 17 00:00:00 2001 From: Tor-Einar Skog <tor-einar.skog@nibio.no> Date: Tue, 21 May 2019 13:03:05 +0200 Subject: [PATCH] Upgrade to new version of JTS, with all kinds of horrible repercussions --- pom.xml | 2 +- .../entity/PointWeatherObservationList.java | 11 ++-- .../nibio/vips/entity/ResultDeserializer.java | 5 +- .../nibio/vips/entity/ResultSerializer.java | 8 ++- .../no/nibio/vips/gis/CoordinateProxy.java | 53 +++++++++++++++++++ src/main/java/no/nibio/vips/gis/GISUtil.java | 22 ++++++++ 6 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 src/main/java/no/nibio/vips/gis/CoordinateProxy.java diff --git a/pom.xml b/pom.xml index 3b68546..ea5b449 100755 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,7 @@ <dependency> <groupId>org.locationtech.jts</groupId> <artifactId>jts-core</artifactId> - <version>1.16.0</version> + <version>1.16.1</version> <type>jar</type> </dependency> <dependency> diff --git a/src/main/java/no/nibio/vips/entity/PointWeatherObservationList.java b/src/main/java/no/nibio/vips/entity/PointWeatherObservationList.java index f6edfc0..4abbdb2 100644 --- a/src/main/java/no/nibio/vips/entity/PointWeatherObservationList.java +++ b/src/main/java/no/nibio/vips/entity/PointWeatherObservationList.java @@ -21,7 +21,8 @@ package no.nibio.vips.entity; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import org.locationtech.jts.geom.Coordinate; +//import org.locationtech.jts.geom.Coordinate; +import no.nibio.vips.gis.CoordinateProxy; import java.util.List; /** @@ -29,7 +30,7 @@ import java.util.List; * @author Tor-Einar Skog <tor-einar.skog@nibio.no> */ public class PointWeatherObservationList { - private Coordinate coordinate; + private CoordinateProxy coordinate; private List<WeatherObservation> observations; private String timeZone; @@ -39,7 +40,7 @@ public class PointWeatherObservationList { @JsonCreator public PointWeatherObservationList( - @JsonProperty("coordinate") Coordinate coordinate, + @JsonProperty("coordinate") CoordinateProxy coordinate, @JsonProperty("observations") List<WeatherObservation> observations, @JsonProperty("timeZone") String timeZone ) @@ -52,14 +53,14 @@ public class PointWeatherObservationList { /** * @return the coordinate */ - public Coordinate getCoordinate() { + public CoordinateProxy getCoordinate() { return coordinate; } /** * @param coordinate the coordinate to set */ - public void setCoordinate(Coordinate coordinate) { + public void setCoordinate(CoordinateProxy coordinate) { this.coordinate = coordinate; } diff --git a/src/main/java/no/nibio/vips/entity/ResultDeserializer.java b/src/main/java/no/nibio/vips/entity/ResultDeserializer.java index 5e2f4d5..127e106 100644 --- a/src/main/java/no/nibio/vips/entity/ResultDeserializer.java +++ b/src/main/java/no/nibio/vips/entity/ResultDeserializer.java @@ -28,7 +28,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.LineString; -import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.Polygon; import java.io.IOException; import java.util.Date; @@ -41,8 +40,10 @@ import no.nibio.vips.gis.GISUtil; */ public class ResultDeserializer extends StdDeserializer<Result>{ + GISUtil gisUtil; public ResultDeserializer(){ this(null); + this.gisUtil = new GISUtil(); } public ResultDeserializer(Class<?>vc){ @@ -71,7 +72,7 @@ public class ResultDeserializer extends StdDeserializer<Result>{ { switch(geomNode.get("type").asText()){ case "Point": - validGeometry = objectMapper.convertValue(geomNode, Point.class); + validGeometry = this.gisUtil.getLocationTechPoint(objectMapper.convertValue(geomNode, com.vividsolutions.jts.geom.Point.class)); break; case "Polygon": validGeometry = objectMapper.convertValue(geomNode, Polygon.class); diff --git a/src/main/java/no/nibio/vips/entity/ResultSerializer.java b/src/main/java/no/nibio/vips/entity/ResultSerializer.java index 3d8d565..ae2539a 100644 --- a/src/main/java/no/nibio/vips/entity/ResultSerializer.java +++ b/src/main/java/no/nibio/vips/entity/ResultSerializer.java @@ -30,6 +30,7 @@ import org.locationtech.jts.geom.LineString; import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.Polygon; import java.io.IOException; +import no.nibio.vips.gis.GISUtil; /** * Ref http://www.baeldung.com/jackson-custom-serialization @@ -39,8 +40,10 @@ import java.io.IOException; class ResultSerializer extends StdSerializer<ResultImpl>{ ObjectMapper objectMapper; GeometryFactory gf; + GISUtil gisUtil; public ResultSerializer() { this(null); + this.gisUtil = new GISUtil(); } public ResultSerializer(Class<ResultImpl> t) { @@ -52,12 +55,11 @@ class ResultSerializer extends StdSerializer<ResultImpl>{ @Override public void serialize(ResultImpl t, JsonGenerator jg, SerializerProvider sp) throws IOException { - jg.writeStartObject(); jg.writeStringField("validTimeStart", this.objectMapper.writeValueAsString(t.getValidTimeStart()).replaceAll("\"", "")); jg.writeStringField("validTimeEnd", this.objectMapper.writeValueAsString(t.getValidTimeEnd()).replaceAll("\"", "")); String geoJSON = t.getValidGeometry() == null ? "null" - : t.getValidGeometry().getGeometryType().equals("Point") ? objectMapper.writeValueAsString((Point) t.getValidGeometry()) + : t.getValidGeometry().getGeometryType().equals("Point") ? objectMapper.writeValueAsString(this.gisUtil.getVividPoint((Point) t.getValidGeometry())) : t.getValidGeometry().getGeometryType().equals("LineString") ? objectMapper.writeValueAsString((LineString) t.getValidGeometry()) :t.getValidGeometry().getGeometryType().equals("Polygon") ? objectMapper.writeValueAsString((Polygon) t.getValidGeometry()) : "null"; @@ -71,5 +73,7 @@ class ResultSerializer extends StdSerializer<ResultImpl>{ jg.writeStringField("keys", this.objectMapper.writeValueAsString(t.getKeys())); jg.writeEndObject(); } + + } diff --git a/src/main/java/no/nibio/vips/gis/CoordinateProxy.java b/src/main/java/no/nibio/vips/gis/CoordinateProxy.java new file mode 100644 index 0000000..2cb53de --- /dev/null +++ b/src/main/java/no/nibio/vips/gis/CoordinateProxy.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSCommon. + * VIPSCommon is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSCommon is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSCommon. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.gis; + +/** + * @copyright 2019 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +public class CoordinateProxy extends org.locationtech.jts.geom.Coordinate{ + + public CoordinateProxy() + { + super(); + } + + public CoordinateProxy(Double longitude, Double latitude) + { + super(longitude,latitude); + } + + public CoordinateProxy(Double longitude, Double latitude, Double altitude) + { + super(longitude,latitude,altitude); + } + + /** + * We needed to override this in order to not break deserializing with Jackson. Phew.... + * @param m + */ + @Override + public void setM(double m) + { + + } + +} diff --git a/src/main/java/no/nibio/vips/gis/GISUtil.java b/src/main/java/no/nibio/vips/gis/GISUtil.java index 3f5f6d0..41e901c 100644 --- a/src/main/java/no/nibio/vips/gis/GISUtil.java +++ b/src/main/java/no/nibio/vips/gis/GISUtil.java @@ -285,4 +285,26 @@ public class GISUtil { throw new LonLatStringFormatException(ex.getMessage()); } } + + public com.vividsolutions.jts.geom.Point getVividPoint(org.locationtech.jts.geom.Point locationTechPoint) + { + com.vividsolutions.jts.geom.Coordinate vividCoordinate = new com.vividsolutions.jts.geom.Coordinate( + locationTechPoint.getCoordinate().x, + locationTechPoint.getCoordinate().y, + locationTechPoint.getCoordinate().z + ); + com.vividsolutions.jts.geom.Point vividPoint = new com.vividsolutions.jts.geom.GeometryFactory().createPoint(vividCoordinate); + return vividPoint; + } + + public org.locationtech.jts.geom.Point getLocationTechPoint(com.vividsolutions.jts.geom.Point vividPoint) + { + org.locationtech.jts.geom.Coordinate locationTechCoordinate = new org.locationtech.jts.geom.Coordinate( + vividPoint.getCoordinate().x, + vividPoint.getCoordinate().y, + vividPoint.getCoordinate().z + ); + org.locationtech.jts.geom.Point locationTechPoint = new org.locationtech.jts.geom.GeometryFactory().createPoint(locationTechCoordinate); + return locationTechPoint; + } } -- GitLab