Statistics
| Revision:

root / branches / v05 / extensions / extWMS / src / com / iver / cit / gvsig / fmap / layers / FLyrWMS.java @ 4125

History | View | Annotate | Download (30 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.Component;
44
import java.awt.Dimension;
45
import java.awt.Graphics2D;
46
import java.awt.Point;
47
import java.awt.Rectangle;
48
import java.awt.geom.AffineTransform;
49
import java.awt.geom.NoninvertibleTransformException;
50
import java.awt.geom.Point2D;
51
import java.awt.geom.Rectangle2D;
52
import java.awt.image.BufferedImage;
53
import java.io.File;
54
import java.io.IOException;
55
import java.lang.reflect.Constructor;
56
import java.lang.reflect.InvocationTargetException;
57
import java.net.MalformedURLException;
58
import java.net.URL;
59
import java.util.ArrayList;
60
import java.util.HashMap;
61
import java.util.Hashtable;
62
import java.util.Iterator;
63
import java.util.Vector;
64

    
65
import javax.swing.JOptionPane;
66

    
67
import org.cresques.geo.ViewPortData;
68
import org.cresques.io.GdalFile;
69
import org.cresques.io.GeoRasterFile;
70
import org.cresques.io.raster.RasterFilterStack;
71
import org.cresques.io.raster.RasterFilterStackManager;
72
import org.cresques.px.Extent;
73
import org.cresques.px.PxRaster;
74
import org.exolab.castor.xml.ValidationException;
75
import org.gvsig.remoteClient.utils.Utilities;
76
import org.gvsig.remoteClient.wms.WMSStatus;
77

    
78
import com.iver.andami.PluginServices;
79
import com.iver.cit.gvsig.fmap.DriverException;
80
import com.iver.cit.gvsig.fmap.ViewPort;
81
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
82
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException;
83
import com.iver.cit.gvsig.fmap.drivers.WMSException;
84
import com.iver.cit.gvsig.fmap.drivers.wms.FMapWMSDriver;
85
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
86
import com.iver.cit.gvsig.fmap.operations.Cancellable;
87
import com.iver.utiles.StringUtilities;
88
import com.iver.utiles.XMLEntity;
89

    
90

    
91
/**
92
* FMap's WMS Layer class.
93
*
94
* @author Jaume Dominguez Faus
95
*                   Nacho Brodin
96
* 
97
*/
98
public class FLyrWMS extends FLyrDefault implements InfoByPoint, RasterOperations {
99
        private boolean isPrinting = false;
100
        private boolean mustTileDraw = false;
101
        private boolean mustTilePrint = true;
102
        private final int maxTileDrawWidth = -1;
103
        private final int maxTileDrawHeight = -1;
104
        private final int maxTilePrintWidth = 1023;
105
        private final int maxTilePrintHeight = 1023;
106
    
107
    public URL host;
108
    public String m_Format;
109
    
110
        private String m_SRS;
111
        private String layerQuery;
112
        private String infoLayerQuery;
113
        private FMapWMSDriver wms;
114
        private WMSStatus wmsStatus = new WMSStatus();
115
        private Rectangle2D fullExtent;
116
        private boolean wmsTransparency;
117
    private Vector styles;
118
    private Vector dimensions;
119
        private StatusRasterInterface                status = null;
120
        private int                                                 posX = 0, posY = 0;
121
        private double                                                 posXWC = 0, posYWC = 0;
122
        private int                                                 r = 0, g = 0, b = 0;
123
        private GeoRasterFile                                 rasterFile = null;
124
        private PxRaster                                         raster = null;
125
        private RasterFilterStack                         filterStack = null;
126
        private boolean                                                firstLoad = false;
127
        private int                                                 transparency = -1;
128
        private int                                                 rband = 0, gband = 1, bband = 2;
129
        private RasterFilterStackManager        stackManager = null;
130
        private Hashtable onlineResources = new Hashtable();
131
        private Dimension fixedSize;
132
        private boolean queryable = true;
133

    
134

    
135

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

    
146
                // Full extent
147
                xml.putProperty("fullExtent", StringUtilities.rect2String(fullExtent));
148
                
149
                // Host
150
                xml.putProperty("host", host.toExternalForm());
151
                
152
                // Part of the query that is not the host, or the
153
                // layer names, or other not listed bellow
154
                xml.putProperty("infoLayerQuery", infoLayerQuery);
155
                
156
                // Part of the query containing the layer names
157
                xml.putProperty("layerQuery", layerQuery);
158
                
159
                // Format
160
                xml.putProperty("format", m_Format);
161
                
162
                // SRS
163
                xml.putProperty("srs", m_SRS);
164
                if (status!=null)
165
                        status.getXMLEntity(xml, true, this);
166
                else{
167
                        status = new StatusLayerRaster();
168
                        status.getXMLEntity(xml, true, this);
169
                }
170
                
171
        // Transparency
172
        xml.putProperty("wms_transparency", wmsTransparency);
173
        
174
        // Styles
175
        if (styles!=null){
176
            String stylePr = "";
177
            for (int i = 0; i < styles.size(); i++) {
178
                stylePr += (String) styles.get(i);
179
                if (i<styles.size()-1)
180
                    stylePr += ",";
181
            }
182
            if (stylePr.endsWith(","))
183
                    stylePr += " ";
184
            xml.putProperty("styles", stylePr);
185
        }
186
        
187
        // Dimensions 
188
        if (dimensions!=null){
189
            String dim = "";
190
            for (int i = 0; i < dimensions.size(); i++) {
191
                dim += (String) dimensions.get(i);
192
                if (i<dimensions.size()-1)
193
                    dim += ",";
194
            }
195
            if (dim.endsWith(","))
196
                    dim += " ";
197
            xml.putProperty("dimensions", dim);
198
        }
199
        
200
        // OnlineResources
201
        Iterator it = onlineResources.keySet().iterator();
202
        String strOnlines = "";
203
        while (it.hasNext()) {
204
                String key = (String) it.next();
205
                String value = (String) onlineResources.get(key);
206
                strOnlines = key+"~##SEP2##~"+value;
207
                if (it.hasNext())
208
                        strOnlines += "~##SEP1##~";
209
        }
210
        xml.putProperty("onlineResources", strOnlines);
211
        
212
        // Queryable
213
        xml.putProperty("queryable", queryable);
214
        return xml;
215
        }
216

    
217
        /**
218
         * A partir del XMLEntity reproduce la capa.
219
         *
220
         * @param xml XMLEntity
221
         *
222
         * @throws XMLException
223
         * @throws DriverException
224
         * @throws DriverIOException
225
         */
226
        public void setXMLEntity03(XMLEntity xml)
227
                throws XMLException {
228
                super.setXMLEntity(xml);
229
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
230
                                        "fullExtent"));
