Statistics
| Revision:

svn-gvsig-desktop / branches / Fmap_GisPlanet / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrWMS.java @ 1841

History | View | Annotate | Download (11.2 KB)

1 1100 fjp
/* 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 851 fernando
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 1034 vcaballero
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
46 851 fernando
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 1034 vcaballero
50 851 fernando
import com.iver.utiles.StringUtilities;
51
import com.iver.utiles.XMLEntity;
52 1034 vcaballero
53 851 fernando
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 1034 vcaballero
import org.exolab.castor.xml.ValidationException;
60 851 fernando
61 1034 vcaballero
import java.awt.Graphics2D;
62
import java.awt.Point;
63 1488 luisw
import java.awt.geom.AffineTransform;
64
import java.awt.geom.NoninvertibleTransformException;
65 1318 vcaballero
import java.awt.geom.Point2D;
66 1034 vcaballero
import java.awt.geom.Rectangle2D;
67
import java.awt.image.BufferedImage;
68
69
import java.io.ByteArrayInputStream;
70
import java.io.IOException;
71
72
import java.net.MalformedURLException;
73
import java.net.URL;
74
75
import javax.imageio.ImageIO;
76
77
78 851 fernando
/**
79 1034 vcaballero
 * Capa WMS.
80 851 fernando
 *
81
 * @author Fernando Gonz?lez Cort?s
82
 */
83
public class FLyrWMS extends FLyrDefault implements InfoByPoint {
84 1488 luisw
        boolean isPrinting = false;
85
        boolean mustTileDraw = false;
86 1501 luisw
        boolean mustTilePrint = true;
87 1488 luisw
        int maxTileDrawWidth = -1;
88
        int maxTileDrawHeight = -1;
89
        int maxTilePrintWidth = 1500;
90
        int maxTilePrintHeight = 1500;
91
92 851 fernando
        private String m_SRS;
93
        private String m_Format;
94
        private String layerQuery;
95
        private String infoLayerQuery;
96
        private URL host;
97
        private WMSClient wmsClient;
98
        private MapQuery lastMapQuery;
99
        private Rectangle2D fullExtent;
100
101
        /**
102 1034 vcaballero
         * Slecciona el formato del WMS.
103 851 fernando
         *
104 1034 vcaballero
         * @return formato seleccionado.
105 851 fernando
         *
106
         * @throws WMSException
107
         * @throws IllegalStateException
108
         * @throws ValidationException
109
         * @throws UnsupportedVersionException
110
         * @throws IOException
111
         */
112
        private String selectFormat()
113
                throws WMSException, IllegalStateException, ValidationException,
114
                        UnsupportedVersionException, IOException {
115
                String[] formats;
116
                formats = getWmsClient().getInfoFormats();
117
118
                for (int i = 0; i < formats.length; i++) {
119
                        if (formats[i].equals("GML.1")) {
120
                                return formats[i];
121
                        }
122
123
                        if (formats[i].equals("GML.2")) {
124
                                return formats[i];
125
                        }
126
127
                        if (formats[i].equals("GML.3")) {
128
                                return formats[i];
129
                        }
130
131
                        if (formats[i].equals("application/vnd.ogc.gml")) {
132
                                return formats[i];
133
                        }
134
135
                        if (formats[i].indexOf("XML") != -1) {
136
                                return formats[i];
137
                        }
138
                }
139
140
                throw new WMSException("No format supported");
141
        }
142
143 1034 vcaballero
        /**
144
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir la
145
         * capa.
146
         *
147
         * @return XMLEntity.
148
         */
149 851 fernando
        public XMLEntity getXMLEntity() {
150
                XMLEntity xml = super.getXMLEntity();
151
152
                xml.putProperty("fullExtent", StringUtilities.rect2String(fullExtent));
153
                xml.putProperty("host", host.toExternalForm());
154
                xml.putProperty("infoLayerQuery", infoLayerQuery);
155
                xml.putProperty("layerQuery", layerQuery);
156
                xml.putProperty("format", m_Format);
157
                xml.putProperty("srs", m_SRS);
158
159
                return xml;
160
        }
161 1034 vcaballero
162
        /**
163
         * A partir del XMLEntity reproduce la capa.
164
         *
165
         * @param xml XMLEntity
166
         *
167
         * @throws XMLException
168
         * @throws DriverException
169
         * @throws DriverIOException
170
         */
171
        public void setXMLEntity(XMLEntity xml)
172 1056 vcaballero
                throws XMLException {
173 851 fernando
                super.setXMLEntity(xml);
174 1034 vcaballero
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
175
                                        "fullExtent"));
