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

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

improve download cancellation

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