Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extWMS / src / com / iver / cit / gvsig / fmap / layers / FLyrWMS.java @ 3333

History | View | Annotate | Download (12.9 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 java.awt.Graphics2D;
44
import java.awt.Point;
45
import java.awt.geom.AffineTransform;
46
import java.awt.geom.NoninvertibleTransformException;
47
import java.awt.geom.Point2D;
48
import java.awt.geom.Rectangle2D;
49
import java.awt.image.BufferedImage;
50
import java.io.ByteArrayInputStream;
51
import java.io.IOException;
52
import java.net.MalformedURLException;
53
import java.net.URL;
54

    
55
import javax.imageio.ImageIO;
56

    
57
import org.exolab.castor.xml.ValidationException;
58

    
59
import com.iver.cit.gvsig.fmap.DriverException;
60
import com.iver.cit.gvsig.fmap.ViewPort;
61
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
62
import com.iver.cit.gvsig.fmap.drivers.WMSException;
63
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
64
import com.iver.cit.gvsig.fmap.operations.Cancellable;
65
import com.iver.utiles.StringUtilities;
66
import com.iver.utiles.XMLEntity;
67
import com.iver.wmsclient.FeatureInfoQuery;
68
import com.iver.wmsclient.MapQuery;
69
import com.iver.wmsclient.UnsupportedVersionException;
70
import com.iver.wmsclient.WMSClient;
71
import com.iver.wmsclient.WMSClientFactory;
72

    
73

    
74
/**
75
 * Capa WMS.
76
 *
77
 * @author Fernando Gonz?lez Cort?s
78
 */
79
public class FLyrWMS extends FLyrDefault implements InfoByPoint {
80
        boolean isPrinting = false;
81
        boolean mustTileDraw = false;
82
        boolean mustTilePrint = true;
83
        int maxTileDrawWidth = -1;
84
        int maxTileDrawHeight = -1;
85
        int maxTilePrintWidth = 1500;
86
        int maxTilePrintHeight = 1500;
87

    
88
        private String m_SRS;
89
        private String m_Format;
90
        private String layerQuery;
91
        private String infoLayerQuery;
92
        private URL host;
93
        private WMSClient wmsClient;
94
        private MapQuery lastMapQuery;
95
        private Rectangle2D fullExtent;
96

    
97
        /**
98
         * Inicializa una capa WMS con el driver que se le pasa como par?metro y
99
         * guard?ndose el nombre del fichero para realizar los accesos, la capa
100
         * tendr? asociada la proyecci?n que se pasa como parametro tambi?n
101
         *
102
         * @param layerName Nombre de la capa.
103
         * @param rect extent
104
         * @param host URL.
105
         * @param format Formato
106
         * @param query Consulta.
107
         * @param infoQuery inforamci?n de la consulta.
108
         * @param srs SRS.
109
         */
110
        public void init(String layerName, Rectangle2D rect,
111
                                URL host, String format, String query, String infoQuery, String srs) {
112
                FLyrWMS layer = this;
113
                layer.setHost(host);
114
                layer.setFullExtent(rect);
115
                layer.setFormat(format);
116
                layer.setLayerQuery(query);
117
                layer.setInfoLayerQuery(infoQuery);
118
                layer.setSRS(srs);
119
                layer.setName(layerName);
120
        }
121
        
122
        /**
123
         * Slecciona el formato del WMS.
124
         *
125
         * @return formato seleccionado.
126
         *
127
         * @throws WMSException
128
         * @throws IllegalStateException
129
         * @throws ValidationException
130
         * @throws UnsupportedVersionException
131
         * @throws IOException
132
         */
133
        private String selectFormat()
134
                throws WMSException, IllegalStateException, ValidationException, 
135
                        UnsupportedVersionException, IOException {
136
                String[] formats;
137
                formats = getWmsClient().getInfoFormats();
138

    
139
                for (int i = 0; i < formats.length; i++) {
140
                        if (formats[i].equals("GML.1")) {
141
                                return formats[i];
142
                        }
143

    
144
                        if (formats[i].equals("GML.2")) {
145
                                return formats[i];
146
                        }
147

    
148
                        if (formats[i].equals("GML.3")) {
149
                                return formats[i];
150
                        }
151

    
152
                        if (formats[i].equals("application/vnd.ogc.gml")) {
153
                                return formats[i];
154
                        }
155

    
156
                        if (formats[i].indexOf("XML") != -1) {
157
                                return formats[i];
158
                        }
159
                }
160

    
161
                throw new WMSException("No format supported");
162
        }
163

    
164
        /**
165
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir la
166
         * capa.
167
         *
168
         * @return XMLEntity.
169
         * @throws XMLException
170
         */
171
        public XMLEntity getXMLEntity() throws XMLException {
172
                XMLEntity xml = super.getXMLEntity();
173

    
174
                xml.putProperty("fullExtent", StringUtilities.rect2String(fullExtent));
175
                xml.putProperty("host", host.toExternalForm());
176
                xml.putProperty("infoLayerQuery", infoLayerQuery);
177
                xml.putProperty("layerQuery", layerQuery);
178
                xml.putProperty("format", m_Format);
179
                xml.putProperty("srs", m_SRS);
180

    
181
                return xml;
182
        }
183

    
184
        /**
185
         * A partir del XMLEntity reproduce la capa.
186
         *
187
         * @param xml XMLEntity
188
         *
189
         * @throws XMLException
190
         * @throws DriverException
191
         * @throws DriverIOException
192
         */
193
        public void setXMLEntity03(XMLEntity xml)
194
                throws XMLException {
195
                super.setXMLEntity(xml);
196
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
197
                                        "fullExtent"));
198

    
199
                try {
200
                        host = new URL(xml.getStringProperty("host"));
201
                } catch (MalformedURLException e) {
202
                        throw new XMLException(e);
203
                }
204

    
205
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
206
                layerQuery = xml.getStringProperty("layerQuery");
207
                m_Format = xml.getStringProperty("format");
208
                m_SRS = xml.getStringProperty("srs");
209
        }
