Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrWMS.java @ 1100

History | View | Annotate | Download (9.67 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.layers;
42

    
43
import com.iver.cit.gvsig.fmap.DriverException;
44
import com.iver.cit.gvsig.fmap.ViewPort;
45
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
46
import com.iver.cit.gvsig.fmap.drivers.WMSException;
47
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
48
import com.iver.cit.gvsig.fmap.operations.Cancellable;
49

    
50
import com.iver.utiles.StringUtilities;
51
import com.iver.utiles.XMLEntity;
52

    
53
import com.iver.wmsclient.FeatureInfoQuery;
54
import com.iver.wmsclient.MapQuery;
55
import com.iver.wmsclient.UnsupportedVersionException;
56
import com.iver.wmsclient.WMSClient;
57
import com.iver.wmsclient.WMSClientFactory;
58

    
59
import org.exolab.castor.xml.ValidationException;
60

    
61
import java.awt.Graphics2D;
62
import java.awt.Point;
63
import java.awt.geom.Rectangle2D;
64
import java.awt.image.BufferedImage;
65

    
66
import java.io.ByteArrayInputStream;
67
import java.io.IOException;
68

    
69
import java.net.MalformedURLException;
70
import java.net.URL;
71

    
72
import javax.imageio.ImageIO;
73

    
74

    
75
/**
76
 * Capa WMS.
77
 *
78
 * @author Fernando Gonz?lez Cort?s
79
 */
80
public class FLyrWMS extends FLyrDefault implements InfoByPoint {
81
        private String m_SRS;
82
        private String m_Format;
83
        private String layerQuery;
84
        private String infoLayerQuery;
85
        private URL host;
86
        private WMSClient wmsClient;
87
        private MapQuery lastMapQuery;
88
        private Rectangle2D fullExtent;
89

    
90
        /**
91
         * Slecciona el formato del WMS.
92
         *
93
         * @return formato seleccionado.
94
         *
95
         * @throws WMSException
96
         * @throws IllegalStateException
97
         * @throws ValidationException
98
         * @throws UnsupportedVersionException
99
         * @throws IOException
100
         */
101
        private String selectFormat()
102
                throws WMSException, IllegalStateException, ValidationException, 
103
                        UnsupportedVersionException, IOException {
104
                String[] formats;
105
                formats = getWmsClient().getInfoFormats();
106

    
107
                for (int i = 0; i < formats.length; i++) {
108
                        if (formats[i].equals("GML.1")) {
109
                                return formats[i];
110
                        }
111

    
112
                        if (formats[i].equals("GML.2")) {
113
                                return formats[i];
114
                        }
115

    
116
                        if (formats[i].equals("GML.3")) {
117
                                return formats[i];
118
                        }
119

    
120
                        if (formats[i].equals("application/vnd.ogc.gml")) {
121
                                return formats[i];
122
                        }
123

    
124
                        if (formats[i].indexOf("XML") != -1) {
125
                                return formats[i];
126
                        }
127
                }
128

    
129
                throw new WMSException("No format supported");
130
        }
131

    
132
        /**
133
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir la
134
         * capa.
135
         *
136
         * @return XMLEntity.
137
         */
138
        public XMLEntity getXMLEntity() {
139
                XMLEntity xml = super.getXMLEntity();
140

    
141
                xml.putProperty("fullExtent", StringUtilities.rect2String(fullExtent));
142
                xml.putProperty("host", host.toExternalForm());
143
                xml.putProperty("infoLayerQuery", infoLayerQuery);
144
                xml.putProperty("layerQuery", layerQuery);
145
                xml.putProperty("format", m_Format);
146
                xml.putProperty("srs", m_SRS);
147

    
148
                return xml;
149
        }
150

    
151
        /**
152
         * A partir del XMLEntity reproduce la capa.
153
         *
154
         * @param xml XMLEntity
155
         *
156
         * @throws XMLException
157
         * @throws DriverException
158
         * @throws DriverIOException
159
         */
160
        public void setXMLEntity(XMLEntity xml)
161
                throws XMLException {
162
                super.setXMLEntity(xml);
163
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
164
                                        "fullExtent"));
165

    
166
                try {
167
                        host = new URL(xml.getStringProperty("host"));
168
                } catch (MalformedURLException e) {
169
                        throw new XMLException(e);
170
                }
171

    
172
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
173
                layerQuery = xml.getStringProperty("layerQuery");
174
                m_Format = xml.getStringProperty("format");
175
                m_SRS = xml.getStringProperty("srs");
176
        }
177

    
178
        /**
179
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(com.iver.cit.gvsig.fmap.operations.QueriedPoint)
180
         */
181
        public String queryByPoint(Point p) throws DriverException {
182
                FeatureInfoQuery query = new FeatureInfoQuery(lastMapQuery);
183
                query.setFeatureCount(Integer.MAX_VALUE);
184
                query.setX((int) p.getX());
185
                query.setY((int) p.getY());
186
                query.setInfoQuery(infoLayerQuery);
187

    
188
                try {
189
                        query.setInfoFormat(selectFormat());
190

    
191
                        return new String(getWmsClient().doFeatureInfo(query));
192
                } catch (WMSException e) {
193
                        return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><exception>" +
194
                        e.getMessage() + "</exception>";
195
                } catch (ValidationException e) {
196
                        /*
197
                         * TODO Las traducciones en este m?todo han de ser
198
                         * las mismas que en el m?todo de dibujado
199
                         */
200
                        throw new DriverException("No se reconoce el formato de la respuesta",
201
                                e);
202
                } catch (UnsupportedVersionException e) {
203
                        throw new DriverException("Conflicto de versiones", e);
204
                } catch (IOException e) {
205
                        throw new DriverException("Error en la conexi?n", e);
206
                } catch (com.iver.wmsclient.WMSException e) {
207
                        throw new DriverException(e.getMessage(), e);
208
                } catch (NoSuchFieldException e) {
209
                        throw new RuntimeException(
210
                                "No se rellenaron todos los campos de la petici?n");
211
                }
212
        }
