// License: GPL. For details, see LICENSE file. package cadastre_fr; import static org.openstreetmap.josm.tools.I18n.tr; import java.awt.GridBagLayout; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.CookieHandler; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JOptionPane; import javax.swing.JPanel; import org.openstreetmap.josm.Main; import org.openstreetmap.josm.data.coor.EastNorth; import org.openstreetmap.josm.data.validation.util.Entities; import org.openstreetmap.josm.gui.layer.Layer; import org.openstreetmap.josm.gui.progress.NullProgressMonitor; import org.openstreetmap.josm.gui.util.GuiHelper; import org.openstreetmap.josm.io.OsmTransferException; import org.openstreetmap.josm.io.ProgressInputStream; import org.openstreetmap.josm.tools.GBC; public class CadastreInterface { public boolean downloadCanceled; private HttpURLConnection urlConn; private String cookie; private String interfaceRef; private String lastWMSLayerName; private URL searchFormURL; private List listOfCommunes = new ArrayList<>(); private List listOfTA = new ArrayList<>(); static class PlanImage { String name; String ref; PlanImage(String name, String ref) { this.name = name; this.ref = ref; } } private List listOfFeuilles = new ArrayList<>(); private long cookieTimestamp; static final String BASE_URL = "https://www.cadastre.gouv.fr"; static final String C_IMAGE_FORMAT = "Cette commune est au format "; static final String C_COMMUNE_LIST_START = " * * * * * The returned string is the interface name used in further requests, e.g. "afficherCarteCommune.do?c=QP224" * where QP224 is the code commune known by the WMS (or "afficherCarteTa.do?c=..." for raster images). * * @return retURL url to available code commune in the cadastre; "" if not found */ private String postForm(WMSLayer wmsLayer, String codeCommune) throws IOException { try { listOfCommunes.clear(); listOfTA.clear(); // send a POST request with a city/town/village name String content = "numerovoie="; content += "&indiceRepetition="; content += "&nomvoie="; content += "&lieuDit="; if (codeCommune.isEmpty()) { content += "&ville=" + java.net.URLEncoder.encode(wmsLayer.getLocation(), "UTF-8"); content += "&codePostal="; } else { content += "&codeCommune=" + codeCommune; } content += "&codeDepartement="; content += wmsLayer.getDepartement(); content += "&nbResultatParPage=10"; content += "&x=0&y=0"; searchFormURL = new URL(BASE_URL + "/scpc/rechercherPlan.do"); urlConn = (HttpURLConnection) searchFormURL.openConnection(); urlConn.setRequestMethod("POST"); urlConn.setDoOutput(true); urlConn.setDoInput(true); setCookie(); try (OutputStream wr = urlConn.getOutputStream()) { wr.write(content.getBytes(StandardCharsets.UTF_8)); Main.info("POST "+content); wr.flush(); } String ln; StringBuilder sb = new StringBuilder(); try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8))) { while ((ln = rd.readLine()) != null) { sb.append(ln); } } String lines = sb.toString(); urlConn.disconnect(); if (lines != null) { if (lines.indexOf(C_IMAGE_FORMAT) != -1) { int i = lines.indexOf(C_IMAGE_FORMAT); int j = lines.indexOf('.', i); wmsLayer.setRaster("image".equals(lines.substring(i+C_IMAGE_FORMAT.length(), j))); } if (!wmsLayer.isRaster() && lines.indexOf(C_INTERFACE_VECTOR) != -1) { // "afficherCarteCommune.do" // shall be something like: interfaceRef = "afficherCarteCommune.do?c=X2269"; lines = lines.substring(lines.indexOf(C_INTERFACE_VECTOR), lines.length()); lines = lines.substring(0, lines.indexOf('\'')); lines = Entities.unescape(lines); Main.info("interface ref.:"+lines); return lines; } else if (wmsLayer.isRaster() && lines.indexOf(C_INTERFACE_RASTER_TA) != -1) { // "afficherCarteTa.do" // list of values parsed in listOfFeuilles (list all non-georeferenced images) lines = getFeuillesList(); if (!downloadCanceled) { parseFeuillesList(lines); if (!listOfFeuilles.isEmpty()) { int res = selectFeuilleDialog(); if (res != -1) { wmsLayer.setCodeCommune(listOfFeuilles.get(res).name); checkLayerDuplicates(wmsLayer); interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune()); wmsLayer.setCodeCommune(listOfFeuilles.get(res).ref); lines = buildRasterFeuilleInterfaceRef(listOfFeuilles.get(res).ref); lines = Entities.unescape(lines); Main.info("interface ref.:"+lines); return lines; } } } return null; } else if (lines.indexOf(C_COMMUNE_LIST_START) != -1 && lines.indexOf(C_COMMUNE_LIST_END) != -1) { // list of values parsed in listOfCommunes int i = lines.indexOf(C_COMMUNE_LIST_START); int j = lines.indexOf(C_COMMUNE_LIST_END, i); parseCommuneList(lines.substring(i, j)); } } } catch (MalformedURLException e) { throw (IOException) new IOException("Illegal url.").initCause(e); } catch (DuplicateLayerException e) { Main.error(e); } return null; } private void parseCommuneList(String input) { if (input.indexOf(C_OPTION_LIST_START) != -1) { while (input.indexOf("