X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.app.core%2Fsrc%2Forg%2Fargeo%2Fapp%2Fgeo%2FGpxUtils.java;fp=org.argeo.app.core%2Fsrc%2Forg%2Fargeo%2Fapp%2Fgeo%2FGpxUtils.java;h=be028d3f4a79bd65be30b935514074920b81b94d;hb=07d4329086be993ff25fc6342c97b681b2e07433;hp=0000000000000000000000000000000000000000;hpb=2ac2baf40fd2fece9cbbd063c984aed4b67f2e20;p=gpl%2Fargeo-suite.git diff --git a/org.argeo.app.core/src/org/argeo/app/geo/GpxUtils.java b/org.argeo.app.core/src/org/argeo/app/geo/GpxUtils.java new file mode 100644 index 0000000..be028d3 --- /dev/null +++ b/org.argeo.app.core/src/org/argeo/app/geo/GpxUtils.java @@ -0,0 +1,68 @@ +package org.argeo.app.geo; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.geotools.data.DataUtilities; +import org.geotools.feature.SchemaException; +import org.geotools.feature.simple.SimpleFeatureBuilder; +import org.geotools.geometry.jts.JTSFactoryFinder; +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.GeometryFactory; +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; + +/** Utilities around the GPX format. */ +public class GpxUtils { + + public static SimpleFeature parseGpxToPolygon(InputStream in) { + try { + final SimpleFeatureType TYPE = DataUtilities.createType("Area", "the_geom:Polygon:srid=4326"); + SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE); + + GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); + List coordinates = new ArrayList<>(); + SAXParserFactory factory = SAXParserFactory.newInstance(); + Double[] startCoord = new Double[2]; + SAXParser saxParser = factory.newSAXParser(); + + saxParser.parse(in, new DefaultHandler() { + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) + throws SAXException { + if ("trkpt".equals(qName)) { + Double latitude = Double.parseDouble(attributes.getValue("lat")); + Double longitude = Double.parseDouble(attributes.getValue("lon")); + Coordinate coordinate = new Coordinate(longitude, latitude); + coordinates.add(coordinate); + } + } + + }); + // close the line string + coordinates.add(coordinates.get(0)); + + Polygon polygon = geometryFactory.createPolygon(coordinates.toArray(new Coordinate[coordinates.size()])); + featureBuilder.add(polygon); + SimpleFeature area = featureBuilder.buildFeature(null); + return area; + } catch (ParserConfigurationException | SAXException | IOException | SchemaException e) { + throw new RuntimeException("Cannot convert GPX", e); + } + } + + /** Singleton. */ + private GpxUtils() { + } +}