Ignore:
Timestamp:
2009-08-16T23:36:16+02:00 (17 years ago)
Author:
pieren
Message:

raster image feature implementation

File:
1 edited

Legend:

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

    r16791 r17089  
    1414
    1515import javax.swing.JComboBox;
     16import javax.swing.JDialog;
    1617import javax.swing.JOptionPane;
    1718import javax.swing.JPanel;
     
    2627    public HttpURLConnection urlConn = null;
    2728
    28     private CadastreGrabber cadastreGrabber;
    2929    private String cookie;
    3030    private String interfaceRef = null;
     31    private String lastWMSLayerName = null;
    3132    private URL searchFormURL;
    3233    private Vector<String> listOfCommunes = new Vector<String>();
    3334    private Vector<String> listOfTA = new Vector<String>();
     35    class PlanImage {
     36        String name;
     37        String ref;
     38        PlanImage(String name, String ref) {
     39            this.name = name;
     40            this.ref = ref;
     41        }
     42    }
     43    private Vector<PlanImage> listOfFeuilles = new Vector<PlanImage>();
    3444
    3545    final String baseURL = "http://www.cadastre.gouv.fr";
     
    4353
    4454    final String cInterfaceVector = "afficherCarteCommune.do";
    45     final String cInterfaceRaster = "afficherCarteTa.do";
    46 
    47     CadastreInterface(CadastreGrabber cadastreGrabber) {
    48         this.cadastreGrabber = cadastreGrabber;
    49     }
     55    final String cInterfaceRasterTA = "afficherCarteTa.do";
     56    final String cInterfaceRasterFeuille = "afficherCarteFeuille.do";
     57    final String cImageLinkStart = "title=\"image\"><a href=\"#\" onClick=\"popup('afficherCarteFeuille.do?f=";
     58    final String cImageNameStart = ">Feuille ";
    5059
    5160    public boolean retrieveInterface(WMSLayer wmsLayer) throws DuplicateLayerException {
     
    5564        downloadCancelled = false;
    5665        try {
    57             if (cookie == null || !wmsLayer.getName().equals(cadastreGrabber.getLastWMSLayerName())) {
     66            if (cookie == null || !wmsLayer.getName().equals(lastWMSLayerName)) {
    5867                getCookie();
    5968                getInterface(wmsLayer);
    60                 cadastreGrabber.setLastWMSLayerName(wmsLayer.getName());
     69                this.lastWMSLayerName = wmsLayer.getName();
    6170            }
    6271            openInterface();
    6372        } catch (IOException e) {
    64             JOptionPane.showMessageDialog(Main.parent,
     73            /*JOptionPane.showMessageDialog(Main.parent,
    6574                    tr("Town/city {0} not found or not available in WMS.\n" +
    66                             "Please check its availibility on www.cadastre.gouv.fr", wmsLayer.getLocation()));
     75                            "Please check its availibility on www.cadastre.gouv.fr", wmsLayer.getLocation()));*/
     76            JOptionPane pane = new JOptionPane(
     77                    tr("Town/city {0} not found or not available in WMS.\n" +
     78                            "Please check its availibility on www.cadastre.gouv.fr", wmsLayer.getLocation()),
     79                            JOptionPane.INFORMATION_MESSAGE);
     80            // this below is a temporary workaround to fix the "always on top" issue
     81            JDialog dialog = pane.createDialog(Main.parent, tr("Select commune"));
     82            CadastrePlugin.prepareDialog(dialog);
     83            dialog.setVisible(true);
     84            // till here
    6785            return false;
    6886        }
     
    8098                throw new IOException("Cannot get Cadastre cookie.");
    8199            }
     100            System.out.println("GET "+searchFormURL);
    82101            BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    83102            while(in.readLine() != null) {}  // read the buffer otherwise we sent POST too early
     
    97116
    98117    public void resetCookie() {
    99         cadastreGrabber.setLastWMSLayerName(null);
     118        lastWMSLayerName = null;
    100119    }
    101120
    102121    public void resetCookieIfNewLayer(String newWMSLayerName) {
    103         if (!newWMSLayerName.equals(cadastreGrabber.getLastWMSLayerName())) {
     122        if (!newWMSLayerName.equals(lastWMSLayerName)) {
    104123            resetCookie();
    105124        }
     
    107126
    108127    public void setCookie() {
    109         urlConn.setRequestProperty("Cookie", cookie);
    110     }
    111 
     128        this.urlConn.setRequestProperty("Cookie", this.cookie);
     129    }
     130
     131    public void setCookie(HttpURLConnection urlConn) {
     132        urlConn.setRequestProperty("Cookie", this.cookie);
     133    }
     134   
    112135    private void getInterface(WMSLayer wmsLayer) throws IOException, DuplicateLayerException {
    113136        // first attempt : search for given name without codeCommune
     
    126149                    interfaceRef = postForm(wmsLayer, wmsLayer.getCodeCommune());
    127150                }
    128                 if (wmsLayer.isRaster() && listOfTA.size() > 1) {
    129                     // commune known but raster format. Select "tableau d'assemblage" from list.
    130                     wmsLayer.setCodeCommune(selectTADialog());
    131                     checkLayerDuplicates(wmsLayer);
    132                     interfaceRef = buildRasterInterfaceRef(wmsLayer.getCodeCommune());
     151                if (listOfCommunes.size() == 1 && wmsLayer.isRaster()) {
     152                    // commune known but raster format. Select "Feuille" (non-georeferenced image) from list.
     153                    int res = selectFeuilleDialog();
     154                    if (res != -1) {
     155                        // TODO
     156                        wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).name);
     157                        checkLayerDuplicates(wmsLayer);
     158                        interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune());
     159                    }
    133160                }
    134161            }
     
    142169        try {
    143170            // finally, open the interface on server side giving access to the wms server
     171            String lines = null;
     172            String ln = null;
    144173            URL interfaceURL = new URL(baseURL + "/scpc/"+interfaceRef);
    145174            urlConn = (HttpURLConnection)interfaceURL.openConnection();
     
    150179                throw new IOException("Cannot open Cadastre interface. GET response:"+urlConn.getResponseCode());
    151180            }
     181            System.out.println("GET "+interfaceURL);
    152182            BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    153             while(in.readLine() != null) {}  // read the buffer otherwise we sent POST too early
    154             System.out.println("GET to open interface sent");
     183            //while(in.readLine() != null) {}  // read the buffer otherwise we sent POST too early
     184            while ((ln = in.readLine()) != null) {
     185                lines += ln;
     186            }
    155187        } catch (MalformedURLException e) {
    156188            throw (IOException) new IOException(
     
    171203     *   <option value="QK066" >COLMAR - 68000</option>
    172204     *   </select>
     205     * The returned string is the interface name used in further requests, e.g. "afficherCarteCommune.do?c=QP224"
     206     * where QP224 is the code commune known by the WMS (or "afficherCarteTa.do?c=..." for raster images).
    173207     *
    174208     * @param location
    175209     * @param codeCommune
    176      * @return retURL url to available cadastre vectorised master piece; "" if not found
     210     * @return retURL url to available code commune in the cadastre; "" if not found
    177211     * @throws IOException
    178212     */
     
    180214        try {
    181215            String ln = null;
    182             String line = null;
     216            String lines = null;
    183217            listOfCommunes.clear();
    184218            listOfTA.clear();
     
    203237            OutputStream wr = urlConn.getOutputStream();
    204238            wr.write(content.getBytes());
     239            System.out.println("POST "+content);
    205240            wr.flush();
    206241            wr.close();
    207242            BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    208243            while ((ln = rd.readLine()) != null) {
    209                 line += ln;
     244                lines += ln;
    210245            }
    211246            rd.close();
    212247            urlConn.disconnect();
    213             System.out.println("POST="+line);
    214             if (line.indexOf(cImageFormat) != -1) {
    215                 int i = line.indexOf(cImageFormat);
    216                 int j = line.indexOf(".", i);
    217                 wmsLayer.setRaster(line.substring(i+cImageFormat.length(), j).equals("image"));
    218             }
    219             if (!wmsLayer.isRaster() && line.indexOf(cInterfaceVector) != -1) {  // "afficherCarteCommune.do"
     248            if (lines.indexOf(cImageFormat) != -1) {
     249                int i = lines.indexOf(cImageFormat);
     250                int j = lines.indexOf(".", i);
     251                wmsLayer.setRaster(lines.substring(i+cImageFormat.length(), j).equals("image"));
     252            }
     253            if (!wmsLayer.isRaster() && lines.indexOf(cInterfaceVector) != -1) {  // "afficherCarteCommune.do"
    220254                // shall be something like: interfaceRef = "afficherCarteCommune.do?c=X2269";
    221                 line = line.substring(line.indexOf(cInterfaceVector),line.length());
    222                 line = line.substring(0, line.indexOf("'"));
    223                 System.out.println("interface ref.:"+line);
    224                 return line;
    225             } else if (wmsLayer.isRaster() && line.indexOf(cInterfaceRaster) != -1) { // "afficherCarteTa.do"
    226                 // list of values parsed in listOfTA (Tableau d'assemblage)
    227                 parseTAList(line.substring(line.indexOf(cInterfaceRaster)));
    228                 if (listOfTA.size() == 1) {
    229                     wmsLayer.setCodeCommune(listOfTA.firstElement());
    230                     return buildRasterInterfaceRef(listOfTA.firstElement());
     255                lines = lines.substring(lines.indexOf(cInterfaceVector),lines.length());
     256                lines = lines.substring(0, lines.indexOf("'"));
     257                System.out.println("interface ref.:"+lines);
     258                return lines;
     259            } else if (wmsLayer.isRaster() && lines.indexOf(cInterfaceRasterTA) != -1) { // "afficherCarteTa.do"
     260                // list of values parsed in listOfFeuilles (list all non-georeferenced images)
     261                lines = getFeuillesList();
     262                parseFeuillesList(lines);
     263                if (listOfFeuilles.size() > 0) {
     264                    int res = selectFeuilleDialog();
     265                    if (res != -1) {
     266                        wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).name);
     267                        checkLayerDuplicates(wmsLayer);
     268                        interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune());
     269                        wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).ref);
     270                        lines = buildRasterFeuilleInterfaceRef(listOfFeuilles.elementAt(res).ref);
     271                        System.out.println("interface ref.:"+lines);
     272                        return lines;
     273                    }
    231274                }
    232275                return null;
    233             } else if (line.indexOf(cCommuneListStart) != -1 && line.indexOf(cCommuneListEnd) != -1) {
     276            } else if (lines.indexOf(cCommuneListStart) != -1 && lines.indexOf(cCommuneListEnd) != -1) {
    234277                // list of values parsed in listOfCommunes
    235                 int i = line.indexOf(cCommuneListStart);
    236                 int j = line.indexOf(cCommuneListEnd, i);
    237                 parseCommuneList(line.substring(i, j));
     278                int i = lines.indexOf(cCommuneListStart);
     279                int j = lines.indexOf(cCommuneListEnd, i);
     280                parseCommuneList(lines.substring(i, j));
    238281            }
    239282        } catch (MalformedURLException e) {
     
    265308    }
    266309
    267     private void parseTAList(String input) {
    268         while (input.indexOf(cInterfaceRaster) != -1) {
    269             input = input.substring(input.indexOf(cInterfaceRaster));
    270             String codeTA = input.substring(0, input.indexOf("'"));
    271             codeTA = codeTA.substring(codeTA.indexOf("=")+1);
    272             if (!listOfTA.contains(codeTA)) {
    273                 System.out.println("parse "+codeTA);
    274                 listOfTA.add(codeTA);
    275             }
    276             input = input.substring(cInterfaceRaster.length());
    277         }
    278     }
    279 
     310    private String getFeuillesList() {
     311        // get all images in one html page
     312        String ln = null;
     313        String lines = null;
     314        HttpURLConnection urlConn2 = null;
     315        try {
     316            URL getAllImagesURL = new URL(baseURL + "/scpc/listerFeuillesParcommune.do?keepVolatileSession=&offset=2000");
     317            urlConn2 = (HttpURLConnection)getAllImagesURL.openConnection();
     318            setCookie(urlConn2);
     319            urlConn2.connect();
     320            System.out.println("GET "+getAllImagesURL);
     321            BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn2.getInputStream()));
     322            while ((ln = rd.readLine()) != null) {
     323                lines += ln;
     324            }
     325            rd.close();
     326            urlConn2.disconnect();
     327            //System.out.println("GET="+lines);
     328        } catch (IOException e) {
     329            listOfFeuilles.clear();
     330            e.printStackTrace();
     331        }
     332        return lines;
     333    }
     334   
     335    private void parseFeuillesList(String input) {
     336        listOfFeuilles.clear();
     337        while (input.indexOf(cImageLinkStart) != -1) {
     338            input = input.substring(input.indexOf(cImageLinkStart)+cImageLinkStart.length());
     339            String refFeuille = input.substring(0, input.indexOf("'"));
     340            String nameFeuille = input.substring(
     341                    input.indexOf(cImageNameStart)+cImageNameStart.length(),
     342                    input.indexOf(" -"));
     343            listOfFeuilles.add(new PlanImage(nameFeuille, refFeuille));
     344        }
     345    }
     346   
    280347    private String selectCommuneDialog() {
    281348        JPanel p = new JPanel(new GridBagLayout());
     
    290357            private static final long serialVersionUID = 1L;
    291358        };
    292         pane.createDialog(Main.parent, tr("Select commune")).setVisible(true);
     359        //pane.createDialog(Main.parent, tr("Select commune")).setVisible(true);
     360        // this below is a temporary workaround to fix the "always on top" issue
     361        JDialog dialog = pane.createDialog(Main.parent, tr("Select commune"));
     362        CadastrePlugin.prepareDialog(dialog);
     363        dialog.setVisible(true);
     364        // till here
    293365        if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
    294366            return null;
     
    297369    }
    298370
    299     private String selectTADialog() {
     371    private int selectFeuilleDialog() {
    300372        JPanel p = new JPanel(new GridBagLayout());
    301         JComboBox inputTAList = new JComboBox(listOfTA);
    302         p.add(inputTAList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
    303         JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null) {
    304             private static final long serialVersionUID = 1L;
    305         };
    306         pane.createDialog(Main.parent, tr("Select Tableau d'Assemblage")).setVisible(true);
     373        Vector<String> ImageNames = new Vector<String>();
     374        for (PlanImage src : listOfFeuilles) {
     375            ImageNames.add(src.name);
     376        }
     377        JComboBox inputFeuilleList = new JComboBox(ImageNames);
     378        p.add(inputFeuilleList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
     379        JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null);
     380        //pane.createDialog(Main.parent, tr("Select Feuille")).setVisible(true);
     381        // this below is a temporary workaround to fix the "always on top" issue
     382        JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
     383        CadastrePlugin.prepareDialog(dialog);
     384        dialog.setVisible(true);
     385        // till here
    307386        if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
    308             return null;
    309         String result = listOfTA.elementAt(inputTAList.getSelectedIndex());
     387            return -1;
     388        int result = inputFeuilleList.getSelectedIndex();
    310389        return result;
    311390    }
    312391
    313     private String buildRasterInterfaceRef(String codeCommune) {
    314         return cInterfaceRaster + "?f=" + codeCommune;
     392    private String buildRasterFeuilleInterfaceRef(String codeCommune) {
     393        return cInterfaceRasterFeuille + "?f=" + codeCommune;
    315394    }
    316395
     
    324403        content += "&dontSaveLastForward&keepVolatileSession=";
    325404        searchFormURL = new URL(content);
    326         System.out.println("HEAD:"+content);
    327405        urlConn = (HttpURLConnection)searchFormURL.openConnection();
    328406        urlConn.setRequestMethod("GET");
     
    332410            throw new IOException("Cannot get Cadastre response.");
    333411        }
     412        System.out.println("GET "+searchFormURL);
    334413        BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    335414        while ((ln = in.readLine()) != null) {
     
    377456        }
    378457        downloadCancelled = true;
    379         cadastreGrabber.setLastWMSLayerName(null);
     458        lastWMSLayerName = null;
    380459    }
    381460
Note: See TracChangeset for help on using the changeset viewer.