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.image.BufferedImage;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.net.HttpURLConnection;
|
---|
10 | import java.net.MalformedURLException;
|
---|
11 | import java.net.URL;
|
---|
12 |
|
---|
13 | import javax.imageio.ImageIO;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
16 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
17 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
18 | import org.openstreetmap.josm.io.ProgressInputStream;
|
---|
19 |
|
---|
20 | public class CadastreGrabber {
|
---|
21 |
|
---|
22 | private CadastreInterface wmsInterface = new CadastreInterface();
|
---|
23 |
|
---|
24 | public GeorefImage grab(WMSLayer wmsLayer, EastNorth lambertMin, EastNorth lambertMax) throws IOException, OsmTransferException {
|
---|
25 |
|
---|
26 | try {
|
---|
27 | URL url = null;
|
---|
28 | if (wmsLayer.isRaster())
|
---|
29 | url = getURLRaster(wmsLayer, lambertMin, lambertMax);
|
---|
30 | else
|
---|
31 | url = getURLVector(lambertMin, lambertMax);
|
---|
32 | BufferedImage img = grab(url);
|
---|
33 | ImageModifier imageModified;
|
---|
34 | if (wmsLayer.isRaster())
|
---|
35 | imageModified = new RasterImageModifier(img);
|
---|
36 | else
|
---|
37 | imageModified = new VectorImageModifier(img, false);
|
---|
38 | return new GeorefImage(imageModified.bufferedImage, lambertMin, lambertMax);
|
---|
39 | } catch (MalformedURLException e) {
|
---|
40 | throw (IOException) new IOException(tr("CadastreGrabber: Illegal url.")).initCause(e);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | private URL getURLRaster(WMSLayer wmsLayer, EastNorth lambertMin, EastNorth lambertMax) throws MalformedURLException {
|
---|
45 | // 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
|
---|
46 | final int cRasterX = CadastrePlugin.imageWidth; // keep width constant and adjust width to original image proportions
|
---|
47 | String str = new String(wmsInterface.baseURL+"/scpc/wms?version=1.1&request=GetMap");
|
---|
48 | str += "&layers=CDIF:PMC@";
|
---|
49 | str += wmsLayer.getCodeCommune();
|
---|
50 | str += "&format=image/png";
|
---|
51 | //str += "&format=image/jpeg";
|
---|
52 | str += "&bbox=";
|
---|
53 | str += wmsLayer.eastNorth2raster(lambertMin, lambertMax);
|
---|
54 | str += "&width="+cRasterX+"&height="; // maximum allowed by wms server (576/345, 800/378, 1000/634)
|
---|
55 | str += (int)(cRasterX*(wmsLayer.communeBBox.max.getY() - wmsLayer.communeBBox.min.getY())/(wmsLayer.communeBBox.max.getX() - wmsLayer.communeBBox.min.getX()));
|
---|
56 | str += "&exception=application/vnd.ogc.se_inimage&styles="; // required for raster images
|
---|
57 | System.out.println("URL="+str);
|
---|
58 | return new URL(str.replace(" ", "%20"));
|
---|
59 | }
|
---|
60 |
|
---|
61 | private URL buildURLVector(String layers, String styles,
|
---|
62 | int width, int height,
|
---|
63 | EastNorth lambertMin, EastNorth lambertMax) throws MalformedURLException {
|
---|
64 | String str = new String(wmsInterface.baseURL+"/scpc/wms?version=1.1&request=GetMap");
|
---|
65 | str += "&layers="+ layers;
|
---|
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="+width+"&height="+height;
|
---|
73 | str += "&exception=application/vnd.ogc.se_inimage"; // works also without (but slower ?)
|
---|
74 | str += "&styles=" + styles;
|
---|
75 | System.out.println("URL="+str);
|
---|
76 | return new URL(str.replace(" ", "%20"));
|
---|
77 | }
|
---|
78 |
|
---|
79 | private URL getURLVector(EastNorth lambertMin, EastNorth lambertMax) throws MalformedURLException {
|
---|
80 | return buildURLVector(CadastrePlugin.grabLayers, CadastrePlugin.grabStyles,
|
---|
81 | CadastrePlugin.imageWidth, CadastrePlugin.imageHeight,
|
---|
82 | lambertMin, lambertMax);
|
---|
83 | }
|
---|
84 |
|
---|
85 | private BufferedImage grab(URL url) throws IOException, OsmTransferException {
|
---|
86 | wmsInterface.urlConn = (HttpURLConnection)url.openConnection();
|
---|
87 | wmsInterface.urlConn.setRequestMethod("GET");
|
---|
88 | wmsInterface.setCookie();
|
---|
89 | InputStream is = new ProgressInputStream(wmsInterface.urlConn, NullProgressMonitor.INSTANCE);
|
---|
90 | BufferedImage img = ImageIO.read(is);
|
---|
91 | is.close();
|
---|
92 | return img;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public CadastreInterface getWmsInterface() {
|
---|
96 | return wmsInterface;
|
---|
97 | }
|
---|
98 |
|
---|
99 | }
|
---|