231

    
232
                try {
233
                        host = new URL(xml.getStringProperty("host"));
234
                } catch (MalformedURLException e) {
235
                        throw new XMLException(e);
236
                }
237

    
238
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
239
                layerQuery = xml.getStringProperty("layerQuery");
240
                m_Format = xml.getStringProperty("format");
241
                m_SRS = xml.getStringProperty("srs");
242
        }
243

    
244
        /**
245
         * A partir del XMLEntity reproduce la capa.
246
         *
247
         * @param xml XMLEntity
248
         *
249
         * @throws XMLException
250
         * @throws DriverException
251
         * @throws DriverIOException
252
         */
253
        public void setXMLEntity(XMLEntity xml)
254
                throws XMLException {
255
                super.setXMLEntity(xml);
256
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
257
                                        "fullExtent"));
258
                
259
                // Host
260
                try {
261
                        host = new URL(xml.getStringProperty("host"));
262
                } catch (MalformedURLException e) {
263
                        throw new XMLException(e);
264
                }
265

    
266
                // Part of the query that is not the host, or the
267
                // layer names, or other not listed bellow
268
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
269

    
270
                // Part of the query containing the layer names
271
                layerQuery = xml.getStringProperty("layerQuery");
272
                
273
                // Format
274
                m_Format = xml.getStringProperty("format");
275
                
276
                // SRS
277
                m_SRS = xml.getStringProperty("srs");
278
                
279
                String claseStr = StatusLayerRaster.defaultClass;
280
                if (xml.contains("raster.class")) {
281
                        claseStr = xml.getStringProperty("raster.class");
282
                }
283
                
284
                // Transparency
285
        if (xml.contains("wms_transparency"))
286
            wmsTransparency = xml.getBooleanProperty("wms_transparency");
287
        
288
        // Styles
