]> git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.app.core/src/org/argeo/app/geo/GpxUtils.java
Releasing
[gpl/argeo-suite.git] / org.argeo.app.core / src / org / argeo / app / geo / GpxUtils.java
1 package org.argeo.app.geo;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.io.OutputStreamWriter;
7 import java.io.Writer;
8 import java.nio.charset.StandardCharsets;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Objects;
12 import java.util.StringTokenizer;
13
14 import javax.xml.parsers.ParserConfigurationException;
15 import javax.xml.parsers.SAXParser;
16 import javax.xml.parsers.SAXParserFactory;
17
18 import org.geotools.data.DataUtilities;
19 import org.geotools.feature.SchemaException;
20 import org.geotools.feature.simple.SimpleFeatureBuilder;
21 import org.geotools.geometry.jts.JTSFactoryFinder;
22 import org.locationtech.jts.geom.Coordinate;
23 import org.locationtech.jts.geom.GeometryFactory;
24 import org.locationtech.jts.geom.Polygon;
25 import org.opengis.feature.simple.SimpleFeature;
26 import org.opengis.feature.simple.SimpleFeatureType;
27 import org.xml.sax.Attributes;
28 import org.xml.sax.SAXException;
29 import org.xml.sax.helpers.DefaultHandler;
30
31 /** Utilities around the GPX format. */
32 public class GpxUtils {
33
34 public static SimpleFeature parseGpxToPolygon(InputStream in) throws IOException {
35 try {
36 final SimpleFeatureType TYPE = DataUtilities.createType("Area", "the_geom:Polygon:srid=4326");
37 SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
38
39 GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
40 List<Coordinate> coordinates = new ArrayList<>();
41 SAXParserFactory factory = SAXParserFactory.newInstance();
42 SAXParser saxParser = factory.newSAXParser();
43
44 saxParser.parse(in, new DefaultHandler() {
45
46 @Override
47 public void startElement(String uri, String localName, String qName, Attributes attributes)
48 throws SAXException {
49 if ("trkpt".equals(qName)) {
50 Double latitude = Double.parseDouble(attributes.getValue("lat"));
51 Double longitude = Double.parseDouble(attributes.getValue("lon"));
52 Coordinate coordinate = new Coordinate(longitude, latitude);
53 coordinates.add(coordinate);
54 }
55 }
56
57 });
58 // close the line string
59 coordinates.add(coordinates.get(0));
60
61 Polygon polygon = geometryFactory.createPolygon(coordinates.toArray(new Coordinate[coordinates.size()]));
62 featureBuilder.add(polygon);
63 SimpleFeature area = featureBuilder.buildFeature(null);
64 return area;
65 } catch (ParserConfigurationException | SAXException | SchemaException e) {
66 throw new RuntimeException("Cannot convert GPX", e);
67 }
68 }
69
70 public static void writeGeoShapeAsGpx(String geoShape, OutputStream out) throws IOException {
71 Objects.requireNonNull(geoShape);
72 Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
73 writer.append("<gpx><trk><trkseg>");
74 StringTokenizer stSeg = new StringTokenizer(geoShape.trim(), ";");
75 while (stSeg.hasMoreTokens()) {
76 StringTokenizer stPt = new StringTokenizer(stSeg.nextToken().trim(), " ");
77 String lat = stPt.nextToken();
78 String lng = stPt.nextToken();
79 String alt = stPt.nextToken();
80 // String precision = stPt.nextToken();
81 writer.append("<trkpt");
82 writer.append(" lat=\"").append(lat).append('\"');
83 writer.append(" lon=\"").append(lng).append('\"');
84 if (!alt.equals("0.0")) {
85 writer.append('>');
86 writer.append("<ele>").append(alt).append("</ele>");
87 writer.append("</trkpt>");
88 } else {
89 writer.append("/>");
90 }
91 }
92 writer.append("</trkseg></trk></gpx>");
93 writer.flush();
94 }
95
96 /** Singleton. */
97 private GpxUtils() {
98 }
99 }