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

Last change on this file since 20425 was 20390, checked in by pieren, 15 years ago

Many small fixes and improvements

  • Property svn:eol-style set to native
File size: 6.0 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 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 public GeorefImage grabBuildings(WMSLayer wmsLayer, EastNorth lambertMin, EastNorth lambertMax) throws IOException, OsmTransferException {
45 try {
46 URL url = getURLVectorBuildings(lambertMin, lambertMax);
47 BufferedImage img = grab(url);
48 ImageModifier imageModified = new VectorImageModifier(img, true);
49 return new GeorefImage(imageModified.bufferedImage, lambertMin, lambertMax);
50 } catch (MalformedURLException e) {
51 throw (IOException) new IOException(tr("CadastreGrabber: Illegal url.")).initCause(e);
52 }
53 }
54
55 public GeorefImage grabParcels(WMSLayer wmsLayer, EastNorth lambertMin, EastNorth lambertMax) throws IOException, OsmTransferException {
56 try {
57 URL url = getURLVectorParcels(lambertMin, lambertMax);
58 BufferedImage img = grab(url);
59 //ImageModifier imageModified = new VectorImageModifier(img, true);
60 return new GeorefImage(/*imageModified.bufferedImage*/img, lambertMin, lambertMax);
61 } catch (MalformedURLException e) {
62 throw (IOException) new IOException(tr("CadastreGrabber: Illegal url.")).initCause(e);
63 }
64 }
65
66 private URL getURLRaster(WMSLayer wmsLayer, EastNorth lambertMin, EastNorth lambertMax) throws MalformedURLException {
67 // 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
68 final int cRasterX = CadastrePlugin.imageWidth; // keep width constant and adjust width to original image proportions
69 String str = new String(wmsInterface.baseURL+"/scpc/wms?version=1.1&request=GetMap");
70 str += "&layers=CDIF:PMC@";
71 str += wmsLayer.getCodeCommune();
72 str += "&format=image/png";
73 //str += "&format=image/jpeg";
74 str += "&bbox=";
75 str += wmsLayer.eastNorth2raster(lambertMin, lambertMax);
76 str += "&width="+cRasterX+"&height="; // maximum allowed by wms server (576/345, 800/378, 1000/634)
77 str += (int)(cRasterX*(wmsLayer.communeBBox.max.getY() - wmsLayer.communeBBox.min.getY())/(wmsLayer.communeBBox.max.getX() - wmsLayer.communeBBox.min.getX()));
78 str += "&exception=application/vnd.ogc.se_inimage&styles="; // required for raster images
79 System.out.println("URL="+str);
80 return new URL(str.replace(" ", "%20"));
81 }
82
83 private URL buildURLVector(String layers, String styles,
84 int width, int height,
85 EastNorth lambertMin, EastNorth lambertMax) throws MalformedURLException {
86 String str = new String(wmsInterface.baseURL+"/scpc/wms?version=1.1&request=GetMap");
87 str += "&layers="+ layers;
88 str += "&format=image/png";
89 //str += "&format=image/jpeg";
90 str += "&bbox="+lambertMin.east()+",";
91 str += lambertMin.north() + ",";
92 str += lambertMax.east() + ",";
93 str += lambertMax.north();
94 str += "&width="+width+"&height="+height;
95 str += "&exception=application/vnd.ogc.se_inimage"; // works also without (but slower ?)
96 str += "&styles=" + styles;
97 System.out.println("URL="+str);
98 return new URL(str.replace(" ", "%20"));
99 }
100
101 private URL getURLVector(EastNorth lambertMin, EastNorth lambertMax) throws MalformedURLException {
102 return buildURLVector(CadastrePlugin.grabLayers, CadastrePlugin.grabStyles,
103 CadastrePlugin.imageWidth, CadastrePlugin.imageHeight,
104 lambertMin, lambertMax);
105 }
106
107 private URL getURLVectorBuildings(EastNorth lambertMin, EastNorth lambertMax) throws MalformedURLException {
108 return buildURLVector("CDIF:LS2", "LS2_90", 1000, 800, lambertMin, lambertMax);
109 }
110
111 private URL getURLVectorParcels(EastNorth lambertMin, EastNorth lambertMax) throws MalformedURLException {
112 return buildURLVector("CDIF:PARCELLE", "PARCELLE_90", 1000, 800, lambertMin, lambertMax);
113 }
114
115 private BufferedImage grab(URL url) throws IOException, OsmTransferException {
116 wmsInterface.urlConn = (HttpURLConnection)url.openConnection();
117 wmsInterface.urlConn.setRequestMethod("GET");
118 wmsInterface.setCookie();
119 InputStream is = new ProgressInputStream(wmsInterface.urlConn, NullProgressMonitor.INSTANCE);
120 BufferedImage img = ImageIO.read(is);
121 is.close();
122 return img;
123 }
124
125 public CadastreInterface getWmsInterface() {
126 return wmsInterface;
127 }
128
129}
Note: See TracBrowser for help on using the repository browser.