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

Last change on this file since 17089 was 17089, checked in by pieren, 16 years ago

raster image feature implementation

  • Property svn:eol-style set to native
File size: 20.4 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 in WMS.\n" +
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
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.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"
254 // shall be something like: interfaceRef = "afficherCarteCommune.do?c=X2269";
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 }
274 }
275 return null;
276 } else if (lines.indexOf(cCommuneListStart) != -1 && lines.indexOf(cCommuneListEnd) != -1) {
277 // list of values parsed in listOfCommunes
278 int i = lines.indexOf(cCommuneListStart);
279 int j = lines.indexOf(cCommuneListEnd, i);
280 parseCommuneList(lines.substring(i, j));
281 }
282 } catch (MalformedURLException e) {
283 throw (IOException) new IOException(
284 "Illegal url.").initCause(e);
285 } catch (Exception e){
286 e.printStackTrace();
287 }
288 return null;
289 }
290
291 private void parseCommuneList(String input) {
292 if (input.indexOf(c0ptionListStart) != -1) {
293 while (input.indexOf("<option value=\"") != -1) {
294 int i = input.indexOf(c0ptionListStart);
295 int j = input.indexOf(cOptionListEnd, i+c0ptionListStart.length());
296 int k = input.indexOf("\"", i+c0ptionListStart.length());
297 if (j != -1 && k > (i + c0ptionListStart.length())) {
298 String lov = new String(input.substring(i+c0ptionListStart.length()-1, j));
299 if (lov.indexOf(">") != -1) {
300 System.out.println("parse "+lov);
301 listOfCommunes.add(lov);
302 } else
303 System.err.println("unable to parse commune string:"+lov);
304 }
305 input = input.substring(j+cOptionListEnd.length());
306 }
307 }
308 }
309
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
347 private String selectCommuneDialog() {
348 JPanel p = new JPanel(new GridBagLayout());
349 String[] communeList = new String[listOfCommunes.size() + 1];
350 communeList[0] = tr("Choose from...");
351 for (int i = 0; i < listOfCommunes.size(); i++) {
352 communeList[i + 1] = listOfCommunes.elementAt(i).substring(listOfCommunes.elementAt(i).indexOf(">")+1);
353 }
354 JComboBox inputCommuneList = new JComboBox(communeList);
355 p.add(inputCommuneList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
356 JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null) {
357 private static final long serialVersionUID = 1L;
358 };
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
365 if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
366 return null;
367 String result = listOfCommunes.elementAt(inputCommuneList.getSelectedIndex()-1);
368 return result.substring(1, result.indexOf(">")-2);
369 }
370
371 private int selectFeuilleDialog() {
372 JPanel p = new JPanel(new GridBagLayout());
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
386 if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
387 return -1;
388 int result = inputFeuilleList.getSelectedIndex();
389 return result;
390 }
391
392 private String buildRasterFeuilleInterfaceRef(String codeCommune) {
393 return cInterfaceRasterFeuille + "?f=" + codeCommune;
394 }
395
396 public EastNorthBound retrieveCommuneBBox() throws IOException {
397 if (interfaceRef == null)
398 return null;
399 String ln = null;
400 String line = null;
401 // send GET opening normally the small window with the commune overview
402 String content = baseURL + "/scpc/" + interfaceRef;
403 content += "&dontSaveLastForward&keepVolatileSession=";
404 searchFormURL = new URL(content);
405 urlConn = (HttpURLConnection)searchFormURL.openConnection();
406 urlConn.setRequestMethod("GET");
407 setCookie();
408 urlConn.connect();
409 if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
410 throw new IOException("Cannot get Cadastre response.");
411 }
412 System.out.println("GET "+searchFormURL);
413 BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
414 while ((ln = in.readLine()) != null) {
415 line += ln;
416 }
417 in.close();
418 urlConn.disconnect();
419 return parseBBoxCommune(line);
420 }
421
422 private EastNorthBound parseBBoxCommune(String input) {
423 if (input.indexOf(cBBoxCommunStart) != -1) {
424 input = input.substring(input.indexOf(cBBoxCommunStart));
425 int i = input.indexOf(",");
426 double minx = Double.parseDouble(input.substring(cBBoxCommunStart.length(), i));
427 int j = input.indexOf(",", i+1);
428 double miny = Double.parseDouble(input.substring(i+1, j));
429 int k = input.indexOf(",", j+1);
430 double maxx = Double.parseDouble(input.substring(j+1, k));
431 int l = input.indexOf(cBBoxCommunEnd, k+1);
432 double maxy = Double.parseDouble(input.substring(k+1, l));
433 return new EastNorthBound(new EastNorth(minx,miny), new EastNorth(maxx,maxy));
434 }
435 return null;
436 }
437
438 private void checkLayerDuplicates(WMSLayer wmsLayer) throws DuplicateLayerException {
439 if (Main.map != null) {
440 for (Layer l : Main.map.mapView.getAllLayers()) {
441 if (l instanceof WMSLayer && l.getName().equals(wmsLayer.getName()) && (l != wmsLayer)) {
442 System.out.println("Try to grab into a new layer when "+wmsLayer.getName()+" is already opened.");
443 // remove the duplicated layer
444 Main.map.mapView.removeLayer(wmsLayer);
445 throw new DuplicateLayerException();
446 }
447 }
448 }
449 }
450
451 public void cancel() {
452 if (urlConn != null) {
453 urlConn.setConnectTimeout(1);
454 urlConn.setReadTimeout(1);
455 //urlConn.disconnect();
456 }
457 downloadCancelled = true;
458 lastWMSLayerName = null;
459 }
460
461}
Note: See TracBrowser for help on using the repository browser.