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

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

Added autodetection and use of raster images already georeferenced

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