Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrWMS.java @ 1076

History | View | Annotate | Download (8.48 KB)

1 851 fernando
package com.iver.cit.gvsig.fmap.layers;
2
3
import com.iver.cit.gvsig.fmap.DriverException;
4
import com.iver.cit.gvsig.fmap.ViewPort;
5 1034 vcaballero
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
6 851 fernando
import com.iver.cit.gvsig.fmap.drivers.WMSException;
7
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
8
import com.iver.cit.gvsig.fmap.operations.Cancellable;
9 1034 vcaballero
10 851 fernando
import com.iver.utiles.StringUtilities;
11
import com.iver.utiles.XMLEntity;
12 1034 vcaballero
13 851 fernando
import com.iver.wmsclient.FeatureInfoQuery;
14
import com.iver.wmsclient.MapQuery;
15
import com.iver.wmsclient.UnsupportedVersionException;
16
import com.iver.wmsclient.WMSClient;
17
import com.iver.wmsclient.WMSClientFactory;
18
19 1034 vcaballero
import org.exolab.castor.xml.ValidationException;
20 851 fernando
21 1034 vcaballero
import java.awt.Graphics2D;
22
import java.awt.Point;
23
import java.awt.geom.Rectangle2D;
24
import java.awt.image.BufferedImage;
25
26
import java.io.ByteArrayInputStream;
27
import java.io.IOException;
28
29
import java.net.MalformedURLException;
30
import java.net.URL;
31
32
import javax.imageio.ImageIO;
33
34
35 851 fernando
/**
36 1034 vcaballero
 * Capa WMS.
37 851 fernando
 *
38
 * @author Fernando Gonz?lez Cort?s
39
 */
40
public class FLyrWMS extends FLyrDefault implements InfoByPoint {
41
        private String m_SRS;
42
        private String m_Format;
43
        private String layerQuery;
44
        private String infoLayerQuery;
45
        private URL host;
46
        private WMSClient wmsClient;
47
        private MapQuery lastMapQuery;
48
        private Rectangle2D fullExtent;
49
50
        /**
51 1034 vcaballero
         * Slecciona el formato del WMS.
52 851 fernando
         *
53 1034 vcaballero
         * @return formato seleccionado.
54 851 fernando
         *
55
         * @throws WMSException
56
         * @throws IllegalStateException
57
         * @throws ValidationException
58
         * @throws UnsupportedVersionException
59
         * @throws IOException
60
         */
61
        private String selectFormat()
62
                throws WMSException, IllegalStateException, ValidationException,
63
                        UnsupportedVersionException, IOException {
64
                String[] formats;
65
                formats = getWmsClient().getInfoFormats();
66
67
                for (int i = 0; i < formats.length; i++) {
68
                        if (formats[i].equals("GML.1")) {
69
                                return formats[i];
70
                        }
71
72
                        if (formats[i].equals("GML.2")) {
73
                                return formats[i];
74
                        }
75
76
                        if (formats[i].equals("GML.3")) {
77
                                return formats[i];
78
                        }
79
80
                        if (formats[i].equals("application/vnd.ogc.gml")) {
81
                                return formats[i];
82
                        }
83
84
                        if (formats[i].indexOf("XML") != -1) {
85
                                return formats[i];
86
                        }
87
                }
88
89
                throw new WMSException("No format supported");
90
        }
91
92 1034 vcaballero
        /**
93
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir la
94
         * capa.
95
         *
96
         * @return XMLEntity.
97
         */
98 851 fernando
        public XMLEntity getXMLEntity() {
99
                XMLEntity xml = super.getXMLEntity();
100
101
                xml.putProperty("fullExtent", StringUtilities.rect2String(fullExtent));
102
                xml.putProperty("host", host.toExternalForm());
103
                xml.putProperty("infoLayerQuery", infoLayerQuery);
104
                xml.putProperty("layerQuery", layerQuery);
105
                xml.putProperty("format", m_Format);
106
                xml.putProperty("srs", m_SRS);
107
108
                return xml;
109
        }
110 1034 vcaballero
111
        /**
112
         * A partir del XMLEntity reproduce la capa.
113
         *
114
         * @param xml XMLEntity
115
         *
116
         * @throws XMLException
117
         * @throws DriverException
118
         * @throws DriverIOException
119
         */
120
        public void setXMLEntity(XMLEntity xml)
121 1056 vcaballero
                throws XMLException {
122 851 fernando
                super.setXMLEntity(xml);
123 1034 vcaballero
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
124
                                        "fullExtent"));
