X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.app.geo%2Fsrc%2Forg%2Fargeo%2Fapp%2Fgeo%2FGpxUtils.java;h=44afa2c01f0a9341700aa644a4324ef207407d8b;hb=e9978679e86dcd297270432e4ed953b782f1e7c6;hp=2bcff158712a38eb5a32059ea2a48fcf2e2ddc1f;hpb=57f3528fda509d840818a72ebcd38b6ab3afa435;p=gpl%2Fargeo-suite.git diff --git a/org.argeo.app.geo/src/org/argeo/app/geo/GpxUtils.java b/org.argeo.app.geo/src/org/argeo/app/geo/GpxUtils.java index 2bcff15..44afa2c 100644 --- a/org.argeo.app.geo/src/org/argeo/app/geo/GpxUtils.java +++ b/org.argeo.app.geo/src/org/argeo/app/geo/GpxUtils.java @@ -15,6 +15,8 @@ import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; +import org.geotools.api.feature.simple.SimpleFeature; +import org.geotools.api.feature.simple.SimpleFeatureType; import org.geotools.data.DataUtilities; import org.geotools.feature.SchemaException; import org.geotools.feature.simple.SimpleFeatureBuilder; @@ -22,9 +24,8 @@ import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.LineString; +import org.locationtech.jts.geom.MultiPoint; import org.locationtech.jts.geom.Polygon; -import org.opengis.feature.simple.SimpleFeature; -import org.opengis.feature.simple.SimpleFeatureType; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; @@ -52,7 +53,7 @@ public class GpxUtils { */ @SuppressWarnings("unchecked") public static T parseGpxTrackTo(InputStream in, Class clss) throws IOException { - GeometryFactory geometryFactory = JTS.GEOMETRY_FACTORY; + GeometryFactory geometryFactory = JTS.GEOMETRY_FACTORY_WGS84; List coordinates = new ArrayList<>(); try { SAXParserFactory factory = SAXParserFactory.newInstance(); @@ -67,7 +68,7 @@ public class GpxUtils { Double latitude = Double.parseDouble(attributes.getValue("lat")); Double longitude = Double.parseDouble(attributes.getValue("lon")); // TODO elevation in 3D context - Coordinate coordinate = new Coordinate(longitude, latitude); + Coordinate coordinate = new Coordinate(latitude, longitude); coordinates.add(coordinate); } } @@ -81,14 +82,21 @@ public class GpxUtils { LineString lineString = geometryFactory .createLineString(coordinates.toArray(new Coordinate[coordinates.size()])); return (T) lineString; + } else if (MultiPoint.class.isAssignableFrom(clss)) { + MultiPoint multiPoint = geometryFactory + .createMultiPointFromCoords(coordinates.toArray(new Coordinate[coordinates.size()])); + // multiPoint.normalize(); + return (T) multiPoint; } else if (Polygon.class.isAssignableFrom(clss)) { - // close the line string - coordinates.add(coordinates.get(0)); + Coordinate first = coordinates.get(0); + Coordinate last = coordinates.get(coordinates.size() - 1); + if (!(first.getX() == last.getX() && first.getY() == last.getY())) { + // close the line string + coordinates.add(first); + } Polygon polygon = geometryFactory.createPolygon(coordinates.toArray(new Coordinate[coordinates.size()])); return (T) polygon; - } - // TODO MultiPoint? MultiLine? etc. - else if (SimpleFeature.class.isAssignableFrom(clss)) { + } else if (SimpleFeature.class.isAssignableFrom(clss)) { SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(LINESTRING_FEATURE_TYPE); LineString lineString = geometryFactory .createLineString(coordinates.toArray(new Coordinate[coordinates.size()])); @@ -110,7 +118,7 @@ public class GpxUtils { return area; } - /** Write ODK GepShape as a GPX file. */ + /** Write ODK GeoShape as a GPX file. */ public static void writeGeoShapeAsGpx(String geoShape, OutputStream out) throws IOException { Objects.requireNonNull(geoShape); Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);