Changeset 17089 in osm for applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreInterface.java
- Timestamp:
- 2009-08-16T23:36:16+02:00 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreInterface.java
r16791 r17089 14 14 15 15 import javax.swing.JComboBox; 16 import javax.swing.JDialog; 16 17 import javax.swing.JOptionPane; 17 18 import javax.swing.JPanel; … … 26 27 public HttpURLConnection urlConn = null; 27 28 28 private CadastreGrabber cadastreGrabber;29 29 private String cookie; 30 30 private String interfaceRef = null; 31 private String lastWMSLayerName = null; 31 32 private URL searchFormURL; 32 33 private Vector<String> listOfCommunes = new Vector<String>(); 33 34 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>(); 34 44 35 45 final String baseURL = "http://www.cadastre.gouv.fr"; … … 43 53 44 54 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 "; 50 59 51 60 public boolean retrieveInterface(WMSLayer wmsLayer) throws DuplicateLayerException { … … 55 64 downloadCancelled = false; 56 65 try { 57 if (cookie == null || !wmsLayer.getName().equals( cadastreGrabber.getLastWMSLayerName())) {66 if (cookie == null || !wmsLayer.getName().equals(lastWMSLayerName)) { 58 67 getCookie(); 59 68 getInterface(wmsLayer); 60 cadastreGrabber.setLastWMSLayerName(wmsLayer.getName());69 this.lastWMSLayerName = wmsLayer.getName(); 61 70 } 62 71 openInterface(); 63 72 } catch (IOException e) { 64 JOptionPane.showMessageDialog(Main.parent, 73 /*JOptionPane.showMessageDialog(Main.parent, 65 74 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 67 85 return false; 68 86 } … … 80 98 throw new IOException("Cannot get Cadastre cookie."); 81 99 } 100 System.out.println("GET "+searchFormURL); 82 101 BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); 83 102 while(in.readLine() != null) {} // read the buffer otherwise we sent POST too early … … 97 116 98 117 public void resetCookie() { 99 cadastreGrabber.setLastWMSLayerName(null);118 lastWMSLayerName = null; 100 119 } 101 120 102 121 public void resetCookieIfNewLayer(String newWMSLayerName) { 103 if (!newWMSLayerName.equals( cadastreGrabber.getLastWMSLayerName())) {122 if (!newWMSLayerName.equals(lastWMSLayerName)) { 104 123 resetCookie(); 105 124 } … … 107 126 108 127 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 112 135 private void getInterface(WMSLayer wmsLayer) throws IOException, DuplicateLayerException { 113 136 // first attempt : search for given name without codeCommune … … 126 149 interfaceRef = postForm(wmsLayer, wmsLayer.getCodeCommune()); 127 150 } 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 } 133 160 } 134 161 } … … 142 169 try { 143 170 // finally, open the interface on server side giving access to the wms server 171 String lines = null; 172 String ln = null; 144 173 URL interfaceURL = new URL(baseURL + "/scpc/"+interfaceRef); 145 174 urlConn = (HttpURLConnection)interfaceURL.openConnection(); … … 150 179 throw new IOException("Cannot open Cadastre interface. GET response:"+urlConn.getResponseCode()); 151 180 } 181 System.out.println("GET "+interfaceURL); 152 182 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 } 155 187 } catch (MalformedURLException e) { 156 188 throw (IOException) new IOException( … … 171 203 * <option value="QK066" >COLMAR - 68000</option> 172 204 * </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). 173 207 * 174 208 * @param location 175 209 * @param codeCommune 176 * @return retURL url to available c adastre vectorised master piece; "" if not found210 * @return retURL url to available code commune in the cadastre; "" if not found 177 211 * @throws IOException 178 212 */ … … 180 214 try { 181 215 String ln = null; 182 String line = null; 216 String lines = null; 183 217 listOfCommunes.clear(); 184 218 listOfTA.clear(); … … 203 237 OutputStream wr = urlConn.getOutputStream(); 204 238 wr.write(content.getBytes()); 239 System.out.println("POST "+content); 205 240 wr.flush(); 206 241 wr.close(); 207 242 BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); 208 243 while ((ln = rd.readLine()) != null) { 209 line += ln; 244 lines += ln; 210 245 } 211 246 rd.close(); 212 247 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" 220 254 // 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 } 231 274 } 232 275 return null; 233 } else if (line.indexOf(cCommuneListStart) != -1 && line.indexOf(cCommuneListEnd) != -1) { 276 } else if (lines.indexOf(cCommuneListStart) != -1 && lines.indexOf(cCommuneListEnd) != -1) { 234 277 // 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)); 238 281 } 239 282 } catch (MalformedURLException e) { … … 265 308 } 266 309 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 280 347 private String selectCommuneDialog() { 281 348 JPanel p = new JPanel(new GridBagLayout()); … … 290 357 private static final long serialVersionUID = 1L; 291 358 }; 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 293 365 if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue())) 294 366 return null; … … 297 369 } 298 370 299 private StringselectTADialog() {371 private int selectFeuilleDialog() { 300 372 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 307 386 if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue())) 308 return null;309 Stringresult =listOfTA.elementAt(inputTAList.getSelectedIndex());387 return -1; 388 int result = inputFeuilleList.getSelectedIndex(); 310 389 return result; 311 390 } 312 391 313 private String buildRasterInterfaceRef(String codeCommune) { 314 return cInterfaceRaster + "?f=" + codeCommune; 392 private String buildRasterFeuilleInterfaceRef(String codeCommune) { 393 return cInterfaceRasterFeuille + "?f=" + codeCommune; 315 394 } 316 395 … … 324 403 content += "&dontSaveLastForward&keepVolatileSession="; 325 404 searchFormURL = new URL(content); 326 System.out.println("HEAD:"+content);327 405 urlConn = (HttpURLConnection)searchFormURL.openConnection(); 328 406 urlConn.setRequestMethod("GET"); … … 332 410 throw new IOException("Cannot get Cadastre response."); 333 411 } 412 System.out.println("GET "+searchFormURL); 334 413 BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); 335 414 while ((ln = in.readLine()) != null) { … … 377 456 } 378 457 downloadCancelled = true; 379 cadastreGrabber.setLastWMSLayerName(null);458 lastWMSLayerName = null; 380 459 } 381 460
Note:
See TracChangeset
for help on using the changeset viewer.
