1 package org
.argeo
.app
.geo
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.io
.OutputStream
;
6 import java
.io
.OutputStreamWriter
;
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
;
14 import javax
.xml
.parsers
.ParserConfigurationException
;
15 import javax
.xml
.parsers
.SAXParser
;
16 import javax
.xml
.parsers
.SAXParserFactory
;
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
;
31 /** Utilities around the GPX format. */
32 public class GpxUtils
{
34 public static SimpleFeature
parseGpxToPolygon(InputStream in
) throws IOException
{
36 final SimpleFeatureType TYPE
= DataUtilities
.createType("Area", "the_geom:Polygon:srid=4326");
37 SimpleFeatureBuilder featureBuilder
= new SimpleFeatureBuilder(TYPE
);
39 GeometryFactory geometryFactory
= JTSFactoryFinder
.getGeometryFactory();
40 List
<Coordinate
> coordinates
= new ArrayList
<>();
41 SAXParserFactory factory
= SAXParserFactory
.newInstance();
42 SAXParser saxParser
= factory
.newSAXParser();
44 saxParser
.parse(in
, new DefaultHandler() {
47 public void startElement(String uri
, String localName
, String qName
, Attributes attributes
)
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
);
58 // close the line string
59 coordinates
.add(coordinates
.get(0));
61 Polygon polygon
= geometryFactory
.createPolygon(coordinates
.toArray(new Coordinate
[coordinates
.size()]));
62 featureBuilder
.add(polygon
);
63 SimpleFeature area
= featureBuilder
.buildFeature(null);
65 } catch (ParserConfigurationException
| SAXException
| SchemaException e
) {
66 throw new RuntimeException("Cannot convert GPX", e
);
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")) {
86 writer
.append("<ele>").append(alt
).append("</ele>");
87 writer
.append("</trkpt>");
92 writer
.append("</trkseg></trk></gpx>");