| 1 | package cadastre_fr;
|
|---|
| 2 |
|
|---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 4 |
|
|---|
| 5 | import java.awt.image.BufferedImage;
|
|---|
| 6 | import java.io.IOException;
|
|---|
| 7 | import java.io.InputStream;
|
|---|
| 8 | import java.net.HttpURLConnection;
|
|---|
| 9 | import java.net.MalformedURLException;
|
|---|
| 10 | import java.net.URL;
|
|---|
| 11 |
|
|---|
| 12 | import javax.imageio.ImageIO;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 15 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
|---|
| 16 | import org.openstreetmap.josm.io.OsmTransferException;
|
|---|
| 17 | import org.openstreetmap.josm.io.ProgressInputStream;
|
|---|
| 18 |
|
|---|
| 19 | public class CadastreGrabber {
|
|---|
| 20 |
|
|---|
| 21 | public final static double epsilon = 1e-11;
|
|---|
| 22 |
|
|---|
| 23 | private CadastreInterface wmsInterface = new CadastreInterface();
|
|---|
| 24 |
|
|---|
| 25 | public GeorefImage grab(WMSLayer wmsLayer, EastNorth lambertMin, EastNorth lambertMax) throws IOException, OsmTransferException {
|
|---|
| 26 |
|
|---|
| 27 | try {
|
|---|
| 28 | URL url = null;
|
|---|
| 29 | if (wmsLayer.isRaster())
|
|---|
| 30 | url = getURLRaster(wmsLayer, lambertMin, lambertMax);
|
|---|
| 31 | else
|
|---|
| 32 | url = getURLVector(lambertMin, lambertMax);
|
|---|
| 33 | BufferedImage img = grab(url);
|
|---|
| 34 | ImageModifier imageModified;
|
|---|
| 35 | if (wmsLayer.isRaster())
|
|---|
| 36 | imageModified = new RasterImageModifier(img);
|
|---|
| 37 | else
|
|---|
| 38 | imageModified = new VectorImageModifier(img);
|
|---|
| 39 | return new GeorefImage(imageModified.bufferedImage, lambertMin, lambertMax);
|
|---|
| 40 | } catch (MalformedURLException e) {
|
|---|
| 41 | throw (IOException) new IOException(tr("CadastreGrabber: Illegal url.")).initCause(e);
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | private URL getURLRaster(WMSLayer wmsLayer, EastNorth lambertMin, EastNorth lambertMax) throws MalformedURLException {
|
|---|
| 46 | // GET /scpc/wms?version=1.1&request=GetMap&layers=CDIF:PMC@QH4480001701&format=image/png&bbox=-1186,0,13555,8830&width=576&height=345&exception=application/vnd.ogc.se_inimage&styles= HTTP/1.1
|
|---|
| 47 | final int cRasterX = 1000; // keep width constant and adjust width to original image proportions
|
|---|
| 48 | String str = new String(wmsInterface.baseURL+"/scpc/wms?version=1.1&request=GetMap");
|
|---|
| 49 | str += "&layers=CDIF:PMC@";
|
|---|
| 50 | str += wmsLayer.getCodeCommune();
|
|---|
| 51 | str += "&format=image/png";
|
|---|
| 52 | str += "&bbox=";
|
|---|
| 53 | str += wmsLayer.eastNorth2raster(lambertMin, lambertMax);
|
|---|
| 54 | //str += "&width=800&height=800"; // maximum allowed by wms server
|
|---|
| 55 | str += "&width="+cRasterX+"&height="; // maximum allowed by wms server (576/345, 800/378, 1000/634)
|
|---|
| 56 | str += (int)(cRasterX*(wmsLayer.communeBBox.max.getY() - wmsLayer.communeBBox.min.getY())/(wmsLayer.communeBBox.max.getX() - wmsLayer.communeBBox.min.getX()));
|
|---|
| 57 | str += "&exception=application/vnd.ogc.se_inimage&styles=";
|
|---|
| 58 | return new URL(str.replace(" ", "%20"));
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | private URL getURLVector(EastNorth lambertMin, EastNorth lambertMax) throws MalformedURLException {
|
|---|
| 62 | String str = new String(wmsInterface.baseURL+"/scpc/wms?version=1.1&request=GetMap");
|
|---|
| 63 | str += "&layers=CDIF:LS3,CDIF:LS2,CDIF:LS1,CDIF:PARCELLE,CDIF:NUMERO";
|
|---|
| 64 | str += ",CDIF:PT3,CDIF:PT2,CDIF:PT1,CDIF:LIEUDIT";
|
|---|
| 65 | str += ",CDIF:SUBSECTION";
|
|---|
| 66 | str += ",CDIF:SECTION";
|
|---|
| 67 | str += ",CDIF:COMMUNE";
|
|---|
| 68 | str += "&format=image/png";
|
|---|
| 69 | //str += "&format=image/jpeg";
|
|---|
| 70 | str += "&bbox="+lambertMin.east()+",";
|
|---|
| 71 | str += lambertMin.north() + ",";
|
|---|
| 72 | str += lambertMax.east() + ",";
|
|---|
| 73 | str += lambertMax.north();
|
|---|
| 74 | //str += "&width=800&height=600"; // maximum allowed by wms server
|
|---|
| 75 | str += "&width=1000&height=800"; // maximum allowed by wms server
|
|---|
| 76 | str += "&styles=LS3_90,LS2_90,LS1_90,PARCELLE_90,NUMERO_90,PT3_90,PT2_90,PT1_90,LIEUDIT_90";
|
|---|
| 77 | str += ",SUBSECTION_90";
|
|---|
| 78 | str += ",SECTION_90";
|
|---|
| 79 | str += ",COMMUNE_90";
|
|---|
| 80 | System.out.println("URL="+str);
|
|---|
| 81 | return new URL(str.replace(" ", "%20"));
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | private BufferedImage grab(URL url) throws IOException, OsmTransferException {
|
|---|
| 85 | wmsInterface.urlConn = (HttpURLConnection)url.openConnection();
|
|---|
| 86 | wmsInterface.urlConn.setRequestMethod("GET");
|
|---|
| 87 | wmsInterface.setCookie();
|
|---|
| 88 | InputStream is = new ProgressInputStream(wmsInterface.urlConn, NullProgressMonitor.INSTANCE);
|
|---|
| 89 | BufferedImage img = ImageIO.read(is);
|
|---|
| 90 | is.close();
|
|---|
| 91 | return img;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | public CadastreInterface getWmsInterface() {
|
|---|
| 95 | return wmsInterface;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | }
|
|---|