176
177 851 fernando
                try {
178
                        host = new URL(xml.getStringProperty("host"));
179
                } catch (MalformedURLException e) {
180
                        throw new XMLException(e);
181
                }
182 1034 vcaballero
183 851 fernando
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
184
                layerQuery = xml.getStringProperty("layerQuery");
185
                m_Format = xml.getStringProperty("format");
186
                m_SRS = xml.getStringProperty("srs");
187
        }
188
189
        /**
190
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(com.iver.cit.gvsig.fmap.operations.QueriedPoint)
191
         */
192 1011 fernando
        public String queryByPoint(Point p) throws DriverException {
193 851 fernando
                FeatureInfoQuery query = new FeatureInfoQuery(lastMapQuery);
194
                query.setFeatureCount(Integer.MAX_VALUE);
195
                query.setX((int) p.getX());
196
                query.setY((int) p.getY());
197
                query.setInfoQuery(infoLayerQuery);
198
199
                try {
200
                        query.setInfoFormat(selectFormat());
201
202
                        return new String(getWmsClient().doFeatureInfo(query));
203
                } catch (WMSException e) {
204
                        return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><exception>" +
205
                        e.getMessage() + "</exception>";
206
                } catch (ValidationException e) {
207
                        /*
208
                         * TODO Las traducciones en este m?todo han de ser
209
                         * las mismas que en el m?todo de dibujado
210
                         */
211
                        throw new DriverException("No se reconoce el formato de la respuesta",
212 1034 vcaballero
                                e);
213 851 fernando
                } catch (UnsupportedVersionException e) {
214
                        throw new DriverException("Conflicto de versiones", e);
215
                } catch (IOException e) {
216
                        throw new DriverException("Error en la conexi?n", e);
217
                } catch (com.iver.wmsclient.WMSException e) {
218
                        throw new DriverException(e.getMessage(), e);
219
                } catch (NoSuchFieldException e) {
220
                        throw new RuntimeException(
221
                                "No se rellenaron todos los campos de la petici?n");
222
                }
223
        }
224
225
        /**
226
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
227
         */
228
        public Rectangle2D getFullExtent() throws DriverException {
229
                return fullExtent;
230
        }
231
232
        /**
233
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
234
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
235
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
236
         */
237
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
238
                Cancellable cancel) throws DriverException {
239
                try {
240
                        lastMapQuery = getWmsClient().createQuery();
241
                        lastMapQuery.setBBOX(viewPort.getAdjustedExtent());
242
                        lastMapQuery.setFormat(m_Format);
243
                        lastMapQuery.setHeight(viewPort.getImageHeight());
244
245
                        // System.err.println("m_Mapa.getHeight() = " + m_Mapa.getHeight());
246
                        lastMapQuery.setLayers(layerQuery);
247
                        lastMapQuery.setSRS(m_SRS);
248
                        lastMapQuery.setStyles("");
249
                        lastMapQuery.setWidth(viewPort.getImageWidth());
250
                        lastMapQuery.setExceptions("application/vnd.ogc.se_xml");
251
252
                        byte[] bytes;
253
254
                        bytes = getWmsClient().doMapQuery(lastMapQuery);
255
256
                        ByteArrayInputStream inbytes = new ByteArrayInputStream(bytes);
257
                        BufferedImage tempImg = ImageIO.read(inbytes);
258 1501 luisw
                        // LWS Cambio de estrategia en el posicionado para la impresi?n.
259
                        Point2D p2=new Point2D.Double(viewPort.getAdjustedExtent().getX(), viewPort.getAdjustedExtent().getMaxY()); //viewPort.getOffset();
260
                        viewPort.getAffineTransform().transform(p2, p2);
261 1318 vcaballero
                        g.drawImage(tempImg,(int)p2.getX(),(int)p2.getY(), null);
262 851 fernando
                } catch (ValidationException e) {
263
                        throw new DriverException("No se reconoce el formato de la respuesta",
264
                                e);
265
                } catch (UnsupportedVersionException e) {
266
                        throw new DriverException("Conflicto de versiones", e);
267
                } catch (IOException e) {
268
                        throw new DriverException("Error en la conexi?n", e);
269
                } catch (com.iver.wmsclient.WMSException e) {
270
                        throw new DriverException(e.getMessage(), e);
271
                } catch (NoSuchFieldException e) {
272
                        throw new RuntimeException(
273
                                "No se rellenaron todos los campos de la petici?n");
274
                }
275
        }