289
        if (xml.contains("styles")){
290
            styles = new Vector();
291
            String[] stl = xml.getStringProperty("styles").split(",");
292
            
293
            for (int i = 0; i < stl.length; i++) {
294
                    if (stl[i].equals(" "))
295
                            stl[i]="";
296
                styles.add(stl[i]);
297
            }
298
        }
299
        
300
        // Dimensions
301
        if (xml.contains("dimensions")){
302
            dimensions = new Vector();
303
            String[] dims = xml.getStringProperty("dimensions").split(",");
304
            for (int i = 0; i < dims.length; i++){
305
                    if (dims[i].equals(" "))
306
                            dims[i]="";
307
                
308
                dimensions.add(dims[i]);
309
            }
310
        }
311
        
312
        // OnlineResources
313
        if (xml.contains("onlineResources")) {
314
                String[] operations = xml.getStringProperty("onlineResources").split("~##SEP1##~");
315
                for (int i = 0; i < operations.length; i++) {
316
                                String[] resources = operations[i].split("~##SEP2##~");
317
                                if (resources.length==2 && resources[1]!="")
318
                                        onlineResources.put(resources[0], resources[1]);
319
                        }
320
        }
321
        
322
        // Queryable
323
        queryable = true; // let's assume that the layer is queryable by default
324
        if (xml.contains("queryable"))
325
                queryable = xml.getBooleanProperty("queryable");
326
        
327
                if(status!=null)
328
                        status.setXMLEntity(xml, this);
329
                else{
330
                        if(claseStr!=null && !claseStr.equals("")){
331
                                try{
332
                                        Class clase = Class.forName(claseStr);
333
                                        Constructor constr = clase.getConstructor(null);
334
                                        status = (StatusRasterInterface)constr.newInstance(null);
335
                                        if(status!=null)
336
                                                status.setXMLEntity(xml, this);
337
                                }catch(ClassNotFoundException exc){
338
                                        exc.printStackTrace();
339
                                }catch(InstantiationException exc){
340
                                        exc.printStackTrace();
341
                                }catch(IllegalAccessException exc){
342
                                        exc.printStackTrace();
343
                                }catch(NoSuchMethodException exc){
344
                                        exc.printStackTrace();
345
                                }catch(InvocationTargetException exc){
346
                                        exc.printStackTrace();
347
                                }                                        
348
                        }
349
                }
350
                firstLoad = true;
351
        }
352

    
353
        /**
354
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(com.iver.cit.gvsig.fmap.operations.QueriedPoint)
355
         */
356
        public String queryByPoint(Point p) throws DriverException {
357
                try {
358
                        if (queryable)
359
                        {
360
                                //TODO
361
                                // check if there are layers which are not queryable
362
                                ViewPort viewPort = getFMap().getViewPort();
363

    
364
                                Point tiledPoint = new Point((int) p.getX() % maxTilePrintWidth, (int) p.getY() % maxTilePrintHeight);
365
                                Rectangle rect = new Rectangle(0, 0, viewPort.getImageWidth() - 1, viewPort.getImageHeight() - 1);
366
                                Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, rect);
367
                                tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
368
                                int nCols = tiles.getNumCols();
369

    
370
                                int col = (int) p.getX() / maxTilePrintWidth;
371
                                int row = (int) p.getY() / maxTilePrintHeight;
372
                                int tileIndex = (row*nCols) + col;
373
                                
374
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileIndex);
375
                                wmsStatus.setExtent(vp.getExtent());
376
                                wmsStatus.setHeight(vp.getImageHeight());
377
                                wmsStatus.setWidth(vp.getImageWidth());
378
                                wmsStatus.setOnlineResource((String) onlineResources.get("GetFeatureInfo"));
379
                                return new String(getDriver()
380
                                                .getFeatureInfo(wmsStatus, (int) tiledPoint.getX(), (int) tiledPoint.getY(), Integer.MAX_VALUE));
381
                        }
382
                        else
383
                        {
384
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame()
385
                                                                        ,PluginServices.getText(this, "wms_not_queryable"));
386
                                return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><info></info>";
387
                        }
388
                } catch (WMSException  e) {
389
                        return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><exception>" +
390
                        e.getMessage() + "</exception>";
391
                } catch (ValidationException e) {
392
                        throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
393
                } catch (UnsupportedVersionException e) {
394
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
395
                } catch (IOException e) {
396
                        throw new DriverException(PluginServices.getText(this, "connect_error"), e);
397
                } catch (NoninvertibleTransformException e) {
398
                        // TODO Auto-generated catch block
399
                        e.printStackTrace();
400
                }
