Changeset 18068 in josm for trunk/src


Ignore:
Timestamp:
2021-07-18T19:53:41+02:00 (3 years ago)
Author:
Don-vip
Message:

fix #21136 - Export GeoImage layer to GPX

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/GpxExportAction.java

    r17188 r18068  
    1717import org.openstreetmap.josm.gui.MainApplication;
    1818import org.openstreetmap.josm.gui.io.importexport.FileExporter;
     19import org.openstreetmap.josm.gui.io.importexport.GpxExporter;
    1920import org.openstreetmap.josm.gui.io.importexport.GpxImporter;
    20 import org.openstreetmap.josm.gui.layer.GpxLayer;
    2121import org.openstreetmap.josm.gui.layer.Layer;
    22 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    2322import org.openstreetmap.josm.tools.CheckParameterUtil;
    2423import org.openstreetmap.josm.tools.Shortcut;
     
    5958    /**
    6059     * Get the layer to export.
    61      * @return The layer to export, either a {@link GpxLayer} or {@link OsmDataLayer}.
     60     * @return The layer to export, if supported, otherwise {@code null}.
    6261     */
    6362    protected Layer getLayer() {
    6463        Layer layer = getLayerManager().getActiveLayer();
    65         return (layer instanceof GpxLayer || layer instanceof OsmDataLayer) ? layer : null;
     64        return GpxExporter.isSupportedLayer(layer) ? layer : null;
    6665    }
    6766
     
    8685     * Exports a layer to a file. Launches a file chooser to request the user to enter a file name.
    8786     *
    88      * <code>layer</code> must not be null. <code>layer</code> must be an instance of
    89      * {@link OsmDataLayer} or {@link GpxLayer}.
     87     * <code>layer</code> must not be null. <code>layer</code> must be of a supported type.
    9088     *
    9189     * @param layer the layer
    9290     * @throws IllegalArgumentException if layer is null
    93      * @throws IllegalArgumentException if layer is neither an instance of {@link OsmDataLayer}
    94      *  nor of {@link GpxLayer}
     91     * @throws IllegalArgumentException if layer is not of a supported type.
     92     * @see GpxExporter#isSupportedLayer
    9593     */
    9694    public void export(Layer layer) {
    9795        CheckParameterUtil.ensureParameterNotNull(layer, "layer");
    98         if (!(layer instanceof OsmDataLayer) && !(layer instanceof GpxLayer))
    99             throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer or GpxLayer. Got ''{0}''.",
    100                     layer.getClass().getName()));
     96        if (!GpxExporter.isSupportedLayer(layer))
     97            throw new IllegalArgumentException(MessageFormat.format("Expected instance of {0}. Got ''{1}''.",
     98                    GpxExporter.getSupportedLayers(), layer.getClass().getName()));
    10199
    102100        File file = createAndOpenSaveFileChooser(tr("Export GPX file"), GpxImporter.getFileFilter());
  • trunk/src/org/openstreetmap/josm/gui/io/importexport/GpxExporter.java

    r16629 r18068  
    1414import java.time.Year;
    1515import java.time.ZoneId;
     16import java.util.Arrays;
     17import java.util.List;
    1618import java.util.Optional;
    1719
     
    3335import org.openstreetmap.josm.gui.layer.Layer;
    3436import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     37import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer;
    3538import org.openstreetmap.josm.gui.widgets.JosmTextArea;
    3639import org.openstreetmap.josm.gui.widgets.JosmTextField;
     
    4750public class GpxExporter extends FileExporter implements GpxConstants {
    4851
     52    private static final List<Class<? extends Layer>> SUPPORTED_LAYERS = Arrays.asList(
     53            GpxLayer.class, OsmDataLayer.class, GeoImageLayer.class);
     54
    4955    private static final String GPL_WARNING = "<html><font color='red' size='-2'>"
    5056        + tr("Note: GPL is not compatible with the OSM license. Do not upload GPL licensed tracks.") + "</html>";
     
    7379    @Override
    7480    public boolean acceptFile(File pathname, Layer layer) {
    75         if (!(layer instanceof OsmDataLayer) && !(layer instanceof GpxLayer))
    76             return false;
    77         return super.acceptFile(pathname, layer);
     81        return isSupportedLayer(layer) ? super.acceptFile(pathname, layer) : false;
    7882    }
    7983
     
    9094    private void exportData(File file, Layer layer, boolean quiet) throws IOException {
    9195        CheckParameterUtil.ensureParameterNotNull(layer, "layer");
    92         if (!(layer instanceof OsmDataLayer) && !(layer instanceof GpxLayer))
     96        if (!isSupportedLayer(layer))
    9397            throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer or GpxLayer. Got ''{0}''.", layer
    9498                    .getClass().getName()));
     
    120124        if (layer instanceof GpxLayer) {
    121125            gpxData = ((GpxLayer) layer).data;
     126        } else if (layer instanceof GeoImageLayer) {
     127            gpxData = ((GeoImageLayer) layer).getFauxGpxData();
    122128        } else {
    123129            gpxData = new GpxData();
     
    270276    }
    271277
     278    /**
     279     * Returns the list of supported layers.
     280     * @return the list of supported layers
     281     * @since 18068
     282     */
     283    public static List<Class<? extends Layer>> getSupportedLayers() {
     284        return SUPPORTED_LAYERS;
     285    }
     286
     287    /**
     288     * Determines if the given layer is supported by this action.
     289     * @param layer layer to test
     290     * @return {@code true} if the given layer is supported by this action
     291     * @since 18068
     292     */
     293    public static boolean isSupportedLayer(Layer layer) {
     294        return SUPPORTED_LAYERS.stream().anyMatch(c -> c.isInstance(layer));
     295    }
     296
    272297    private static GpxData getGpxData(Layer layer, File file) {
    273298        if (layer instanceof OsmDataLayer) {
     
    275300        } else if (layer instanceof GpxLayer) {
    276301            return ((GpxLayer) layer).data;
     302        } else if (layer instanceof GeoImageLayer) {
     303            return ((GeoImageLayer) layer).getFauxGpxData();
    277304        }
    278305        return OsmDataLayer.toGpxData(MainApplication.getLayerManager().getEditDataSet(), file);
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/GeoImageLayer.java

    r18065 r18068  
    257257        if (ExpertToggleAction.isExpert()) {
    258258            entries.add(new EditImagesSequenceAction(this));
     259            entries.add(new LayerGpxExportAction(this));
    259260        }
    260261        entries.add(new ShowThumbnailAction(this));
Note: See TracChangeset for help on using the changeset viewer.