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