| 1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
|
|---|
| 2 | package cadastre_fr;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.util.ArrayList;
|
|---|
| 9 |
|
|---|
| 10 | import javax.swing.JOptionPane;
|
|---|
| 11 |
|
|---|
| 12 | import org.openstreetmap.josm.Main;
|
|---|
| 13 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 14 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 15 |
|
|---|
| 16 | public class MenuActionGrabPlanImage extends JosmAction implements Runnable {
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Action calling the wms grabber for non georeferenced images called "plan image"
|
|---|
| 20 | */
|
|---|
| 21 | private static final long serialVersionUID = 1L;
|
|---|
| 22 |
|
|---|
| 23 | public static String name = marktr("Georeference an image");
|
|---|
| 24 |
|
|---|
| 25 | private DownloadWMSPlanImage downloadWMSPlanImage;
|
|---|
| 26 | private WMSLayer wmsLayer;
|
|---|
| 27 | private RasterImageGeoreferencer rasterImageGeoreferencer;
|
|---|
| 28 |
|
|---|
| 29 | public MenuActionGrabPlanImage() {
|
|---|
| 30 | super(tr(name), "cadastre_small", tr("Grab non-georeferenced image"), null, false, "cadastrefr/grabplanimage", true);
|
|---|
| 31 | rasterImageGeoreferencer = new RasterImageGeoreferencer();
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | @Override
|
|---|
| 35 | protected void updateEnabledState() {
|
|---|
| 36 | if (wmsLayer == null || Main.map == null || Main.map.mapView == null) return;
|
|---|
| 37 | if (!rasterImageGeoreferencer.isRunning()) return;
|
|---|
| 38 | for (Layer l : Main.map.mapView.getAllLayersAsList())
|
|---|
| 39 | if (l == wmsLayer)
|
|---|
| 40 | return;
|
|---|
| 41 | JOptionPane.showMessageDialog(Main.parent, tr("Georeferencing interrupted"));
|
|---|
| 42 | rasterImageGeoreferencer.actionInterrupted();
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | public void actionPerformed(ActionEvent ae) {
|
|---|
| 46 | if (Main.map != null) {
|
|---|
| 47 | if (CadastrePlugin.isCadastreProjection()) {
|
|---|
| 48 | wmsLayer = new MenuActionNewLocation().addNewLayer(new ArrayList<WMSLayer>());
|
|---|
| 49 | if (wmsLayer == null) return;
|
|---|
| 50 | downloadWMSPlanImage = new DownloadWMSPlanImage();
|
|---|
| 51 | downloadWMSPlanImage.download(wmsLayer);
|
|---|
| 52 | // download sub-images of the cadastre scan and join them into one single
|
|---|
| 53 | Main.worker.execute(this);
|
|---|
| 54 | } else {
|
|---|
| 55 | CadastrePlugin.askToChangeProjection();
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public void run() {
|
|---|
| 61 | // wait until plan image is fully loaded and joined into one single image
|
|---|
| 62 | boolean loadedFromCache = downloadWMSPlanImage.waitFinished();
|
|---|
| 63 | if (loadedFromCache) {
|
|---|
| 64 | Main.map.repaint();
|
|---|
| 65 | } else if (wmsLayer.getImages().size() == 0) {
|
|---|
| 66 | // action canceled or image loaded from cache (and already georeferenced)
|
|---|
| 67 | rasterImageGeoreferencer.actionInterrupted();
|
|---|
| 68 | } else {
|
|---|
| 69 | int reply = JOptionPane.CANCEL_OPTION;
|
|---|
| 70 | if (wmsLayer.isAlreadyGeoreferenced()) {
|
|---|
| 71 | reply = JOptionPane.showConfirmDialog(null,
|
|---|
| 72 | tr("This image contains georeference data.\n"+
|
|---|
| 73 | "Do you want to use them ?"),
|
|---|
| 74 | null,
|
|---|
| 75 | JOptionPane.YES_NO_OPTION);
|
|---|
| 76 | }
|
|---|
| 77 | if (reply == JOptionPane.OK_OPTION) {
|
|---|
| 78 | rasterImageGeoreferencer.transformGeoreferencedImg();
|
|---|
| 79 | } else {
|
|---|
| 80 | rasterImageGeoreferencer.addListener();
|
|---|
| 81 | if (Main.pref.getBoolean("cadastrewms.noImageCropping", false) == false)
|
|---|
| 82 | rasterImageGeoreferencer.startCropping(wmsLayer);
|
|---|
| 83 | else
|
|---|
| 84 | rasterImageGeoreferencer.startGeoreferencing(wmsLayer);
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|