Fix lat/lon vs. lon/lat issues
[gpl/argeo-suite.git] / org.argeo.app.geo / src / org / argeo / app / geo / GpxUtils.java
index 5b07d1677fe72166ae9329327062812ddf35b37b..44afa2c01f0a9341700aa644a4324ef207407d8b 100644 (file)
@@ -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;
@@ -24,8 +26,6 @@ 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;
@@ -68,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);
                                        }
                                }
@@ -88,8 +88,12 @@ public class GpxUtils {
                        // 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;
                } else if (SimpleFeature.class.isAssignableFrom(clss)) {
@@ -103,7 +107,7 @@ public class GpxUtils {
                        throw new IllegalArgumentException("Unsupported format " + clss);
                }
        }
-       
+
        /** @deprecated Use {@link #parseGpxTrackTo(InputStream, Class)} instead. */
        @Deprecated
        public static SimpleFeature parseGpxToPolygon(InputStream in) throws IOException {
@@ -114,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);