401
                return null;
402
        }
403

    
404
        /**
405
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
406
         */
407
        public Rectangle2D getFullExtent() {
408
                return fullExtent;
409
        }
410

    
411
        /**
412
         * 
413
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
414
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
415
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
416
         */
417
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
418
                        Cancellable cancel,double scale) throws DriverException {
419
                
420
                if (isWithinScale(scale)){
421
                        
422
                        Point2D p = viewPort.getOffset();
423
                        // p will be (0, 0) when drawing a view or other when painting onto
424
                        // the Layout.
425
                        Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), viewPort.getImageWidth() - 1, viewPort.getImageHeight() - 1);
426
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, r);
427
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
428
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
429
                            // drawing part
430
                            try {
431
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
432
                                _draw(null, g, vp, cancel,scale);
433
                                } catch (NoninvertibleTransformException e) {
434
                                        e.printStackTrace();
435
                                }
436
                }
437
                }
438
        }
439
                
440
        /**
441
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
442
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
443
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
444
         */
445
        public void _draw(BufferedImage image, Graphics2D g, ViewPort vp,
446
                        Cancellable cancel,double scale) throws DriverException {
447

    
448
                // Compute the query geometry 
449
                // 1. Check if it is within borders
450
                Rectangle2D extent = getFullExtent();
451
        if ((vp.getExtent().getMinX() > extent.getMaxX()) ||
452
                (vp.getExtent().getMinY() > extent.getMaxY()) ||
453
                (vp.getExtent().getMaxX() < extent.getMinX()) ||
454
                (vp.getExtent().getMaxY() < extent.getMinY())) {
455
            return;
456
        }
457
        
458
        // 2. Compute extent.
459
        Rectangle2D bBox = new Rectangle2D.Double();
460
        Rectangle2D.intersect(vp.getExtent(), extent, bBox);
461
        
462
        // 3. Compute size in pixels
463
        double scalex = vp.getAffineTransform().getScaleX(); 
464
        double scaley = vp.getAffineTransform().getScaleY(); 
465
        int wImg = (int) Math.ceil(Math.abs(bBox.getWidth() * scalex) + 1);
466
        int hImg = (int) Math.ceil(Math.abs(bBox.getHeight() * scaley) + 1);
467
        Dimension sz = new Dimension(wImg, hImg);
468

    
469
        if ((wImg <= 0) || (hImg <= 0)) {
470
            return;
471
        }
472
                
473
                try {                        
474
                        wmsStatus.setExtent( bBox );
475
                        wmsStatus.setFormat(m_Format);
476
                        wmsStatus.setHeight( hImg );
477
                        wmsStatus.setWidth( wImg );
478
                        wmsStatus.setLayerNames(Utilities.createVector(layerQuery,","));
479
                        wmsStatus.setSrs(m_SRS);
480
                        wmsStatus.setStyles(styles);
481
                        wmsStatus.setDimensions(dimensions);
482
                        wmsStatus.setTransparency(wmsTransparency);
483
                        wmsStatus.setOnlineResource((String) onlineResources.get("GetMap"));
484
                        
485
                        File f = getDriver().getMap(wmsStatus);
486
                        String nameWordFile = f.getPath() + getExtensionWordFile();
487
                        com.iver.andami.Utilities.createTemp(nameWordFile, this.getDataWordFile(bBox, sz));
488
                        
489
                        if(status!=null && firstLoad){
490
                                status.applyStatus(this);
491
                                firstLoad = false;
492
                        }
493
                        ViewPortData vpData = new ViewPortData(
494
                                vp.getProjection(), new Extent(bBox), sz );
495
                        vpData.setMat(vp.getAffineTransform());
496

    
497
                        rasterProcess(g, vpData, f);
498
                        
499
                } catch (ValidationException e) {
500
                        throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
501
                } catch (UnsupportedVersionException e) {
502
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
503
                } catch (IOException e) {
504
                        throw new DriverException(PluginServices.getText(this, "connect_error"), e);
505
                } catch (WMSException e) {
506
            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
507
                        this.setVisible(false);
508
                }
509
                
510
        }
511

    
512
        /**
513
         * Obtiene la extensi?n del fichero de georreferenciaci?n
514
         * @return String con la extensi?n del fichero de georreferenciaci?n dependiendo
515
         * del valor del formato obtenido del servidor. Por defecto asignaremos un .wld 
516
         */