210

    
211
        /**
212
         * A partir del XMLEntity reproduce la capa.
213
         *
214
         * @param xml XMLEntity
215
         *
216
         * @throws XMLException
217
         * @throws DriverException
218
         * @throws DriverIOException
219
         */
220
        public void setXMLEntity(XMLEntity xml)
221
                throws XMLException {
222
                super.setXMLEntity(xml);
223
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
224
                                        "fullExtent"));
225

    
226
                try {
227
                        host = new URL(xml.getStringProperty("host"));
228
                } catch (MalformedURLException e) {
229
                        throw new XMLException(e);
230
                }
231

    
232
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
233
                layerQuery = xml.getStringProperty("layerQuery");
234
                m_Format = xml.getStringProperty("format");
235
                m_SRS = xml.getStringProperty("srs");
236
        }
237

    
238
        /**
239
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(com.iver.cit.gvsig.fmap.operations.QueriedPoint)
240
         */
241
        public String queryByPoint(Point p) throws DriverException {
242
                FeatureInfoQuery query = new FeatureInfoQuery(lastMapQuery);
243
                query.setFeatureCount(Integer.MAX_VALUE);
244
                query.setX((int) p.getX());
245
                query.setY((int) p.getY());
246
                query.setInfoQuery(infoLayerQuery);
247
                
248
                try {
249
                        query.setInfoFormat(selectFormat());
250

    
251
                        return new String(getWmsClient().doFeatureInfo(query));
252
                } catch (WMSException e) {
253
                        return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><exception>" +
254
                        e.getMessage() + "</exception>";
255
                } catch (ValidationException e) {
256
                        /*
257
                         * TODO Las traducciones en este m?todo han de ser
258
                         * las mismas que en el m?todo de dibujado
259
                         */
260
                        throw new DriverException("No se reconoce el formato de la respuesta",
261
                                e);
262
                } catch (UnsupportedVersionException e) {
263
                        throw new DriverException("Conflicto de versiones", e);
264
                } catch (IOException e) {
265
                        throw new DriverException("Error en la conexi?n", e);
266
                } catch (com.iver.wmsclient.WMSException e) {
267
                        throw new DriverException(e.getMessage(), e);
268
                } catch (NoSuchFieldException e) {
269
                        throw new RuntimeException(
270
                                "No se rellenaron todos los campos de la petici?n");
271
                }
272
        }
273

    
274
        /**
275
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
276
         */
277
        public Rectangle2D getFullExtent() throws DriverException {
278
                return fullExtent;
279
        }
280

    
281
        /**
282
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
283
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
284
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
285
         */
286
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
287
                Cancellable cancel,double scale) throws DriverException {
288
                if (isWithinScale(scale)){        
289
                try {
290
                        MapQuery mapQuery = getWmsClient().createQuery();
291
                        mapQuery.setBBOX(viewPort.getAdjustedExtent());
292
                        mapQuery.setFormat(m_Format);
293
                        mapQuery.setHeight(viewPort.getImageHeight());
294

    
295
                        // System.err.println("m_Mapa.getHeight() = " + m_Mapa.getHeight());
296
                        mapQuery.setLayers(layerQuery);
297
                        mapQuery.setSRS(m_SRS);
298
                        mapQuery.setStyles("");
299
                        mapQuery.setWidth(viewPort.getImageWidth());
300
                        mapQuery.setExceptions("application/vnd.ogc.se_xml");
301

    
302
                        byte[] bytes;
303
                        lastMapQuery = mapQuery;
304
                        bytes = getWmsClient().doMapQuery(lastMapQuery);
305

    
306
                        ByteArrayInputStream inbytes = new ByteArrayInputStream(bytes);
307
                        BufferedImage tempImg = ImageIO.read(inbytes);
308
                        // LWS Cambio de estrategia en el posicionado para la impresi?n.
309
                        Point2D p2=new Point2D.Double(viewPort.getAdjustedExtent().getX(), viewPort.getAdjustedExtent().getMaxY()); //viewPort.getOffset();
310
                        viewPort.getAffineTransform().transform(p2, p2);
311
                        g.drawImage(tempImg,(int)p2.getX(),(int)p2.getY(), null);
312
                } catch (ValidationException e) {
313
                        throw new DriverException("No se reconoce el formato de la respuesta",
314
                                e);
315
                } catch (UnsupportedVersionException e) {
316
                        throw new DriverException("Conflicto de versiones", e);
317
                } catch (IOException e) {
318
                        throw new DriverException("Error en la conexi?n", e);
319
                } catch (com.iver.wmsclient.WMSException e) {
320
                        throw new DriverException(e.getMessage(), e);
321
                } catch (NoSuchFieldException e) {
322
                        throw new RuntimeException(
323
                                "No se rellenaron todos los campos de la petici?n");
324
                }
325
                }
326
        }
