X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fsave%2FKmlExporter.java;h=a5a4ee26a87e0bc7d383acf70d24fbc42434f270;hb=1ee49ae3c8ef3aa2e63eadd458531e5f8bd4f92c;hp=67ad261f62a5883583c2697ba4937dee7746aabf;hpb=54b9d8bc8f0025ccf97a67d9dd217ef1f9cf082f;p=GpsPrune.git diff --git a/tim/prune/save/KmlExporter.java b/tim/prune/save/KmlExporter.java index 67ad261..a5a4ee2 100644 --- a/tim/prune/save/KmlExporter.java +++ b/tim/prune/save/KmlExporter.java @@ -1,11 +1,14 @@ package tim.prune.save; import java.awt.BorderLayout; +import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; @@ -33,22 +36,25 @@ import javax.swing.JTextField; import javax.swing.SwingConstants; import tim.prune.App; -import tim.prune.Config; import tim.prune.GenericFunction; import tim.prune.I18nManager; import tim.prune.UpdateMessageBroker; +import tim.prune.config.ColourUtils; +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.Track; import tim.prune.data.TrackInfo; +import tim.prune.gui.ColourChooser; +import tim.prune.gui.ColourPatch; import tim.prune.gui.ImageUtils; import tim.prune.load.GenericFileFilter; /** * Class to export track information - * into a specified Kml file + * into a specified Kml or Kmz file */ public class KmlExporter extends GenericFunction implements Runnable { @@ -56,18 +62,30 @@ public class KmlExporter extends GenericFunction implements Runnable private Track _track = null; private JDialog _dialog = null; private JTextField _descriptionField = null; + private PointTypeSelector _pointTypeSelector = null; private JCheckBox _altitudesCheckbox = null; private JCheckBox _kmzCheckbox = null; private JCheckBox _exportImagesCheckbox = null; + private ColourPatch _colourPatch = null; + private JLabel _progressLabel = null; private JProgressBar _progressBar = null; + private Dimension[] _imageDimensions = null; private JFileChooser _fileChooser = null; private File _exportFile = null; + private JButton _okButton = null; + private boolean _cancelPressed = false; + private ColourChooser _colourChooser = null; // Filename of Kml file within zip archive private static final String KML_FILENAME_IN_KMZ = "doc.kml"; - // Width and height of thumbnail images in Kmz - private static final int THUMBNAIL_WIDTH = 240; - private static final int THUMBNAIL_HEIGHT = 180; + // Default width and height of thumbnail images in Kmz + private static final int DEFAULT_THUMBNAIL_WIDTH = 240; + private static final int DEFAULT_THUMBNAIL_HEIGHT = 240; + // Actual selected width and height of thumbnail images in Kmz + private static int THUMBNAIL_WIDTH = 0; + private static int THUMBNAIL_HEIGHT = 0; + // Default track colour + private static final Color DEFAULT_TRACK_COLOUR = new Color(204, 0, 0); // red /** @@ -99,8 +117,12 @@ public class KmlExporter extends GenericFunction implements Runnable _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); _dialog.getContentPane().add(makeDialogComponents()); _dialog.pack(); + _colourChooser = new ColourChooser(_dialog); } enableCheckboxes(); + _descriptionField.setEnabled(true); + _okButton.setEnabled(true); + _progressLabel.setText(""); _progressBar.setVisible(false); _dialog.setVisible(true); } @@ -113,7 +135,7 @@ public class KmlExporter extends GenericFunction implements Runnable private Component makeDialogComponents() { JPanel dialogPanel = new JPanel(); - dialogPanel.setLayout(new BorderLayout()); + dialogPanel.setLayout(new BorderLayout(0, 5)); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); // Make a central panel with the text box and checkboxes @@ -122,15 +144,39 @@ public class KmlExporter extends GenericFunction implements Runnable descPanel.add(new JLabel(I18nManager.getText("dialog.exportkml.text"))); _descriptionField = new JTextField(20); descPanel.add(_descriptionField); + descPanel.setAlignmentX(Component.CENTER_ALIGNMENT); mainPanel.add(descPanel); dialogPanel.add(mainPanel, BorderLayout.CENTER); + // point type selection + _pointTypeSelector = new PointTypeSelector(); + _pointTypeSelector.setAlignmentX(Component.CENTER_ALIGNMENT); + mainPanel.add(_pointTypeSelector); + // Colour definition + Color trackColour = ColourUtils.colourFromHex(Config.getConfigString(Config.KEY_KML_TRACK_COLOUR)); + if (trackColour == null) { + trackColour = DEFAULT_TRACK_COLOUR; + } + _colourPatch = new ColourPatch(trackColour); + _colourPatch.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + _colourChooser.showDialog(_colourPatch.getBackground()); + Color colour = _colourChooser.getChosenColour(); + if (colour != null) _colourPatch.setColour(colour); + } + }); + JPanel colourPanel = new JPanel(); + colourPanel.add(new JLabel(I18nManager.getText("dialog.exportkml.trackcolour"))); + colourPanel.add(_colourPatch); + mainPanel.add(colourPanel); // Checkbox for altitude export _altitudesCheckbox = new JCheckBox(I18nManager.getText("dialog.exportkml.altitude")); _altitudesCheckbox.setHorizontalTextPosition(SwingConstants.LEFT); + _altitudesCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT); mainPanel.add(_altitudesCheckbox); // Checkboxes for kmz export and image export _kmzCheckbox = new JCheckBox(I18nManager.getText("dialog.exportkml.kmz")); _kmzCheckbox.setHorizontalTextPosition(SwingConstants.LEFT); + _kmzCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT); _kmzCheckbox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -141,29 +187,35 @@ public class KmlExporter extends GenericFunction implements Runnable mainPanel.add(_kmzCheckbox); _exportImagesCheckbox = new JCheckBox(I18nManager.getText("dialog.exportkml.exportimages")); _exportImagesCheckbox.setHorizontalTextPosition(SwingConstants.LEFT); + _exportImagesCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT); mainPanel.add(_exportImagesCheckbox); mainPanel.add(Box.createVerticalStrut(10)); + _progressLabel = new JLabel("..."); + _progressLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + mainPanel.add(_progressLabel); _progressBar = new JProgressBar(0, 100); _progressBar.setVisible(false); + _progressBar.setAlignmentX(Component.CENTER_ALIGNMENT); mainPanel.add(_progressBar); mainPanel.add(Box.createVerticalStrut(10)); // button panel at bottom JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); - JButton okButton = new JButton(I18nManager.getText("button.ok")); + _okButton = new JButton(I18nManager.getText("button.ok")); ActionListener okListener = new ActionListener() { public void actionPerformed(ActionEvent e) { startExport(); } }; - okButton.addActionListener(okListener); + _okButton.addActionListener(okListener); _descriptionField.addActionListener(okListener); - buttonPanel.add(okButton); + buttonPanel.add(_okButton); JButton cancelButton = new JButton(I18nManager.getText("button.cancel")); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { + _cancelPressed = true; _dialog.dispose(); } }); @@ -178,6 +230,7 @@ public class KmlExporter extends GenericFunction implements Runnable */ private void enableCheckboxes() { + _pointTypeSelector.init(_trackInfo); boolean hasAltitudes = _track.hasData(Field.ALTITUDE); if (!hasAltitudes) {_altitudesCheckbox.setSelected(false);} boolean hasPhotos = _trackInfo.getPhotoList() != null && _trackInfo.getPhotoList().getNumPhotos() > 0; @@ -191,23 +244,27 @@ public class KmlExporter extends GenericFunction implements Runnable */ private void startExport() { - // OK pressed, so choose output file + // OK pressed, now validate selection checkboxes + if (!_pointTypeSelector.getAnythingSelected()) { + JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.save.notypesselected"), + I18nManager.getText("dialog.saveoptions.title"), JOptionPane.WARNING_MESSAGE); + return; + } + // Choose output file if (_fileChooser == null) { _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 configDir = Config.getConfigString(Config.KEY_TRACK_DIR); + if (configDir != null) {_fileChooser.setCurrentDirectory(new File(configDir));} } String requiredExtension = null, otherExtension = null; - if (_kmzCheckbox.isSelected()) - { + if (_kmzCheckbox.isSelected()) { requiredExtension = ".kmz"; otherExtension = ".kml"; } - else - { + else { requiredExtension = ".kml"; otherExtension = ".kmz"; } _fileChooser.setAcceptAllFileFilterUsed(false); @@ -239,6 +296,7 @@ public class KmlExporter extends GenericFunction implements Runnable { // New file or overwrite confirmed, so initiate export in separate thread _exportFile = file; + _cancelPressed = false; new Thread(this).start(); } else @@ -255,12 +313,25 @@ public class KmlExporter extends GenericFunction implements Runnable */ public void run() { - // Initialise progress bar + // Disable ok button to stop second go + _okButton.setEnabled(false); + _descriptionField.setEnabled(false); + // Initialise progress indicators + _progressLabel.setText(I18nManager.getText("confirm.running")); _progressBar.setVisible(true); _progressBar.setValue(0); boolean exportToKmz = _kmzCheckbox.isSelected(); boolean exportImages = exportToKmz && _exportImagesCheckbox.isSelected(); _progressBar.setMaximum(exportImages?getNumPhotosToExport():1); + + // Determine photo thumbnail size from config + THUMBNAIL_WIDTH = Config.getConfigInt(Config.KEY_KMZ_IMAGE_WIDTH); + if (THUMBNAIL_WIDTH < DEFAULT_THUMBNAIL_WIDTH) {THUMBNAIL_WIDTH = DEFAULT_THUMBNAIL_WIDTH;} + THUMBNAIL_HEIGHT = Config.getConfigInt(Config.KEY_KMZ_IMAGE_HEIGHT); + if (THUMBNAIL_HEIGHT < DEFAULT_THUMBNAIL_HEIGHT) {THUMBNAIL_HEIGHT = DEFAULT_THUMBNAIL_HEIGHT;} + // Create array for image dimensions in case it's required + _imageDimensions = new Dimension[_track.getNumPoints()]; + OutputStreamWriter writer = null; ZipOutputStream zipOutputStream = null; try @@ -275,13 +346,22 @@ public class KmlExporter extends GenericFunction implements Runnable { // kmz requested - need zip output stream zipOutputStream = new ZipOutputStream(new FileOutputStream(_exportFile)); + // Export images into zip file too if requested + if (exportImages) + { + // Create thumbnails of each photo in turn and add to zip as images/image.jpg + // This is done first so that photo sizes are known for later + exportThumbnails(zipOutputStream); + } writer = new OutputStreamWriter(zipOutputStream); // Make an entry in the zip file for the kml file ZipEntry kmlEntry = new ZipEntry(KML_FILENAME_IN_KMZ); zipOutputStream.putNextEntry(kmlEntry); } // write file - int numPoints = exportData(writer, exportImages); + final int numPoints = exportData(writer, exportImages); + // update config with selected track colour + Config.setConfigString(Config.KEY_KML_TRACK_COLOUR, ColourUtils.makeHexCode(_colourPatch.getBackground())); // update progress bar _progressBar.setValue(1); @@ -292,18 +372,13 @@ public class KmlExporter extends GenericFunction implements Runnable writer.flush(); // Close off this entry in the zip file zipOutputStream.closeEntry(); - // Export images into zip file too if requested - if (exportImages) - { - // Create thumbnails of each photo in turn and add to zip as images/image.jpg - exportThumbnails(zipOutputStream); - } } // close file writer.close(); + _imageDimensions = null; // 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") @@ -314,7 +389,6 @@ public class KmlExporter extends GenericFunction implements Runnable } catch (IOException ioe) { - // System.out.println("Exception: " + ioe.getClass().getName() + " - " + ioe.getMessage()); try { if (writer != null) writer.close(); } @@ -337,6 +411,10 @@ public class KmlExporter extends GenericFunction implements Runnable private int exportData(OutputStreamWriter inWriter, boolean inExportImages) throws IOException { + boolean writeTrack = _pointTypeSelector.getTrackpointsSelected(); + boolean writeWaypoints = _pointTypeSelector.getWaypointsSelected(); + boolean writePhotos = _pointTypeSelector.getPhotopointsSelected(); + boolean justSelection = _pointTypeSelector.getJustSelection(); inWriter.write("\n\n\n"); inWriter.write("\t"); if (_descriptionField != null && _descriptionField.getText() != null && !_descriptionField.getText().equals("")) @@ -349,28 +427,42 @@ public class KmlExporter extends GenericFunction implements Runnable } inWriter.write("\n"); - boolean exportAltitudes = _altitudesCheckbox.isSelected(); + // Examine selection if required + int selStart = -1, selEnd = -1; + if (justSelection) { + selStart = _trackInfo.getSelection().getStart(); + selEnd = _trackInfo.getSelection().getEnd(); + } + + boolean absoluteAltitudes = _altitudesCheckbox.isSelected(); int i = 0; DataPoint point = null; boolean hasTrackpoints = false; - // Loop over waypoints boolean writtenPhotoHeader = false; - int numPoints = _track.getNumPoints(); + final int numPoints = _track.getNumPoints(); + int numSaved = 0; int photoNum = 0; + // Loop over waypoints for (i=0; i=selStart && i<=selEnd); // Make a blob for each waypoint if (point.isWaypoint()) { - exportWaypoint(point, inWriter, exportAltitudes); + if (writeWaypoints && writeCurrentPoint) + { + exportWaypoint(point, inWriter, absoluteAltitudes); + numSaved++; + } } else if (point.getPhoto() == null) { hasTrackpoints = true; } // Make a blob with description for each photo - if (point.getPhoto() != null) + // Photos have already been written so picture sizes already known + if (point.getPhoto() != null && writePhotos && writeCurrentPoint) { if (!writtenPhotoHeader) { @@ -378,20 +470,25 @@ public class KmlExporter extends GenericFunction implements Runnable writtenPhotoHeader = true; } photoNum++; - exportPhotoPoint(point, inWriter, inExportImages, photoNum, exportAltitudes); + exportPhotoPoint(point, inWriter, inExportImages, i, photoNum, absoluteAltitudes); + numSaved++; } } // Make a line for the track, if there is one - if (hasTrackpoints) + if (hasTrackpoints && writeTrack) { // Set up strings for start and end of track segment String trackStart = "\t\n\t\ttrack\n\t\t\n\t\t\n"; - if (exportAltitudes) { + if (absoluteAltitudes) { trackStart += "\t\t\t1\n\t\t\tabsolute\n"; } + else { + trackStart += "\t\t\tclampToGround\n"; + } trackStart += "\t\t\t"; String trackEnd = "\t\t\t\n\t\t\n\t"; @@ -402,47 +499,64 @@ public class KmlExporter extends GenericFunction implements Runnable for (i=0; i=selStart && i<=selEnd); + if (!point.isWaypoint() && writeCurrentPoint) { - exportTrackpoint(point, inWriter, exportAltitudes); - firstTrackpoint = false; + // start new track segment if necessary + if (point.getSegmentStart() && !firstTrackpoint) { + inWriter.write(trackEnd); + inWriter.write(trackStart); + } + if (point.getPhoto() == null) + { + exportTrackpoint(point, inWriter); + numSaved++; + firstTrackpoint = false; + } } } // end segment inWriter.write(trackEnd); } inWriter.write("\n"); - return numPoints; + return numSaved; } + /** + * Reverse the hex code for the colours for KML's stupid backwards format + * @param inCode colour code rrggbb + * @return kml code bbggrr + */ + private static String reverse(String inCode) + { + return inCode.substring(4, 6) + inCode.substring(2, 4) + inCode.substring(0, 2); + } /** * Export the specified waypoint into the file * @param inPoint waypoint to export * @param inWriter writer object - * @param inExportAltitude true to include altitude + * @param inAbsoluteAltitude true for absolute altitude * @throws IOException on write failure */ - private void exportWaypoint(DataPoint inPoint, Writer inWriter, boolean inExportAltitude) throws IOException + private void exportWaypoint(DataPoint inPoint, Writer inWriter, boolean inAbsoluteAltitude) throws IOException { inWriter.write("\t\n\t\t"); inWriter.write(inPoint.getWaypointName().trim()); inWriter.write("\n"); inWriter.write("\t\t\n"); - if (inExportAltitude && inPoint.hasAltitude()) { + if (inAbsoluteAltitude && inPoint.hasAltitude()) { inWriter.write("\t\t\tabsolute\n"); } + else { + inWriter.write("\t\t\tclampToGround\n"); + } inWriter.write("\t\t\t"); inWriter.write(inPoint.getLongitude().output(Coordinate.FORMAT_DECIMAL_FORCE_POINT)); inWriter.write(','); inWriter.write(inPoint.getLatitude().output(Coordinate.FORMAT_DECIMAL_FORCE_POINT)); inWriter.write(","); - if (inExportAltitude && inPoint.hasAltitude()) { + if (inPoint.hasAltitude()) { inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.Format.METRES)); } else { @@ -457,12 +571,13 @@ public class KmlExporter extends GenericFunction implements Runnable * @param inPoint data point including photo * @param inWriter writer object * @param inImageLink flag to set whether to export image links or not + * @param inPointNumber number of point for accessing dimensions * @param inImageNumber number of image for filename - * @param inExportAltitude true to include altitude + * @param inAbsoluteAltitude true for absolute altitudes * @throws IOException on write failure */ private void exportPhotoPoint(DataPoint inPoint, Writer inWriter, boolean inImageLink, - int inImageNumber, boolean inExportAltitude) + int inPointNumber, int inImageNumber, boolean inAbsoluteAltitude) throws IOException { inWriter.write("\t\n\t\t"); @@ -470,25 +585,27 @@ public class KmlExporter extends GenericFunction implements Runnable inWriter.write("\n"); if (inImageLink) { - // Work out image dimensions of thumbnail - Dimension picSize = inPoint.getPhoto().getSize(); - Dimension thumbSize = ImageUtils.getThumbnailSize(picSize.width, picSize.height, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT); + Dimension imageSize = _imageDimensions[inPointNumber]; // Write out some html for the thumbnail images inWriter.write("" - + "
Caption for the photo
]]>
"); + + inImageNumber + ".jpg' width='" + imageSize.width + "' height='" + imageSize.height + "'>" + + "
" + inPoint.getPhoto().getFile().getName() + "
]]>"); } inWriter.write("#camera_icon\n"); inWriter.write("\t\t\n"); - if (inExportAltitude && inPoint.hasAltitude()) { + if (inAbsoluteAltitude && inPoint.hasAltitude()) { inWriter.write("\t\t\tabsolute\n"); } + else { + inWriter.write("\t\t\tclampToGround\n"); + } inWriter.write("\t\t\t"); inWriter.write(inPoint.getLongitude().output(Coordinate.FORMAT_DECIMAL_FORCE_POINT)); inWriter.write(','); inWriter.write(inPoint.getLatitude().output(Coordinate.FORMAT_DECIMAL_FORCE_POINT)); inWriter.write(","); - if (inExportAltitude && inPoint.hasAltitude()) { + // Altitude if point has one + if (inPoint.hasAltitude()) { inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.Format.METRES)); } else { @@ -502,16 +619,15 @@ public class KmlExporter extends GenericFunction implements Runnable * Export the specified trackpoint into the file * @param inPoint trackpoint to export * @param inWriter writer object - * @param inExportAltitude true to include altitude */ - private void exportTrackpoint(DataPoint inPoint, Writer inWriter, boolean inExportAltitude) throws IOException + private void exportTrackpoint(DataPoint inPoint, Writer inWriter) throws IOException { inWriter.write(inPoint.getLongitude().output(Coordinate.FORMAT_DECIMAL_FORCE_POINT)); inWriter.write(','); inWriter.write(inPoint.getLatitude().output(Coordinate.FORMAT_DECIMAL_FORCE_POINT)); - // Altitude either absolute or locked to ground by Google Earth + // Altitude if point has one inWriter.write(","); - if (inExportAltitude && inPoint.hasAltitude()) { + if (inPoint.hasAltitude()) { inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.Format.METRES)); } else { @@ -535,14 +651,22 @@ public class KmlExporter extends GenericFunction implements Runnable } ImageWriter imageWriter = writers.next(); + // Check selection checkbox + boolean justSelection = _pointTypeSelector.getJustSelection(); + int selStart = -1, selEnd = -1; + if (justSelection) { + selStart = _trackInfo.getSelection().getStart(); + selEnd = _trackInfo.getSelection().getEnd(); + } + int numPoints = _track.getNumPoints(); DataPoint point = null; int photoNum = 0; // Loop over all points in track - for (int i=0; i=selStart && i<=selEnd))) { photoNum++; // Make a new entry in zip file @@ -552,10 +676,10 @@ public class KmlExporter extends GenericFunction implements Runnable ImageIcon icon = new ImageIcon(point.getPhoto().getFile().getAbsolutePath()); // Scale and smooth image to required size - Dimension outputSize = ImageUtils.getThumbnailSize( - point.getPhoto().getWidth(), point.getPhoto().getHeight(), - THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT); - BufferedImage bufferedImage = ImageUtils.createScaledImage(icon.getImage(), outputSize.width, outputSize.height); + BufferedImage bufferedImage = ImageUtils.rotateImage(icon.getImage(), + THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, point.getPhoto().getRotationDegrees()); + // Store image dimensions so that it doesn't have to be calculated again for the points + _imageDimensions[i] = new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()); imageWriter.setOutput(ImageIO.createImageOutputStream(inZipStream)); imageWriter.write(bufferedImage); @@ -580,8 +704,7 @@ public class KmlExporter extends GenericFunction implements Runnable for (int i=0; i