517
        private String getExtensionWordFile(){
518
                String extWorldFile = ".wld";
519
            if(m_Format.equals("image/tif") || m_Format.equals("image/tiff"))
520
                    extWorldFile = ".tfw";
521
            if(m_Format.equals("image/jpeg"))
522
                    extWorldFile = ".jpgw";
523
            return extWorldFile;
524
        }
525
        
526
        /**
527
         * Calcula el contenido del fichero de georreferenciaci?n de una imagen.
528
         * @param bBox Tama?o y posici?n de la imagen (en coordenadas de usuario)
529
         * @param sz Tama?o de la imagen en p?xeles.
530
         * @return el 'WorldFile', como String.
531
         * @throws IOException
532
         */
533
        public String getDataWordFile(Rectangle2D bBox, Dimension sz) throws IOException {
534
                StringBuffer data = new StringBuffer();
535
            data.append((bBox.getMaxX() - bBox.getMinX())/(sz.getWidth() - 1)+"\n");
536
            data.append("0.0\n");
537
            data.append("0.0\n");
538
            data.append((bBox.getMaxY() - bBox.getMinY())/(sz.getHeight() - 1)+"\n");
539
            data.append(""+bBox.getMinX()+"\n");
540
            data.append(""+bBox.getMinY()+"\n");
541
            return data.toString();
542
        }
543
                
544
        /**
545
         * Dibuja una im?gen usando PxRaster
546
         * @param g        Graphics2D en el que hay que dibujar.
547
         * @param vpData Par?metros de visualizaci?n
548
         * @param file La im?gen en cuesti?n.
549
         */
550
        private void rasterProcess(Graphics2D g, ViewPortData vpData, File file) {
551
                
552
                //Creamos el PxRaster        
553
                rasterFile = new GdalFile(vpData.getProjection(), file.getAbsolutePath());
554
                raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
555
                
556
                //Recuperamos la pila de filtros si ya hubiese sido cargado antes
557
                if(this.filterStack!=null)
558
                        raster.filterStack = this.filterStack;
559
                
560
                raster.setTransparency(false);
561
                                                
562
                //Asignamos transparencia y orden de bandas
563
                if(this.transparency==-1 && !firstLoad);
564
                else
565
                        raster.setTransparency(this.transparency);
566
                
567
                raster.setBand(GeoRasterFile.RED_BAND,rband);
568
                raster.setBand(GeoRasterFile.GREEN_BAND, gband);
569
                raster.setBand(GeoRasterFile.BLUE_BAND, bband);
570
        
571
                //Despues del primer pxRaster asignamos el stackManager guardado para los siguientes.
572
                //Con esto conseguimos asignar los cambios que se hayan producido desde el cuadro de 
573
                //propiedades cuando creamos un nuevo pxRaster
574
                if(this.stackManager != null)
575
                        raster.setStackManager(this.stackManager); 
576

    
577
                raster.draw(g, vpData);
578
                
579
                //En el primer pxRaster de una imagen obtenemos el Stack Manager para poder modificarlo
580
                //si queremos desde las propiedades
581
                
582
                if(this.stackManager == null)
583
                        this.stackManager = raster.getStackManager(); 
584
                
585
                if(this.filterStack == null)
586
                        this.filterStack = raster.filterStack;
587
        }
588
        
589
        /**
590
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
591
         *                 com.iver.cit.gvsig.fmap.ViewPort,
592
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
593
         */
594
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
595
                throws DriverException {
596
                if (isVisible() && isWithinScale(scale)){        
597
                isPrinting = true;
598
                if (!mustTilePrint) {
599
                        draw(null, g, viewPort, cancel,scale);
600
                } else {
601
                // Para no pedir imagenes demasiado grandes, vamos
602
                // a hacer lo mismo que hace EcwFile: chunkear.
603
                // Llamamos a drawView con cuadraditos m?s peque?os
604
                // del BufferedImage ni caso, cuando se imprime viene con null
605
                        
606
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
607
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
608
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
609
                            // Parte que dibuja
610
                            try {
611
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
612
                                draw(null, g, vp, cancel,scale);
613
                                } catch (NoninvertibleTransformException e) {
614
                                        e.printStackTrace();
615
                                }
616
                }
617
                }
618
            isPrinting = false;
619
                }
