Statistics
| Revision:

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

History | View | Annotate | Download (12.1 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
         * Slecciona el formato del WMS.
99
         *
100
         * @return formato seleccionado.
101
         *
102
         * @throws WMSException
103
         * @throws IllegalStateException
104
         * @throws ValidationException
105
         * @throws UnsupportedVersionException
106
         * @throws IOException
107
         */
108
        private String selectFormat()
109
                throws WMSException, IllegalStateException, ValidationException, 
110
                        UnsupportedVersionException, IOException {
111
                String[] formats;
112
                formats = getWmsClient().getInfoFormats();
113

    
114
                for (int i = 0; i < formats.length; i++) {
115
                        if (formats[i].equals("GML.1")) {
116
                                return formats[i];
117
                        }
118

    
119
                        if (formats[i].equals("GML.2")) {
120
                                return formats[i];
121
                        }
122

    
123
                        if (formats[i].equals("GML.3")) {
124
                                return formats[i];
125
                        }
126

    
127
                        if (formats[i].equals("application/vnd.ogc.gml")) {
128
                                return formats[i];
129
                        }
130

    
131
                        if (formats[i].indexOf("XML") != -1) {
132
                                return formats[i];
133
                        }
134
                }
135

    
136
                throw new WMSException("No format supported");
137
        }
138

    
139
        /**
140
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir la
141
         * capa.
142
         *
143
         * @return XMLEntity.
144
         * @throws XMLException
145
         */
146
        public XMLEntity getXMLEntity() throws XMLException {
147
                XMLEntity xml = super.getXMLEntity();
148

    
149
                xml.putProperty("fullExtent", StringUtilities.rect2String(fullExtent));
150
                xml.putProperty("host", host.toExternalForm());
151
                xml.putProperty("infoLayerQuery", infoLayerQuery);
152
                xml.putProperty("layerQuery", layerQuery);
153
                xml.putProperty("format", m_Format);
154
                xml.putProperty("srs", m_SRS);
155

    
156
                return xml;
157
        }
158

    
159
        /**
160
         * A partir del XMLEntity reproduce la capa.
161
         *
162
         * @param xml XMLEntity
163
         *
164
         * @throws XMLException
165
         * @throws DriverException
166
         * @throws DriverIOException
167
         */
168
        public void setXMLEntity03(XMLEntity xml)
169
                throws XMLException {
170
                super.setXMLEntity(xml);
171
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
172
                                        "fullExtent"));
173

    
174
                try {
175
                        host = new URL(xml.getStringProperty("host"));
176
                } catch (MalformedURLException e) {
177
                        throw new XMLException(e);
178
                }
179

    
180
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
181
                layerQuery = xml.getStringProperty("layerQuery");
182
                m_Format = xml.getStringProperty("format");
183
                m_SRS = xml.getStringProperty("srs");
184
        }
185

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

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

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

    
213
        /**
214
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(com.iver.cit.gvsig.fmap.operations.QueriedPoint)
215
         */
216
        public String queryByPoint(Point p) throws DriverException {
217
                FeatureInfoQuery query = new FeatureInfoQuery(lastMapQuery);
218
                query.setFeatureCount(Integer.MAX_VALUE);
219
                query.setX((int) p.getX());
220
                query.setY((int) p.getY());
221
                query.setInfoQuery(infoLayerQuery);
222
                
223
                try {
224
                        query.setInfoFormat(selectFormat());
225

    
226
                        return new String(getWmsClient().doFeatureInfo(query));
227
                } catch (WMSException e) {
228
                        return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><exception>" +
229
                        e.getMessage() + "</exception>";
230
                } catch (ValidationException e) {
231
                        /*
232
                         * TODO Las traducciones en este m?todo han de ser
233
                         * las mismas que en el m?todo de dibujado
234
                         */
235
                        throw new DriverException("No se reconoce el formato de la respuesta",
236
                                e);
237
                } catch (UnsupportedVersionException e) {
238
                        throw new DriverException("Conflicto de versiones", e);
239
                } catch (IOException e) {
240
                        throw new DriverException("Error en la conexi?n", e);
241
                } catch (com.iver.wmsclient.WMSException e) {
242
                        throw new DriverException(e.getMessage(), e);
243
                } catch (NoSuchFieldException e) {
244
                        throw new RuntimeException(
245
                                "No se rellenaron todos los campos de la petici?n");
246
                }
247
        }
248

    
249
        /**
250
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
251
         */
252
        public Rectangle2D getFullExtent() throws DriverException {
253
                return fullExtent;
254
        }
255

    
256
        /**
257
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
258
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
259
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
260
         */
