source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreInterface.java@ 21777

Last change on this file since 21777 was 21516, checked in by pieren, 15 years ago

Fixed bug when switching to another municipality

  • Property svn:eol-style set to native
File size: 24.7 KB
Line 
1// License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
2package cadastre_fr;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.io.BufferedReader;
8import java.io.IOException;
9import java.io.InputStreamReader;
10import java.io.OutputStream;
11import java.net.HttpURLConnection;
12import java.net.MalformedURLException;
13import java.net.URL;
14import java.util.Date;
15import java.util.Vector;
16
17import javax.swing.JComboBox;
18import javax.swing.JDialog;
19import javax.swing.JOptionPane;
20import javax.swing.JPanel;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.data.coor.EastNorth;
24import org.openstreetmap.josm.gui.layer.Layer;
25import org.openstreetmap.josm.tools.GBC;
26
27public class CadastreInterface {
28 public boolean downloadCancelled = false;
29 public HttpURLConnection urlConn = null;
30
31 private String cookie;
32 private String interfaceRef = null;
33 private String lastWMSLayerName = null;
34 private URL searchFormURL;
35 private Vector<String> listOfCommunes = new Vector<String>();
36 private Vector<String> listOfTA = new Vector<String>();
37 class PlanImage {
38 String name;
39 String ref;
40 PlanImage(String name, String ref) {
41 this.name = name;
42 this.ref = ref;
43 }
44 }
45 private Vector<PlanImage> listOfFeuilles = new Vector<PlanImage>();
46 private long cookieTimestamp;
47
48 final String baseURL = "http://www.cadastre.gouv.fr";
49 final String cImageFormat = "Cette commune est au format ";
50 final String cCommuneListStart = "<select name=\"codeCommune\"";
51 final String cCommuneListEnd = "</select>";
52 final String c0ptionListStart = "<option value=\"";
53 final String cOptionListEnd = "</option>";
54 final String cBBoxCommunStart = "new GeoBox(";
55 final String cBBoxCommunEnd = ")";
56
57 final String cInterfaceVector = "afficherCarteCommune.do";
58 final String cInterfaceRasterTA = "afficherCarteTa.do";
59 final String cInterfaceRasterFeuille = "afficherCarteFeuille.do";
60 final String cImageLinkStart = "title=\"image\"><a href=\"#\" onClick=\"popup('afficherCarteFeuille.do?f=";
61 final String cImageNameStart = ">Feuille ";
62
63 final static long cCookieExpiration = 30 * 60 * 1000; // 30 minutes expressed in milliseconds
64
65 final int cRetriesGetCookie = 10; // 10 times every 3 seconds means 30 seconds trying to get a cookie
66
67 public boolean retrieveInterface(WMSLayer wmsLayer) throws DuplicateLayerException, WMSException {
68 if (wmsLayer.getName().equals(""))
69 return false;
70 boolean isCookieExpired = isCookieExpired();
71 if (wmsLayer.getName().equals(lastWMSLayerName) && !isCookieExpired)
72 return true;
73 // open the session with the French Cadastre web front end
74 downloadCancelled = false;
75 try {
76 if (cookie == null || isCookieExpired) {
77 getCookie();
78 interfaceRef = null;
79 }
80 if (cookie == null)
81 throw new WMSException(tr("Cannot open a new client session.\nServer in maintenance or temporary overloaded."));
82 if (interfaceRef == null) {
83 getInterface(wmsLayer);
84 this.lastWMSLayerName = wmsLayer.getName();
85 }
86 openInterface();
87 } catch (IOException e) {
88 JOptionPane.showMessageDialog(Main.parent,
89 tr("Town/city {0} not found or not available\n" +
90 "or action canceled", wmsLayer.getLocation()));
91 return false;
92 }
93 return true;
94 }
95
96 /**
97 *
98 * @return true if a cookie is delivered by WMS and false is WMS is not opening a client session
99 * (too many clients or in maintenance)
100 * @throws IOException
101 */
102 private void getCookie() throws IOException {
103 boolean cookied = false;
104 int retries = cRetriesGetCookie;
105 try {
106 searchFormURL = new URL(baseURL + "/scpc/accueil.do");
107 while (cookied == false && retries > 0) {
108 urlConn = (HttpURLConnection)searchFormURL.openConnection();
109 urlConn.setRequestMethod("GET");
110 urlConn.connect();
111 if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
112 System.out.println("GET "+searchFormURL);
113 BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
114 while(in.readLine() != null) {} // read the buffer otherwise we sent POST too early
115 String headerName=null;
116 for (int i=1; (headerName = urlConn.getHeaderFieldKey(i))!=null; i++) {
117 if (headerName.equals("Set-Cookie")) {
118 cookie = urlConn.getHeaderField(i);
119 cookie = cookie.substring(0, cookie.indexOf(";"));
120 cookieTimestamp = new Date().getTime();
121 System.out.println("received cookie=" + cookie + " at " + new Date(cookieTimestamp));
122 cookied = true;
123 }
124 }
125 } else {
126 System.out.println("Request to home page failed. Http error:"+urlConn.getResponseCode()+". Try again "+retries+" times");
127 CadastrePlugin.safeSleep(3000);
128 retries --;
129 }
130 }
131 } catch (MalformedURLException e) {
132 throw (IOException) new IOException(
133 "Illegal url.").initCause(e);
134 }
135 }
136
137 public void resetCookie() {
138 lastWMSLayerName = null;
139 cookie = null;
140 }
141
142 public boolean isCookieExpired() {
143 long now = new Date().getTime();
144 if ((now - cookieTimestamp) > cCookieExpiration) {
145 System.out.println("cookie received at "+new Date(cookieTimestamp)+" expired (now is "+new Date(now)+")");
146 return true;
147 }
148 return false;
149 }
150
151 public void resetInterfaceRefIfNewLayer(String newWMSLayerName) {
152 if (!newWMSLayerName.equals(lastWMSLayerName)) {
153 interfaceRef = null;
154 cookie = null; // new since WMS server requires that we come back to the main form
155 }
156 }
157
158 public void setCookie() {
159 this.urlConn.setRequestProperty("Cookie", this.cookie);
160 }
161
162 public void setCookie(HttpURLConnection urlConn) {
163 urlConn.setRequestProperty("Cookie", this.cookie);
164 }
165
166 private void getInterface(WMSLayer wmsLayer) throws IOException, DuplicateLayerException {
167 // first attempt : search for given name without codeCommune
168 interfaceRef = postForm(wmsLayer, "");
169 // second attempt either from known codeCommune (e.g. from cache) or from ComboBox
170 if (interfaceRef == null) {
171 if (!wmsLayer.getCodeCommune().equals("")) {
172 // codeCommune is already known (from previous request or from cache on disk)
173 interfaceRef = postForm(wmsLayer, wmsLayer.getCodeCommune());
174 } else {
175 if (listOfCommunes.size() > 1) {
176 // commune unknown, prompt the list of communes from
177 // server and try with codeCommune
178 String selected = selectMunicipalityDialog(wmsLayer);
179 if (selected != null) {
180 String newCodeCommune = selected.substring(1, selected.indexOf(">")-2);
181 String newLocation = selected.substring(selected.indexOf(">")+1, selected.lastIndexOf(" - "));
182 wmsLayer.setCodeCommune(newCodeCommune);
183 wmsLayer.setLocation(newLocation);
184 Main.pref.put("cadastrewms.codeCommune", newCodeCommune);
185 Main.pref.put("cadastrewms.location", newLocation);
186 }
187 checkLayerDuplicates(wmsLayer);
188 interfaceRef = postForm(wmsLayer, wmsLayer.getCodeCommune());
189 }
190 if (listOfCommunes.size() == 1 && wmsLayer.isRaster()) {
191 // commune known but raster format. Select "Feuille" (non-georeferenced image) from list.
192 int res = selectFeuilleDialog();
193 if (res != -1) {
194 wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).name);
195 checkLayerDuplicates(wmsLayer);
196 interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune());
197 }
198 }
199 }
200 }
201
202 if (interfaceRef == null)
203 throw new IOException("Town/city " + wmsLayer.getLocation() + " not found.");
204 }
205
206 private void openInterface() throws IOException {
207 try {
208 // finally, open the interface on server side giving access to the wms server
209 String lines = null;
210 String ln = null;
211 URL interfaceURL = new URL(baseURL + "/scpc/"+interfaceRef);
212 urlConn = (HttpURLConnection)interfaceURL.openConnection();
213 urlConn.setRequestMethod("GET");
214 setCookie();
215 urlConn.connect();
216 if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
217 throw new IOException("Cannot open Cadastre interface. GET response:"+urlConn.getResponseCode());
218 }
219 System.out.println("GET "+interfaceURL);
220 BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
221 // read the buffer otherwise we sent POST too early
222 while ((ln = in.readLine()) != null) {
223 lines += ln;
224 }
225 } catch (MalformedURLException e) {
226 throw (IOException) new IOException(
227 "CadastreGrabber: Illegal url.").initCause(e);
228 }
229 }
230
231 /**
232 * Post the form with the commune name and check the returned answer which is embedded
233 * in HTTP XML packets. This function doesn't use an XML parser yet but that would be a good idea
234 * for the next releases.
235 * Two possibilities :
236 * - either the commune name matches and we receive an URL starting with "afficherCarteCommune.do" or
237 * - we don't receive a single answer but a list of possible values. This answer looks like:
238 * <select name="codeCommune" class="long erreur" id="codeCommune">
239 * <option value="">Choisir</option>
240 * <option value="50061" >COLMARS - 04370</option>
241 * <option value="QK066" >COLMAR - 68000</option>
242 * </select>
243 * The returned string is the interface name used in further requests, e.g. "afficherCarteCommune.do?c=QP224"
244 * where QP224 is the code commune known by the WMS (or "afficherCarteTa.do?c=..." for raster images).
245 *
246 * @param location
247 * @param codeCommune
248 * @return retURL url to available code commune in the cadastre; "" if not found
249 * @throws IOException
250 */
251 private String postForm(WMSLayer wmsLayer, String codeCommune) throws IOException {
252 try {
253 String ln = null;
254 String lines = null;
255 listOfCommunes.clear();
256 listOfTA.clear();
257 // send a POST request with a city/town/village name
258 String content = "numerovoie=";
259 content += "&indiceRepetition=";
260 content += "&nomvoie=";
261 content += "&lieuDit=";
262 if (codeCommune == "") {
263 content += "&ville=" + new String(java.net.URLEncoder.encode(wmsLayer.getLocation(), "UTF-8"));
264 content += "&codePostal=";
265 } else {
266 content += "&codeCommune=" + codeCommune;
267 }
268 content += "&codeDepartement=";
269 content += wmsLayer.getDepartement();
270 content += "&nbResultatParPage=10";
271 content += "&x=0&y=0";
272 searchFormURL = new URL(baseURL + "/scpc/rechercherPlan.do");
273 urlConn = (HttpURLConnection)searchFormURL.openConnection();
274 urlConn.setRequestMethod("POST");
275 urlConn.setDoOutput(true);
276 urlConn.setDoInput(true);
277 setCookie();
278 OutputStream wr = urlConn.getOutputStream();
279 wr.write(content.getBytes());
280 System.out.println("POST "+content);
281 wr.flush();
282 wr.close();
283 BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
284 while ((ln = rd.readLine()) != null) {
285 lines += ln;
286 }
287 rd.close();
288 urlConn.disconnect();
289 if (lines != null) {
290 if (lines.indexOf(cImageFormat) != -1) {
291 int i = lines.indexOf(cImageFormat);
292 int j = lines.indexOf(".", i);
293 wmsLayer.setRaster(lines.substring(i+cImageFormat.length(), j).equals("image"));
294 }
295 if (!wmsLayer.isRaster() && lines.indexOf(cInterfaceVector) != -1) { // "afficherCarteCommune.do"
296 // shall be something like: interfaceRef = "afficherCarteCommune.do?c=X2269";
297 lines = lines.substring(lines.indexOf(cInterfaceVector),lines.length());
298 lines = lines.substring(0, lines.indexOf("'"));
299 System.out.println("interface ref.:"+lines);
300 return lines;
301 } else if (wmsLayer.isRaster() && lines.indexOf(cInterfaceRasterTA) != -1) { // "afficherCarteTa.do"
302 // list of values parsed in listOfFeuilles (list all non-georeferenced images)
303 lines = getFeuillesList();
304 if (!downloadCancelled) {
305 parseFeuillesList(lines);
306 if (listOfFeuilles.size() > 0) {
307 int res = selectFeuilleDialog();
308 if (res != -1) {
309 wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).name);
310 checkLayerDuplicates(wmsLayer);
311 interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune());
312 wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).ref);
313 lines = buildRasterFeuilleInterfaceRef(listOfFeuilles.elementAt(res).ref);
314 System.out.println("interface ref.:"+lines);
315 return lines;
316 }
317 }
318 }
319 return null;
320 } else if (lines.indexOf(cCommuneListStart) != -1 && lines.indexOf(cCommuneListEnd) != -1) {
321 // list of values parsed in listOfCommunes
322 int i = lines.indexOf(cCommuneListStart);
323 int j = lines.indexOf(cCommuneListEnd, i);
324 parseCommuneList(lines.substring(i, j));
325 }
326 }
327 } catch (MalformedURLException e) {
328 throw (IOException) new IOException(
329 "Illegal url.").initCause(e);
330 } catch (Exception e){
331 e.printStackTrace();
332 }
333 return null;
334 }
335
336 private void parseCommuneList(String input) {
337 if (input.indexOf(c0ptionListStart) != -1) {
338 while (input.indexOf("<option value=\"") != -1) {
339 int i = input.indexOf(c0ptionListStart);
340 int j = input.indexOf(cOptionListEnd, i+c0ptionListStart.length());
341 int k = input.indexOf("\"", i+c0ptionListStart.length());
342 if (j != -1 && k > (i + c0ptionListStart.length())) {
343 String lov = new String(input.substring(i+c0ptionListStart.length()-1, j));
344 if (lov.indexOf(">") != -1) {
345 System.out.println("parse "+lov);
346 listOfCommunes.add(lov);
347 } else
348 System.err.println("unable to parse commune string:"+lov);
349 }
350 input = input.substring(j+cOptionListEnd.length());
351 }
352 }
353 }
354
355 private String getFeuillesList() {
356 // get all images in one html page
357 String ln = null;
358 String lines = null;
359 HttpURLConnection urlConn2 = null;
360 try {
361 URL getAllImagesURL = new URL(baseURL + "/scpc/listerFeuillesParcommune.do?keepVolatileSession=&offset=2000");
362 urlConn2 = (HttpURLConnection)getAllImagesURL.openConnection();
363 setCookie(urlConn2);
364 urlConn2.connect();
365 System.out.println("GET "+getAllImagesURL);
366 BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn2.getInputStream()));
367 while ((ln = rd.readLine()) != null) {
368 lines += ln;
369 }
370 rd.close();
371 urlConn2.disconnect();
372 //System.out.println("GET="+lines);
373 } catch (IOException e) {
374 listOfFeuilles.clear();
375 e.printStackTrace();
376 }
377 return lines;
378 }
379
380 private void parseFeuillesList(String input) {
381 listOfFeuilles.clear();
382 while (input.indexOf(cImageLinkStart) != -1) {
383 input = input.substring(input.indexOf(cImageLinkStart)+cImageLinkStart.length());
384 String refFeuille = input.substring(0, input.indexOf("'"));
385 String nameFeuille = input.substring(
386 input.indexOf(cImageNameStart)+cImageNameStart.length(),
387 input.indexOf(" -"));
388 listOfFeuilles.add(new PlanImage(nameFeuille, refFeuille));
389 }
390 }
391
392 private String selectMunicipalityDialog(WMSLayer wmsLayer) {
393 JPanel p = new JPanel(new GridBagLayout());
394 String[] communeList = new String[listOfCommunes.size() + 1];
395 communeList[0] = tr("Choose from...");
396 for (int i = 0; i < listOfCommunes.size(); i++) {
397 communeList[i + 1] = listOfCommunes.elementAt(i).substring(listOfCommunes.elementAt(i).indexOf(">")+1);
398 }
399 JComboBox inputCommuneList = new JComboBox(communeList);
400 p.add(inputCommuneList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
401 JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null) {
402 private static final long serialVersionUID = 1L;
403 };
404 //pane.createDialog(Main.parent, tr("Select commune")).setVisible(true);
405 // this below is a temporary workaround to fix the "always on top" issue
406 JDialog dialog = pane.createDialog(Main.parent, tr("Select commune"));
407 CadastrePlugin.prepareDialog(dialog);
408 dialog.setVisible(true);
409 // till here
410 if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
411 return null;
412 return listOfCommunes.elementAt(inputCommuneList.getSelectedIndex()-1);
413 }
414
415 private int selectFeuilleDialog() {
416 JPanel p = new JPanel(new GridBagLayout());
417 Vector<String> ImageNames = new Vector<String>();
418 for (PlanImage src : listOfFeuilles) {
419 ImageNames.add(src.name);
420 }
421 JComboBox inputFeuilleList = new JComboBox(ImageNames);
422 p.add(inputFeuilleList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
423 JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null);
424 //pane.createDialog(Main.parent, tr("Select Feuille")).setVisible(true);
425 // this below is a temporary workaround to fix the "always on top" issue
426 JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
427 CadastrePlugin.prepareDialog(dialog);
428 dialog.setVisible(true);
429 // till here
430 if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
431 return -1;
432 int result = inputFeuilleList.getSelectedIndex();
433 return result;
434 }
435
436 private String buildRasterFeuilleInterfaceRef(String codeCommune) {
437 return cInterfaceRasterFeuille + "?f=" + codeCommune;
438 }
439
440 /**
441 * Retrieve the bounding box size in pixels of the whole commune (point 0,0 at top, left corner)
442 * and store it in given wmsLayer
443 * In case of raster image, we also check in the same http request if the image is already georeferenced
444 * and store the result in the wmsLayer as well.
445 * @param wmsLayer the WMSLayer where the commune data and images are stored
446 * @throws IOException
447 */
448 public void retrieveCommuneBBox(WMSLayer wmsLayer) throws IOException {
449 if (interfaceRef == null)
450 return;
451 String ln = null;
452 String line = null;
453 // send GET opening normally the small window with the commune overview
454 String content = baseURL + "/scpc/" + interfaceRef;
455 content += "&dontSaveLastForward&keepVolatileSession=";
456 searchFormURL = new URL(content);
457 urlConn = (HttpURLConnection)searchFormURL.openConnection();
458 urlConn.setRequestMethod("GET");
459 setCookie();
460 urlConn.connect();
461 if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
462 throw new IOException("Cannot get Cadastre response.");
463 }
464 System.out.println("GET "+searchFormURL);
465 BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
466 while ((ln = in.readLine()) != null) {
467 line += ln;
468 }
469 in.close();
470 urlConn.disconnect();
471 parseBBoxCommune(wmsLayer, line);
472 if (wmsLayer.isRaster() && !wmsLayer.isAlreadyGeoreferenced()) {
473 parseGeoreferences(wmsLayer, line);
474 }
475 }
476
477 private void parseBBoxCommune(WMSLayer wmsLayer, String input) {
478 if (input.indexOf(cBBoxCommunStart) != -1) {
479 input = input.substring(input.indexOf(cBBoxCommunStart));
480 int i = input.indexOf(",");
481 double minx = Double.parseDouble(input.substring(cBBoxCommunStart.length(), i));
482 int j = input.indexOf(",", i+1);
483 double miny = Double.parseDouble(input.substring(i+1, j));
484 int k = input.indexOf(",", j+1);
485 double maxx = Double.parseDouble(input.substring(j+1, k));
486 int l = input.indexOf(cBBoxCommunEnd, k+1);
487 double maxy = Double.parseDouble(input.substring(k+1, l));
488 wmsLayer.setCommuneBBox( new EastNorthBound(new EastNorth(minx,miny), new EastNorth(maxx,maxy)));
489 }
490 }
491
492 private void parseGeoreferences(WMSLayer wmsLayer, String input) {
493 if (input.lastIndexOf(cBBoxCommunStart) != -1) {
494 input = input.substring(input.lastIndexOf(cBBoxCommunStart));
495 input = input.substring(input.indexOf(cBBoxCommunEnd)+cBBoxCommunEnd.length());
496 int i = input.indexOf(",");
497 int j = input.indexOf(",", i+1);
498 double angle = Double.parseDouble(input.substring(i+1, j));
499 int k = input.indexOf(",", j+1);
500 double scale_origin = Double.parseDouble(input.substring(j+1, k));
501 int l = input.indexOf(",", k+1);
502 double dpi = Double.parseDouble(input.substring(k+1, l));
503 int m = input.indexOf(",", l+1);
504 double fX = Double.parseDouble(input.substring(l+1, m));
505 int n = input.indexOf(",", m+1);
506 double fY = Double.parseDouble(input.substring(m+1, n));
507 int o = input.indexOf(",", n+1);
508 double X0 = Double.parseDouble(input.substring(n+1, o));
509 int p = input.indexOf(",", o+1);
510 double Y0 = Double.parseDouble(input.substring(o+1, p));
511 if (X0 != 0.0 && Y0 != 0) {
512 wmsLayer.setAlreadyGeoreferenced(true);
513 wmsLayer.fX = fX;
514 wmsLayer.fY = fY;
515 wmsLayer.angle = angle;
516 wmsLayer.X0 = X0;
517 wmsLayer.Y0 = Y0;
518 }
519 System.out.println("parse georef:"+angle+","+scale_origin+","+dpi+","+fX+","+
520 fY+","+X0+","+Y0);
521 }
522 }
523
524 private void checkLayerDuplicates(WMSLayer wmsLayer) throws DuplicateLayerException {
525 if (Main.map != null) {
526 for (Layer l : Main.map.mapView.getAllLayers()) {
527 if (l instanceof WMSLayer && l.getName().equals(wmsLayer.getName()) && (l != wmsLayer)) {
528 System.out.println("Try to grab into a new layer when "+wmsLayer.getName()+" is already opened.");
529 // remove the duplicated layer
530 Main.map.mapView.removeLayer(wmsLayer);
531 throw new DuplicateLayerException();
532 }
533 }
534 }
535 }
536
537 public void cancel() {
538 if (urlConn != null) {
539 urlConn.setConnectTimeout(1);
540 urlConn.setReadTimeout(1);
541 //urlConn.disconnect();
542 }
543 downloadCancelled = true;
544 lastWMSLayerName = null;
545 }
546
547}
Note: See TracBrowser for help on using the repository browser.