620
        }
621
        
622
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
623
                throws DriverException {
624
                draw(null, g, viewPort, cancel,scale);
625
        }
626

    
627
        /**
628
         * Devuelve el FMapWMSDriver.
629
         *
630
         * @return FMapWMSDriver
631
         *
632
         * @throws IllegalStateException
633
         * @throws ValidationException
634
         * @throws UnsupportedVersionException
635
         * @throws IOException
636
         */
637
        private FMapWMSDriver getDriver()
638
                throws IllegalStateException, ValidationException, 
639
                        UnsupportedVersionException, IOException {
640
                if (wms == null) {
641
                        //wmsClient = WMSClientFactory.getClient(host);
642
                        wms = new FMapWMSDriver();
643
            wms.createClient(host);
644
            
645
                }
646

    
647
                return wms;
648
        }
649
        
650
        /**
651
         * Devuelve el FMapWMSDriver.
652
         *
653
         * @return FMapWMSDriver
654
         *
655
         * @throws IllegalStateException
656
         * @throws ValidationException
657
         * @throws UnsupportedVersionException
658
         * @throws IOException
659
         */
660
        public void setDriver(FMapWMSDriver drv) {
661
                wms = drv;
662
        }
663

    
664
        /**
665
         * Devuelve el URL.
666
         *
667
         * @return URL.
668
         */
669
        public URL getHost() {
670
                return host;
671
        }
672

    
673
        /**
674
         * Inserta el URL.
675
         *
676
         * @param host URL.
677
         */
678
        public void setHost(URL host) {
679
                this.host = host;
680
        }
681

    
682
        /**
683
         * Devuelve la informaci?n de la consulta.
684
         *
685
         * @return String.
686
         */
687
        public String getInfoLayerQuery() {
688
                return infoLayerQuery;
689
        }
690

    
691
        /**
692
         * Inserta la informaci?n de la consulta.
693
         *
694
         * @param infoLayerQuery String.
695
         */
696
        public void setInfoLayerQuery(String infoLayerQuery) {
697
                this.infoLayerQuery = infoLayerQuery;
698
        }
699

    
700
        /**
701
         * Devuelve la consulta.
702
         *
703
         * @return String.
704
         */
705
        public String getLayerQuery() {
706
                return layerQuery;
707
        }
708

    
709
        /**
710
         * Inserta la consulta.
711
         *
712
         * @param layerQuery consulta.
713
         */
714
        public void setLayerQuery(String layerQuery) {
715
                this.layerQuery = layerQuery;
716
        }
717

    
718
        /**
719
         * Devuelve el formato.
720
         *
721
         * @return Formato.
722
         */
723
        public String getFormat() {
724
                return m_Format;
725
        }
726

    
727
        /**
728
         * Inserta el formato.
729
         *
730
         * @param format Formato.
731
         */
732
        public void setFormat(String format) {
733
                m_Format = format;
734
        }
735

    
736
        /**
737
         * Devuelve el SRS.
738
         *
739
         * @return SRS.
740
         */
741
        public String getSRS() {
742
                return m_SRS;
743
        }
744

    
745
        /**
746
         * Inserta el SRS.
747
         *
748
         * @param m_srs SRS.
749
         */
750
        public void setSRS(String m_srs) {
751
                m_SRS = m_srs;
752
        }
753

    
754
        /**
755
         * Inserta la extensi?n total de la capa.
756
         *
757
         * @param fullExtent Rect?ngulo.
758
         */
759
        public void setFullExtent(Rectangle2D fullExtent) {
760
                this.fullExtent = fullExtent;
761
        }
762
        
763
        public HashMap getProperties() {
764
                HashMap info = new HashMap();
765
        String[] layerNames = getLayerQuery().split(",");
766
        Vector layers = new Vector(layerNames.length);
767
        try {
768
            if(getDriver().connect()){
769
                for (int i = 0; i < layerNames.length; i++) {
770
                    layers.add(i, wms.getLayer(layerNames[i]));
771
                }
772
                info.put("name", getName());
773
                info.put("selectedLayers", layers);
774
                info.put("host", getHost());
775
                info.put("srs", getSRS());
776
                info.put("format", getFormat());
777
                info.put("wmsTransparency", new Boolean(wmsTransparency));
778
                info.put("styles", styles);
779
                info.put("dimensions", dimensions);
780
                return info;
781
            }
782
        } catch (Exception e) {
783
            e.printStackTrace();
784
        }
785
        return null;
786
        }