125
126 851 fernando
                try {
127
                        host = new URL(xml.getStringProperty("host"));
128
                } catch (MalformedURLException e) {
129
                        throw new XMLException(e);
130
                }
131 1034 vcaballero
132 851 fernando
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
133
                layerQuery = xml.getStringProperty("layerQuery");
134
                m_Format = xml.getStringProperty("format");
135
                m_SRS = xml.getStringProperty("srs");
136
        }
137
138
        /**
139
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(com.iver.cit.gvsig.fmap.operations.QueriedPoint)
140
         */
141 1011 fernando
        public String queryByPoint(Point p) throws DriverException {
142 851 fernando
                FeatureInfoQuery query = new FeatureInfoQuery(lastMapQuery);
143
                query.setFeatureCount(Integer.MAX_VALUE);
144
                query.setX((int) p.getX());
145
                query.setY((int) p.getY());
146
                query.setInfoQuery(infoLayerQuery);
147
148
                try {
149
                        query.setInfoFormat(selectFormat());
150
151
                        return new String(getWmsClient().doFeatureInfo(query));
152
                } catch (WMSException e) {
153
                        return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><exception>" +
154
                        e.getMessage() + "</exception>";
155
                } catch (ValidationException e) {
156
                        /*
157
                         * TODO Las traducciones en este m?todo han de ser
158
                         * las mismas que en el m?todo de dibujado
159
                         */
160
                        throw new DriverException("No se reconoce el formato de la respuesta",
161 1034 vcaballero
                                e);
162 851 fernando
                } catch (UnsupportedVersionException e) {
163
                        throw new DriverException("Conflicto de versiones", e);
164
                } catch (IOException e) {
165
                        throw new DriverException("Error en la conexi?n", e);
166
                } catch (com.iver.wmsclient.WMSException e) {
167
                        throw new DriverException(e.getMessage(), e);
168
                } catch (NoSuchFieldException e) {
169
                        throw new RuntimeException(
170
                                "No se rellenaron todos los campos de la petici?n");
171
                }
172
        }
173
174
        /**
175
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
176
         */
177
        public Rectangle2D getFullExtent() throws DriverException {
178
                return fullExtent;
179
        }
180
181
        /**
182
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
183
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
184
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
185
         */
186
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
187
                Cancellable cancel) throws DriverException {
188
                try {
189
                        lastMapQuery = getWmsClient().createQuery();
190
                        lastMapQuery.setBBOX(viewPort.getAdjustedExtent());
191
                        lastMapQuery.setFormat(m_Format);
192
                        lastMapQuery.setHeight(viewPort.getImageHeight());
193
194
                        // System.err.println("m_Mapa.getHeight() = " + m_Mapa.getHeight());
195
                        lastMapQuery.setLayers(layerQuery);
196
                        lastMapQuery.setSRS(m_SRS);
197
                        lastMapQuery.setStyles("");
198
                        lastMapQuery.setWidth(viewPort.getImageWidth());
199
                        lastMapQuery.setExceptions("application/vnd.ogc.se_xml");
200
201
                        byte[] bytes;
202
203
                        bytes = getWmsClient().doMapQuery(lastMapQuery);
204
205
                        ByteArrayInputStream inbytes = new ByteArrayInputStream(bytes);
206
                        BufferedImage tempImg = ImageIO.read(inbytes);
207
                        g.drawImage(tempImg, 0, 0, null);
208
                } catch (ValidationException e) {
209
                        throw new DriverException("No se reconoce el formato de la respuesta",
210
                                e);
211
                } catch (UnsupportedVersionException e) {
212
                        throw new DriverException("Conflicto de versiones", e);
213
                } catch (IOException e) {
214
                        throw new DriverException("Error en la conexi?n", e);
215
                } catch (com.iver.wmsclient.WMSException e) {
216
                        throw new DriverException(e.getMessage(), e);
217
                } catch (NoSuchFieldException e) {
218
                        throw new RuntimeException(
219
                                "No se rellenaron todos los campos de la petici?n");
220
                }
221
        }
