Skip to content

Commit

Permalink
Issue #47 - Adds the track description into KML, GPX, and TXT files
Browse files Browse the repository at this point in the history
  • Loading branch information
GrazianoCapelli committed Apr 29, 2021
1 parent f509640 commit b5ebecf
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions app/src/main/java/eu/basicairdata/graziano/gpslogger/Exporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package eu.basicairdata.graziano.gpslogger;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedWriter;
Expand All @@ -38,8 +37,8 @@

class Exporter extends Thread {

private Track track;
private ExportingTask exportingTask;
private final Track track;
private final ExportingTask exportingTask;
private boolean ExportKML = true;
private boolean ExportGPX = true;
private boolean ExportTXT = true;
Expand All @@ -57,6 +56,23 @@ class Exporter extends Thread {
private final AsyncGeopointsLoader asyncGeopointsLoader = new AsyncGeopointsLoader();


private String stringToXML(String str) {
if (str == null) return "";
return str.replace("<","&lt;")
.replace("&","&amp;")
.replace(">","&gt;")
.replace("\"","&quot;")
.replace("'","&apos;");
}


private String stringToCDATA(String str) {
if (str == null) return "";
return str.replace("[","(")
.replace("]",")");
}


public Exporter(ExportingTask exportingTask, boolean ExportKML, boolean ExportGPX, boolean ExportTXT, String SaveIntoFolder) {
this.exportingTask = exportingTask;
this.exportingTask.setNumberOfPoints_Processed(0);
Expand Down Expand Up @@ -247,8 +263,8 @@ public void run() {
KMLbw.write("<kml xmlns=\"http://www.opengis.net/kml/2.2\">" + newLine);
KMLbw.write(" <Document>" + newLine);
KMLbw.write(" <name>GPS Logger " + track.getName() + "</name>" + newLine);
KMLbw.write(" <description><![CDATA[" + String.valueOf(track.getNumberOfLocations()) + " Trackpoints<br>" +
String.valueOf(track.getNumberOfPlacemarks()) + " Placemarks]]></description>" + newLine);
KMLbw.write(" <description><![CDATA[" + (track.getDescription().isEmpty() ? "" : stringToCDATA(track.getDescription()) + newLine));
KMLbw.write(String.valueOf(track.getNumberOfLocations()) + " Trackpoints + " + String.valueOf(track.getNumberOfPlacemarks()) + " Placemarks]]></description>" + newLine);
if (track.getNumberOfLocations() > 0) {
KMLbw.write(" <Style id=\"TrackStyle\">" + newLine);
KMLbw.write(" <LineStyle>" + newLine);
Expand Down Expand Up @@ -326,6 +342,7 @@ public void run() {
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + newLine
+ " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">" + newLine);
GPXbw.write("<name>GPS Logger " + track.getName() + "</name>" + newLine);
GPXbw.write("<desc>" + stringToXML(track.getDescription()) + "</desc>" + newLine);
GPXbw.write("<time>" + dfdtGPX_NoMillis.format(creationTime) + "</time>" + newLine + newLine);
}
if (getPrefGPXVersion == GPX1_1) { // GPX 1.1
Expand All @@ -339,6 +356,7 @@ public void run() {
// + " xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\">" + newLine); //
GPXbw.write("<metadata> " + newLine); // GPX Metadata
GPXbw.write(" <name>GPS Logger " + track.getName() + "</name>" + newLine);
GPXbw.write(" <desc>" + stringToXML(track.getDescription()) + "</desc>" + newLine);
GPXbw.write(" <time>" + dfdtGPX_NoMillis.format(creationTime) + "</time>" + newLine);
GPXbw.write("</metadata>" + newLine + newLine);
}
Expand Down Expand Up @@ -382,12 +400,7 @@ public void run() {
if (ExportKML) {
KMLbw.write(" <Placemark id=\"" + placemark_id + "\">" + newLine);
KMLbw.write(" <name>");
KMLbw.write(loc.getDescription()
.replace("<","&lt;")
.replace("&","&amp;")
.replace(">","&gt;")
.replace("\"","&quot;")
.replace("'","&apos;"));
KMLbw.write(stringToXML(loc.getDescription()));
KMLbw.write("</name>" + newLine);
KMLbw.write(" <styleUrl>#PlacemarkStyle</styleUrl>" + newLine);
KMLbw.write(" <Point>" + newLine);
Expand Down Expand Up @@ -419,12 +432,7 @@ public void run() {
dfdtGPX.format(loc.getLocation().getTime()));
GPXbw.write("</time>");
GPXbw.write("<name>"); // Name
GPXbw.write(loc.getDescription()
.replace("<","&lt;")
.replace("&","&amp;")
.replace(">","&gt;")
.replace("\"","&quot;")
.replace("'","&apos;"));
GPXbw.write(stringToXML(loc.getDescription()));
GPXbw.write("</name>");
if (loc.getNumberOfSatellitesUsedInFix() > 0) { // Satellites used in fix
GPXbw.write("<sat>");
Expand Down Expand Up @@ -509,7 +517,8 @@ public void run() {
phdAltitudeGap = phdformatter.format(track.getEstimatedAltitudeGap(GPSApp.getPrefEGM96AltitudeCorrection()),PhysicalDataFormatter.FORMAT_ALTITUDE);
phdOverallDirection = phdformatter.format(track.getBearing(),PhysicalDataFormatter.FORMAT_BEARING);

String TrackDesc = GPSApp.getApplicationContext().getString(R.string.distance) + " = " + phdDistance.Value + " " + phdDistance.UM +
String TrackDesc = (track.getDescription().isEmpty() ? "" : "<b>" + stringToCDATA(track.getDescription()) + "</b><br><br>")
+ GPSApp.getApplicationContext().getString(R.string.distance) + " = " + phdDistance.Value + " " + phdDistance.UM +
"<br>" + GPSApp.getApplicationContext().getString(R.string.duration) + " = " + phdDuration.Value + " | " + phdDurationMoving.Value +
"<br>" + GPSApp.getApplicationContext().getString(R.string.altitude_gap) + " = " + phdAltitudeGap.Value + " " + phdAltitudeGap.UM +
"<br>" + GPSApp.getApplicationContext().getString(R.string.max_speed) + " = " + phdSpeedMax.Value + " " + phdSpeedMax.UM +
Expand Down Expand Up @@ -621,7 +630,8 @@ public void run() {
TXTbw.write(String.valueOf(loc.getNumberOfSatellites()));
TXTbw.write(",");
if (TXTFirstTrackpointFlag) { // First trackpoint of the track: add the description
TXTbw.write(track.getName() + ",GPS Logger: " + track.getName());
if (track.getDescription().isEmpty()) TXTbw.write(track.getName() + ",GPS Logger: " + track.getName());
else TXTbw.write(track.getName() + ",GPS Logger: " + track.getName() + " - " + track.getDescription().replace(",", "_"));
TXTFirstTrackpointFlag = false;
} else TXTbw.write(",");
TXTbw.write(newLine);
Expand Down

0 comments on commit b5ebecf

Please sign in to comment.