787
        
788
        /**
789
         * Asignar el estado del raster
790
         * @param status
791
         */
792
        public void setStatus(StatusRasterInterface status){
793
                this.status = status;
794
        }
795
        
796
        /**
797
         * Obtiene el estado del raster
798
         * @return
799
         */
800
        public StatusRasterInterface getStatus(){
801
                return this.status;
802
        }
803
        
804
        /* (non-Javadoc)
805
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getAttributes()
806
         */
807
        public ArrayList getAttributes() {
808
                if(rasterFile != null){
809
                        ArrayList attr = new ArrayList();
810
/*                        String dataType = "Byte";
811
                        if (rasterFile.getDataType() == DataBuffer.TYPE_BYTE) dataType = "Byte";
812
                        else if (rasterFile.getDataType() == DataBuffer.TYPE_SHORT)
813
                                dataType = "Short";
814
                        else if (rasterFile.getDataType() == DataBuffer.TYPE_USHORT)
815
                                dataType = "Unsigned Short";
816
                        else if (rasterFile.getDataType() == DataBuffer.TYPE_INT)
817
                                dataType = "Integer";
818
                        else if (rasterFile.getDataType() == DataBuffer.TYPE_FLOAT)
819
                                dataType = "Float";
820
                        else if (rasterFile.getDataType() == DataBuffer.TYPE_DOUBLE)
821
                                dataType = "Double";
822
                        else
823
                                dataType = "Unknown";
824
*/
825
                        Object [][] a = {
826
                                {"Filename",rasterFile.getName().substring(rasterFile.getName().lastIndexOf("/")+1, rasterFile.getName().length())},
827
                                {"Filesize",new Long(rasterFile.getFileSize())},
828
                                {"Width",new Integer(rasterFile.getWidth())},
829
                                {"Height", new Integer(rasterFile.getHeight())},
830
                                {"Bands", new Integer(rasterFile.getBandCount())}
831
                                //{"BandDataType", dataType}
832
                        };
833
                        for (int i=0; i<a.length; i++){
834
                                System.out.println("===> "+a[i][0]+" "+a[i][1]);
835
                                attr.add(a[i]);
836
                        }
837
                        return attr;
838
                }
839
                return  null;
840
        }
841
        /* (non-Javadoc)
842
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getFilterStack()
843
         */
844
        public RasterFilterStack getFilterStack() {
845
                if(raster!=null)
846
                        return raster.filterStack;
847
                return null;
848
        }
849
        /* (non-Javadoc)
850
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getHeight()
851
         */
852
        public double getHeight() {
853
                if(getGeoRasterFile() != null)
854
                        return getGeoRasterFile().getHeight();
855
        return 0;
856
        }
857
        /* (non-Javadoc)
858
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxX()
859
         */
860
        public double getMaxX() {
861
                if(getPxRaster() != null)
862
                        return getPxRaster().getExtent().getMax().getX();
863
        return 0;
864
        }
865
        /* (non-Javadoc)
866
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxY()
867
         */
868
        public double getMaxY() {
869
                if(getPxRaster() != null)
870
                        return getPxRaster().getExtent().getMax().getY();
871
        return 0;
872
        }
873
        /* (non-Javadoc)
874
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinX()
875
         */
876
        public double getMinX() {
877
                if(getPxRaster() != null)
878
                        return getPxRaster().getExtent().getMin().getX();
879
        return 0;
880
        }
881
        /* (non-Javadoc)
882
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinY()
883
         */
884
        public double getMinY() {
885
                if(getPxRaster() != null)
886
                        return getPxRaster().getExtent().getMin().getY();
887
        return 0;
888
        }
889
        /* (non-Javadoc)
890
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getPixel(double, double)
891
         */
892
        public int[] getPixel(double wcx, double wcy) {
893
                if(getPxRaster() != null)
894
                        return getPxRaster().getPixel(wcx, wcy);
895
        return null;
896
        }
897
        /* (non-Javadoc)
898
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getSource()
899
         */
900
        public RasterAdapter getSource() {
901
                return null;
902
        }
903
        /* (non-Javadoc)
904
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getWidth()
905
         */
906
        public double getWidth() {
907
                if(getGeoRasterFile() != null)
908
                        return getGeoRasterFile().getWidth();
909
        return 0;
910
        }
