Skip to content

Commit

Permalink
Added small script and improved gpx2json parser
Browse files Browse the repository at this point in the history
  • Loading branch information
nioe committed Aug 21, 2015
1 parent fa62cf0 commit b5953ed
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
20 changes: 20 additions & 0 deletions scripts/gpx2json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
#######################################################################################################################
# Constants
#######################################################################################################################
readonly TRIPLOG_TOOLS_JAR="/Users/nici/workspace/triplog/triplog-server-tools/target/triplog-server-tools-1.0-SNAPSHOT-jar-with-dependencies.jar"


if [ -z "$1" ]; then
echo "Usage: Please specify .gpx File as first prameter"
exit 1
fi

gpxFile=$1

if [ ! -f "${gpxFile}" ]; then
echo "GPX-File '${gpxFile}' not found"
exit 1
fi

java -jar ${TRIPLOG_TOOLS_JAR} ${gpxFile}
33 changes: 33 additions & 0 deletions triplog-server-tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,37 @@
<version>2.11.4</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>ch.exq.triplog.server.tool.gpxparser.GpxParser</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,66 @@ public class GpxParser {

public static final String GPX_PACKAGE = "ch.exq.triplog.server.tool.gpxparser.gpx";

public static void main(String[] args) throws JAXBException, IOException {
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("Please provide path to GPX file as first argument!");
System.exit(1);
}

File gpxFile = new File(args[0]);

JAXBContext jc = JAXBContext.newInstance(GPX_PACKAGE);
Unmarshaller unmarshaller = jc.createUnmarshaller();
if (!gpxFile.exists()) {
System.err.println("GPX File " + gpxFile.getAbsolutePath() + " could not be found!");
System.exit(2);
}

System.out.println("Converting " + gpxFile.getAbsolutePath() + " to JSON...");

GpxType root = getGpxRootNode(gpxFile);
List<GpsPoint> gpsPoints = getGpsPoints(root);
createJsonFile(gpxFile, gpsPoints);
}


GpxType root = ((JAXBElement<GpxType>) unmarshaller.unmarshal(gpxFile)).getValue();
private static GpxType getGpxRootNode(File gpxFile) {
GpxType root = null;
try {
JAXBContext jc = JAXBContext.newInstance(GPX_PACKAGE);
Unmarshaller unmarshaller = jc.createUnmarshaller();
root = ((JAXBElement<GpxType>) unmarshaller.unmarshal(gpxFile)).getValue();
} catch (JAXBException e) {
System.err.println("Could not parse GPX file. Error: " + e.getMessage());
System.exit(3);
}

return root;
}
private static List<GpsPoint> getGpsPoints(GpxType root) {
List<GpsPoint> gpsPoints = new ArrayList<>();
for (TrkType trk : root.getTrk()) {
for (TrksegType trkseg : trk.getTrkseg()) {
gpsPoints.addAll(trkseg.getTrkpt().stream().map(trkpt -> new GpsPoint(trkpt.getLat(), trkpt.getLon())).collect(Collectors.toList()));
}
}

System.out.println("Parsed " + gpsPoints.size() + " GPS points.");

return gpsPoints;
}

private static void createJsonFile(File gpxFile, List<GpsPoint> gpsPoints) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JaxbAnnotationModule());

String jsonFileName = gpxFile.getParent() + File.separator + gpxFile.getName().substring(0, gpxFile.getName().lastIndexOf(".")) + ".json";
System.out.println(jsonFileName);

mapper.writeValue(new File(jsonFileName), gpsPoints);
try {
mapper.writeValue(new File(jsonFileName), gpsPoints);
} catch (IOException e) {
System.err.println("Could not create JSON file. Error: " + e.getMessage());
System.exit(4);
}

System.out.println("Successfully created JSON file: " + jsonFileName);
}
}

0 comments on commit b5953ed

Please sign in to comment.