261
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
262
                Cancellable cancel,double scale) throws DriverException {
263
                if (isWithinScale(scale)){        
264
                try {
265
                        MapQuery mapQuery = getWmsClient().createQuery();
266
                        mapQuery.setBBOX(viewPort.getAdjustedExtent());
267
                        mapQuery.setFormat(m_Format);
268
                        mapQuery.setHeight(viewPort.getImageHeight());
269

    
270
                        // System.err.println("m_Mapa.getHeight() = " + m_Mapa.getHeight());
271
                        mapQuery.setLayers(layerQuery);
272
                        mapQuery.setSRS(m_SRS);
273
                        mapQuery.setStyles("");
274
                        mapQuery.setWidth(viewPort.getImageWidth());
275
                        mapQuery.setExceptions("application/vnd.ogc.se_xml");
276

    
277
                        byte[] bytes;
278
                        lastMapQuery = mapQuery;
279
                        bytes = getWmsClient().doMapQuery(lastMapQuery);
280

    
281
                        ByteArrayInputStream inbytes = new ByteArrayInputStream(bytes);
282
                        BufferedImage tempImg = ImageIO.read(inbytes);
283
                        // LWS Cambio de estrategia en el posicionado para la impresi?n.
284
                        Point2D p2=new Point2D.Double(viewPort.getAdjustedExtent().getX(), viewPort.getAdjustedExtent().getMaxY()); //viewPort.getOffset();
285
                        viewPort.getAffineTransform().transform(p2, p2);
286
                        g.drawImage(tempImg,(int)p2.getX(),(int)p2.getY(), null);
287
                } catch (ValidationException e) {
288
                        throw new DriverException("No se reconoce el formato de la respuesta",
289
                                e);
290
                } catch (UnsupportedVersionException e) {
291
                        throw new DriverException("Conflicto de versiones", e);
292
                } catch (IOException e) {
293
                        throw new DriverException("Error en la conexi?n", e);
294
                } catch (com.iver.wmsclient.WMSException e) {
295
                        throw new DriverException(e.getMessage(), e);
296
                } catch (NoSuchFieldException e) {
297
                        throw new RuntimeException(
298
                                "No se rellenaron todos los campos de la petici?n");
299
                }
300
                }
301
        }
302

    
303
        /**
304
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
305
         *                 com.iver.cit.gvsig.fmap.ViewPort,
306
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
307
         */
308
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
309
                throws DriverException {
310
                if (isVisible() && isWithinScale(scale)){        
311
                isPrinting = true;
312
                if (!mustTilePrint) {
313
                        draw(null, g, viewPort, cancel,scale);
314
                } else {
315
                // Para no pedir imagenes demasiado grandes, vamos
316
                // a hacer lo mismo que hace EcwFile: chunkear.
317
                // Llamamos a drawView con cuadraditos m?s peque?os
318
                // del BufferedImage ni caso, cuando se imprime viene con null
319
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
320
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
321
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
322
                            // Parte que dibuja
323
                            try {
324
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
325
                                draw(null, g, vp, cancel,scale);
326
                                } catch (NoninvertibleTransformException e) {
327
                                        // TODO Auto-generated catch block
328
                                        e.printStackTrace();
329
                                }
330
                }
331
                }
332
            isPrinting = false;
333
                }
334
        }
335
        
336
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
337
                throws DriverException {
338
                draw(null, g, viewPort, cancel,scale);
339
        }
340

    
341
        /**
342
         * Devuelve el WMSClient.
343
         *
344
         * @return WMSClient
345
         *
346
         * @throws IllegalStateException
347
         * @throws ValidationException
348
         * @throws UnsupportedVersionException
349
         * @throws IOException
350
         */
351
        private WMSClient getWmsClient()
352
                throws IllegalStateException, ValidationException, 
353
                        UnsupportedVersionException, IOException {
354
                if (wmsClient == null) {
355
                        wmsClient = WMSClientFactory.getClient(host);
356
                }
357

    
358
                return wmsClient;
359
        }
360

    
361
        /**
362
         * Devuelve el URL.
363
         *
364
         * @return URL.
365
         */
366
        public URL getHost() {
367
                return host;
368
        }
369

    
370
        /**
371
         * Inserta el URL.
372
         *
373
         * @param host URL.
374
         */
375
        public void setHost(URL host) {
376
                this.host = host;
377
        }
378

    
379
        /**
380
         * Devuelve la informaci?n de la consulta.
381
         *
382
         * @return String.
383
         */
384
        public String getInfoLayerQuery() {
385
                return infoLayerQuery;
386
        }
387

    
388
        /**
389
         * Inserta la informaci?n de la consulta.
390
         *
391
         * @param infoLayerQuery String.
392
         */
393
        public void setInfoLayerQuery(String infoLayerQuery) {
394
                this.infoLayerQuery = infoLayerQuery;
395
        }
396

    
397
        /**
398
         * Devuelve la consulta.
399
         *
400
         * @return String.
401
         */
402
        public String getLayerQuery() {
403
                return layerQuery;
404
        }
405

    
406
        /**
407
         * Inserta la consulta.
408
         *
409
         * @param layerQuery consulta.
410
         */
411
        public void setLayerQuery(String layerQuery) {
412
                this.layerQuery = layerQuery;
413
        }
414

    
415
        /**
416
         * Devuelve el formato.
417
         *
418
         * @return Formato.
419
         */
420
        public String getFormat() {
421
                return m_Format;
422
        }
423

    
424
        /**
425
         * Inserta el formato.
426
         *
427
         * @param format Formato.
428
         */
429
        public void setFormat(String format) {
430
                m_Format = format;
431
        }
432

    
433
        /**
434
         * Devuelve el SRS.
435
         *
436
         * @return SRS.
437
         */
438
        public String getSRS() {
439
                return m_SRS;
440
        }
441

    
442
        /**
443
         * Inserta el SRS.
444
         *
445
         * @param m_srs SRS.
446
         */
447
        public void setSRS(String m_srs) {
448
                m_SRS = m_srs;
449
        }
450

    
451
        /**
452
         * Inserta la extensi?n total de la capa.
453
         *
454
         * @param fullExtent Rect?ngulo.
455
         */
456
        public void setFullExtent(Rectangle2D fullExtent) {
457
                this.fullExtent = fullExtent;
458
        }
459
}