source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreGrabber.java@ 18838

Last change on this file since 18838 was 18544, checked in by pieren, 15 years ago

Add licence in headers for GPL compliance.

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