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

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

Improve renaming after municipality selection

  • Property svn:eol-style set to native
File size: 24.6 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 }
155 }
156
157 public void setCookie() {
158 this.urlConn.setRequestProperty("Cookie", this.cookie);
159 }
160
161 public void setCookie(HttpURLConnection urlConn) {
162 urlConn.setRequestProperty("Cookie", this.cookie);
163 }
164
165 private void getInterface(WMSLayer wmsLayer) throws IOException, DuplicateLayerException {
166 // first attempt : search for given name without codeCommune
167 interfaceRef = postForm(wmsLayer, "");
168 // second attempt either from known codeCommune (e.g. from cache) or from ComboBox
169 if (interfaceRef == null) {
170 if (!wmsLayer.getCodeCommune().equals("")) {
171 // codeCommune is already known (from previous request or from cache on disk)
172 interfaceRef = postForm(wmsLayer, wmsLayer.getCodeCommune());
173 } else {
174 if (listOfCommunes.size() > 1) {
175 // commune unknown, prompt the list of communes from
176 // server and try with codeCommune
177 String selected = selectMunicipalityDialog(wmsLayer);
178 if (selected != null) {
179 String newCodeCommune = selected.substring(1, selected.indexOf(">")-2);
180 String newLocation = selected.substring(selected.indexOf(">")+1, selected.lastIndexOf(" - "));
181 wmsLayer.setCodeCommune(newCodeCommune);
182 wmsLayer.setLocation(newLocation);
183 Main.pref.put("cadastrewms.codeCommune", newCodeCommune);
184 Main.pref.put("cadastrewms.location", newLocation);
185 }
186 checkLayerDuplicates(wmsLayer);
187 interfaceRef = postForm(wmsLayer, wmsLayer.getCodeCommune());
188 }
189 if (listOfCommunes.size() == 1 && wmsLayer.isRaster()) {
190 // commune known but raster format. Select "Feuille" (non-georeferenced image) from list.
191 int res = selectFeuilleDialog();
192 if (res != -1) {
193 wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).name);
194 checkLayerDuplicates(wmsLayer);
195 interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune());
196 }
197 }
198 }
199 }
200
201 if (interfaceRef == null)
202 throw new IOException("Town/city " + wmsLayer.getLocation() + " not found.");
203 }
204
205 private void openInterface() throws IOException {
206 try {
207 // finally, open the interface on server side giving access to the wms server
208 String lines = null;
209 String ln = null;
210 URL interfaceURL = new URL(baseURL + "/scpc/"+interfaceRef);
211 urlConn = (HttpURLConnection)interfaceURL.openConnection();
212 urlConn.setRequestMethod("GET");
213 setCookie();
214 urlConn.connect();
215 if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
216 throw new IOException("Cannot open Cadastre interface. GET response:"+urlConn.getResponseCode());
217 }
218 System.out.println("GET "+interfaceURL);
219 BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
220 // read the buffer otherwise we sent POST too early
221 while ((ln = in.readLine()) != null) {
222 lines += ln;
223 }
224 } catch (MalformedURLException e) {
225 throw (IOException) new IOException(
226 "CadastreGrabber: Illegal url.").initCause(e);
227 }
228 }
229
230 /**
231 * Post the form with the commune name and check the returned answer which is embedded
232 * in HTTP XML packets. This function doesn't use an XML parser yet but that would be a good idea
233 * for the next releases.
234 * Two possibilities :
235 * - either the commune name matches and we receive an URL starting with "afficherCarteCommune.do" or
236 * - we don't receive a single answer but a list of possible values. This answer looks like:
237 * <select name="codeCommune" class="long erreur" id="codeCommune">
238 * <option value="">Choisir</option>
239 * <option value="50061" >COLMARS - 04370</option>
240 * <option value="QK066" >COLMAR - 68000</option>
241 * </select>
242 * The returned string is the interface name used in further requests, e.g. "afficherCarteCommune.do?c=QP224"
243 * where QP224 is the code commune known by the WMS (or "afficherCarteTa.do?c=..." for raster images).
244 *
245 * @param location
246 * @param codeCommune
247 * @return retURL url to available code commune in the cadastre; "" if not found
248 * @throws IOException
249 */
250 private String postForm(WMSLayer wmsLayer, String codeCommune) throws IOException {
251 try {
252 String ln = null;
253 String lines = null;
254 listOfCommunes.clear();
255 listOfTA.clear();
256 // send a POST request with a city/town/village name
257 String content = "numerovoie=";
258 content += "&indiceRepetition=";
259 content += "&nomvoie=";
260 content += "&lieuDit=";
261 if (codeCommune == "") {
262 content += "&ville=" + new String(java.net.URLEncoder.encode(wmsLayer.getLocation(), "UTF-8"));
263 content += "&codePostal=";
264 } else {
265 content += "&codeCommune=" + codeCommune;
266 }
267 content += "&codeDepartement=";
268 content += wmsLayer.getDepartement();
269 content += "&nbResultatParPage=10";
270 content += "&x=0&y=0";
271 searchFormURL = new URL(baseURL + "/scpc/rechercherPlan.do");
272 urlConn = (HttpURLConnection)searchFormURL.openConnection();
273 urlConn.setRequestMethod("POST");
274 urlConn.setDoOutput(true);
275 urlConn.setDoInput(true);
276 setCookie();
277 OutputStream wr = urlConn.getOutputStream();
278 wr.write(content.getBytes());
279 System.out.println("POST "+content);
280 wr.flush();
281 wr.close();
282 BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
283 while ((ln = rd.readLine()) != null) {
284 lines += ln;
285 }
286 rd.close();
287 urlConn.disconnect();
288 if (lines != null) {
289 if (lines.indexOf(cImageFormat) != -1) {
290 int i = lines.indexOf(cImageFormat);
291 int j = lines.indexOf(".", i);
292 wmsLayer.setRaster(lines.substring(i+cImageFormat.length(), j).equals("image"));
293 }
294 if (!wmsLayer.isRaster() && lines.indexOf(cInterfaceVector) != -1) { // "afficherCarteCommune.do"
295 // shall be something like: interfaceRef = "afficherCarteCommune.do?c=X2269";
296 lines = lines.substring(lines.indexOf(cInterfaceVector),lines.length());
297 lines = lines.substring(0, lines.indexOf("'"));
298 System.out.println("interface ref.:"+lines);
299 return lines;
300 } else if (wmsLayer.isRaster() && lines.indexOf(cInterfaceRasterTA) != -1) { // "afficherCarteTa.do"
301 // list of values parsed in listOfFeuilles (list all non-georeferenced images)
302 lines = getFeuillesList();
303 if (!downloadCancelled) {
304 parseFeuillesList(lines);
305 if (listOfFeuilles.size() > 0) {
306 int res = selectFeuilleDialog();
307 if (res != -1) {
308 wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).name);
309 checkLayerDuplicates(wmsLayer);
310 interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune());
311 wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).ref);
312 lines = buildRasterFeuilleInterfaceRef(listOfFeuilles.elementAt(res).ref);
313 System.out.println("interface ref.:"+lines);
314 return lines;
315 }
316 }
317 }
318 return null;
319 } else if (lines.indexOf(cCommuneListStart) != -1 && lines.indexOf(cCommuneListEnd) != -1) {
320 // list of values parsed in listOfCommunes
321 int i = lines.indexOf(cCommuneListStart);
322 int j = lines.indexOf(cCommuneListEnd, i);
323 parseCommuneList(lines.substring(i, j));
324 }
325 }
326 } catch (MalformedURLException e) {
327 throw (IOException) new IOException(
328 "Illegal url.").initCause(e);
329 } catch (Exception e){
330 e.printStackTrace();
331 }
332 return null;
333 }
334
335 private void parseCommuneList(String input) {
336 if (input.indexOf(c0ptionListStart) != -1) {
337 while (input.indexOf("<option value=\"") != -1) {
338 int i = input.indexOf(c0ptionListStart);
339 int j = input.indexOf(cOptionListEnd, i+c0ptionListStart.length());
340 int k = input.indexOf("\"", i+c0ptionListStart.length());
341 if (j != -1 && k > (i + c0ptionListStart.length())) {
342 String lov = new String(input.substring(i+c0ptionListStart.length()-1, j));
343 if (lov.indexOf(">") != -1) {
344 System.out.println("parse "+lov);
345 listOfCommunes.add(lov);
346 } else
347 System.err.println("unable to parse commune string:"+lov);
348 }
349 input = input.substring(j+cOptionListEnd.length());
350 }
351 }
352 }
353
354 private String getFeuillesList() {
355 // get all images in one html page
356 String ln = null;
357 String lines = null;
358 HttpURLConnection urlConn2 = null;
359 try {
360 URL getAllImagesURL = new URL(baseURL + "/scpc/listerFeuillesParcommune.do?keepVolatileSession=&offset=2000");
361 urlConn2 = (HttpURLConnection)getAllImagesURL.openConnection();
362 setCookie(urlConn2);
363 urlConn2.connect();
364 System.out.println("GET "+getAllImagesURL);
365 BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn2.getInputStream()));
366 while ((ln = rd.readLine()) != null) {
367 lines += ln;
368 }
369 rd.close();
370 urlConn2.disconnect();
371 //System.out.println("GET="+lines);
372 } catch (IOException e) {
373 listOfFeuilles.clear();
374 e.printStackTrace();
375 }
376 return lines;
377 }
378
379 private void parseFeuillesList(String input) {
380 listOfFeuilles.clear();
381 while (input.indexOf(cImageLinkStart) != -1) {
382 input = input.substring(input.indexOf(cImageLinkStart)+cImageLinkStart.length());
383 String refFeuille = input.substring(0, input.indexOf("'"));
384 String nameFeuille = input.substring(
385 input.indexOf(cImageNameStart)+cImageNameStart.length(),
386 input.indexOf(" -"));
387 listOfFeuilles.add(new PlanImage(nameFeuille, refFeuille));
388 }
389 }
390
391 private String selectMunicipalityDialog(WMSLayer wmsLayer) {
392 JPanel p = new JPanel(new GridBagLayout());
393 String[] communeList = new String[listOfCommunes.size() + 1];
394 communeList[0] = tr("Choose from...");
395 for (int i = 0; i < listOfCommunes.size(); i++) {
396 communeList[i + 1] = listOfCommunes.elementAt(i).substring(listOfCommunes.elementAt(i).indexOf(">")+1);
397 }
398 JComboBox inputCommuneList = new JComboBox(communeList);
399 p.add(inputCommuneList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
400 JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null) {
401 private static final long serialVersionUID = 1L;
402 };
403 //pane.createDialog(Main.parent, tr("Select commune")).setVisible(true);
404 // this below is a temporary workaround to fix the "always on top" issue
405 JDialog dialog = pane.createDialog(Main.parent, tr("Select commune"));
406 CadastrePlugin.prepareDialog(dialog);
407 dialog.setVisible(true);
408 // till here
409 if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
410 return null;
411 return listOfCommunes.elementAt(inputCommuneList.getSelectedIndex()-1);
412 }
413
414 private int selectFeuilleDialog() {
415 JPanel p = new JPanel(new GridBagLayout());
416 Vector<String> ImageNames = new Vector<String>();
417 for (PlanImage src : listOfFeuilles) {
418 ImageNames.add(src.name);
419 }
420 JComboBox inputFeuilleList = new JComboBox(ImageNames);
421 p.add(inputFeuilleList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
422 JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null);
423 //pane.createDialog(Main.parent, tr("Select Feuille")).setVisible(true);
424 // this below is a temporary workaround to fix the "always on top" issue
425 JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
426 CadastrePlugin.prepareDialog(dialog);
427 dialog.setVisible(true);
428 // till here
429 if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
430 return -1;
431 int result = inputFeuilleList.getSelectedIndex();
432 return result;
433 }
434
435 private String buildRasterFeuilleInterfaceRef(String codeCommune) {
436 return cInterfaceRasterFeuille + "?f=" + codeCommune;
437 }
438
439 /**
440 * Retrieve the bounding box size in pixels of the whole commune (point 0,0 at top, left corner)
441 * and store it in given wmsLayer
442 * In case of raster image, we also check in the same http request if the image is already georeferenced
443 * and store the result in the wmsLayer as well.
444 * @param wmsLayer the WMSLayer where the commune data and images are stored
445 * @throws IOException
446 */
447 public void retrieveCommuneBBox(WMSLayer wmsLayer) throws IOException {
448 if (interfaceRef == null)
449 return;
450 String ln = null;
451 String line = null;
452 // send GET opening normally the small window with the commune overview
453 String content = baseURL + "/scpc/" + interfaceRef;
454 content += "&dontSaveLastForward&keepVolatileSession=";
455 searchFormURL = new URL(content);
456 urlConn = (HttpURLConnection)searchFormURL.openConnection();
457 urlConn.setRequestMethod("GET");
458 setCookie();
459 urlConn.connect();
460 if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
461 throw new IOException("Cannot get Cadastre response.");
462 }
463 System.out.println("GET "+searchFormURL);
464 BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
465 while ((ln = in.readLine()) != null) {
466 line += ln;
467 }
468 in.close();
469 urlConn.disconnect();
470 parseBBoxCommune(wmsLayer, line);
471 if (wmsLayer.isRaster() && !wmsLayer.isAlreadyGeoreferenced()) {
472 parseGeoreferences(wmsLayer, line);
473 }
474 }
475
476 private void parseBBoxCommune(WMSLayer wmsLayer, String input) {
477 if (input.indexOf(cBBoxCommunStart) != -1) {
478 input = input.substring(input.indexOf(cBBoxCommunStart));
479 int i = input.indexOf(",");
480 double minx = Double.parseDouble(input.substring(cBBoxCommunStart.length(), i));
481 int j = input.indexOf(",", i+1);
482 double miny = Double.parseDouble(input.substring(i+1, j));
483 int k = input.indexOf(",", j+1);
484 double maxx = Double.parseDouble(input.substring(j+1, k));
485 int l = input.indexOf(cBBoxCommunEnd, k+1);
486 double maxy = Double.parseDouble(input.substring(k+1, l));
487 wmsLayer.setCommuneBBox( new EastNorthBound(new EastNorth(minx,miny), new EastNorth(maxx,maxy)));
488 }
489 }
490
491 private void parseGeoreferences(WMSLayer wmsLayer, String input) {
492 if (input.lastIndexOf(cBBoxCommunStart) != -1) {
493 input = input.substring(input.lastIndexOf(cBBoxCommunStart));
494 input = input.substring(input.indexOf(cBBoxCommunEnd)+cBBoxCommunEnd.length());
495 int i = input.indexOf(",");
496 int j = input.indexOf(",", i+1);
497 double angle = Double.parseDouble(input.substring(i+1, j));
498 int k = input.indexOf(",", j+1);
499 double scale_origin = Double.parseDouble(input.substring(j+1, k));
500 int l = input.indexOf(",", k+1);
501 double dpi = Double.parseDouble(input.substring(k+1, l));
502 int m = input.indexOf(",", l+1);
503 double fX = Double.parseDouble(input.substring(l+1, m));
504 int n = input.indexOf(",", m+1);
505 double fY = Double.parseDouble(input.substring(m+1, n));
506 int o = input.indexOf(",", n+1);
507 double X0 = Double.parseDouble(input.substring(n+1, o));
508 int p = input.indexOf(",", o+1);
509 double Y0 = Double.parseDouble(input.substring(o+1, p));
510 if (X0 != 0.0 && Y0 != 0) {
511 wmsLayer.setAlreadyGeoreferenced(true);
512 wmsLayer.fX = fX;
513 wmsLayer.fY = fY;
514 wmsLayer.angle = angle;
515 wmsLayer.X0 = X0;
516 wmsLayer.Y0 = Y0;
517 }
518 System.out.println("parse georef:"+angle+","+scale_origin+","+dpi+","+fX+","+
519 fY+","+X0+","+Y0);
520 }
521 }
522
523 private void checkLayerDuplicates(WMSLayer wmsLayer) throws DuplicateLayerException {
524 if (Main.map != null) {
525 for (Layer l : Main.map.mapView.getAllLayers()) {
526 if (l instanceof WMSLayer && l.getName().equals(wmsLayer.getName()) && (l != wmsLayer)) {
527 System.out.println("Try to grab into a new layer when "+wmsLayer.getName()+" is already opened.");
528 // remove the duplicated layer
529 Main.map.mapView.removeLayer(wmsLayer);
530 throw new DuplicateLayerException();
531 }
532 }
533 }
534 }
535
536 public void cancel() {
537 if (urlConn != null) {
538 urlConn.setConnectTimeout(1);
539 urlConn.setReadTimeout(1);
540 //urlConn.disconnect();
541 }
542 downloadCancelled = true;
543 lastWMSLayerName = null;
544 }
545
546}
Note: See TracBrowser for help on using the repository browser.