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