X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fsave%2FGpxExporter.java;h=a662e2175c9e95097f28a610bdf135383df67535;hb=6814c830d470f73c7ec57c71235de333f5ea4279;hp=89d40ef2efcb2ca2bc8b8e5ab1a78e9277584878;hpb=54b9d8bc8f0025ccf97a67d9dd217ef1f9cf082f;p=GpsPrune.git diff --git a/tim/prune/save/GpxExporter.java b/tim/prune/save/GpxExporter.java index 89d40ef..a662e21 100644 --- a/tim/prune/save/GpxExporter.java +++ b/tim/prune/save/GpxExporter.java @@ -13,28 +13,33 @@ import java.io.OutputStreamWriter; import java.io.Writer; import javax.swing.BorderFactory; +import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JFileChooser; +import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import tim.prune.App; -import tim.prune.Config; import tim.prune.GenericFunction; import tim.prune.GpsPruner; import tim.prune.I18nManager; import tim.prune.UpdateMessageBroker; +import tim.prune.config.Config; import tim.prune.data.Altitude; import tim.prune.data.Coordinate; import tim.prune.data.DataPoint; +import tim.prune.data.Field; import tim.prune.data.Timestamp; -import tim.prune.data.Track; +import tim.prune.data.TrackInfo; import tim.prune.load.GenericFileFilter; +import tim.prune.save.xml.GpxCacherList; + /** * Class to export track information @@ -42,16 +47,15 @@ import tim.prune.load.GenericFileFilter; */ public class GpxExporter extends GenericFunction implements Runnable { - private Track _track = null; + private TrackInfo _trackInfo = null; private JDialog _dialog = null; private JTextField _nameField = null; private JTextField _descriptionField = null; + private PointTypeSelector _pointTypeSelector = null; private JCheckBox _timestampsCheckbox = null; - private JFileChooser _fileChooser = null; + private JCheckBox _copySourceCheckbox = null; private File _exportFile = null; - /** version number of Gpx */ - private static final String GPX_VERSION_NUMBER = "1.0"; /** this program name */ private static final String GPX_CREATOR = "Prune v" + GpsPruner.VERSION_NUMBER + " activityworkshop.net"; @@ -63,7 +67,7 @@ public class GpxExporter extends GenericFunction implements Runnable public GpxExporter(App inApp) { super(inApp); - _track = inApp.getTrackInfo().getTrack(); + _trackInfo = inApp.getTrackInfo(); } /** Get name key */ @@ -85,6 +89,7 @@ public class GpxExporter extends GenericFunction implements Runnable _dialog.getContentPane().add(makeDialogComponents()); _dialog.pack(); } + _pointTypeSelector.init(_app.getTrackInfo()); _dialog.setVisible(true); } @@ -109,10 +114,19 @@ public class GpxExporter extends GenericFunction implements Runnable _descriptionField = new JTextField(10); descPanel.add(_descriptionField); mainPanel.add(descPanel); - // checkbox for timestamps + mainPanel.add(Box.createVerticalStrut(5)); + // point type selection (track points, waypoints, photo points) + _pointTypeSelector = new PointTypeSelector(); + mainPanel.add(_pointTypeSelector); + // checkboxes for timestamps and copying + JPanel checkPanel = new JPanel(); _timestampsCheckbox = new JCheckBox(I18nManager.getText("dialog.exportgpx.includetimestamps")); _timestampsCheckbox.setSelected(true); - mainPanel.add(_timestampsCheckbox); + checkPanel.add(_timestampsCheckbox); + _copySourceCheckbox = new JCheckBox(I18nManager.getText("dialog.exportgpx.copysource")); + _copySourceCheckbox.setSelected(true); + checkPanel.add(_copySourceCheckbox); + mainPanel.add(checkPanel); dialogPanel.add(mainPanel, BorderLayout.CENTER); // button panel at bottom @@ -147,26 +161,47 @@ public class GpxExporter extends GenericFunction implements Runnable */ private void startExport() { - // OK pressed, so choose output file - if (_fileChooser == null) + // OK pressed, so check selections + if (!_pointTypeSelector.getAnythingSelected()) { + JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.save.notypesselected"), + I18nManager.getText("dialog.saveoptions.title"), JOptionPane.WARNING_MESSAGE); + return; + } + // Choose output file + File saveFile = chooseGpxFile(_parentFrame); + if (saveFile != null) { - _fileChooser = new JFileChooser(); - _fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); - _fileChooser.setFileFilter(new GenericFileFilter("filetype.gpx", new String[] {"gpx"})); - _fileChooser.setAcceptAllFileFilterUsed(false); - // start from directory in config which should be set - File configDir = Config.getWorkingDirectory(); - if (configDir != null) {_fileChooser.setCurrentDirectory(configDir);} + // New file or overwrite confirmed, so initiate export in separate thread + _exportFile = saveFile; + new Thread(this).start(); } + } + + /** + * Select a GPX file to save to + * @param inParentFrame parent frame for file chooser dialog + * @return selected File, or null if selection cancelled + */ + public static File chooseGpxFile(JFrame inParentFrame) + { + File saveFile = null; + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); + fileChooser.setFileFilter(new GenericFileFilter("filetype.gpx", new String[] {"gpx"})); + fileChooser.setAcceptAllFileFilterUsed(false); + // start from directory in config which should be set + String configDir = Config.getConfigString(Config.KEY_TRACK_DIR); + if (configDir != null) {fileChooser.setCurrentDirectory(new File(configDir));} + // Allow choose again if an existing file is selected boolean chooseAgain = false; do { chooseAgain = false; - if (_fileChooser.showSaveDialog(_parentFrame) == JFileChooser.APPROVE_OPTION) + if (fileChooser.showSaveDialog(inParentFrame) == JFileChooser.APPROVE_OPTION) { // OK pressed and file chosen - File file = _fileChooser.getSelectedFile(); + File file = fileChooser.getSelectedFile(); // Check file extension if (!file.getName().toLowerCase().endsWith(".gpx")) { @@ -174,25 +209,25 @@ public class GpxExporter extends GenericFunction implements Runnable } // Check if file exists and if necessary prompt for overwrite Object[] buttonTexts = {I18nManager.getText("button.overwrite"), I18nManager.getText("button.cancel")}; - if (!file.exists() || JOptionPane.showOptionDialog(_parentFrame, + if (!file.exists() || JOptionPane.showOptionDialog(inParentFrame, I18nManager.getText("dialog.save.overwrite.text"), I18nManager.getText("dialog.save.overwrite.title"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1]) == JOptionPane.YES_OPTION) { - // New file or overwrite confirmed, so initiate export in separate thread - _exportFile = file; - new Thread(this).start(); + // new file or overwrite confirmed + saveFile = file; } else { + // file exists and overwrite cancelled - select again chooseAgain = true; } } } while (chooseAgain); + return saveFile; } - /** * Run method for controlling separate thread for exporting */ @@ -203,14 +238,17 @@ public class GpxExporter extends GenericFunction implements Runnable { // normal writing to file writer = new OutputStreamWriter(new FileOutputStream(_exportFile)); + boolean[] saveFlags = {_pointTypeSelector.getTrackpointsSelected(), _pointTypeSelector.getWaypointsSelected(), + _pointTypeSelector.getPhotopointsSelected(), _pointTypeSelector.getJustSelection(), + _timestampsCheckbox.isSelected()}; // write file - int numPoints = exportData(writer, _track, _nameField.getText(), - _descriptionField.getText(), _timestampsCheckbox.isSelected()); + final int numPoints = exportData(writer, _trackInfo, _nameField.getText(), + _descriptionField.getText(), saveFlags, _copySourceCheckbox.isSelected()); // close file writer.close(); // Store directory in config for later - Config.setWorkingDirectory(_exportFile.getParentFile()); + Config.setConfigString(Config.KEY_TRACK_DIR, _exportFile.getParentFile().getAbsolutePath()); // Show confirmation UpdateMessageBroker.informSubscribers(I18nManager.getText("confirm.save.ok1") + " " + numPoints + " " + I18nManager.getText("confirm.save.ok2") @@ -238,23 +276,23 @@ public class GpxExporter extends GenericFunction implements Runnable /** * Export the information to the given writer * @param inWriter writer object - * @param inTrack track object containing data + * @param inInfo track info object * @param inName name of track (optional) * @param inDesc description of track (optional) - * @param inTimestamps true to export timestamps + * @param inSaveFlags array of booleans to export tracks, waypoints, photos, timestamps + * @param inUseCopy true to copy source if available * @return number of points written * @throws IOException if io errors occur on write */ - public static int exportData(OutputStreamWriter inWriter, Track inTrack, String inName, - String inDesc, boolean inTimestamps) throws IOException + public static int exportData(OutputStreamWriter inWriter, TrackInfo inInfo, String inName, + String inDesc, boolean[] inSaveFlags, boolean inUseCopy) throws IOException { - inWriter.write("\n\n"); + // 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)); // Name field String trackName = "PruneTrack"; if (inName != null && !inName.equals("")) @@ -266,58 +304,220 @@ public class GpxExporter extends GenericFunction implements Runnable } // Description field inWriter.write("\t"); - if (inDesc != null && !inDesc.equals("")) { - inWriter.write(inDesc); - } - else - { - inWriter.write("Export from Prune"); - } + inWriter.write((inDesc != null && !inDesc.equals(""))?inDesc:"Export from Prune"); inWriter.write("\n"); int i = 0; DataPoint point = null; boolean hasTrackpoints = false; + final boolean exportTrackpoints = inSaveFlags[0]; + final boolean exportWaypoints = inSaveFlags[1]; + final boolean exportPhotos = inSaveFlags[2]; + final boolean exportSelection = inSaveFlags[3]; + final boolean exportTimestamps = inSaveFlags[4]; + // Examine selection + int selStart = -1, selEnd = -1; + if (exportSelection) { + selStart = inInfo.getSelection().getStart(); + selEnd = inInfo.getSelection().getEnd(); + } // Loop over waypoints - int numPoints = inTrack.getNumPoints(); + final int numPoints = inInfo.getTrack().getNumPoints(); + int numSaved = 0; for (i=0; i=selStart && i<=selEnd)) { + // Make a wpt element for each waypoint + if (point.isWaypoint()) { + if (exportWaypoints) + { + String pointSource = (inUseCopy?getPointSource(gpxCachers, point):null); + if (pointSource != null) { + inWriter.write(pointSource); + inWriter.write('\n'); + } + else { + exportWaypoint(point, inWriter, exportTimestamps); + } + numSaved++; + } + } + else { + hasTrackpoints = true; + } } } - // Output the track, if there is one - if (hasTrackpoints) + // Export both route points and then track points + if (hasTrackpoints && (exportTrackpoints || exportPhotos)) { - boolean firstPoint = true; - inWriter.write("\t" + trackName + "1\n"); - // Loop over track points - for (i=0; i1\n", null, "\t\n"); + // Output all track points, if any + String trackStart = "\t" + trackName + "1\n"; + numSaved += writeTrackPoints(inWriter, inInfo, exportSelection, exportTrackpoints, exportPhotos, + exportTimestamps, false, gpxCachers, "\n\t\n", + "\t\n"); + } + + inWriter.write("\n"); + return numSaved; + } + + /** + * Loop through the track outputting the relevant track points + * @param inWriter writer object for output + * @param inInfo track info object containing track + * @param inExportSelection true to just output current selection + * @param inExportTrackpoints true to output track points + * @param inExportPhotos true to output photo points + * @param exportTimestamps true to include timestamps in export + * @param inOnlyCopies true to only export if source can be copied + * @param inCachers list of GpxCachers + * @param inPointTag tag to match for each point + * @param inStartTag start tag to output + * @param inSegmentTag tag to output between segments (or null) + * @param inEndTag end tag to output + */ + private static int writeTrackPoints(OutputStreamWriter inWriter, + TrackInfo inInfo, boolean inExportSelection, boolean inExportTrackpoints, + boolean inExportPhotos, boolean exportTimestamps, boolean inOnlyCopies, + GpxCacherList inCachers, String inPointTag, String inStartTag, + String inSegmentTag, String inEndTag) + throws IOException + { + // Note: far too many input parameters to this method but avoids duplication + // of output functionality for writing track points and route points + int numPoints = inInfo.getTrack().getNumPoints(); + int selStart = inInfo.getSelection().getStart(); + int selEnd = inInfo.getSelection().getEnd(); + int numSaved = 0; + // Loop over track points + for (int i=0; i=selStart && i<=selEnd)) && !point.isWaypoint()) { - point = inTrack.getPoint(i); - // restart track segment if necessary - if (point.getSegmentStart() && !firstPoint) { - inWriter.write("\t\n\t\n"); - } - if (!point.isWaypoint()) { - // export the track point - exportTrackpoint(point, inWriter, inTimestamps); - firstPoint = false; + if ((point.getPhoto()==null && inExportTrackpoints) || (point.getPhoto()!=null && inExportPhotos)) + { + // get the source from the point (if any) + String pointSource = getPointSource(inCachers, point); + boolean writePoint = (pointSource != null && pointSource.toLowerCase().startsWith(inPointTag)) + || (pointSource == null && !inOnlyCopies); + if (writePoint) + { + // restart track segment if necessary + if ((numSaved > 0) && point.getSegmentStart() && (inSegmentTag != null)) { + inWriter.write(inSegmentTag); + } + if (numSaved == 0) {inWriter.write(inStartTag);} + if (pointSource != null) { + inWriter.write(pointSource); + inWriter.write('\n'); + } + else { + if (!inOnlyCopies) {exportTrackpoint(point, inWriter, exportTimestamps);} + } + numSaved++; + } } } - inWriter.write("\t\n"); } - inWriter.write("\n"); - return numPoints; + if (numSaved > 0) {inWriter.write(inEndTag);} + return numSaved; + } + + + /** + * Get the point source for the specified point + * @param inCachers list of GPX cachers to ask for source + * @param inPoint point object + * @return xml source if available, or null otherwise + */ + private static String getPointSource(GpxCacherList inCachers, DataPoint inPoint) + { + if (inCachers == null || inPoint == null) {return null;} + String source = inCachers.getSourceString(inPoint); + if (source == null || !inPoint.isModified()) {return source;} + // Point has been modified - maybe it's possible to modify the source + source = replaceGpxTags(source, "lat=\"", "\"", inPoint.getLatitude().output(Coordinate.FORMAT_DECIMAL_FORCE_POINT)); + source = replaceGpxTags(source, "lon=\"", "\"", inPoint.getLongitude().output(Coordinate.FORMAT_DECIMAL_FORCE_POINT)); + source = replaceGpxTags(source, "", "", inPoint.getAltitude().getStringValue(Altitude.Format.METRES)); + source = replaceGpxTags(source, "", inPoint.getTimestamp().getText(Timestamp.FORMAT_ISO_8601)); + if (inPoint.isWaypoint()) {source = replaceGpxTags(source, "", "", inPoint.getWaypointName());} // only for waypoints + return source; + } + + /** + * Replace the given value into the given XML string + * @param inSource source XML for point + * @param inStartTag start tag for field + * @param inEndTag end tag for field + * @param inValue value to replace between start tag and end tag + * @return modified String, or null if not possible + */ + private static String replaceGpxTags(String inSource, String inStartTag, String inEndTag, String inValue) + { + if (inSource == null) {return null;} + // Look for start and end tags within source + final int startPos = inSource.indexOf(inStartTag); + final int endPos = inSource.indexOf(inEndTag, startPos+inStartTag.length()); + if (startPos > 0 && endPos > 0) + { + String origValue = inSource.substring(startPos + inStartTag.length(), endPos); + if (inValue != null && origValue.equals(inValue)) { + // Value unchanged + return inSource; + } + else if (inValue == null || inValue.equals("")) { + // Need to delete value + return inSource.substring(0, startPos) + inSource.substring(endPos + inEndTag.length()); + } + else { + // Need to replace value + return inSource.substring(0, startPos+inStartTag.length()) + inValue + inSource.substring(endPos); + } + } + // Value not found for this field in original source + if (inValue == null || inValue.equals("")) {return inSource;} + return null; } + /** + * Get the header string for the xml document including encoding + * @param inWriter writer object + * @return header string defining encoding + */ + private static String getXmlHeaderString(OutputStreamWriter inWriter) + { + String encoding = inWriter.getEncoding(); + final String encodingUpper = encoding.toUpperCase(); + if (encodingUpper.equals("UTF8") || encodingUpper.equals("UTF-8")) { + encoding = "UTF-8"; + } + return "\n"; + } + + /** + * Get the header string for the gpx tag + * @param inCachers cacher list to ask for headers, if available + * @return header string from cachers or as default + */ + private static String getGpxHeaderString(GpxCacherList inCachers) + { + String gpxHeader = null; + if (inCachers != null) {gpxHeader = inCachers.getFirstHeader();} + if (gpxHeader == null || gpxHeader.length() < 5) + { + // Create default (1.0) header + gpxHeader = "\n"; + } + return gpxHeader + "\n"; + } /** * Export the specified waypoint into the file @@ -341,10 +541,6 @@ public class GpxExporter extends GenericFunction implements Runnable inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.Format.METRES)); inWriter.write("\n"); } - // write waypoint name after elevation - inWriter.write("\t\t"); - inWriter.write(inPoint.getWaypointName().trim()); - inWriter.write("\n"); // timestamp if available (point might have timestamp and then be turned into a waypoint) if (inPoint.hasTimestamp() && inTimestamps) { @@ -352,7 +548,22 @@ public class GpxExporter extends GenericFunction implements Runnable inWriter.write(inPoint.getTimestamp().getText(Timestamp.FORMAT_ISO_8601)); inWriter.write("\n"); } - // TODO: Include waypt type in Gpx + // write waypoint name after elevation and time + inWriter.write("\t\t"); + inWriter.write(inPoint.getWaypointName().trim()); + inWriter.write("\n"); + // write waypoint type if any + String type = inPoint.getFieldValue(Field.WAYPT_TYPE); + if (type != null) + { + type = type.trim(); + if (!type.equals("")) + { + inWriter.write("\t\t"); + inWriter.write(type); + inWriter.write("\n"); + } + } inWriter.write("\t\n"); }