source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreInterface.java@ 33514

Last change on this file since 33514 was 33514, checked in by donvip, 7 years ago

fix #josm15178 - use new HTTPS links from French cadastre - requires JOSM 12623+ to load Certigna certificate from platform keystore (not included in JRE keystore)

  • Property svn:eol-style set to native
File size: 27.9 KB
RevLine 
[32556]1// License: GPL. For details, see LICENSE file.
[13497]2package cadastre_fr;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.io.BufferedReader;
8import java.io.IOException;
[33514]9import java.io.InputStream;
[13497]10import java.io.InputStreamReader;
11import java.io.OutputStream;
[32212]12import java.net.CookieHandler;
[13497]13import java.net.HttpURLConnection;
14import java.net.MalformedURLException;
[32212]15import java.net.URISyntaxException;
[13497]16import java.net.URL;
[32211]17import java.nio.charset.StandardCharsets;
18import java.util.ArrayList;
[19267]19import java.util.Date;
[32212]20import java.util.HashMap;
[32211]21import java.util.List;
[13497]22
23import javax.swing.JComboBox;
[17089]24import javax.swing.JDialog;
[13497]25import javax.swing.JOptionPane;
26import javax.swing.JPanel;
27
28import org.openstreetmap.josm.Main;
29import org.openstreetmap.josm.data.coor.EastNorth;
[32212]30import org.openstreetmap.josm.data.validation.util.Entities;
[13497]31import org.openstreetmap.josm.gui.layer.Layer;
[33514]32import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
33import org.openstreetmap.josm.gui.util.GuiHelper;
34import org.openstreetmap.josm.io.OsmTransferException;
35import org.openstreetmap.josm.io.ProgressInputStream;
[13497]36import org.openstreetmap.josm.tools.GBC;
37
38public class CadastreInterface {
[32211]39 public boolean downloadCanceled;
[33514]40 private HttpURLConnection urlConn;
[13497]41
42 private String cookie;
[32211]43 private String interfaceRef;
44 private String lastWMSLayerName;
[13497]45 private URL searchFormURL;
[32211]46 private List<String> listOfCommunes = new ArrayList<>();
47 private List<String> listOfTA = new ArrayList<>();
48 static class PlanImage {
[17089]49 String name;
50 String ref;
51 PlanImage(String name, String ref) {
52 this.name = name;
53 this.ref = ref;
54 }
55 }
[32556]56
[32211]57 private List<PlanImage> listOfFeuilles = new ArrayList<>();
[19267]58 private long cookieTimestamp;
[13497]59
[33514]60 static final String BASE_URL = "https://www.cadastre.gouv.fr";
[32211]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 = ")";
[15961]68
[32211]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>";
[23190]76
[32211]77 static final long COOKIE_EXPIRATION = 30 * 60 * 1000L; // 30 minutes expressed in milliseconds
[15961]78
[32211]79 static final int RETRIES_GET_COOKIE = 10; // 10 times every 3 seconds means 30 seconds trying to get a cookie
[19267]80
[20412]81 public boolean retrieveInterface(WMSLayer wmsLayer) throws DuplicateLayerException, WMSException {
[32211]82 if (wmsLayer.getName().isEmpty())
[13497]83 return false;
[20765]84 boolean isCookieExpired = isCookieExpired();
85 if (wmsLayer.getName().equals(lastWMSLayerName) && !isCookieExpired)
[19267]86 return true;
[24954]87 if (!wmsLayer.getName().equals(lastWMSLayerName))
88 interfaceRef = null;
[16007]89 // open the session with the French Cadastre web front end
[26509]90 downloadCanceled = false;
[13497]91 try {
[20765]92 if (cookie == null || isCookieExpired) {
[13497]93 getCookie();
[20765]94 interfaceRef = null;
95 }
[20412]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) {
[19267]99 getInterface(wmsLayer);
100 this.lastWMSLayerName = wmsLayer.getName();
[13497]101 }
102 openInterface();
103 } catch (IOException e) {
[32211]104 Main.error(e);
[33514]105 GuiHelper.runInEDT(() ->
106 JOptionPane.showMessageDialog(Main.parent,
[17181]107 tr("Town/city {0} not found or not available\n" +
[33514]108 "or action canceled", wmsLayer.getLocation())));
[13497]109 return false;
110 }
111 return true;
112 }
113
[19267]114 /**
[23190]115 *
[19267]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 */
[13497]119 private void getCookie() throws IOException {
[32211]120 boolean success = false;
121 int retries = RETRIES_GET_COOKIE;
[13497]122 try {
[32211]123 searchFormURL = new URL(BASE_URL + "/scpc/accueil.do");
124 while (!success && retries > 0) {
[32556]125 urlConn = (HttpURLConnection) searchFormURL.openConnection();
[28961]126 urlConn.setRequestProperty("Connection", "close");
[19267]127 urlConn.setRequestMethod("GET");
128 urlConn.connect();
129 if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
[30859]130 Main.info("GET "+searchFormURL);
[32211]131 BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8));
[32556]132 while (in.readLine() != null) {
[32211]133 // read the buffer otherwise we sent POST too early
134 }
135 success = true;
[32212]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) {
[32556]140 if (handleCookie(cookieHandler.get(searchFormURL.toURI(), new HashMap<String, List<String>>()).get("Cookie").get(0))) {
[32212]141 break;
[32205]142 }
[32212]143 } else {
144 String headerName;
[32556]145 for (int i = 1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++) {
[32212]146 if ("Set-Cookie".equals(headerName) && handleCookie(urlConn.getHeaderField(i))) {
[32205]147 break;
[32556]148 }
[32212]149 }
[19267]150 }
151 } else {
[30859]152 Main.warn("Request to home page failed. Http error:"+urlConn.getResponseCode()+". Try again "+retries+" times");
[19267]153 CadastrePlugin.safeSleep(3000);
[32556]154 retries--;
[13497]155 }
156 }
[32212]157 } catch (MalformedURLException | URISyntaxException e) {
[30859]158 throw new IOException("Illegal url.", e);
[13497]159 }
160 }
161
[32212]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
[13497]178 public void resetCookie() {
[17089]179 lastWMSLayerName = null;
[19267]180 cookie = null;
[13497]181 }
[23190]182
[19267]183 public boolean isCookieExpired() {
184 long now = new Date().getTime();
[32211]185 if ((now - cookieTimestamp) > COOKIE_EXPIRATION) {
[30859]186 Main.info("cookie received at "+new Date(cookieTimestamp)+" expired (now is "+new Date(now)+")");
[19267]187 return true;
188 }
189 return false;
190 }
[15961]191
[19267]192 public void resetInterfaceRefIfNewLayer(String newWMSLayerName) {
[17089]193 if (!newWMSLayerName.equals(lastWMSLayerName)) {
[19267]194 interfaceRef = null;
[21516]195 cookie = null; // new since WMS server requires that we come back to the main form
[13497]196 }
197 }
[15961]198
[33514]199 private void setCookie() {
[17089]200 this.urlConn.setRequestProperty("Cookie", this.cookie);
[13497]201 }
[15961]202
[17089]203 public void setCookie(HttpURLConnection urlConn) {
204 urlConn.setRequestProperty("Cookie", this.cookie);
205 }
[23190]206
[13497]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) {
[32211]212 if (!wmsLayer.getCodeCommune().isEmpty()) {
[13497]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) {
[32211]217 // commune unknown, prompt the list of communes from server and try with codeCommune
218 String selected = selectMunicipalityDialog();
[21493]219 if (selected != null) {
[32211]220 String newCodeCommune = selected.substring(1, selected.indexOf('>') - 2);
221 String newLocation = selected.substring(selected.indexOf('>') + 1, selected.lastIndexOf(" - "));
[21493]222 wmsLayer.setCodeCommune(newCodeCommune);
223 wmsLayer.setLocation(newLocation);
224 Main.pref.put("cadastrewms.codeCommune", newCodeCommune);
225 Main.pref.put("cadastrewms.location", newLocation);
226 }
[13497]227 checkLayerDuplicates(wmsLayer);
228 interfaceRef = postForm(wmsLayer, wmsLayer.getCodeCommune());
229 }
[17089]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) {
[32211]234 wmsLayer.setCodeCommune(listOfFeuilles.get(res).name);
[17089]235 checkLayerDuplicates(wmsLayer);
236 interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune());
237 }
[13497]238 }
239 }
240 }
[15961]241
[13497]242 if (interfaceRef == null)
243 throw new IOException("Town/city " + wmsLayer.getLocation() + " not found.");
244 }
[15961]245
[32556]246 private void openInterface() throws IOException {
[13497]247 try {
248 // finally, open the interface on server side giving access to the wms server
[32211]249 URL interfaceURL = new URL(BASE_URL + "/scpc/"+interfaceRef);
[32556]250 urlConn = (HttpURLConnection) interfaceURL.openConnection();
[13497]251 urlConn.setRequestMethod("GET");
252 setCookie();
253 urlConn.connect();
254 if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
[16581]255 throw new IOException("Cannot open Cadastre interface. GET response:"+urlConn.getResponseCode());
[13497]256 }
[30859]257 Main.info("GET "+interfaceURL);
[32211]258 BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8));
[18838]259 // read the buffer otherwise we sent POST too early
[32211]260 StringBuilder lines = new StringBuilder();
261 String ln;
[17089]262 while ((ln = in.readLine()) != null) {
[32211]263 if (Main.isDebugEnabled()) {
264 lines.append(ln);
265 }
[17089]266 }
[30701]267 if (Main.isDebugEnabled()) {
[32211]268 Main.debug(lines.toString());
[30701]269 }
[13497]270 } catch (MalformedURLException e) {
271 throw (IOException) new IOException(
272 "CadastreGrabber: Illegal url.").initCause(e);
273 }
274 }
[15961]275
[13497]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.
[15961]280 * Two possibilities :
[13497]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>
[17089]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).
[15961]290 *
[17089]291 * @return retURL url to available code commune in the cadastre; "" if not found
[13497]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=";
[32211]302 if (codeCommune.isEmpty()) {
303 content += "&ville=" + java.net.URLEncoder.encode(wmsLayer.getLocation(), "UTF-8");
[13497]304 content += "&codePostal=";
305 } else {
306 content += "&codeCommune=" + codeCommune;
307 }
308 content += "&codeDepartement=";
[21191]309 content += wmsLayer.getDepartement();
[13497]310 content += "&nbResultatParPage=10";
[19078]311 content += "&x=0&y=0";
[32211]312 searchFormURL = new URL(BASE_URL + "/scpc/rechercherPlan.do");
[32556]313 urlConn = (HttpURLConnection) searchFormURL.openConnection();
[13497]314 urlConn.setRequestMethod("POST");
315 urlConn.setDoOutput(true);
316 urlConn.setDoInput(true);
317 setCookie();
[30738]318 try (OutputStream wr = urlConn.getOutputStream()) {
[32211]319 wr.write(content.getBytes(StandardCharsets.UTF_8));
[30738]320 Main.info("POST "+content);
321 wr.flush();
[13497]322 }
[32211]323 String ln;
324 StringBuilder sb = new StringBuilder();
325 try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8))) {
[30738]326 while ((ln = rd.readLine()) != null) {
[32211]327 sb.append(ln);
[30738]328 }
329 }
[32211]330 String lines = sb.toString();
[13497]331 urlConn.disconnect();
[17215]332 if (lines != null) {
[32211]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)));
[13497]337 }
[32211]338 if (!wmsLayer.isRaster() && lines.indexOf(C_INTERFACE_VECTOR) != -1) { // "afficherCarteCommune.do"
[17215]339 // shall be something like: interfaceRef = "afficherCarteCommune.do?c=X2269";
[32556]340 lines = lines.substring(lines.indexOf(C_INTERFACE_VECTOR), lines.length());
[32211]341 lines = lines.substring(0, lines.indexOf('\''));
[32212]342 lines = Entities.unescape(lines);
[30859]343 Main.info("interface ref.:"+lines);
[17215]344 return lines;
[32211]345 } else if (wmsLayer.isRaster() && lines.indexOf(C_INTERFACE_RASTER_TA) != -1) { // "afficherCarteTa.do"
[17215]346 // list of values parsed in listOfFeuilles (list all non-georeferenced images)
347 lines = getFeuillesList();
[26509]348 if (!downloadCanceled) {
[17215]349 parseFeuillesList(lines);
[32211]350 if (!listOfFeuilles.isEmpty()) {
[17215]351 int res = selectFeuilleDialog();
352 if (res != -1) {
[32211]353 wmsLayer.setCodeCommune(listOfFeuilles.get(res).name);
[17215]354 checkLayerDuplicates(wmsLayer);
355 interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune());
[32211]356 wmsLayer.setCodeCommune(listOfFeuilles.get(res).ref);
357 lines = buildRasterFeuilleInterfaceRef(listOfFeuilles.get(res).ref);
[32212]358 lines = Entities.unescape(lines);
[30859]359 Main.info("interface ref.:"+lines);
[17215]360 return lines;
361 }
362 }
363 }
364 return null;
[32211]365 } else if (lines.indexOf(C_COMMUNE_LIST_START) != -1 && lines.indexOf(C_COMMUNE_LIST_END) != -1) {
[17215]366 // list of values parsed in listOfCommunes
[32211]367 int i = lines.indexOf(C_COMMUNE_LIST_START);
368 int j = lines.indexOf(C_COMMUNE_LIST_END, i);
[17215]369 parseCommuneList(lines.substring(i, j));
370 }
[13497]371 }
372 } catch (MalformedURLException e) {
[32211]373 throw (IOException) new IOException("Illegal url.").initCause(e);
[32556]374 } catch (DuplicateLayerException e) {
[32211]375 Main.error(e);
[13497]376 }
377 return null;
378 }
[15961]379
[13497]380 private void parseCommuneList(String input) {
[32211]381 if (input.indexOf(C_OPTION_LIST_START) != -1) {
[13497]382 while (input.indexOf("<option value=\"") != -1) {
[32211]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) {
[32060]389 Main.info("parse "+lov);
[13497]390 listOfCommunes.add(lov);
391 } else
[30859]392 Main.error("unable to parse commune string:"+lov);
[13497]393 }
[32211]394 input = input.substring(j+C_OPTION_LIST_END.length());
[13497]395 }
396 }
397 }
[15961]398
[17089]399 private String getFeuillesList() {
400 // get all images in one html page
401 String ln = null;
[32211]402 StringBuilder lines = new StringBuilder();
[17089]403 HttpURLConnection urlConn2 = null;
404 try {
[32211]405 URL getAllImagesURL = new URL(BASE_URL + "/scpc/listerFeuillesParcommune.do?keepVolatileSession=&offset=2000");
[32556]406 urlConn2 = (HttpURLConnection) getAllImagesURL.openConnection();
[17089]407 setCookie(urlConn2);
408 urlConn2.connect();
[30738]409 Main.info("GET "+getAllImagesURL);
[32211]410 try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn2.getInputStream(), StandardCharsets.UTF_8))) {
[30738]411 while ((ln = rd.readLine()) != null) {
[32211]412 lines.append(ln);
[30738]413 }
[13497]414 }
[17089]415 urlConn2.disconnect();
416 } catch (IOException e) {
417 listOfFeuilles.clear();
[30738]418 Main.error(e);
[13497]419 }
[32211]420 return lines.toString();
[13497]421 }
[23190]422
[17089]423 private void parseFeuillesList(String input) {
424 listOfFeuilles.clear();
[22387]425 // get "Tableau d'assemblage"
[27034]426 String inputTA = input;
[22387]427 if (Main.pref.getBoolean("cadastrewms.useTA", false)) {
[32211]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('<'));
[22387]433 listOfFeuilles.add(new PlanImage(nameTA, refTA));
434 }
435 }
436 // get "Feuilles"
[32211]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('\''));
[17089]440 String nameFeuille = input.substring(
[32211]441 input.indexOf(C_IMAGE_NAME_START)+C_IMAGE_NAME_START.length(),
[17089]442 input.indexOf(" -"));
443 listOfFeuilles.add(new PlanImage(nameFeuille, refFeuille));
444 }
445 }
[23190]446
[32211]447 private String selectMunicipalityDialog() {
[13497]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++) {
[32211]452 communeList[i + 1] = listOfCommunes.get(i).substring(listOfCommunes.get(i).indexOf('>')+1);
[13497]453 }
[30532]454 JComboBox<String> inputCommuneList = new JComboBox<>(communeList);
[13497]455 p.add(inputCommuneList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
[31993]456 JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null);
[17089]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
[13497]462 if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
463 return null;
[32211]464 return listOfCommunes.get(inputCommuneList.getSelectedIndex()-1);
[13497]465 }
466
[17089]467 private int selectFeuilleDialog() {
[13497]468 JPanel p = new JPanel(new GridBagLayout());
[32211]469 List<String> imageNames = new ArrayList<>();
[17089]470 for (PlanImage src : listOfFeuilles) {
[30532]471 imageNames.add(src.name);
[17089]472 }
[32211]473 JComboBox<String> inputFeuilleList = new JComboBox<>(imageNames.toArray(new String[]{}));
[17089]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
[13497]481 if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
[17089]482 return -1;
[32211]483 return inputFeuilleList.getSelectedIndex();
[13497]484 }
485
[32211]486 private static String buildRasterFeuilleInterfaceRef(String codeCommune) {
487 return C_INTERFACE_RASTER_FEUILLE + "?f=" + codeCommune;
[13497]488 }
[15961]489
[18271]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
[23190]494 * and store the result in the wmsLayer as well.
[18271]495 * @param wmsLayer the WMSLayer where the commune data and images are stored
496 */
497 public void retrieveCommuneBBox(WMSLayer wmsLayer) throws IOException {
[13497]498 if (interfaceRef == null)
[18271]499 return;
[13497]500 // send GET opening normally the small window with the commune overview
[32211]501 String content = BASE_URL + "/scpc/" + interfaceRef;
[13497]502 content += "&dontSaveLastForward&keepVolatileSession=";
503 searchFormURL = new URL(content);
[32556]504 urlConn = (HttpURLConnection) searchFormURL.openConnection();
[13497]505 urlConn.setRequestMethod("GET");
506 setCookie();
507 urlConn.connect();
508 if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
[16581]509 throw new IOException("Cannot get Cadastre response.");
[13497]510 }
[30859]511 Main.info("GET "+searchFormURL);
[32211]512 String ln;
513 StringBuilder sb = new StringBuilder();
514 try (BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8))) {
[30738]515 while ((ln = in.readLine()) != null) {
[32211]516 sb.append(ln);
[30738]517 }
[13497]518 }
519 urlConn.disconnect();
[32211]520 String line = sb.toString();
[18271]521 parseBBoxCommune(wmsLayer, line);
522 if (wmsLayer.isRaster() && !wmsLayer.isAlreadyGeoreferenced()) {
523 parseGeoreferences(wmsLayer, line);
524 }
[13497]525 }
[15961]526
[32211]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);
[13497]533 double miny = Double.parseDouble(input.substring(i+1, j));
[32211]534 int k = input.indexOf(',', j+1);
[13497]535 double maxx = Double.parseDouble(input.substring(j+1, k));
[32211]536 int l = input.indexOf(C_BBOX_COMMUN_END, k+1);
[13497]537 double maxy = Double.parseDouble(input.substring(k+1, l));
[32556]538 wmsLayer.setCommuneBBox(new EastNorthBound(new EastNorth(minx, miny), new EastNorth(maxx, maxy)));
[13497]539 }
540 }
[23190]541
[32211]542 private static void parseGeoreferences(WMSLayer wmsLayer, String input) {
[30738]543 /* commented since cadastre WMS changes mid july 2013
[29801]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// }
[32211]574// Main.info("parse georef:"+unknown_yet+","+angle+","+scale_origin+","+dpi+","+fX+","+fY+","+X0+","+Y0);
[29801]575// }
[18271]576 }
[23190]577
[32211]578 private static void checkLayerDuplicates(WMSLayer wmsLayer) throws DuplicateLayerException {
[13497]579 if (Main.map != null) {
[32329]580 for (Layer l : Main.getLayerManager().getLayers()) {
[32211]581 if (l instanceof WMSLayer && l.getName().equals(wmsLayer.getName()) && (!l.equals(wmsLayer))) {
[32060]582 Main.info("Try to grab into a new layer when "+wmsLayer.getName()+" is already opened.");
[13497]583 // remove the duplicated layer
[32425]584 Main.getLayerManager().removeLayer(wmsLayer);
[13497]585 throw new DuplicateLayerException();
586 }
587 }
588 }
589 }
[15961]590
591 public void cancel() {
[13497]592 if (urlConn != null) {
593 urlConn.setConnectTimeout(1);
594 urlConn.setReadTimeout(1);
595 }
[26509]596 downloadCanceled = true;
[17089]597 lastWMSLayerName = null;
[13497]598 }
[33514]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 }
[13497]607}
Note: See TracBrowser for help on using the repository browser.