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