327

    
328
        /**
329
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
330
         *                 com.iver.cit.gvsig.fmap.ViewPort,
331
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
332
         */
333
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
334
                throws DriverException {
335
                if (isVisible() && isWithinScale(scale)){        
336
                isPrinting = true;
337
                if (!mustTilePrint) {
338
                        draw(null, g, viewPort, cancel,scale);
339
                } else {
340
                // Para no pedir imagenes demasiado grandes, vamos
341
                // a hacer lo mismo que hace EcwFile: chunkear.
342
                // Llamamos a drawView con cuadraditos m?s peque?os
343
                // del BufferedImage ni caso, cuando se imprime viene con null
344
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
345
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
346
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
347
                            // Parte que dibuja
348
                            try {
349
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
350
                                draw(null, g, vp, cancel,scale);
351
                                } catch (NoninvertibleTransformException e) {
352
                                        // TODO Auto-generated catch block
353
                                        e.printStackTrace();
354
                                }
355
                }
356
                }
357
            isPrinting = false;
358
                }
359
        }
360
        
361
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
362
                throws DriverException {
363
                draw(null, g, viewPort, cancel,scale);
364
        }
365

    
366
        /**
367
         * Devuelve el WMSClient.
368
         *
369
         * @return WMSClient
370
         *
371
         * @throws IllegalStateException
372
         * @throws ValidationException
373
         * @throws UnsupportedVersionException
374
         * @throws IOException
375
         */
376
        private WMSClient getWmsClient()
377
                throws IllegalStateException, ValidationException, 
378
                        UnsupportedVersionException, IOException {
379
                if (wmsClient == null) {
380
                        wmsClient = WMSClientFactory.getClient(host);
381
                }
382

    
383
                return wmsClient;
384
        }
385

    
386
        /**
387
         * Devuelve el URL.
388
         *
389
         * @return URL.
390
         */
391
        public URL getHost() {
392
                return host;
393
        }
394

    
395
        /**
396
         * Inserta el URL.
397
         *
398
         * @param host URL.
399
         */
400
        public void setHost(URL host) {
401
                this.host = host;
402
        }
403

    
404
        /**
405
         * Devuelve la informaci?n de la consulta.
406
         *
407
         * @return String.
408
         */
409
        public String getInfoLayerQuery() {
410
                return infoLayerQuery;
411
        }
412

    
413
        /**
414
         * Inserta la informaci?n de la consulta.
415
         *
416
         * @param infoLayerQuery String.
417
         */
418
        public void setInfoLayerQuery(String infoLayerQuery) {
419
                this.infoLayerQuery = infoLayerQuery;
420
        }
421

    
422
        /**
423
         * Devuelve la consulta.
424
         *
425
         * @return String.
426
         */
427
        public String getLayerQuery() {
428
                return layerQuery;
429
        }
430

    
431
        /**
432
         * Inserta la consulta.
433
         *
434
         * @param layerQuery consulta.
435
         */
436
        public void setLayerQuery(String layerQuery) {
437
                this.layerQuery = layerQuery;
438
        }
439

    
440
        /**
441
         * Devuelve el formato.
442
         *
443
         * @return Formato.
444
         */
445
        public String getFormat() {
446
                return m_Format;
447
        }
448

    
449
        /**
450
         * Inserta el formato.
451
         *
452
         * @param format Formato.
453
         */
454
        public void setFormat(String format) {
455
                m_Format = format;
456
        }
457

    
458
        /**
459
         * Devuelve el SRS.
460
         *
461
         * @return SRS.
462
         */
463
        public String getSRS() {
464
                return m_SRS;
465
        }
466

    
467
        /**
468
         * Inserta el SRS.
469
         *
470
         * @param m_srs SRS.
471
         */
472
        public void setSRS(String m_srs) {
473
                m_SRS = m_srs;
474
        }
475

    
476
        /**
477
         * Inserta la extensi?n total de la capa.
478
         *
479
         * @param fullExtent Rect?ngulo.
480
         */
481
        public void setFullExtent(Rectangle2D fullExtent) {
482
                this.fullExtent = fullExtent;
483
        }
484
}