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