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