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