X-Git-Url: https://git.argeo.org/?p=gpl%2Fargeo-suite.git;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=0000000000000000000000000000000000000000;hp=645b08bda8d9ed13b05c4488e1c3ad326e3f660f;hb=37c5768d88096f4eb7be02f18770b6a40be5c110;hpb=9803af38d9ec2eccc2cb5e01a276cbf2edd0d50c 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 deleted file mode 100644 index 645b08b..0000000 --- a/org.argeo.app.core/src/org/argeo/app/geo/GpxUtils.java +++ /dev/null @@ -1,99 +0,0 @@ -package org.argeo.app.geo; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.StringTokenizer; - -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) throws IOException { - 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(); - 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 | SchemaException e) { - throw new RuntimeException("Cannot convert GPX", e); - } - } - - public static void writeGeoShapeAsGpx(String geoShape, OutputStream out) throws IOException { - Objects.requireNonNull(geoShape); - Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8); - writer.append(""); - StringTokenizer stSeg = new StringTokenizer(geoShape.trim(), ";"); - while (stSeg.hasMoreTokens()) { - StringTokenizer stPt = new StringTokenizer(stSeg.nextToken().trim(), " "); - String lat = stPt.nextToken(); - String lng = stPt.nextToken(); - String alt = stPt.nextToken(); - // String precision = stPt.nextToken(); - writer.append("'); - writer.append("").append(alt).append(""); - writer.append(""); - } else { - writer.append("/>"); - } - } - writer.append(""); - writer.flush(); - } - - /** Singleton. */ - private GpxUtils() { - } -}