]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/save/KmlExporter.java
Version 6, October 2008
[GpsPrune.git] / tim / prune / save / KmlExporter.java
index c091d0c74ac0f705be6acd1ebd977b6010a0af86..82b4f1c12ca7cc7f679f8896bbe86555142137a0 100644 (file)
@@ -32,9 +32,10 @@ import javax.swing.JPanel;
 import javax.swing.JProgressBar;
 import javax.swing.JTextField;
 import javax.swing.SwingConstants;
-import javax.swing.filechooser.FileFilter;
 
+import tim.prune.Config;
 import tim.prune.I18nManager;
+import tim.prune.UpdateMessageBroker;
 import tim.prune.data.Altitude;
 import tim.prune.data.Coordinate;
 import tim.prune.data.DataPoint;
@@ -42,6 +43,7 @@ import tim.prune.data.Field;
 import tim.prune.data.Track;
 import tim.prune.data.TrackInfo;
 import tim.prune.gui.ImageUtils;
+import tim.prune.load.GenericFileFilter;
 
 /**
  * Class to export track information
@@ -188,19 +190,14 @@ public class KmlExporter implements Runnable
        {
                // OK pressed, so choose output file
                if (_fileChooser == null)
-                       {_fileChooser = new JFileChooser();}
-               _fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
-               _fileChooser.setFileFilter(new FileFilter() {
-                       public boolean accept(File f)
-                       {
-                               return (f != null && (f.isDirectory()
-                                       || f.getName().toLowerCase().endsWith(".kml") || f.getName().toLowerCase().endsWith(".kmz")));
-                       }
-                       public String getDescription()
-                       {
-                               return I18nManager.getText("dialog.exportkml.filetype");
-                       }
-               });
+               {
+                       _fileChooser = new JFileChooser();
+                       _fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
+                       _fileChooser.setFileFilter(new GenericFileFilter("filetype.kmlkmz", new String[] {"kml", "kmz"}));
+                       // start from directory in config which should be set
+                       File configDir = Config.getWorkingDirectory();
+                       if (configDir != null) {_fileChooser.setCurrentDirectory(configDir);}
+               }
                String requiredExtension = null, otherExtension = null;
                if (_kmzCheckbox.isSelected())
                {
@@ -302,10 +299,12 @@ public class KmlExporter implements Runnable
 
                        // close file
                        writer.close();
-                       JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.save.ok1")
-                                + " " + numPoints + " " + I18nManager.getText("dialog.save.ok2")
-                                + " " + _exportFile.getAbsolutePath(),
-                               I18nManager.getText("dialog.save.oktitle"), JOptionPane.INFORMATION_MESSAGE);
+                       // Store directory in config for later
+                       Config.setWorkingDirectory(_exportFile.getParentFile());
+                       // show confirmation
+                       UpdateMessageBroker.informSubscribers(I18nManager.getText("confirm.save.ok1")
+                                + " " + numPoints + " " + I18nManager.getText("confirm.save.ok2")
+                                + " " + _exportFile.getAbsolutePath());
                        // export successful so need to close dialog and return
                        _dialog.dispose();
                        return;
@@ -335,7 +334,6 @@ public class KmlExporter implements Runnable
        private int exportData(OutputStreamWriter inWriter, boolean inExportImages)
        throws IOException
        {
-               // TODO: Look at segments of track, and split into separate lines in Kml if necessary
                inWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://earth.google.com/kml/2.1\">\n<Folder>\n");
                inWriter.write("\t<name>");
                if (_descriptionField != null && _descriptionField.getText() != null && !_descriptionField.getText().equals(""))
@@ -383,24 +381,37 @@ public class KmlExporter implements Runnable
                // Make a line for the track, if there is one
                if (hasTrackpoints)
                {
-                       inWriter.write("\t<Placemark>\n\t\t<name>track</name>\n\t\t<Style>\n\t\t\t<LineStyle>\n"
+                       // Set up strings for start and end of track segment
+                       String trackStart = "\t<Placemark>\n\t\t<name>track</name>\n\t\t<Style>\n\t\t\t<LineStyle>\n"
                                + "\t\t\t\t<color>cc0000cc</color>\n\t\t\t\t<width>4</width>\n\t\t\t</LineStyle>\n"
                                + "\t\t\t<PolyStyle><color>33cc0000</color></PolyStyle>\n"
-                               + "\t\t</Style>\n\t\t<LineString>\n");
+                               + "\t\t</Style>\n\t\t<LineString>\n";
                        if (exportAltitudes) {
-                               inWriter.write("\t\t\t<extrude>1</extrude>\n\t\t\t<altitudeMode>absolute</altitudeMode>\n");
+                               trackStart += "\t\t\t<extrude>1</extrude>\n\t\t\t<altitudeMode>absolute</altitudeMode>\n";
                        }
-                       inWriter.write("\t\t\t<coordinates>");
+                       trackStart += "\t\t\t<coordinates>";
+                       String trackEnd = "\t\t\t</coordinates>\n\t\t</LineString>\n\t</Placemark>";
+
+                       // Start segment
+                       inWriter.write(trackStart);
                        // Loop over track points
+                       boolean firstTrackpoint = true;
                        for (i=0; i<numPoints; i++)
                        {
                                point = _track.getPoint(i);
+                               // start new track segment if necessary
+                               if (point.getSegmentStart() && !firstTrackpoint) {
+                                       inWriter.write(trackEnd);
+                                       inWriter.write(trackStart);
+                               }
                                if (!point.isWaypoint() && point.getPhoto() == null)
                                {
                                        exportTrackpoint(point, inWriter, exportAltitudes);
+                                       firstTrackpoint = false;
                                }
                        }
-                       inWriter.write("\t\t\t</coordinates>\n\t\t</LineString>\n\t</Placemark>");
+                       // end segment
+                       inWriter.write(trackEnd);
                }
                inWriter.write("</Folder>\n</kml>");
                return numPoints;
@@ -429,7 +440,7 @@ public class KmlExporter implements Runnable
                inWriter.write(inPoint.getLatitude().output(Coordinate.FORMAT_DEG_WITHOUT_CARDINAL));
                inWriter.write(",");
                if (inExportAltitude && inPoint.hasAltitude()) {
-                       inWriter.write("" + inPoint.getAltitude().getValue(Altitude.FORMAT_METRES));
+                       inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.FORMAT_METRES));
                }
                else {
                        inWriter.write("0");
@@ -475,7 +486,7 @@ public class KmlExporter implements Runnable
                inWriter.write(inPoint.getLatitude().output(Coordinate.FORMAT_DEG_WITHOUT_CARDINAL));
                inWriter.write(",");
                if (inExportAltitude && inPoint.hasAltitude()) {
-                       inWriter.write("" + inPoint.getAltitude().getValue(Altitude.FORMAT_METRES));
+                       inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.FORMAT_METRES));
                }
                else {
                        inWriter.write("0");
@@ -498,7 +509,7 @@ public class KmlExporter implements Runnable
                // Altitude either absolute or locked to ground by Google Earth
                inWriter.write(",");
                if (inExportAltitude && inPoint.hasAltitude()) {
-                       inWriter.write("" + inPoint.getAltitude().getValue(Altitude.FORMAT_METRES));
+                       inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.FORMAT_METRES));
                }
                else {
                        inWriter.write("0");