276
277
        /**
278
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
279
         *                 com.iver.cit.gvsig.fmap.ViewPort,
280
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
281
         */
282
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
283
                throws DriverException {
284 1488 luisw
                isPrinting = true;
285
                if (!mustTilePrint) {
286
                        draw(null, g, viewPort, cancel);
287
                } else {
288
                // Para no pedir imagenes demasiado grandes, vamos
289
                // a hacer lo mismo que hace EcwFile: chunkear.
290
                // Llamamos a drawView con cuadraditos m?s peque?os
291
                // del BufferedImage ni caso, cuando se imprime viene con null
292
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
293
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
294
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
295
                            // Parte que dibuja
296
                            try {
297
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
298
                                draw(null, g, vp, cancel);
299
                                } catch (NoninvertibleTransformException e) {
300
                                        // TODO Auto-generated catch block
301
                                        e.printStackTrace();
302
                                }
303
                }
304
                }
305
            isPrinting = false;
306
        }
307
308
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
309
                throws DriverException {
310 851 fernando
                draw(null, g, viewPort, cancel);
311
        }
312 1034 vcaballero
313 851 fernando
        /**
314 1034 vcaballero
         * Devuelve el WMSClient.
315 851 fernando
         *
316 1034 vcaballero
         * @return WMSClient
317 851 fernando
         *
318 1034 vcaballero
         * @throws IllegalStateException
319
         * @throws ValidationException
320
         * @throws UnsupportedVersionException
321
         * @throws IOException
322 851 fernando
         */
323
        private WMSClient getWmsClient()
324
                throws IllegalStateException, ValidationException,
325
                        UnsupportedVersionException, IOException {
326
                if (wmsClient == null) {
327
                        wmsClient = WMSClientFactory.getClient(host);
328
                }
329
330
                return wmsClient;
331
        }
332
333 1034 vcaballero
        /**
334
         * Devuelve el URL.
335
         *
336
         * @return URL.
337
         */
338 851 fernando
        public URL getHost() {
339
                return host;
340
        }
341 1034 vcaballero
342
        /**
343
         * Inserta el URL.
344
         *
345
         * @param host URL.
346
         */
347 851 fernando
        public void setHost(URL host) {
348
                this.host = host;
349
        }
350 1034 vcaballero
351
        /**
352
         * Devuelve la informaci?n de la consulta.
353
         *
354
         * @return String.
355
         */
356 851 fernando
        public String getInfoLayerQuery() {
357
                return infoLayerQuery;
358
        }
359 1034 vcaballero
360
        /**
361
         * Inserta la informaci?n de la consulta.
362
         *
363
         * @param infoLayerQuery String.
364
         */
365 851 fernando
        public void setInfoLayerQuery(String infoLayerQuery) {
366
                this.infoLayerQuery = infoLayerQuery;
367
        }
368 1034 vcaballero
369
        /**
370
         * Devuelve la consulta.
371
         *
372
         * @return String.
373
         */
374 851 fernando
        public String getLayerQuery() {
375
                return layerQuery;
376
        }
377 1034 vcaballero
378
        /**
379
         * Inserta la consulta.
380
         *
381
         * @param layerQuery consulta.
382
         */
383 851 fernando
        public void setLayerQuery(String layerQuery) {
384
                this.layerQuery = layerQuery;
385
        }
386 1034 vcaballero
387
        /**
388
         * Devuelve el formato.
389
         *
390
         * @return Formato.
391
         */
392 851 fernando
        public String getFormat() {
393
                return m_Format;
394
        }
395 1034 vcaballero
396
        /**
397
         * Inserta el formato.
398
         *
399
         * @param format Formato.
400
         */
401 851 fernando
        public void setFormat(String format) {
402
                m_Format = format;
403
        }
404 1034 vcaballero
405
        /**
406
         * Devuelve el SRS.
407
         *
408
         * @return SRS.
409
         */
410 851 fernando
        public String getSRS() {
411
                return m_SRS;
412
        }
413 1034 vcaballero
414
        /**
415
         * Inserta el SRS.
416
         *
417
         * @param m_srs SRS.
418
         */
419 851 fernando
        public void setSRS(String m_srs) {
420
                m_SRS = m_srs;
421
        }
422 1034 vcaballero
423
        /**
424
         * Inserta la extensi?n total de la capa.
425
         *
426
         * @param fullExtent Rect?ngulo.
427
         */
428 851 fernando
        public void setFullExtent(Rectangle2D fullExtent) {
429
                this.fullExtent = fullExtent;
430
        }
431
}