213

    
214
        /**
215
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
216
         */
217
        public Rectangle2D getFullExtent() throws DriverException {
218
                return fullExtent;
219
        }
220

    
221
        /**
222
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
223
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
224
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
225
         */
226
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
227
                Cancellable cancel) throws DriverException {
228
                try {
229
                        lastMapQuery = getWmsClient().createQuery();
230
                        lastMapQuery.setBBOX(viewPort.getAdjustedExtent());
231
                        lastMapQuery.setFormat(m_Format);
232
                        lastMapQuery.setHeight(viewPort.getImageHeight());
233

    
234
                        // System.err.println("m_Mapa.getHeight() = " + m_Mapa.getHeight());
235
                        lastMapQuery.setLayers(layerQuery);
236
                        lastMapQuery.setSRS(m_SRS);
237
                        lastMapQuery.setStyles("");
238
                        lastMapQuery.setWidth(viewPort.getImageWidth());
239
                        lastMapQuery.setExceptions("application/vnd.ogc.se_xml");
240

    
241
                        byte[] bytes;
242

    
243
                        bytes = getWmsClient().doMapQuery(lastMapQuery);
244

    
245
                        ByteArrayInputStream inbytes = new ByteArrayInputStream(bytes);
246
                        BufferedImage tempImg = ImageIO.read(inbytes);
247
                        g.drawImage(tempImg, 0, 0, null);
248
                } catch (ValidationException e) {
249
                        throw new DriverException("No se reconoce el formato de la respuesta",
250
                                e);
251
                } catch (UnsupportedVersionException e) {
252
                        throw new DriverException("Conflicto de versiones", e);
253
                } catch (IOException e) {
254
                        throw new DriverException("Error en la conexi?n", e);
255
                } catch (com.iver.wmsclient.WMSException e) {
256
                        throw new DriverException(e.getMessage(), e);
257
                } catch (NoSuchFieldException e) {
258
                        throw new RuntimeException(
259
                                "No se rellenaron todos los campos de la petici?n");
260
                }
261
        }
262

    
263
        /**
264
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
265
         *                 com.iver.cit.gvsig.fmap.ViewPort,
266
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
267
         */
268
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
269
                throws DriverException {
270
                draw(null, g, viewPort, cancel);
271
        }
272

    
273
        /**
274
         * Devuelve el WMSClient.
275
         *
276
         * @return WMSClient
277
         *
278
         * @throws IllegalStateException
279
         * @throws ValidationException
280
         * @throws UnsupportedVersionException
281
         * @throws IOException
282
         */
283
        private WMSClient getWmsClient()
284
                throws IllegalStateException, ValidationException, 
285
                        UnsupportedVersionException, IOException {
286
                if (wmsClient == null) {
287
                        wmsClient = WMSClientFactory.getClient(host);
288
                }
289

    
290
                return wmsClient;
291
        }
292

    
293
        /**
294
         * Devuelve el URL.
295
         *
296
         * @return URL.
297
         */
298
        public URL getHost() {
299
                return host;
300
        }
301

    
302
        /**
303
         * Inserta el URL.
304
         *
305
         * @param host URL.
306
         */
307
        public void setHost(URL host) {
308
                this.host = host;
309
        }
310

    
311
        /**
312
         * Devuelve la informaci?n de la consulta.
313
         *
314
         * @return String.
315
         */
316
        public String getInfoLayerQuery() {
317
                return infoLayerQuery;
318
        }
319

    
320
        /**
321
         * Inserta la informaci?n de la consulta.
322
         *
323
         * @param infoLayerQuery String.
324
         */
325
        public void setInfoLayerQuery(String infoLayerQuery) {
326
                this.infoLayerQuery = infoLayerQuery;
327
        }
328

    
329
        /**
330
         * Devuelve la consulta.
331
         *
332
         * @return String.
333
         */
334
        public String getLayerQuery() {
335
                return layerQuery;
336
        }
337

    
338
        /**
339
         * Inserta la consulta.
340
         *
341
         * @param layerQuery consulta.
342
         */
343
        public void setLayerQuery(String layerQuery) {
344
                this.layerQuery = layerQuery;
345
        }
346

    
347
        /**
348
         * Devuelve el formato.
349
         *
350
         * @return Formato.
351
         */
352
        public String getFormat() {
353
                return m_Format;
354
        }
355

    
356
        /**
357
         * Inserta el formato.
358
         *
359
         * @param format Formato.
360
         */
361
        public void setFormat(String format) {
362
                m_Format = format;
363
        }
364

    
365
        /**
366
         * Devuelve el SRS.
367
         *
368
         * @return SRS.
369
         */
370
        public String getSRS() {
371
                return m_SRS;
372
        }
373

    
374
        /**
375
         * Inserta el SRS.
376
         *
377
         * @param m_srs SRS.
378
         */
379
        public void setSRS(String m_srs) {
380
                m_SRS = m_srs;
381
        }
382

    
383
        /**
384
         * Inserta la extensi?n total de la capa.
385
         *
386
         * @param fullExtent Rect?ngulo.
387
         */
388
        public void setFullExtent(Rectangle2D fullExtent) {
389
                this.fullExtent = fullExtent;
390
        }
391
}