1 | package cadastre_fr;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.GridBagLayout;
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.io.OutputStream;
|
---|
10 | import java.net.HttpURLConnection;
|
---|
11 | import java.net.MalformedURLException;
|
---|
12 | import java.net.URL;
|
---|
13 | import java.util.Vector;
|
---|
14 |
|
---|
15 | import javax.swing.JComboBox;
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
21 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
22 | import org.openstreetmap.josm.tools.GBC;
|
---|
23 |
|
---|
24 | public class CadastreInterface {
|
---|
25 | public boolean downloadCancelled = false;
|
---|
26 | public HttpURLConnection urlConn = null;
|
---|
27 |
|
---|
28 | private CadastreGrabber cadastreGrabber;
|
---|
29 | private String cookie;
|
---|
30 | private String interfaceRef = null;
|
---|
31 | private URL searchFormURL;
|
---|
32 | private Vector<String> listOfCommunes = new Vector<String>();
|
---|
33 | private Vector<String> listOfTA = new Vector<String>();
|
---|
34 |
|
---|
35 | final String baseURL = "http://www.cadastre.gouv.fr";
|
---|
36 | final String cImageFormat = "Cette commune est au format ";
|
---|
37 | final String cCommuneListStart = "<select name=\"codeCommune\"";
|
---|
38 | final String cCommuneListEnd = "</select>";
|
---|
39 | final String c0ptionListStart = "<option value=\"";
|
---|
40 | final String cOptionListEnd = "</option>";
|
---|
41 | final String cBBoxCommunStart = "new GeoBox(";
|
---|
42 | final String cBBoxCommunEnd = ")";
|
---|
43 |
|
---|
44 | final String cInterfaceVector = "afficherCarteCommune.do";
|
---|
45 | final String cInterfaceRaster = "afficherCarteTa.do";
|
---|
46 |
|
---|
47 | CadastreInterface(CadastreGrabber cadastreGrabber) {
|
---|
48 | this.cadastreGrabber = cadastreGrabber;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public boolean retrieveInterface(WMSLayer wmsLayer) throws DuplicateLayerException {
|
---|
52 | if (wmsLayer.name.equals(""))
|
---|
53 | return false;
|
---|
54 | // open the session with the french Cadastre web front end
|
---|
55 | downloadCancelled = false;
|
---|
56 | try {
|
---|
57 | if (cookie == null || !wmsLayer.name.equals(cadastreGrabber.getLastWMSLayerName())) {
|
---|
58 | getCookie();
|
---|
59 | getInterface(wmsLayer);
|
---|
60 | cadastreGrabber.setLastWMSLayerName(wmsLayer.name);
|
---|
61 | }
|
---|
62 | openInterface();
|
---|
63 | } catch (IOException e) {
|
---|
64 | JOptionPane.showMessageDialog(Main.parent,
|
---|
65 | tr("Town/city {0} not found or not available in WMS.\n" +
|
---|
66 | "Please check its availibility on www.cadastre.gouv.fr", wmsLayer.getLocation()));
|
---|
67 | return false;
|
---|
68 | }
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void getCookie() throws IOException {
|
---|
73 | try {
|
---|
74 | // first, get the cookie from Cadastre to allow next downloads
|
---|
75 | searchFormURL = new URL(baseURL + "/scpc/rechercherPlan.do");
|
---|
76 | urlConn = (HttpURLConnection)searchFormURL.openConnection();
|
---|
77 | urlConn.setRequestMethod("GET");
|
---|
78 | urlConn.connect();
|
---|
79 | if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
---|
80 | throw (IOException) new IOException("Cannot get Cadastre cookie.");
|
---|
81 | }
|
---|
82 | BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
|
---|
83 | while(in.readLine() != null) {} // read the buffer otherwise we sent POST too early
|
---|
84 | String headerName=null;
|
---|
85 | for (int i=1; (headerName = urlConn.getHeaderFieldKey(i))!=null; i++) {
|
---|
86 | if (headerName.equals("Set-Cookie")) {
|
---|
87 | cookie = urlConn.getHeaderField(i);
|
---|
88 | cookie = cookie.substring(0, cookie.indexOf(";"));
|
---|
89 | System.out.println("Cookie="+cookie);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | } catch (MalformedURLException e) {
|
---|
93 | throw (IOException) new IOException(
|
---|
94 | "Illegal url.").initCause(e);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | public void resetCookie() {
|
---|
99 | cadastreGrabber.setLastWMSLayerName(null);
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void resetCookieIfNewLayer(String newWMSLayerName) {
|
---|
103 | if (!newWMSLayerName.equals(cadastreGrabber.getLastWMSLayerName())) {
|
---|
104 | resetCookie();
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void setCookie() {
|
---|
109 | urlConn.setRequestProperty("Cookie", cookie);
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void getInterface(WMSLayer wmsLayer) throws IOException, DuplicateLayerException {
|
---|
113 | // first attempt : search for given name without codeCommune
|
---|
114 | interfaceRef = postForm(wmsLayer, "");
|
---|
115 | // second attempt either from known codeCommune (e.g. from cache) or from ComboBox
|
---|
116 | if (interfaceRef == null) {
|
---|
117 | if (!wmsLayer.getCodeCommune().equals("")) {
|
---|
118 | // codeCommune is already known (from previous request or from cache on disk)
|
---|
119 | interfaceRef = postForm(wmsLayer, wmsLayer.getCodeCommune());
|
---|
120 | } else {
|
---|
121 | if (listOfCommunes.size() > 1) {
|
---|
122 | // commune unknown, prompt the list of communes from
|
---|
123 | // server and try with codeCommune
|
---|
124 | wmsLayer.setCodeCommune(selectCommuneDialog());
|
---|
125 | checkLayerDuplicates(wmsLayer);
|
---|
126 | interfaceRef = postForm(wmsLayer, wmsLayer.getCodeCommune());
|
---|
127 | }
|
---|
128 | if (wmsLayer.isRaster() && listOfTA.size() > 1) {
|
---|
129 | // commune known but raster format. Select "tableau d'assemblage" from list.
|
---|
130 | wmsLayer.setCodeCommune(selectTADialog());
|
---|
131 | checkLayerDuplicates(wmsLayer);
|
---|
132 | interfaceRef = buildRasterInterfaceRef(wmsLayer.getCodeCommune());
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (interfaceRef == null)
|
---|
138 | throw new IOException("Town/city " + wmsLayer.getLocation() + " not found.");
|
---|
139 | }
|
---|
140 |
|
---|
141 | private void openInterface() throws IOException {
|
---|
142 | try {
|
---|
143 | // finally, open the interface on server side giving access to the wms server
|
---|
144 | URL interfaceURL = new URL(baseURL + "/scpc/"+interfaceRef);
|
---|
145 | urlConn = (HttpURLConnection)interfaceURL.openConnection();
|
---|
146 | urlConn.setRequestMethod("GET");
|
---|
147 | setCookie();
|
---|
148 | urlConn.connect();
|
---|
149 | if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
---|
150 | throw (IOException) new IOException("Cannot open Cadastre interface. GET response:"+urlConn.getResponseCode());
|
---|
151 | }
|
---|
152 | BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
|
---|
153 | while(in.readLine() != null) {} // read the buffer otherwise we sent POST too early
|
---|
154 | System.out.println("GET to open interface sent");
|
---|
155 | } catch (MalformedURLException e) {
|
---|
156 | throw (IOException) new IOException(
|
---|
157 | "CadastreGrabber: Illegal url.").initCause(e);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Post the form with the commune name and check the returned answer which is embedded
|
---|
163 | * in HTTP XML packets. This function doesn't use an XML parser yet but that would be a good idea
|
---|
164 | * for the next releases.
|
---|
165 | * Two possibilities :
|
---|
166 | * - either the commune name matches and we receive an URL starting with "afficherCarteCommune.do" or
|
---|
167 | * - we don't receive a single answer but a list of possible values. This answer looks like:
|
---|
168 | * <select name="codeCommune" class="long erreur" id="codeCommune">
|
---|
169 | * <option value="">Choisir</option>
|
---|
170 | * <option value="50061" >COLMARS - 04370</option>
|
---|
171 | * <option value="QK066" >COLMAR - 68000</option>
|
---|
172 | * </select>
|
---|
173 | *
|
---|
174 | * @param location
|
---|
175 | * @param codeCommune
|
---|
176 | * @return retURL url to available cadastre vectorised master piece; "" if not found
|
---|
177 | * @throws IOException
|
---|
178 | */
|
---|
179 | private String postForm(WMSLayer wmsLayer, String codeCommune) throws IOException {
|
---|
180 | try {
|
---|
181 | String ln = null;
|
---|
182 | String line = null;
|
---|
183 | listOfCommunes.clear();
|
---|
184 | listOfTA.clear();
|
---|
185 | // send a POST request with a city/town/village name
|
---|
186 | String content = "numerovoie=";
|
---|
187 | content += "&indiceRepetition=";
|
---|
188 | content += "&nomvoie=";
|
---|
189 | content += "&lieuDit=";
|
---|
190 | if (codeCommune == "") {
|
---|
191 | content += "&ville=" + new String(java.net.URLEncoder.encode(wmsLayer.getLocation(), "UTF-8"));
|
---|
192 | content += "&codePostal=";
|
---|
193 | } else {
|
---|
194 | content += "&codeCommune=" + codeCommune;
|
---|
195 | }
|
---|
196 | content += "&codeDepartement=";
|
---|
197 | content += "&nbResultatParPage=10";
|
---|
198 | urlConn = (HttpURLConnection)searchFormURL.openConnection();
|
---|
199 | urlConn.setRequestMethod("POST");
|
---|
200 | urlConn.setDoOutput(true);
|
---|
201 | urlConn.setDoInput(true);
|
---|
202 | setCookie();
|
---|
203 | OutputStream wr = urlConn.getOutputStream();
|
---|
204 | wr.write(content.getBytes());
|
---|
205 | wr.flush();
|
---|
206 | wr.close();
|
---|
207 | BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
|
---|
208 | while ((ln = rd.readLine()) != null) {
|
---|
209 | line += ln;
|
---|
210 | }
|
---|
211 | rd.close();
|
---|
212 | urlConn.disconnect();
|
---|
213 | System.out.println("POST="+line);
|
---|
214 | if (line.indexOf(cImageFormat) != -1) {
|
---|
215 | int i = line.indexOf(cImageFormat);
|
---|
216 | int j = line.indexOf(".", i);
|
---|
217 | wmsLayer.setRaster(line.substring(i+cImageFormat.length(), j).equals("image"));
|
---|
218 | }
|
---|
219 | if (!wmsLayer.isRaster() && line.indexOf(cInterfaceVector) != -1) { // "afficherCarteCommune.do"
|
---|
220 | // shall be something like: interfaceRef = "afficherCarteCommune.do?c=X2269";
|
---|
221 | line = line.substring(line.indexOf(cInterfaceVector),line.length());
|
---|
222 | line = line.substring(0, line.indexOf("'"));
|
---|
223 | System.out.println("interface ref.:"+line);
|
---|
224 | return line;
|
---|
225 | } else if (wmsLayer.isRaster() && line.indexOf(cInterfaceRaster) != -1) { // "afficherCarteTa.do"
|
---|
226 | // list of values parsed in listOfTA (Tableau d'assemblage)
|
---|
227 | parseTAList(line.substring(line.indexOf(cInterfaceRaster)));
|
---|
228 | if (listOfTA.size() == 1) {
|
---|
229 | wmsLayer.setCodeCommune(listOfTA.firstElement());
|
---|
230 | return buildRasterInterfaceRef(listOfTA.firstElement());
|
---|
231 | }
|
---|
232 | return null;
|
---|
233 | } else if (line.indexOf(cCommuneListStart) != -1 && line.indexOf(cCommuneListEnd) != -1) {
|
---|
234 | // list of values parsed in listOfCommunes
|
---|
235 | int i = line.indexOf(cCommuneListStart);
|
---|
236 | int j = line.indexOf(cCommuneListEnd, i);
|
---|
237 | parseCommuneList(line.substring(i, j));
|
---|
238 | }
|
---|
239 | } catch (MalformedURLException e) {
|
---|
240 | throw (IOException) new IOException(
|
---|
241 | "Illegal url.").initCause(e);
|
---|
242 | } catch (Exception e){
|
---|
243 | e.printStackTrace();
|
---|
244 | }
|
---|
245 | return null;
|
---|
246 | }
|
---|
247 |
|
---|
248 | private void parseCommuneList(String input) {
|
---|
249 | if (input.indexOf(c0ptionListStart) != -1) {
|
---|
250 | while (input.indexOf("<option value=\"") != -1) {
|
---|
251 | int i = input.indexOf(c0ptionListStart);
|
---|
252 | int j = input.indexOf(cOptionListEnd, i+c0ptionListStart.length());
|
---|
253 | int k = input.indexOf("\"", i+c0ptionListStart.length());
|
---|
254 | if (j != -1 && k > (i + c0ptionListStart.length())) {
|
---|
255 | String lov = new String(input.substring(i+c0ptionListStart.length()-1, j));
|
---|
256 | if (lov.indexOf(">") != -1) {
|
---|
257 | System.out.println("parse "+lov);
|
---|
258 | listOfCommunes.add(lov);
|
---|
259 | } else
|
---|
260 | System.err.println("unable to parse commune string:"+lov);
|
---|
261 | }
|
---|
262 | input = input.substring(j+cOptionListEnd.length());
|
---|
263 | }
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | private void parseTAList(String input) {
|
---|
268 | while (input.indexOf(cInterfaceRaster) != -1) {
|
---|
269 | input = input.substring(input.indexOf(cInterfaceRaster));
|
---|
270 | String codeTA = input.substring(0, input.indexOf("'"));
|
---|
271 | codeTA = codeTA.substring(codeTA.indexOf("=")+1);
|
---|
272 | if (!listOfTA.contains(codeTA)) {
|
---|
273 | System.out.println("parse "+codeTA);
|
---|
274 | listOfTA.add(codeTA);
|
---|
275 | }
|
---|
276 | input = input.substring(cInterfaceRaster.length());
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | private String selectCommuneDialog() {
|
---|
281 | JPanel p = new JPanel(new GridBagLayout());
|
---|
282 | String[] communeList = new String[listOfCommunes.size() + 1];
|
---|
283 | communeList[0] = tr("Choose from...");
|
---|
284 | for (int i = 0; i < listOfCommunes.size(); i++) {
|
---|
285 | communeList[i + 1] = listOfCommunes.elementAt(i).substring(listOfCommunes.elementAt(i).indexOf(">")+1);
|
---|
286 | }
|
---|
287 | JComboBox inputCommuneList = new JComboBox(communeList);
|
---|
288 | p.add(inputCommuneList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
|
---|
289 | JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null) {
|
---|
290 | private static final long serialVersionUID = 1L;
|
---|
291 | };
|
---|
292 | pane.createDialog(Main.parent, tr("Select commune")).setVisible(true);
|
---|
293 | if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
|
---|
294 | return null;
|
---|
295 | String result = listOfCommunes.elementAt(inputCommuneList.getSelectedIndex()-1);
|
---|
296 | return result.substring(1, result.indexOf(">")-2);
|
---|
297 | }
|
---|
298 |
|
---|
299 | private String selectTADialog() {
|
---|
300 | JPanel p = new JPanel(new GridBagLayout());
|
---|
301 | JComboBox inputTAList = new JComboBox(listOfTA);
|
---|
302 | p.add(inputTAList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
|
---|
303 | JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null) {
|
---|
304 | private static final long serialVersionUID = 1L;
|
---|
305 | };
|
---|
306 | pane.createDialog(Main.parent, tr("Select Tableau d'Assemblage")).setVisible(true);
|
---|
307 | if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
|
---|
308 | return null;
|
---|
309 | String result = listOfTA.elementAt(inputTAList.getSelectedIndex());
|
---|
310 | return result;
|
---|
311 | }
|
---|
312 |
|
---|
313 | private String buildRasterInterfaceRef(String codeCommune) {
|
---|
314 | return cInterfaceRaster + "?f=" + codeCommune;
|
---|
315 | }
|
---|
316 |
|
---|
317 | public EastNorthBound retrieveCommuneBBox() throws IOException {
|
---|
318 | if (interfaceRef == null)
|
---|
319 | return null;
|
---|
320 | String ln = null;
|
---|
321 | String line = null;
|
---|
322 | // send GET opening normally the small window with the commune overview
|
---|
323 | String content = baseURL + "/scpc/" + interfaceRef;
|
---|
324 | content += "&dontSaveLastForward&keepVolatileSession=";
|
---|
325 | searchFormURL = new URL(content);
|
---|
326 | System.out.println("HEAD:"+content);
|
---|
327 | urlConn = (HttpURLConnection)searchFormURL.openConnection();
|
---|
328 | urlConn.setRequestMethod("GET");
|
---|
329 | setCookie();
|
---|
330 | urlConn.connect();
|
---|
331 | if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
---|
332 | throw (IOException) new IOException("Cannot get Cadastre response.");
|
---|
333 | }
|
---|
334 | BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
|
---|
335 | while ((ln = in.readLine()) != null) {
|
---|
336 | line += ln;
|
---|
337 | }
|
---|
338 | in.close();
|
---|
339 | urlConn.disconnect();
|
---|
340 | return parseBBoxCommune(line);
|
---|
341 | }
|
---|
342 |
|
---|
343 | private EastNorthBound parseBBoxCommune(String input) {
|
---|
344 | if (input.indexOf(cBBoxCommunStart) != -1) {
|
---|
345 | input = input.substring(input.indexOf(cBBoxCommunStart));
|
---|
346 | int i = input.indexOf(",");
|
---|
347 | double minx = Double.parseDouble(input.substring(cBBoxCommunStart.length(), i));
|
---|
348 | int j = input.indexOf(",", i+1);
|
---|
349 | double miny = Double.parseDouble(input.substring(i+1, j));
|
---|
350 | int k = input.indexOf(",", j+1);
|
---|
351 | double maxx = Double.parseDouble(input.substring(j+1, k));
|
---|
352 | int l = input.indexOf(cBBoxCommunEnd, k+1);
|
---|
353 | double maxy = Double.parseDouble(input.substring(k+1, l));
|
---|
354 | return new EastNorthBound(new EastNorth(minx,miny), new EastNorth(maxx,maxy));
|
---|
355 | }
|
---|
356 | return null;
|
---|
357 | }
|
---|
358 |
|
---|
359 | private void checkLayerDuplicates(WMSLayer wmsLayer) throws DuplicateLayerException {
|
---|
360 | if (Main.map != null) {
|
---|
361 | for (Layer l : Main.map.mapView.getAllLayers()) {
|
---|
362 | if (l instanceof WMSLayer && l.name.equals(wmsLayer.name) && (l != wmsLayer)) {
|
---|
363 | System.out.println("Try to grab into a new layer when "+wmsLayer.name+" is already opened.");
|
---|
364 | // remove the duplicated layer
|
---|
365 | Main.map.mapView.removeLayer(wmsLayer);
|
---|
366 | throw new DuplicateLayerException();
|
---|
367 | }
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | public void cancel() {
|
---|
373 | Main.pleaseWaitDlg.currentAction.setText(tr("Aborting..."));
|
---|
374 | if (urlConn != null) {
|
---|
375 | urlConn.setConnectTimeout(1);
|
---|
376 | urlConn.setReadTimeout(1);
|
---|
377 | //urlConn.disconnect();
|
---|
378 | }
|
---|
379 | downloadCancelled = true;
|
---|
380 | cadastreGrabber.setLastWMSLayerName(null);
|
---|
381 | }
|
---|
382 |
|
---|
383 | }
|
---|