| 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 static final double epsilon = 1e-11;
|
|---|
| 22 |
|
|---|
| 23 | private CadastreInterface wmsInterface = new CadastreInterface(this);
|
|---|
| 24 | private String lastWMSLayerName = null;
|
|---|
| 25 |
|
|---|
| 26 | CadastreGrabber() {
|
|---|
| 27 | getWmsInterface().downloadCancelled = false;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | public GeorefImage grab(WMSLayer wmsLayer, EastNorth lambertMin, EastNorth lambertMax) throws IOException, OsmTransferException {
|
|---|
| 31 |
|
|---|
| 32 | try {
|
|---|
| 33 | URL url = null;
|
|---|
| 34 | if (wmsLayer.isRaster())
|
|---|
| 35 | url = getURLRaster(wmsLayer, lambertMin, lambertMax);
|
|---|
| 36 | else
|
|---|
| 37 | url = getURLVector(lambertMin, lambertMax);
|
|---|
| 38 | System.out.println("grab:"+url);
|
|---|
| 39 | BufferedImage img = grab(url);
|
|---|
| 40 | ImageModifier imageModified = new ImageModifier(img);
|
|---|
| 41 | return new GeorefImage(imageModified.bufferedImage, lambertMin, lambertMax);
|
|---|
| 42 | } catch (MalformedURLException e) {
|
|---|
| 43 | throw (IOException) new IOException(tr("CadastreGrabber: Illegal url.")).initCause(e);
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | private URL getURLRaster(WMSLayer wmsLayer, EastNorth lambertMin, EastNorth lambertMax) throws MalformedURLException {
|
|---|
| 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=600&height=600"; // maximum allowed by wms server
|
|---|
| 55 | str += "&exception=application/vnd.ogc.se_inimage&styles=";
|
|---|
| 56 | return new URL(str.replace(" ", "%20"));
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | private URL getURLVector(EastNorth lambertMin, EastNorth lambertMax) throws MalformedURLException {
|
|---|
| 60 | String str = new String(wmsInterface.baseURL+"/scpc/wms?version=1.1&request=GetMap");
|
|---|
| 61 | str += "&layers=CDIF:LS3,CDIF:LS2,CDIF:LS1,CDIF:PARCELLE,CDIF:NUMERO";
|
|---|
| 62 | str += ",CDIF:PT3,CDIF:PT2,CDIF:PT1,CDIF:LIEUDIT";
|
|---|
| 63 | str += ",CDIF:SUBSECTION";
|
|---|
| 64 | str += ",CDIF:SECTION";
|
|---|
| 65 | str += ",CDIF:COMMUNE";
|
|---|
| 66 | str += "&format=image/png";
|
|---|
| 67 | //str += "&format=image/jpeg";
|
|---|
| 68 | str += "&bbox="+lambertMin.east()+",";
|
|---|
| 69 | str += lambertMin.north() + ",";
|
|---|
| 70 | str += lambertMax.east() + ",";
|
|---|
| 71 | str += lambertMax.north();
|
|---|
| 72 | //str += "&width=800&height=600"; // maximum allowed by wms server
|
|---|
| 73 | str += "&width=1000&height=800"; // maximum allowed by wms server
|
|---|
| 74 | str += "&styles=LS3_90,LS2_90,LS1_90,PARCELLE_90,NUMERO_90,PT3_90,PT2_90,PT1_90,LIEUDIT_90";
|
|---|
| 75 | str += ",SUBSECTION_90";
|
|---|
| 76 | str += ",SECTION_90";
|
|---|
| 77 | str += ",COMMUNE_90";
|
|---|
| 78 | System.out.println("URL="+str);
|
|---|
| 79 | return new URL(str.replace(" ", "%20"));
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | private BufferedImage grab(URL url) throws IOException, OsmTransferException {
|
|---|
| 83 | wmsInterface.urlConn = (HttpURLConnection)url.openConnection();
|
|---|
| 84 | wmsInterface.urlConn.setRequestMethod("GET");
|
|---|
| 85 | wmsInterface.setCookie();
|
|---|
| 86 | InputStream is = new ProgressInputStream(wmsInterface.urlConn, NullProgressMonitor.INSTANCE);
|
|---|
| 87 | BufferedImage img = ImageIO.read(is);
|
|---|
| 88 | is.close();
|
|---|
| 89 | return img;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | public CadastreInterface getWmsInterface() {
|
|---|
| 93 | return wmsInterface;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | public String getLastWMSLayerName() {
|
|---|
| 97 | return lastWMSLayerName;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | public void setLastWMSLayerName(String lastWMSLayerName) {
|
|---|
| 101 | this.lastWMSLayerName = lastWMSLayerName;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | }
|
|---|