Ignore:
Timestamp:
2017-09-29T21:28:46+02:00 (7 years ago)
Author:
donvip
Message:

repair raster georeferencing, must be broken for years... + fix EDT violations

Location:
applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/wms
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/wms/CadastreInterface.java

    r33640 r33682  
    4343    private HttpURLConnection urlConn;
    4444
     45    private String csrfToken;
    4546    private String cookie;
    4647    private String interfaceRef;
     
    7273    static final String C_INTERFACE_VECTOR = "afficherCarteCommune.do";
    7374    static final String C_INTERFACE_RASTER_TA = "afficherCarteTa.do";
    74     static final String C_INTERFACE_RASTER_FEUILLE = "afficherCarteFeuille.do";
    75     static final String C_IMAGE_LINK_START = "<a href=\"#\" class=\"raster\" onClick=\"popup('afficherCarteFeuille.do?f=";
    76     static final String C_TA_IMAGE_LINK_START = "<a href=\"#\" class=\"raster\" onClick=\"popup('afficherCarteTa.do?f=";
     75    static final String C_INTERFACE_RASTER_FEUILLE = "afficherCarteFeuille.do?CSRF_TOKEN=";
     76    static final String C_IMAGE_LINK_START = "<a href=\"#\" class=\"raster\" onClick=\"popup('" + C_INTERFACE_RASTER_FEUILLE;
     77    static final String C_TA_IMAGE_LINK_START = "<a href=\"#\" class=\"raster\" onClick=\"popup('afficherCarteTa.do?CSRF_TOKEN=";
    7778    static final String C_IMAGE_NAME_START = ">Feuille ";
    7879    static final String C_TA_IMAGE_NAME_START = "Tableau d'assemblage <strong>";
     80
     81    static final String C_CSRF_TOKEN = "CSRF_TOKEN=";
     82    static final String C_F = "&amp;f=";
    7983
    8084    static final long COOKIE_EXPIRATION = 30 * 60 * 1000L; // 30 minutes expressed in milliseconds
     
    100104                throw new WMSException(tr("Cannot open a new client session.\nServer in maintenance or temporary overloaded."));
    101105            if (interfaceRef == null) {
    102                     getInterface(wmsLayer);
    103                     this.lastWMSLayerName = wmsLayer.getName();
     106                getInterface(wmsLayer);
     107                this.lastWMSLayerName = wmsLayer.getName();
    104108            }
    105109            openInterface();
     
    237241                        wmsLayer.setCodeCommune(listOfFeuilles.get(res).name);
    238242                        checkLayerDuplicates(wmsLayer);
    239                         interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune());
     243                        interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune(), csrfToken);
    240244                    }
    241245                }
     
    334338            urlConn.disconnect();
    335339            if (lines != null) {
    336                 if (lines.indexOf(C_IMAGE_FORMAT) != -1) {
    337                     int i = lines.indexOf(C_IMAGE_FORMAT);
    338                     int j = lines.indexOf('.', i);
    339                     wmsLayer.setRaster("image".equals(lines.substring(i+C_IMAGE_FORMAT.length(), j)));
    340                 }
    341                 if (!wmsLayer.isRaster() && lines.indexOf(C_INTERFACE_VECTOR) != -1) {  // "afficherCarteCommune.do"
     340                int i = lines.indexOf(C_IMAGE_FORMAT);
     341                if (i > -1) {
     342                    wmsLayer.setRaster("image".equals(lines.substring(i+C_IMAGE_FORMAT.length(), lines.indexOf('.', i))));
     343                }
     344                csrfToken = null;
     345                i = lines.indexOf(C_CSRF_TOKEN);
     346                if (i > -1) {
     347                    csrfToken = lines.substring(i+C_CSRF_TOKEN.length(), Math.min(lines.indexOf('"', i), lines.indexOf('&', i)));
     348                }
     349                i = lines.indexOf(C_INTERFACE_VECTOR);
     350                if (!wmsLayer.isRaster() && i != -1) {  // "afficherCarteCommune.do"
    342351                    // shall be something like: interfaceRef = "afficherCarteCommune.do?c=X2269";
    343                     lines = lines.substring(lines.indexOf(C_INTERFACE_VECTOR), lines.length());
     352                    lines = lines.substring(i, lines.length());
    344353                    lines = lines.substring(0, lines.indexOf('\''));
    345354                    lines = Entities.unescape(lines);
     
    348357                } else if (wmsLayer.isRaster() && lines.indexOf(C_INTERFACE_RASTER_TA) != -1) { // "afficherCarteTa.do"
    349358                    // list of values parsed in listOfFeuilles (list all non-georeferenced images)
    350                     lines = getFeuillesList();
     359                    lines = getFeuillesList(csrfToken);
    351360                    if (!downloadCanceled) {
    352                         parseFeuillesList(lines);
     361                        parseFeuillesList(lines, csrfToken);
    353362                        if (!listOfFeuilles.isEmpty()) {
    354363                            int res = selectFeuilleDialog();
     
    356365                                wmsLayer.setCodeCommune(listOfFeuilles.get(res).name);
    357366                                checkLayerDuplicates(wmsLayer);
    358                                 interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune());
     367                                interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune(), csrfToken);
    359368                                wmsLayer.setCodeCommune(listOfFeuilles.get(res).ref);
    360                                 lines = buildRasterFeuilleInterfaceRef(listOfFeuilles.get(res).ref);
     369                                lines = buildRasterFeuilleInterfaceRef(listOfFeuilles.get(res).ref, csrfToken);
    361370                                lines = Entities.unescape(lines);
    362371                                Logging.info("interface ref.:"+lines);
     
    368377                } else if (lines.indexOf(C_COMMUNE_LIST_START) != -1 && lines.indexOf(C_COMMUNE_LIST_END) != -1) {
    369378                    // list of values parsed in listOfCommunes
    370                     int i = lines.indexOf(C_COMMUNE_LIST_START);
    371                     int j = lines.indexOf(C_COMMUNE_LIST_END, i);
    372                     parseCommuneList(lines.substring(i, j));
     379                    i = lines.indexOf(C_COMMUNE_LIST_START);
     380                    parseCommuneList(lines.substring(i, lines.indexOf(C_COMMUNE_LIST_END, i)));
    373381                }
    374382            }
     
    400408    }
    401409
    402     private String getFeuillesList() {
     410    private String getFeuillesList(String csrfToken) {
    403411        // get all images in one html page
    404412        String ln = null;
     
    406414        HttpURLConnection urlConn2 = null;
    407415        try {
    408             URL getAllImagesURL = new URL(BASE_URL + "/scpc/listerFeuillesParcommune.do?keepVolatileSession=&offset=2000");
     416            URL getAllImagesURL = new URL(BASE_URL + "/scpc/listerFeuillesParcommune.do?CSRF_TOKEN=" +
     417                    csrfToken + "&keepVolatileSession=&offset=2000");
    409418            urlConn2 = (HttpURLConnection) getAllImagesURL.openConnection();
    410419            setCookie(urlConn2);
     
    424433    }
    425434
    426     private void parseFeuillesList(String input) {
     435    private void parseFeuillesList(String input, String csrfToken) {
    427436        listOfFeuilles.clear();
    428437        // get "Tableau d'assemblage"
     
    430439        if (Main.pref.getBoolean("cadastrewms.useTA", false)) {
    431440            while (inputTA.indexOf(C_TA_IMAGE_LINK_START) != -1) {
    432                 inputTA = inputTA.substring(inputTA.indexOf(C_TA_IMAGE_LINK_START) + C_TA_IMAGE_LINK_START.length());
     441                inputTA = inputTA.substring(inputTA.indexOf(C_TA_IMAGE_LINK_START) + C_TA_IMAGE_LINK_START.length()
     442                    + csrfToken.length() + C_F.length());
    433443                String refTA = inputTA.substring(0, inputTA.indexOf('\''));
    434444                String nameTA = inputTA.substring(inputTA.indexOf(C_TA_IMAGE_NAME_START) + C_TA_IMAGE_NAME_START.length());
     
    439449        // get "Feuilles"
    440450        while (input.indexOf(C_IMAGE_LINK_START) != -1) {
    441             input = input.substring(input.indexOf(C_IMAGE_LINK_START)+C_IMAGE_LINK_START.length());
     451            input = input.substring(input.indexOf(C_IMAGE_LINK_START)+C_IMAGE_LINK_START.length()
     452                + csrfToken.length() + C_F.length());
    442453            String refFeuille = input.substring(0, input.indexOf('\''));
    443454            String nameFeuille = input.substring(
     
    449460
    450461    private String selectMunicipalityDialog() {
    451         JPanel p = new JPanel(new GridBagLayout());
    452         String[] communeList = new String[listOfCommunes.size() + 1];
    453         communeList[0] = tr("Choose from...");
    454         for (int i = 0; i < listOfCommunes.size(); i++) {
    455             communeList[i + 1] = listOfCommunes.get(i).substring(listOfCommunes.get(i).indexOf('>')+1);
    456         }
    457         JComboBox<String> inputCommuneList = new JComboBox<>(communeList);
    458         p.add(inputCommuneList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
    459         JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null);
    460         // this below is a temporary workaround to fix the "always on top" issue
    461         JDialog dialog = pane.createDialog(Main.parent, tr("Select commune"));
    462         CadastrePlugin.prepareDialog(dialog);
    463         dialog.setVisible(true);
    464         // till here
    465         if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
    466             return null;
    467         return listOfCommunes.get(inputCommuneList.getSelectedIndex()-1);
     462        return GuiHelper.runInEDTAndWaitAndReturn(() -> {
     463            JPanel p = new JPanel(new GridBagLayout());
     464            String[] communeList = new String[listOfCommunes.size() + 1];
     465            communeList[0] = tr("Choose from...");
     466            for (int i = 0; i < listOfCommunes.size(); i++) {
     467                communeList[i + 1] = listOfCommunes.get(i).substring(listOfCommunes.get(i).indexOf('>')+1);
     468            }
     469            JComboBox<String> inputCommuneList = new JComboBox<>(communeList);
     470            p.add(inputCommuneList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
     471            JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null);
     472            // this below is a temporary workaround to fix the "always on top" issue
     473            JDialog dialog = pane.createDialog(Main.parent, tr("Select commune"));
     474            CadastrePlugin.prepareDialog(dialog);
     475            dialog.setVisible(true);
     476            // till here
     477            if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
     478                return null;
     479            return listOfCommunes.get(inputCommuneList.getSelectedIndex()-1);
     480        });
    468481    }
    469482
    470483    private int selectFeuilleDialog() {
    471         JPanel p = new JPanel(new GridBagLayout());
    472         List<String> imageNames = new ArrayList<>();
    473         for (PlanImage src : listOfFeuilles) {
    474             imageNames.add(src.name);
    475         }
    476         JComboBox<String> inputFeuilleList = new JComboBox<>(imageNames.toArray(new String[]{}));
    477         p.add(inputFeuilleList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
    478         JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null);
    479         // this below is a temporary workaround to fix the "always on top" issue
    480         JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
    481         CadastrePlugin.prepareDialog(dialog);
    482         dialog.setVisible(true);
    483         // till here
    484         if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
    485             return -1;
    486         return inputFeuilleList.getSelectedIndex();
    487     }
    488 
    489     private static String buildRasterFeuilleInterfaceRef(String codeCommune) {
    490         return C_INTERFACE_RASTER_FEUILLE + "?f=" + codeCommune;
     484        return GuiHelper.runInEDTAndWaitAndReturn(() -> {
     485            JPanel p = new JPanel(new GridBagLayout());
     486            List<String> imageNames = new ArrayList<>();
     487            for (PlanImage src : listOfFeuilles) {
     488                imageNames.add(src.name);
     489            }
     490            JComboBox<String> inputFeuilleList = new JComboBox<>(imageNames.toArray(new String[]{}));
     491            p.add(inputFeuilleList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
     492            JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null);
     493            // this below is a temporary workaround to fix the "always on top" issue
     494            JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
     495            CadastrePlugin.prepareDialog(dialog);
     496            dialog.setVisible(true);
     497            // till here
     498            if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
     499                return -1;
     500            return inputFeuilleList.getSelectedIndex();
     501        });
     502    }
     503
     504    private static String buildRasterFeuilleInterfaceRef(String codeCommune, String csrfToken) {
     505        return C_INTERFACE_RASTER_FEUILLE + csrfToken + "&f=" + codeCommune;
    491506    }
    492507
     
    497512     * and store the result in the wmsLayer as well.
    498513     * @param wmsLayer the WMSLayer where the commune data and images are stored
     514     * @throws IOException if any I/O error occurs
    499515     */
    500516    public void retrieveCommuneBBox(WMSLayer wmsLayer) throws IOException {
  • applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/wms/DownloadWMSVectorImage.java

    r33640 r33682  
    1313import org.openstreetmap.josm.gui.MapView;
    1414import org.openstreetmap.josm.gui.PleaseWaitRunnable;
     15import org.openstreetmap.josm.gui.util.GuiHelper;
    1516import org.openstreetmap.josm.tools.Logging;
    1617
     
    4445                    if (wmsLayer.isRaster()) {
    4546                        // set raster image commune bounding box based on current view (before adjustment)
    46                         JOptionPane.showMessageDialog(Main.parent,
    47                                 tr("This commune is not vectorized.\nPlease use the other menu entry to georeference a \"Plan image\""));
     47                        GuiHelper.runInEDT(() ->
     48                            JOptionPane.showMessageDialog(Main.parent,
     49                                tr("This commune is not vectorized.\nPlease use the other menu entry to georeference a \"Plan image\"")));
    4850                        MainApplication.getLayerManager().removeLayer(wmsLayer);
    4951                        wmsLayer = null;
  • applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/wms/RasterImageGeoreferencer.java

    r33640 r33682  
    1919import org.openstreetmap.josm.data.coor.EastNorth;
    2020import org.openstreetmap.josm.gui.MainApplication;
     21import org.openstreetmap.josm.gui.util.GuiHelper;
    2122import org.openstreetmap.josm.tools.GBC;
    2223import org.openstreetmap.josm.tools.Logging;
     
    4647    }
    4748
    48     /**
     49   /**
    4950    *
     51    * @param wmsLayer WMS layer
    5052    * @return false if all operations are canceled
    5153    */
     
    5759       mouseClickedTime = System.currentTimeMillis();
    5860       Object[] options = {"OK", "Cancel"};
    59        int ret = JOptionPane.showOptionDialog(null,
     61       int ret = GuiHelper.runInEDTAndWaitAndReturn(() -> JOptionPane.showOptionDialog(null,
    6062               tr("Click first corner for image cropping\n(two points required)"),
    6163               tr("Image cropping"),
    6264               JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
    63                null, options, options[0]);
     65               null, options, options[0]));
    6466       if (ret == JOptionPane.OK_OPTION) {
    6567           mouseClickedTime = System.currentTimeMillis();
     
    7072   }
    7173
    72    /**
    73     *
    74     * @return false if all operations are canceled
    75     */
     74  /**
     75   * @param wmsLayer WMS layer
     76   * @return false if all operations are canceled
     77   */
    7678  public boolean startGeoreferencing(WMSLayer wmsLayer) {
    7779      this.wmsLayer = wmsLayer;
Note: See TracChangeset for help on using the changeset viewer.