911
        /* (non-Javadoc)
912
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setBand(int, int)
913
         */
914
        public void setBand(int flag, int nBand) {
915
                switch(flag){
916
                case GeoRasterFile.RED_BAND:setBandR(nBand);break;
917
                case GeoRasterFile.GREEN_BAND:setBandG(nBand);break;
918
                case GeoRasterFile.BLUE_BAND:setBandB(nBand);break;
919
                }
920
        }
921
        /* (non-Javadoc)
922
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setFilterStack(org.cresques.io.raster.RasterFilterStack)
923
         */
924
        public void setFilterStack(RasterFilterStack stack) {
925
                this.filterStack = stack;
926
        }
927
        /* (non-Javadoc)
928
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPos(int, int)
929
         */
930
        public void setPos(int x, int y) {
931
                this.posX = x;
932
                this.posY = y;
933
        }
934
        
935
        /* (non-Javadoc)
936
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPosWC(double, double)
937
         */
938
        public void setPosWC(double x, double y) {
939
                this.posXWC = x;
940
                this.posYWC = y;
941
        }
942
        
943
        /* (non-Javadoc)
944
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setRGB(int, int, int)
945
         */
946
        public void setRGB(int r, int g, int b) {
947
                this.r = r;
948
                this.g = g;
949
                this.b = b;
950
        }
951
        /* (non-Javadoc)
952
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setSource(com.iver.cit.gvsig.fmap.layers.RasterAdapter)
953
         */
954
        public void setSource(RasterAdapter ra) {
955
        }
956
        /**
957
         * @return Returns the raster.
958
         */
959
        public PxRaster getPxRaster() {
960
                return raster;
961
        }
962
        /**
963
         * @return Returns the rasterFile.
964
         */
965
        public GeoRasterFile getGeoRasterFile() {
966
                return rasterFile;
967
        }
968
        
969
        public void setTransparency(int trans) {
970
                this.transparency = trans;
971
        }
972
        
973
        /**
974
         * Sets the R-band.
975
         * 
976
         * Asigna la banda R.
977
         * @param r
978
         */
979
        public void setBandR(int r){
980
                this.rband = r;
981
        }
982
        
983
        /**
984
         * Sets the G-band.
985
         * 
986
         * Asigna la banda G
987
         * @param g
988
         */
989
        public void setBandG(int g){
990
                this.gband = g;
991
        }
992
        
993
        /**
994
         * Sets the B-band.
995
         * 
996
         * Asigna la banda B
997
         * @param b
998
         */
999
        public void setBandB(int b){
1000
                this.bband = b;
1001
        }
1002

    
1003
    /**
1004
     * @return Returns the wmsTransparency.
1005
     */
1006
    public boolean isWmsTransparent() {
1007
        return wmsTransparency;
1008
    }
1009

    
1010
    /**
1011
     * @param wmsTransparency The wmsTransparency to set.
1012
     */
1013
    public void setWmsTransparency(boolean wmsTransparency) {
1014
        this.wmsTransparency = wmsTransparency;
1015
    }
1016

    
1017
     /**
1018
     * @param styles
1019
     */
1020
    public void setStyles(Vector styles) {
1021
            //laura:
1022
            //layer query is built with the layer in reverse order
1023
            // so here we build the styles upside-down.
1024
            if (styles != null)
1025
            {
1026
                    this.styles = new Vector();
1027
                    for(int i = styles.size()-1; i>=0; i--)
1028
                    {
1029
                            this.styles.add(styles.elementAt(i));
1030
                    }
1031
            }
1032
    }
1033
    
1034
    /**
1035
     * @param dimensions
1036
     */
1037
    public void setDimensions(Vector dimensions) {
1038
        this.dimensions = dimensions;
1039
    }
1040

    
1041
        public void setOnlineResources(Hashtable onlineResources) {
1042
                this.onlineResources = onlineResources;
1043
        }
1044
        
1045
        public void setFixedSize(Dimension sz) {
1046
                fixedSize = sz;
1047
        }
1048
        
1049
        private boolean isSizeFixed() {
1050
                return fixedSize != null && fixedSize.getWidth() > 0 && fixedSize.getHeight() > 0;
1051
        }
1052

    
1053
        public void setQueryable(boolean b) {
1054
                queryable = b;
1055
        }
1056
        
1057
}