222
223
        /**
224
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
225
         *                 com.iver.cit.gvsig.fmap.ViewPort,
226
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
227
         */
228
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
229
                throws DriverException {
230
                draw(null, g, viewPort, cancel);
231
        }
232 1034 vcaballero
233 851 fernando
        /**
234 1034 vcaballero
         * Devuelve el WMSClient.
235 851 fernando
         *
236 1034 vcaballero
         * @return WMSClient
237 851 fernando
         *
238 1034 vcaballero
         * @throws IllegalStateException
239
         * @throws ValidationException
240
         * @throws UnsupportedVersionException
241
         * @throws IOException
242 851 fernando
         */
243
        private WMSClient getWmsClient()
244
                throws IllegalStateException, ValidationException,
245
                        UnsupportedVersionException, IOException {
246
                if (wmsClient == null) {
247
                        wmsClient = WMSClientFactory.getClient(host);
248
                }
249
250
                return wmsClient;
251
        }
252
253 1034 vcaballero
        /**
254
         * Devuelve el URL.
255
         *
256
         * @return URL.
257
         */
258 851 fernando
        public URL getHost() {
259
                return host;
260
        }
261 1034 vcaballero
262
        /**
263
         * Inserta el URL.
264
         *
265
         * @param host URL.
266
         */
267 851 fernando
        public void setHost(URL host) {
268
                this.host = host;
269
        }
270 1034 vcaballero
271
        /**
272
         * Devuelve la informaci?n de la consulta.
273
         *
274
         * @return String.
275
         */
276 851 fernando
        public String getInfoLayerQuery() {
277
                return infoLayerQuery;
278
        }
279 1034 vcaballero
280
        /**
281
         * Inserta la informaci?n de la consulta.
282
         *
283
         * @param infoLayerQuery String.
284
         */
285 851 fernando
        public void setInfoLayerQuery(String infoLayerQuery) {
286
                this.infoLayerQuery = infoLayerQuery;
287
        }
288 1034 vcaballero
289
        /**
290
         * Devuelve la consulta.
291
         *
292
         * @return String.
293
         */
294 851 fernando
        public String getLayerQuery() {
295
                return layerQuery;
296
        }
297 1034 vcaballero
298
        /**
299
         * Inserta la consulta.
300
         *
301
         * @param layerQuery consulta.
302
         */
303 851 fernando
        public void setLayerQuery(String layerQuery) {
304
                this.layerQuery = layerQuery;
305
        }
306 1034 vcaballero
307
        /**
308
         * Devuelve el formato.
309
         *
310
         * @return Formato.
311
         */
312 851 fernando
        public String getFormat() {
313
                return m_Format;
314
        }
315 1034 vcaballero
316
        /**
317
         * Inserta el formato.
318
         *
319
         * @param format Formato.
320
         */
321 851 fernando
        public void setFormat(String format) {
322
                m_Format = format;
323
        }
324 1034 vcaballero
325
        /**
326
         * Devuelve el SRS.
327
         *
328
         * @return SRS.
329
         */
330 851 fernando
        public String getSRS() {
331
                return m_SRS;
332
        }
333 1034 vcaballero
334
        /**
335
         * Inserta el SRS.
336
         *
337
         * @param m_srs SRS.
338
         */
339 851 fernando
        public void setSRS(String m_srs) {
340
                m_SRS = m_srs;
341
        }
342 1034 vcaballero
343
        /**
344
         * Inserta la extensi?n total de la capa.
345
         *
346
         * @param fullExtent Rect?ngulo.
347
         */
348 851 fernando
        public void setFullExtent(Rectangle2D fullExtent) {
349
                this.fullExtent = fullExtent;
350
        }
351
}