Skip to content

Commit

Permalink
Allow import from GeoImageLayers
Browse files Browse the repository at this point in the history
The import action opens a dialog where you can choose from three options: import files, a directory or from a layer with geotagged images.
The download action is now also accessible when no OSM data layer is open.
  • Loading branch information
floscher committed May 1, 2017
1 parent 81056da commit 3547acd
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 163 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapillary;

import java.io.File;
import java.io.IOException;

import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

import org.apache.commons.jcs.access.CacheAccess;
import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
import org.openstreetmap.josm.data.cache.JCSCacheManager;
import org.openstreetmap.josm.gui.MainMenu;
import org.openstreetmap.josm.gui.MapFrame;
import org.openstreetmap.josm.gui.MapView;
Expand All @@ -22,7 +18,6 @@
import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryDownloadViewAction;
import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryExportAction;
import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryImportAction;
import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryImportIntoSequenceAction;
import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryJoinAction;
import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryUploadAction;
import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryWalkAction;
Expand Down Expand Up @@ -53,12 +48,9 @@ public class MapillaryPlugin extends Plugin {

private static final MapillaryDownloadAction DOWNLOAD_ACTION = new MapillaryDownloadAction();
private static final MapillaryExportAction EXPORT_ACTION = new MapillaryExportAction();
/** Import action */
private static final MapillaryImportAction IMPORT_ACTION = new MapillaryImportAction();
/** Zoom action */
private static final MapillaryZoomAction ZOOM_ACTION = new MapillaryZoomAction();
private static final MapillaryDownloadViewAction DOWNLOAD_VIEW_ACTION = new MapillaryDownloadViewAction();
private static final MapillaryImportIntoSequenceAction IMPORT_INTO_SEQUENCE_ACTION = new MapillaryImportIntoSequenceAction();
private static final MapillaryJoinAction JOIN_ACTION = new MapillaryJoinAction();
/** Walk action */
private static final MapillaryWalkAction WALK_ACTION = new MapillaryWalkAction();
Expand All @@ -67,14 +59,10 @@ public class MapillaryPlugin extends Plugin {

/** Menu button for the {@link MapillaryExportAction} action. */
private static final JMenuItem EXPORT_MENU;
/** Menu button for the {@link MapillaryImportAction} action. */
private static final JMenuItem IMPORT_MENU;
/** Menu button for the {@link MapillaryZoomAction} action. */
private static final JMenuItem ZOOM_MENU;
/** Menu button for the {@link MapillaryDownloadViewAction} action. */
private static final JMenuItem DOWNLOAD_VIEW_MENU;
/** Menu button for the {@link MapillaryImportIntoSequenceAction} action. */
private static final JMenuItem IMPORT_INTO_SEQUENCE_MENU;
/** Menu button for the {@link MapillaryJoinAction} action. */
private static final JMenuItem JOIN_MENU;
/** Menu button for the {@link MapillaryWalkAction} action. */
Expand All @@ -85,25 +73,20 @@ public class MapillaryPlugin extends Plugin {
static {
if (Main.main == null) {
EXPORT_MENU = null;
IMPORT_MENU = null;
ZOOM_MENU = null;
DOWNLOAD_VIEW_MENU = null;
IMPORT_INTO_SEQUENCE_MENU = null;
JOIN_MENU = null;
WALK_MENU = null;
UPLOAD_MENU = null;
} else {
EXPORT_MENU = MainMenu.add(Main.main.menu.fileMenu, EXPORT_ACTION, false, 14);
EXPORT_MENU.setEnabled(false);
MainMenu.add(Main.main.menu.imagerySubMenu, DOWNLOAD_ACTION, false);
IMPORT_MENU = MainMenu.add(Main.main.menu.fileMenu, IMPORT_ACTION, false, 14);
IMPORT_MENU.setEnabled(false);
MainMenu.add(Main.main.menu.fileMenu, new MapillaryImportAction(), false, 14);
ZOOM_MENU = MainMenu.add(Main.main.menu.viewMenu, ZOOM_ACTION, false, 15);
ZOOM_MENU.setEnabled(false);
DOWNLOAD_VIEW_MENU = MainMenu.add(Main.main.menu.fileMenu, DOWNLOAD_VIEW_ACTION, false, 14);
DOWNLOAD_VIEW_MENU.setEnabled(false);
IMPORT_INTO_SEQUENCE_MENU = MainMenu.add(Main.main.menu.fileMenu, IMPORT_INTO_SEQUENCE_ACTION, false, 14);
IMPORT_INTO_SEQUENCE_MENU.setEnabled(false);
JOIN_MENU = MainMenu.add(Main.main.menu.dataMenu, JOIN_ACTION, false);
JOIN_MENU.setEnabled(false);
WALK_MENU = MainMenu.add(Main.main.menu.moreToolsMenu, WALK_ACTION, false);
Expand Down Expand Up @@ -217,8 +200,6 @@ public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
if (MapillaryDownloader.getMode() == DOWNLOAD_MODE.MANUAL_ONLY) {
setMenuEnabled(DOWNLOAD_VIEW_MENU, true);
}
setMenuEnabled(IMPORT_MENU, true);
setMenuEnabled(IMPORT_INTO_SEQUENCE_MENU, true);
}
if (oldFrame != null && newFrame == null) { // map frame destroyed
MapillaryMainDialog.destroyInstance();
Expand All @@ -227,8 +208,6 @@ public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
MapillaryFilterDialog.destroyInstance();
ImageInfoPanel.destroyInstance();
setMenuEnabled(DOWNLOAD_VIEW_MENU, false);
setMenuEnabled(IMPORT_MENU, false);
setMenuEnabled(IMPORT_INTO_SEQUENCE_MENU, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,121 +5,54 @@

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentSkipListSet;

import javax.swing.JFileChooser;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.actions.JosmAction;
import org.openstreetmap.josm.plugins.mapillary.MapillaryAbstractImage;
import org.openstreetmap.josm.plugins.mapillary.MapillaryPlugin;
import org.openstreetmap.josm.plugins.mapillary.gui.dialog.ImportMethodDialog;
import org.openstreetmap.josm.plugins.mapillary.history.MapillaryRecord;
import org.openstreetmap.josm.plugins.mapillary.history.commands.CommandImport;
import org.openstreetmap.josm.plugins.mapillary.utils.ImageImportUtil;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryProperties;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryUtils;
import org.openstreetmap.josm.tools.ImageProvider;
import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
import org.openstreetmap.josm.tools.Shortcut;

/**
* Imports a set of picture files into JOSM. They must be in jpg or png format.
*
* @author nokutu
*
*/
public class MapillaryImportAction extends JosmAction {
private static final long serialVersionUID = 4086660991261961490L;

public MapillaryImportAction() {
this(
tr("Import pictures"),
tr("Import local pictures"),
"Import Mapillary",
tr("Import pictures into Mapillary layer"),
"mapillaryImport"
);
}
private static final long serialVersionUID = -6902666084216980921L;

/**
* Convenience constructor, which calls the super-constructor and disables the action.
* @param name the name of the {@link JosmAction}, as displayed in the menu
* @param tooltip a longer description of the {@link JosmAction}, that is displayed as tooltip
* @param shortcutId an ID for the {@link Shortcut} as described in
* {@link Shortcut#registerShortcut(String, String, int, int)} (the passage about the second parameter)
* @param shortcutName a name for the shortcut (displayed to the user, so use a self-explanatory and translated
* {@link String}). See the description of the second parameter in
* {@link Shortcut#registerShortcut(String, String, int, int)}
* @param toolbarId identifier for the toolbar preferences
* @see JosmAction#JosmAction(String, String, String, Shortcut, boolean, String, boolean)
* @see Shortcut#registerShortcut(String, String, int, int)
*/
protected MapillaryImportAction(
String name, String tooltip, String shortcutId, String shortcutName, String toolbarId
) {
public MapillaryImportAction() {
super(
name,
tr("Import pictures"),
new ImageProvider(MapillaryPlugin.LOGO).setSize(ImageSizes.DEFAULT),
tooltip,
Shortcut.registerShortcut(shortcutId, shortcutName, KeyEvent.CHAR_UNDEFINED, Shortcut.NONE),
tr("Import local pictures"),
Shortcut.registerShortcut("Import Mapillary", tr("Import pictures into Mapillary layer"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE),
false,
toolbarId,
"mapillaryImport",
false
);
setEnabled(false);
}

/**
* Shows the {@link JFileChooser} for selecting images and loads the chosen images into a {@link List}.
* @return A {@link List} containing the loaded {@link MapillaryAbstractImage}s (might be empty but never null).
*/
public List<MapillaryAbstractImage> chooseImages() {
final JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(MapillaryProperties.START_DIR.get()));
chooser.setDialogTitle(tr("Select pictures"));
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setAcceptAllFileFilterUsed(false);
chooser.addChoosableFileFilter(ImageImportUtil.IMAGE_FILE_FILTER);
chooser.setMultiSelectionEnabled(true);

final List<MapillaryAbstractImage> images = new ArrayList<>();

if (chooser.showOpenDialog(Main.parent) == JFileChooser.APPROVE_OPTION) {
for (File file : chooser.getSelectedFiles()) {
try {
images.addAll(ImageImportUtil.readImagesFrom(
file,
Main.map.mapView.getProjection().eastNorth2latlon(Main.map.mapView.getCenter())
));
} catch (IOException e) {
Main.error("Could not read image(s) from "+file.getAbsolutePath());
}
}
if (chooser.getSelectedFiles().length >= 1) {
final File lastSelectedFile = chooser.getSelectedFiles()[chooser.getSelectedFiles().length - 1];
MapillaryProperties.START_DIR.put(
lastSelectedFile.getParent() == null ? lastSelectedFile.getAbsolutePath() : lastSelectedFile.getParent()
);
}
}
return images;
}

/**
* Records in the history, that the given images were loaded
* @param addedImages the images that have been loaded
*/
public void recordChanges(List<MapillaryAbstractImage> addedImages) {
public static void recordChanges(List<MapillaryAbstractImage> addedImages) {
MapillaryRecord.getInstance().addCommand(new CommandImport(new ConcurrentSkipListSet<>(addedImages)));
MapillaryUtils.showAllPictures();
}

@Override
public void actionPerformed(ActionEvent event) {
recordChanges(chooseImages());
new ImportMethodDialog(Main.parent).setVisible(true);
}
}

This file was deleted.

Loading

0 comments on commit 3547acd

Please sign in to comment.