X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Fsave%2FGpxExporter.java;h=19007ac920dc33c45c2522def2adb648ea1368c7;hp=a7f2823c42a2d5dfbd6b5766a4ec5fd619ed3262;hb=4d5796d02a15808311c09448d79e6e7d1de9d636;hpb=649c5da6ee1bbc590699e11a92316ece2ea8512d diff --git a/tim/prune/save/GpxExporter.java b/tim/prune/save/GpxExporter.java index a7f2823..19007ac 100644 --- a/tim/prune/save/GpxExporter.java +++ b/tim/prune/save/GpxExporter.java @@ -278,6 +278,11 @@ public class GpxExporter extends GenericFunction implements Runnable */ public void run() { + // Instantiate source file cachers in case we want to copy output + GpxCacherList gpxCachers = null; + if (_copySourceCheckbox.isSelected()) { + gpxCachers = new GpxCacherList(_trackInfo.getFileInfo()); + } OutputStreamWriter writer = null; try { @@ -291,7 +296,7 @@ public class GpxExporter extends GenericFunction implements Runnable _pointTypeSelector.getJustSelection(), _timestampsCheckbox.isSelected()}; // write file final int numPoints = exportData(writer, _trackInfo, _nameField.getText(), - _descriptionField.getText(), saveFlags, _copySourceCheckbox.isSelected()); + _descriptionField.getText(), saveFlags, gpxCachers); // close file writer.close(); @@ -331,32 +336,21 @@ public class GpxExporter extends GenericFunction implements Runnable * @param inName name of track (optional) * @param inDesc description of track (optional) * @param inSaveFlags array of booleans to export tracks, waypoints, photos, audios, selection, timestamps - * @param inUseCopy true to copy source if available + * @param inGpxCachers list of Gpx cachers containing input data * @return number of points written * @throws IOException if io errors occur on write */ public static int exportData(OutputStreamWriter inWriter, TrackInfo inInfo, String inName, - String inDesc, boolean[] inSaveFlags, boolean inUseCopy) throws IOException + String inDesc, boolean[] inSaveFlags, GpxCacherList inGpxCachers) throws IOException { - // Instantiate source file cachers in case we want to copy output - GpxCacherList gpxCachers = null; - if (inUseCopy) gpxCachers = new GpxCacherList(inInfo.getFileInfo()); // Write or copy headers inWriter.write(getXmlHeaderString(inWriter)); - inWriter.write(getGpxHeaderString(gpxCachers)); + final String gpxHeader = getGpxHeaderString(inGpxCachers); + final boolean isVersion1_1 = (gpxHeader.toUpperCase().indexOf("GPX/1/1") > 0); + inWriter.write(gpxHeader); // Name field - String trackName = "GpsPruneTrack"; - if (inName != null && !inName.equals("")) - { - trackName = inName; - inWriter.write("\t"); - inWriter.write(trackName); - inWriter.write("\n"); - } - // Description field - inWriter.write("\t"); - inWriter.write((inDesc != null && !inDesc.equals(""))?inDesc:"Export from GpsPrune"); - inWriter.write("\n"); + String trackName = (inName != null && !inName.equals("")) ? inName : "GpsPruneTrack"; + writeNameAndDescription(inWriter, inName, inDesc, isVersion1_1); int i = 0; DataPoint point = null; @@ -383,8 +377,13 @@ public class GpxExporter extends GenericFunction implements Runnable // Make a wpt element for each waypoint if (point.isWaypoint() && exportWaypoints) { - String pointSource = (inUseCopy?getPointSource(gpxCachers, point):null); - if (pointSource != null) { + String pointSource = (inGpxCachers == null? null : getPointSource(inGpxCachers, point)); + if (pointSource != null) + { + // If timestamp checkbox is off, strip time + if (!exportTimestamps) { + pointSource = stripTime(pointSource); + } inWriter.write(pointSource); inWriter.write('\n'); } @@ -400,12 +399,12 @@ public class GpxExporter extends GenericFunction implements Runnable { // Output all route points (if any) numSaved += writeTrackPoints(inWriter, inInfo, exportSelection, exportTrackpoints, exportPhotos, - exportAudios, exportTimestamps, true, gpxCachers, "1\n", + exportAudios, exportTimestamps, true, inGpxCachers, "1\n", null, "\t\n"); // Output all track points, if any String trackStart = "\t" + trackName + "1\n"; numSaved += writeTrackPoints(inWriter, inInfo, exportSelection, exportTrackpoints, exportPhotos, - exportAudios, exportTimestamps, false, gpxCachers, "\n\t\n", "\t\n"); } @@ -414,6 +413,38 @@ public class GpxExporter extends GenericFunction implements Runnable } + /** + * Write the name and description according to the GPX version number + * @param inWriter writer object + * @param inName name, or null if none supplied + * @param inDesc description, or null if none supplied + * @param inIsVersion1_1 true if gpx version 1.1, false for version 1.0 + */ + private static void writeNameAndDescription(OutputStreamWriter inWriter, + String inName, String inDesc, boolean inIsVersion1_1) throws IOException + { + String desc = (inDesc != null && !inDesc.equals("")) ? inDesc : "Export from GpsPrune"; + // Position of name and description fields needs to be different for GPX1.0 and GPX1.1 + if (inIsVersion1_1) + { + // GPX 1.1 has the name and description inside a metadata tag + inWriter.write("\t\n"); + } + if (inName != null && !inName.equals("")) + { + inWriter.write("\t\t"); + inWriter.write(inName); + inWriter.write("\n"); + } + inWriter.write("\t\t"); + inWriter.write(desc); + inWriter.write("\n"); + if (inIsVersion1_1) + { + inWriter.write("\t\n"); + } + } + /** * Loop through the track outputting the relevant track points * @param inWriter writer object for output @@ -463,7 +494,12 @@ public class GpxExporter extends GenericFunction implements Runnable inWriter.write(inSegmentTag); } if (numSaved == 0) {inWriter.write(inStartTag);} - if (pointSource != null) { + if (pointSource != null) + { + // If timestamps checkbox is off, strip the time + if (!exportTimestamps) { + pointSource = stripTime(pointSource); + } inWriter.write(pointSource); inWriter.write('\n'); } @@ -606,10 +642,6 @@ public class GpxExporter extends GenericFunction implements Runnable encoding = Charset.forName(encoding).name(); } catch (Exception e) {} // ignore failure to find encoding - // Hack to fix bugs with Mac OSX (which reports MacRoman but is actually UTF-8) - if (encoding == null || encoding.toLowerCase().startsWith("macroman")) { - encoding = "UTF-8"; - } return encoding; } @@ -825,4 +857,15 @@ public class GpxExporter extends GenericFunction implements Runnable // No link available, must have been loaded from zip file - no link possible return ""; } + + + /** + * Strip the time from a GPX point source string + * @param inPointSource point source to copy + * @return point source with timestamp removed + */ + private static String stripTime(String inPointSource) + { + return inPointSource.replaceAll("", ""); + } }