| 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 |
|
|---|
| 6 | import java.awt.GridBagLayout;
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.awt.event.MouseEvent;
|
|---|
| 9 | import java.awt.event.MouseListener;
|
|---|
| 10 | import java.util.ArrayList;
|
|---|
| 11 |
|
|---|
| 12 | import javax.swing.JLabel;
|
|---|
| 13 | import javax.swing.JOptionPane;
|
|---|
| 14 | import javax.swing.JPanel;
|
|---|
| 15 | import javax.swing.JTextField;
|
|---|
| 16 |
|
|---|
| 17 | import org.openstreetmap.josm.Main;
|
|---|
| 18 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 19 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 20 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 21 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 22 |
|
|---|
| 23 | public class MenuActionGrabPlanImage extends JosmAction implements Runnable, MouseListener {
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * Action calling the wms grabber for non georeferenced images called "plan image"
|
|---|
| 27 | */
|
|---|
| 28 | private static final long serialVersionUID = 1L;
|
|---|
| 29 |
|
|---|
| 30 | public static String name = "Georeference an image";
|
|---|
| 31 |
|
|---|
| 32 | private DownloadWMSPlanImage downloadWMSPlanImage;
|
|---|
| 33 | private WMSLayer wmsLayer;
|
|---|
| 34 | private int countMouseClicked = 0;
|
|---|
| 35 | private int mode = 0;
|
|---|
| 36 | private int cGetCorners = 1;
|
|---|
| 37 | private int cGetLambertCrosspieces = 2;
|
|---|
| 38 | private EastNorth ea1;
|
|---|
| 39 | private long mouseClickedTime = 0;
|
|---|
| 40 | private EastNorth georefpoint1;
|
|---|
| 41 | private EastNorth georefpoint2;
|
|---|
| 42 | /**
|
|---|
| 43 | * The time which needs to pass between two clicks during georeferencing, in milliseconds
|
|---|
| 44 | */
|
|---|
| 45 | private int initialClickDelay;
|
|---|
| 46 |
|
|---|
| 47 | public MenuActionGrabPlanImage() {
|
|---|
| 48 | super(tr(name), "cadastre_small", tr("Grab non-georeferenced image"), null, false);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | public void actionCompleted() {
|
|---|
| 52 | countMouseClicked = 0;
|
|---|
| 53 | mode = 0;
|
|---|
| 54 | mouseClickedTime = System.currentTimeMillis();
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | public void actionInterrupted() {
|
|---|
| 58 | actionCompleted();
|
|---|
| 59 | wmsLayer = null;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | @Override
|
|---|
| 63 | protected void updateEnabledState() {
|
|---|
| 64 | if (wmsLayer == null || Main.map == null || Main.map.mapView == null) return;
|
|---|
| 65 | if (countMouseClicked == 0 && mode == 0) return;
|
|---|
| 66 | for (Layer l : Main.map.mapView.getAllLayersAsList())
|
|---|
| 67 | if (l == wmsLayer)
|
|---|
| 68 | return;
|
|---|
| 69 | JOptionPane.showMessageDialog(Main.parent, tr("Georeferencing interrupted"));
|
|---|
| 70 | actionInterrupted();
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | public void actionPerformed(ActionEvent ae) {
|
|---|
| 74 | if (Main.map != null) {
|
|---|
| 75 | if (CadastrePlugin.isCadastreProjection()) {
|
|---|
| 76 | //wmsLayer = WMSDownloadAction.getLayer();
|
|---|
| 77 | wmsLayer = new MenuActionNewLocation().addNewLayer(new ArrayList<WMSLayer>());
|
|---|
| 78 | if (wmsLayer == null) return;
|
|---|
| 79 | downloadWMSPlanImage = new DownloadWMSPlanImage();
|
|---|
| 80 | downloadWMSPlanImage.download(wmsLayer);
|
|---|
| 81 | initialClickDelay = Main.pref.getInteger("cadastrewms.georef-click-delay",200);
|
|---|
| 82 | // download sub-images of the cadastre scan and join them into one single
|
|---|
| 83 | Main.worker.execute(this);
|
|---|
| 84 | } else {
|
|---|
| 85 | JOptionPane.showMessageDialog(Main.parent,
|
|---|
| 86 | tr("To enable the cadastre WMS plugin, change\n"
|
|---|
| 87 | + "the current projection to one of the cadastre\n"
|
|---|
| 88 | + "projections and retry"));
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | public void run() {
|
|---|
| 94 | // wait until plan image is fully loaded and joined into one single image
|
|---|
| 95 | boolean loadedFromCache = downloadWMSPlanImage.waitFinished();
|
|---|
| 96 | if (wmsLayer.images.size() == 1 && !loadedFromCache) {
|
|---|
| 97 | int reply = JOptionPane.CANCEL_OPTION;
|
|---|
| 98 | if (wmsLayer.isAlreadyGeoreferenced()) {
|
|---|
| 99 | reply = JOptionPane.showConfirmDialog(null,
|
|---|
| 100 | tr("This image contains georeference data.\n"+
|
|---|
| 101 | "Do you want to use them ?"),
|
|---|
| 102 | null,
|
|---|
| 103 | JOptionPane.YES_NO_OPTION);
|
|---|
| 104 | }
|
|---|
| 105 | if (reply == JOptionPane.OK_OPTION) {
|
|---|
| 106 | transformGeoreferencedImg();
|
|---|
| 107 | } else {
|
|---|
| 108 | mouseClickedTime = System.currentTimeMillis();
|
|---|
| 109 | Main.map.mapView.addMouseListener(this);
|
|---|
| 110 | if (Main.pref.getBoolean("cadastrewms.noImageCropping", false) == false)
|
|---|
| 111 | startCropping();
|
|---|
| 112 | else
|
|---|
| 113 | startGeoreferencing();
|
|---|
| 114 | }
|
|---|
| 115 | } else // action cancelled or image loaded from cache (and already georeferenced)
|
|---|
| 116 | Main.map.repaint();
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | public void mouseClicked(MouseEvent e) {
|
|---|
| 120 | if (System.currentTimeMillis() - mouseClickedTime < initialClickDelay) {
|
|---|
| 121 | System.out.println("mouse click bounce detected");
|
|---|
| 122 | return; // mouse click anti-bounce
|
|---|
| 123 | }
|
|---|
| 124 | else
|
|---|
| 125 | mouseClickedTime = System.currentTimeMillis();
|
|---|
| 126 | countMouseClicked++;
|
|---|
| 127 | EastNorth ea = Main.proj.latlon2eastNorth(Main.map.mapView.getLatLon(e.getX(), e.getY()));
|
|---|
| 128 | System.out.println("clic:"+countMouseClicked+" ,"+ea+", mode:"+mode);
|
|---|
| 129 | // ignore clicks outside the image
|
|---|
| 130 | if (ea.east() < wmsLayer.images.get(0).min.east() || ea.east() > wmsLayer.images.get(0).max.east()
|
|---|
| 131 | || ea.north() < wmsLayer.images.get(0).min.north() || ea.north() > wmsLayer.images.get(0).max.north())
|
|---|
| 132 | return;
|
|---|
| 133 | if (mode == cGetCorners) {
|
|---|
| 134 | if (countMouseClicked == 1) {
|
|---|
| 135 | ea1 = ea;
|
|---|
| 136 | continueCropping();
|
|---|
| 137 | }
|
|---|
| 138 | if (countMouseClicked == 2) {
|
|---|
| 139 | wmsLayer.cropImage(ea1, ea);
|
|---|
| 140 | Main.map.mapView.repaint();
|
|---|
| 141 | startGeoreferencing();
|
|---|
| 142 | }
|
|---|
| 143 | } else if (mode == cGetLambertCrosspieces) {
|
|---|
| 144 | if (countMouseClicked == 1) {
|
|---|
| 145 | ea1 = ea;
|
|---|
| 146 | if (inputLambertPosition())
|
|---|
| 147 | continueGeoreferencing();
|
|---|
| 148 | }
|
|---|
| 149 | if (countMouseClicked == 2) {
|
|---|
| 150 | if (inputLambertPosition()) {
|
|---|
| 151 | Main.map.mapView.removeMouseListener(this);
|
|---|
| 152 | affineTransform(ea1, ea, georefpoint1, georefpoint2);
|
|---|
| 153 | wmsLayer.saveNewCache();
|
|---|
| 154 | Main.map.mapView.repaint();
|
|---|
| 155 | actionCompleted();
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | *
|
|---|
| 163 | * @return false if all operations are canceled
|
|---|
| 164 | */
|
|---|
| 165 | private boolean startCropping() {
|
|---|
| 166 | mode = cGetCorners;
|
|---|
| 167 | countMouseClicked = 0;
|
|---|
| 168 | Object[] options = { "OK", "Cancel" };
|
|---|
| 169 | int ret = JOptionPane.showOptionDialog( null,
|
|---|
| 170 | tr("Click first corner for image cropping\n(two points required)"),
|
|---|
| 171 | tr("Image cropping"),
|
|---|
| 172 | JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
|
|---|
| 173 | null, options, options[0]);
|
|---|
| 174 | if (ret == JOptionPane.OK_OPTION) {
|
|---|
| 175 | mouseClickedTime = System.currentTimeMillis();
|
|---|
| 176 | } else
|
|---|
| 177 | if (canceledOrRestartCurrAction("image cropping"))
|
|---|
| 178 | return startCropping();
|
|---|
| 179 | return true;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | *
|
|---|
| 184 | * @return false if all operations are canceled
|
|---|
| 185 | */
|
|---|
| 186 | private boolean continueCropping() {
|
|---|
| 187 | Object[] options = { "OK", "Cancel" };
|
|---|
| 188 | int ret = JOptionPane.showOptionDialog( null,
|
|---|
| 189 | tr("Click second corner for image cropping"),
|
|---|
| 190 | tr("Image cropping"),
|
|---|
| 191 | JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
|
|---|
| 192 | null, options, options[0]);
|
|---|
| 193 | if (ret != JOptionPane.OK_OPTION) {
|
|---|
| 194 | if (canceledOrRestartCurrAction("image cropping"))
|
|---|
| 195 | return startCropping();
|
|---|
| 196 | }
|
|---|
| 197 | return true;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /**
|
|---|
| 201 | *
|
|---|
| 202 | * @return false if all operations are canceled
|
|---|
| 203 | */
|
|---|
| 204 | private boolean startGeoreferencing() {
|
|---|
| 205 | countMouseClicked = 0;
|
|---|
| 206 | mode = cGetLambertCrosspieces;
|
|---|
| 207 | Object[] options = { "OK", "Cancel" };
|
|---|
| 208 | int ret = JOptionPane.showOptionDialog( null,
|
|---|
| 209 | tr("Click first Lambert crosspiece for georeferencing\n(two points required)"),
|
|---|
| 210 | tr("Image georeferencing"),
|
|---|
| 211 | JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
|
|---|
| 212 | null, options, options[0]);
|
|---|
| 213 | if (ret == JOptionPane.OK_OPTION) {
|
|---|
| 214 | mouseClickedTime = System.currentTimeMillis();
|
|---|
| 215 | } else
|
|---|
| 216 | if (canceledOrRestartCurrAction("georeferencing"))
|
|---|
| 217 | return startGeoreferencing();
|
|---|
| 218 | return true;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /**
|
|---|
| 222 | *
|
|---|
| 223 | * @return false if all operations are canceled
|
|---|
| 224 | */
|
|---|
| 225 | private boolean continueGeoreferencing() {
|
|---|
| 226 | Object[] options = { "OK", "Cancel" };
|
|---|
| 227 | int ret = JOptionPane.showOptionDialog( null,
|
|---|
| 228 | tr("Click second Lambert crosspiece for georeferencing"),
|
|---|
| 229 | tr("Image georeferencing"),
|
|---|
| 230 | JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
|
|---|
| 231 | null, options, options[0]);
|
|---|
| 232 | if (ret != JOptionPane.OK_OPTION) {
|
|---|
| 233 | if (canceledOrRestartCurrAction("georeferencing"))
|
|---|
| 234 | return startGeoreferencing();
|
|---|
| 235 | }
|
|---|
| 236 | return true;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /**
|
|---|
| 240 | *
|
|---|
| 241 | * @return false if all operations are canceled
|
|---|
| 242 | */
|
|---|
| 243 | private boolean canceledOrRestartCurrAction(String action) {
|
|---|
| 244 | Object[] options = { "Cancel", "Retry" };
|
|---|
| 245 | int selectedValue = JOptionPane.showOptionDialog( null,
|
|---|
| 246 | tr("Do you want to cancel completely\n"+
|
|---|
| 247 | "or just retry "+action+" ?"), "",
|
|---|
| 248 | JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
|
|---|
| 249 | null, options, options[0]);
|
|---|
| 250 | if (selectedValue == 0) { // "Cancel"
|
|---|
| 251 | // remove layer
|
|---|
| 252 | Main.map.mapView.removeLayer(wmsLayer);
|
|---|
| 253 | wmsLayer = null;
|
|---|
| 254 | Main.map.mapView.removeMouseListener(this);
|
|---|
| 255 | return false;
|
|---|
| 256 | } else
|
|---|
| 257 | countMouseClicked = 0;
|
|---|
| 258 | return true;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | private boolean inputLambertPosition() {
|
|---|
| 262 | JLabel labelEnterPosition = new JLabel(tr("Enter cadastre east,north position"));
|
|---|
| 263 | JLabel labelWarning = new JLabel(tr("(Warning: verify north with arrow !!)"));
|
|---|
| 264 | JPanel p = new JPanel(new GridBagLayout());
|
|---|
| 265 | JLabel labelEast = new JLabel(tr("East"));
|
|---|
| 266 | JLabel labelNorth = new JLabel(tr("North"));
|
|---|
| 267 | final JTextField inputEast = new JTextField();
|
|---|
| 268 | final JTextField inputNorth = new JTextField();
|
|---|
| 269 | p.add(labelEnterPosition, GBC.eol());
|
|---|
| 270 | p.add(labelWarning, GBC.eol());
|
|---|
| 271 | p.add(labelEast, GBC.std().insets(0, 0, 10, 0));
|
|---|
| 272 | p.add(inputEast, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 5, 0, 5));
|
|---|
| 273 | p.add(labelNorth, GBC.std().insets(0, 0, 10, 0));
|
|---|
| 274 | p.add(inputNorth, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 5, 0, 5));
|
|---|
| 275 | JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null);
|
|---|
| 276 | String number;
|
|---|
| 277 | if (countMouseClicked == 1) number = "first";
|
|---|
| 278 | else number = "second";
|
|---|
| 279 | pane.createDialog(Main.parent, tr("Set {0} Lambert coordinates",number)).setVisible(true);
|
|---|
| 280 | if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue())) {
|
|---|
| 281 | if (canceledOrRestartCurrAction("georeferencing"))
|
|---|
| 282 | startGeoreferencing();
|
|---|
| 283 | return false;
|
|---|
| 284 | }
|
|---|
| 285 | if (inputEast.getText().length() != 0 && inputNorth.getText().length() != 0) {
|
|---|
| 286 | try {
|
|---|
| 287 | double e = Double.parseDouble(inputEast.getText());
|
|---|
| 288 | double n = Double.parseDouble(inputNorth.getText());
|
|---|
| 289 | if (countMouseClicked == 1)
|
|---|
| 290 | georefpoint1 = new EastNorth(e, n);
|
|---|
| 291 | else
|
|---|
| 292 | georefpoint2 = new EastNorth(e, n);
|
|---|
| 293 | return true;
|
|---|
| 294 | } catch (NumberFormatException e) {
|
|---|
| 295 | return false;
|
|---|
| 296 | }
|
|---|
| 297 | }
|
|---|
| 298 | return false;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | /**
|
|---|
| 302 | * Use point org1 as anchor for scale, then move org1 to dst1, then rotate org2 on dst2
|
|---|
| 303 | * around org1/dst1 anchor
|
|---|
| 304 | * @param org1 first point at original coordinate system (the grabbed image)
|
|---|
| 305 | * @param org2 second point "
|
|---|
| 306 | * @param dst1 first point at final destination coordinate system (the real east/north coordinate system)
|
|---|
| 307 | * @param dst2 second point "
|
|---|
| 308 | */
|
|---|
| 309 | private void affineTransform(EastNorth org1, EastNorth org2, EastNorth dst1, EastNorth dst2) {
|
|---|
| 310 | double angle = dst1.heading(dst2) - org1.heading(org2);
|
|---|
| 311 | double proportion = dst1.distance(dst2)/org1.distance(org2);
|
|---|
| 312 | // move
|
|---|
| 313 | double dx = dst1.getX() - org1.getX();
|
|---|
| 314 | double dy = dst1.getY() - org1.getY();
|
|---|
| 315 | wmsLayer.images.get(0).shear(dx, dy);
|
|---|
| 316 | org1 = org1.add(dx, dy); // org1=dst1 now
|
|---|
| 317 | org2 = org2.add(dx, dy);
|
|---|
| 318 | // rotate : org1(=dst1 now) is anchor for rotation and scale
|
|---|
| 319 | wmsLayer.images.get(0).rotate(dst1, angle);
|
|---|
| 320 | org2 = org2.rotate(dst1, angle);
|
|---|
| 321 | // scale image from anchor org1(=dst1 now)
|
|---|
| 322 | wmsLayer.images.get(0).scale(dst1, proportion);
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | private void transformGeoreferencedImg() {
|
|---|
| 326 | georefpoint1 = new EastNorth(wmsLayer.X0, wmsLayer.Y0);
|
|---|
| 327 | georefpoint2 = new EastNorth(wmsLayer.X0+wmsLayer.fX*wmsLayer.communeBBox.max.getX(),
|
|---|
| 328 | wmsLayer.Y0+wmsLayer.fY*wmsLayer.communeBBox.max.getX());
|
|---|
| 329 | ea1 = new EastNorth(wmsLayer.images.get(0).min.east(), wmsLayer.images.get(0).max.north());
|
|---|
| 330 | EastNorth ea2 = wmsLayer.images.get(0).max;
|
|---|
| 331 | affineTransform(ea1, ea2, georefpoint1, georefpoint2);
|
|---|
| 332 | wmsLayer.saveNewCache();
|
|---|
| 333 | Main.map.mapView.repaint();
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | public void mouseEntered(MouseEvent arg0) {
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | public void mouseExited(MouseEvent arg0) {
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | public void mousePressed(MouseEvent arg0) {
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | public void mouseReleased(MouseEvent arg0) {
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | }
|
|---|