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