Statistics
| Revision:

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

History | View | Annotate | Download (42.7 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.awt.image.DataBuffer;
54
import java.io.File;
55
import java.io.IOException;
56
import java.lang.reflect.Constructor;
57
import java.lang.reflect.InvocationTargetException;
58
import java.net.MalformedURLException;
59
import java.net.URL;
60
import java.util.ArrayList;
61
import java.util.HashMap;
62
import java.util.Hashtable;
63
import java.util.Iterator;
64
import java.util.Map;
65
import java.util.Vector;
66

    
67
import javax.swing.JOptionPane;
68

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

    
81
import com.iver.andami.PluginServices;
82
import com.iver.andami.messages.NotificationManager;
83
import com.iver.cit.gvsig.fmap.DriverException;
84
import com.iver.cit.gvsig.fmap.ViewPort;
85
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
86
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException;
87
import com.iver.cit.gvsig.fmap.drivers.wms.FMapWMSDriver;
88
import com.iver.cit.gvsig.fmap.drivers.wms.FMapWMSDriverFactory;
89
import com.iver.cit.gvsig.fmap.drivers.wms.WMSException;
90
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode.FMapWMSStyle;
91
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
92
import com.iver.cit.gvsig.fmap.layers.layerOperations.StringXMLItem;
93
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
94
import com.iver.cit.gvsig.fmap.rendering.XmlBuilder;
95
import com.iver.cit.gvsig.wmc.WebMapContextTags;
96
import com.iver.utiles.StringUtilities;
97
import com.iver.utiles.XMLEntity;
98
import com.iver.utiles.swing.threads.Cancellable;
99

    
100

    
101

    
102
/**
103
* FMap's WMS Layer class.
104
*
105
* @author Jaume Dominguez Faus
106
*                   Nacho Brodin
107
* 
108
*/
109
public class FLyrWMS extends FLyrDefault implements InfoByPoint, RasterOperations {
110
        private boolean                                         isPrinting = false;
111
        private boolean                                         mustTileDraw = false;
112
        private boolean                                         mustTilePrint = true;
113
        private final int                                         maxTileDrawWidth = -1;
114
        private final int                                         maxTileDrawHeight = -1;
115
        private final int                                         maxTilePrintWidth = 1200;
116
        private final int                                         maxTilePrintHeight = 1200;
117
    
118
    public URL                                                         host;
119
    public String                                                 m_Format;
120
    
121
        private String                                                 m_SRS;
122
        private String                                                 layerQuery;
123
        private String                                                 infoLayerQuery;
124
        private FMapWMSDriver                                 wms;
125
        private WMSStatus                                         wmsStatus = new WMSStatus();
126
        private Rectangle2D                                 fullExtent;
127
        private boolean                                                wmsTransparency;
128
    private Vector                                                 styles;
129
    private Vector                                                 dimensions;
130
        private StatusRasterInterface                status = null;
131
        private int                                                 posX = 0, posY = 0;
132
        private double                                                 posXWC = 0, posYWC = 0;
133
        private int                                                 r = 0, g = 0, b = 0;
134
        private GeoRasterFile                                 rasterFile = null;
135
        private PxRaster                                         raster = null;
136
        private RasterFilterStack                         filterStack = null;
137
        private boolean                                                firstLoad = false;
138
        private int                                                 transparency = -1;
139
        private int                                                 rband = 0, gband = 1, bband = 2;
140
        private RasterFilterStackManager        stackManager = null;
141
        private Hashtable                                         onlineResources = new Hashtable();
142
        private Dimension                                         fixedSize;
143
        private boolean                                         queryable = true;
144
        private VisualStatusWMS                                visualStatus = new VisualStatusWMS();
145
        
146
        
147
        private class MyCancellable implements ICancellable
148
        {
149

    
150
                private Cancellable original;
151
                public MyCancellable(Cancellable cancelOriginal)
152
                {
153
                        this.original = cancelOriginal;
154
                }
155
                public boolean isCanceled() {
156
                        return original.isCanceled();
157
                }
158
                
159
        }
160
        
161
        public FLyrWMS(){
162
                super();
163
        }
164
        
165
        public FLyrWMS(Map args) throws DriverIOException{
166
                FMapWMSDriver drv = null;
167
                String host = (String)args.get("host");
168
                String sLayer = (String)args.get("layer");
169
                Rectangle2D fullExtent = (Rectangle2D)args.get("FullExtent");
170
                String sSRS = (String)args.get("SRS");
171
                String sFormat = (String)args.get("Format");
172
                String[] sLayers = sLayer.split(",");
173
                
174
                try {
175
                        this.setHost(new URL(host));
176
                } catch (MalformedURLException e) {
177
                        //e.printStackTrace();
178
                        throw new DriverIOException("Malformed host URL, '" + host + "' (" + e.toString() + ").");                        
179
                }
180
                try {
181
                        drv = this.getDriver();
182
                } catch (Exception e) {
183
                        // e.printStackTrace();
184
                        throw new DriverIOException("Can't get driver to host '" + host + "' (" + e.toString() + ").");                        
185
                }
186
                if( sFormat == null || sSRS == null || fullExtent == null ) {
187
                        if (!drv.connect(null))
188
                                throw new DriverIOException("Can't connect to host '" + host + "'."); 
189
                        
190
                        WMSLayerNode wmsNode = drv.getLayer(sLayer);
191
                                                        
192
                        if (wmsNode == null){
193
                                throw new DriverIOException("The server '" + host + "' doesn't has the layer '" + sLayer + "'.");
194
                        }                
195
                        if( sFormat == null ) {
196
                                sFormat = this.getGreatFormat(drv.getFormats());
197
                        }
198
                     if( sSRS == null ) {
199
                             sSRS = (String)wmsNode.getAllSrs().get(0);
200
                     }
201
                        if( fullExtent == null ) {
202
                                fullExtent = drv.getLayersExtent(sLayers,(String)wmsNode.getAllSrs().get(0));
203
                        }
204
                }
205
                
206
                                
207
                this.setFullExtent(fullExtent);
208
                this.setFormat(sFormat);
209
                this.setLayerQuery(sLayer);
210
                this.setInfoLayerQuery("");
211
                this.setSRS(sSRS);
212
                this.setName(sLayer);
213
        }
214
        
215
        /**
216
         * It choose the best format to load different maps if the server 
217
         * supports it. This format could be png, because it supports 
218
         * transparency.
219
         * @param formats
220
         * Arraywith all the formats supported by the server
221
         * @return
222
         */        
223
        private String getGreatFormat(Vector formats){
224
            for (int i=0 ; i<formats.size() ; i++){
225
                String format = (String) formats.get(i);
226
                    if (format.equals("image/jpg")){
227
                    return format;
228
                    }
229
                    if (format.equals("image/jpeg")){
230
                    return format;
231
                    }
232
            }
233
                    
234
            return (String)formats.get(0);
235
        }
236
        
237
        /**
238
         * Clase que contiene los datos de visualizaci?n de WMS.
239
         * @author Nacho Brodin (brodin_ign@gva.es)
240
         */
241
        private class VisualStatusWMS{
242
                /**
243
                 * Ancho y alto de la imagen o del conjunto de tiles si los tiene. Coincide con 
244
                 * el ancho y alto del viewPort
245
                 */
246
                private        int                                                        width = 0, height = 0;
247
                private double                                                minX = 0D, minY = 0D, maxX = 0D, maxY = 0D;
248
                private int                                                 bandCount = 0;
249
                private int                                                        dataType = DataBuffer.TYPE_UNDEFINED;
250
        }
251
         
252

    
253
        /**
254
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir la
255
         * capa.
256
         *
257
         * @return XMLEntity.
258
         * @throws XMLException
259
         */
260
        public XMLEntity getXMLEntity() throws XMLException {
261
                XMLEntity xml = super.getXMLEntity();
262

    
263
                // Full extent
264
                xml.putProperty("fullExtent", StringUtilities.rect2String(fullExtent));
265
                
266
                // Host
267
                xml.putProperty("host", host.toExternalForm());
268
                
269
                // Part of the query that is not the host, or the
270
                // layer names, or other not listed bellow
271
                xml.putProperty("infoLayerQuery", infoLayerQuery);
272
                
273
                // Part of the query containing the layer names
274
                xml.putProperty("layerQuery", layerQuery);
275
                
276
                // Format
277
                xml.putProperty("format", m_Format);
278
                
279
                // SRS
280
                xml.putProperty("srs", m_SRS);
281
                if (status!=null)
282
                        status.getXMLEntity(xml, true, this);
283
                else{
284
                        status = new StatusLayerRaster();
285
                        status.getXMLEntity(xml, true, this);
286
                }
287
                
288
        // Transparency
289
        xml.putProperty("wms_transparency", wmsTransparency);
290
        
291
        // Styles
292
        if (styles!=null){
293
            String stylePr = "";
294
            for (int i = 0; i < styles.size(); i++) {
295
                stylePr += (String) styles.get(i);
296
                if (i<styles.size()-1)
297
                    stylePr += ",";
298
            }
299
            if (stylePr.endsWith(","))
300
                    stylePr += " ";
301
            xml.putProperty("styles", stylePr);
302
        }
303
        
304
        // Dimensions 
305
        if (dimensions!=null){
306
            String dim = "";
307
            for (int i = 0; i < dimensions.size(); i++) {
308
                dim += (String) dimensions.get(i);
309
                if (i<dimensions.size()-1)
310
                    dim += ",";
311
            }
312
            if (dim.endsWith(","))
313
                    dim += " ";
314
            xml.putProperty("dimensions", dim);
315
        }
316
        
317
        // OnlineResources
318
        Iterator it = onlineResources.keySet().iterator();
319
        String strOnlines = "";
320
        while (it.hasNext()) {
321
                String key = (String) it.next();
322
                String value = (String) onlineResources.get(key);
323
                strOnlines = key+"~##SEP2##~"+value;
324
                if (it.hasNext())
325
                        strOnlines += "~##SEP1##~";
326
        }
327
        xml.putProperty("onlineResources", strOnlines);
328
        
329
        // Queryable
330
        xml.putProperty("queryable", queryable);
331
        
332
        // fixedSize
333
        if (isSizeFixed()) {
334
                xml.putProperty("fixedSize", true);
335
                xml.putProperty("fixedWidth", fixedSize.width);
336
                xml.putProperty("fixedHeight", fixedSize.height);
337
        }
338
        return xml;
339
        }
340

    
341
        /**
342
         * A partir del XMLEntity reproduce la capa.
343
         *
344
         * @param xml XMLEntity
345
         *
346
         * @throws XMLException
347
         * @throws DriverException
348
         * @throws DriverIOException
349
         */
350
        public void setXMLEntity03(XMLEntity xml)
351
                throws XMLException {
352
                super.setXMLEntity(xml);
353
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
354
                                        "fullExtent"));
355

    
356
                try {
357
                        host = new URL(xml.getStringProperty("host"));
358
                } catch (MalformedURLException e) {
359
                        throw new XMLException(e);
360
                }
361

    
362
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
363
                layerQuery = xml.getStringProperty("layerQuery");
364
                m_Format = xml.getStringProperty("format");
365
                m_SRS = xml.getStringProperty("srs");
366
        }
367

    
368
        /**
369
         * A partir del XMLEntity reproduce la capa.
370
         *
371
         * @param xml XMLEntity
372
         *
373
         * @throws XMLException
374
         * @throws DriverException
375
         * @throws DriverIOException
376
         */
377
        public void setXMLEntity(XMLEntity xml)
378
                throws XMLException {
379
                super.setXMLEntity(xml);
380
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
381
                                        "fullExtent"));
382
                
383
                // Host
384
                try {
385
                        host = new URL(xml.getStringProperty("host"));
386
                } catch (MalformedURLException e) {
387
                        throw new XMLException(e);
388
                }
389

    
390
                // Part of the query that is not the host, or the
391
                // layer names, or other not listed bellow
392
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
393

    
394
                // Part of the query containing the layer names
395
                layerQuery = xml.getStringProperty("layerQuery");
396
                
397
                // Format
398
                m_Format = xml.getStringProperty("format");
399
                
400
                // SRS
401
                m_SRS = xml.getStringProperty("srs");
402
                
403
                String claseStr = StatusLayerRaster.defaultClass;
404
                if (xml.contains("raster.class")) {
405
                        claseStr = xml.getStringProperty("raster.class");
406
                }
407
                
408
                // Transparency
409
        if (xml.contains("wms_transparency"))
410
            wmsTransparency = xml.getBooleanProperty("wms_transparency");
411
        
412
        // Styles
413
        if (xml.contains("styles")){
414
            styles = new Vector();
415
            String[] stl = xml.getStringProperty("styles").split(",");
416
            
417
            for (int i = 0; i < stl.length; i++) {
418
                    if (stl[i].equals(" "))
419
                            stl[i]="";
420
                styles.add(stl[i]);
421
            }
422
        }
423
        
424
        // Dimensions
425
        if (xml.contains("dimensions")){
426
            dimensions = new Vector();
427
            String[] dims = xml.getStringProperty("dimensions").split(",");
428
            for (int i = 0; i < dims.length; i++){
429
                    if (dims[i].equals(" "))
430
                            dims[i]="";
431
                
432
                dimensions.add(dims[i]);
433
            }
434
        }
435
        
436
        // OnlineResources
437
        if (xml.contains("onlineResources")) {
438
                String[] operations = xml.getStringProperty("onlineResources").split("~##SEP1##~");
439
                for (int i = 0; i < operations.length; i++) {
440
                                String[] resources = operations[i].split("~##SEP2##~");
441
                                if (resources.length==2 && resources[1]!="")
442
                                        onlineResources.put(resources[0], resources[1]);
443
                        }
444
        }
445
        
446
        // Queryable
447
        queryable = true; // let's assume that the layer is queryable by default
448
        if (xml.contains("queryable"))
449
                queryable = xml.getBooleanProperty("queryable");
450
        
451
        // fixedSize
452
        if (xml.contains("fixedSize")) {
453
                fixedSize = new Dimension(xml.getIntProperty("fixedWidth"), 
454
                                                  xml.getIntProperty("fixedHeight"));
455
        }
456
        
457
                if(status!=null)
458
                        status.setXMLEntity(xml, this);
459
                else{
460
                        if(claseStr!=null && !claseStr.equals("")){
461
                                try{
462
                                        Class clase = Class.forName(claseStr);
463
                                        Constructor constr = clase.getConstructor(null);
464
                                        status = (StatusRasterInterface)constr.newInstance(null);
465
                                        if(status!=null)
466
                                                status.setXMLEntity(xml, this);
467
                                }catch(ClassNotFoundException exc){
468
                                        exc.printStackTrace();
469
                                }catch(InstantiationException exc){
470
                                        exc.printStackTrace();
471
                                }catch(IllegalAccessException exc){
472
                                        exc.printStackTrace();
473
                                }catch(NoSuchMethodException exc){
474
                                        exc.printStackTrace();
475
                                }catch(InvocationTargetException exc){
476
                                        exc.printStackTrace();
477
                                }                                        
478
                        }
479
                }
480
                firstLoad = true;
481
        }
482

    
483
        /**
484
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(com.iver.cit.gvsig.fmap.operations.QueriedPoint)
485
         */
486
        public XMLItem[] getInfo(Point p, double tolerance) throws DriverException {
487
                XMLItem[] item =  new XMLItem[1];
488
                try {                        
489
                        if (queryable)
490
                        {
491
                                //TODO
492
                                // check if there are layers which are not queryable
493
                                ViewPort viewPort = getFMap().getViewPort();
494

    
495
                                Point tiledPoint = new Point((int) p.getX() % maxTilePrintWidth, (int) p.getY() % maxTilePrintHeight);
496
                                Rectangle rect = new Rectangle(0, 0, viewPort.getImageWidth() - 1, viewPort.getImageHeight() - 1);
497
                                Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, rect);
498
                                tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
499
                                int nCols = tiles.getNumCols();
500

    
501
                                int col = (int) p.getX() / maxTilePrintWidth;
502
                                int row = (int) p.getY() / maxTilePrintHeight;
503
                                int tileIndex = (row*nCols) + col;
504
                                
505
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileIndex);
506
                                wmsStatus.setExtent(vp.getExtent());
507
                                wmsStatus.setHeight(vp.getImageHeight());
508
                                wmsStatus.setWidth(vp.getImageWidth());
509
                                wmsStatus.setOnlineResource((String) onlineResources.get("GetFeatureInfo"));
510
                                item[0] = new StringXMLItem(new String(getDriver()
511
                                                .getFeatureInfo(wmsStatus, (int) tiledPoint.getX(), (int) tiledPoint.getY(), Integer.MAX_VALUE)),this); 
512
                                return item;
513
                        }
514
                        else
515
                        {
516
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
517
                                                PluginServices.getText(this, "wms_not_queryable"));
518
                                item[0] =  new StringXMLItem("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><info></info>",this);
519
                                return item;
520
                        }
521
                } catch (WMSException  e) {
522
                        item[0] = new StringXMLItem("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><exception>" +
523
                        e.getMessage() + "</exception>", this);
524
                        return item;
525
                } catch (ValidationException e) {
526
                        throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
527
                } catch (UnsupportedVersionException e) {
528
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
529
                } catch (IOException e) {
530
                        throw new DriverException(PluginServices.getText(this, "connect_error"), e);
531
                } catch (NoninvertibleTransformException e) {
532
                        NotificationManager.addError("NotinvertibleTransform", e);
533
                }
534
                return null;
535
        }
536

    
537
        /**
538
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
539
         */
540
        public Rectangle2D getFullExtent() {
541
                return fullExtent;
542
        }
543

    
544
        /**
545
         * 
546
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
547
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
548
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
549
         */
550
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
551
                        Cancellable cancel,double scale) throws DriverException {
552
                
553
                if (isWithinScale(scale)){
554
                        Point2D p = viewPort.getOffset();
555
                        // p will be (0, 0) when drawing a view or other when painting onto
556
                        // the Layout.
557
                        visualStatus.width =  viewPort.getImageWidth();
558
                        visualStatus.height =  viewPort.getImageHeight();
559
                        visualStatus.minX = viewPort.getAdjustedExtent().getMinX();
560
                        visualStatus.minY = viewPort.getAdjustedExtent().getMinY();
561
                        visualStatus.maxX = viewPort.getAdjustedExtent().getMaxX();
562
                        visualStatus.maxY = viewPort.getAdjustedExtent().getMaxY();
563
                        if (isSizeFixed()) {
564
                                // This condition handles those situations in which the server can
565
                                // only give static extent and resolution maps despite we need
566
                                // a specific BBOX and pixel WIDTH and HEIGHT
567
                                drawFixedSize(g, viewPort, cancel);
568

    
569
                        } else {
570
                                Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), viewPort.getImageWidth() - 1, viewPort.getImageHeight() - 1);
571
                                Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, r);
572
                                tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
573
                                for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
574
                                        // drawing part
575
                                        try {
576
                                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
577
                                                drawTile(g, vp, cancel);
578
                                        } catch (NoninvertibleTransformException e) {
579
                                                e.printStackTrace();
580
                                        }
581
                                }
582
                        }
583
                }
584
//                Runtime r = Runtime.getRuntime();
585
//                long mem = r.totalMemory() - r.freeMemory();
586
//                System.err.println("Memoria total: " + (mem / 1024) +"KB");
587
        }
588
        
589
        private void drawFixedSize(Graphics2D g, ViewPort vp, Cancellable cancel) throws DriverException {
590
                // This is the extent that will be requested
591
                Rectangle2D bBox = getFullExtent();
592
                MyCancellable c = new MyCancellable(cancel);
593
                
594
                try {                        
595
                        wmsStatus.setExtent( bBox );
596
                        wmsStatus.setFormat( m_Format );
597
                        wmsStatus.setHeight( fixedSize.height );
598
                        wmsStatus.setWidth( fixedSize.width );
599
                        wmsStatus.setLayerNames(Utilities.createVector(layerQuery,","));
600
                        wmsStatus.setSrs(m_SRS);
601
                        wmsStatus.setStyles(styles);
602
                        wmsStatus.setDimensions(dimensions);
603
                        wmsStatus.setTransparency(wmsTransparency);
604
                        wmsStatus.setOnlineResource((String) onlineResources.get("GetMap"));
605
                        File f = getDriver().getMap(wmsStatus, c);
606
                        if (f == null)
607
                                return;
608
                        String nameWorldFile = f.getPath() + getExtensionWorldFile();
609
                        com.iver.andami.Utilities.createTemp(nameWorldFile, this.getDataWorldFile(bBox, fixedSize));
610
                        
611
                        if(status!=null && firstLoad){
612
                                status.applyStatus(this);
613
                                firstLoad = false;
614
                        }
615
                        
616
                        // And finally, obtain the extent intersecting the view and the BBox
617
                        // to draw to.
618
                        Rectangle2D extent = new Rectangle2D.Double();
619
                        Rectangle2D.intersect(vp.getAdjustedExtent(), bBox, extent);
620
                        
621
                        ViewPortData vpData = new ViewPortData(
622
                                vp.getProjection(), new Extent(extent), fixedSize );
623
                        vpData.setMat(vp.getAffineTransform());
624

    
625
                        rasterProcess(g, vpData, f);
626
                        
627
                } catch (ValidationException e) {
628
                        if (!c.isCanceled())
629
                                throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
630
                } catch (UnsupportedVersionException e) {
631
                        if (!c.isCanceled())
632
                                throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
633
                } catch (IOException e) {
634
                        if (!c.isCanceled())
635
                                throw new DriverException(PluginServices.getText(this, "connect_error"), e);
636
                } catch (WMSException e) {
637
                        if (!c.isCanceled()) {
638
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
639
                                this.setVisible(false);
640
                        }
641
                }
642
                
643
                
644
        }
645
        
646
        /**
647
         * This is the method used to draw a tile in a WMS mosaic layer.
648
         */
649
        private void drawTile(Graphics2D g, ViewPort vp, Cancellable cancel) throws DriverException {
650

    
651
                // Compute the query geometry 
652
                // 1. Check if it is within borders
653
                Rectangle2D extent = getFullExtent();
654
        if ((vp.getExtent().getMinX() > extent.getMaxX()) ||
655
                (vp.getExtent().getMinY() > extent.getMaxY()) ||
656
                (vp.getExtent().getMaxX() < extent.getMinX()) ||
657
                (vp.getExtent().getMaxY() < extent.getMinY())) {
658
            return;
659
        }
660
        
661
        // 2. Compute extent to be requested.
662
        Rectangle2D bBox = new Rectangle2D.Double();
663
        Rectangle2D.intersect(vp.getExtent(), extent, bBox);
664
        
665
        // 3. Compute size in pixels
666
        double scalex = vp.getAffineTransform().getScaleX(); 
667
        double scaley = vp.getAffineTransform().getScaleY(); 
668
        int wImg = (int) Math.ceil(Math.abs(bBox.getWidth() * scalex) + 1);
669
        int hImg = (int) Math.ceil(Math.abs(bBox.getHeight() * scaley) + 1);
670
        Dimension sz = new Dimension(wImg, hImg);
671

    
672
        if ((wImg <= 0) || (hImg <= 0)) {
673
            return;
674
        }
675
        MyCancellable c = new MyCancellable(cancel);
676
                
677
                try {                        
678
                        wmsStatus.setExtent( bBox );
679
                        wmsStatus.setFormat(m_Format);
680
                        wmsStatus.setHeight( hImg );
681
                        wmsStatus.setWidth( wImg );
682
                        wmsStatus.setLayerNames(Utilities.createVector(layerQuery,","));
683
                        wmsStatus.setSrs(m_SRS);
684
                        wmsStatus.setStyles(styles);
685
                        wmsStatus.setDimensions(dimensions);
686
                        wmsStatus.setTransparency(wmsTransparency);
687
                        wmsStatus.setOnlineResource((String) onlineResources.get("GetMap"));
688
                        
689
                        File f = getDriver().getMap(wmsStatus, c);
690
                        if (f == null)
691
                                return;
692
                        String nameWordFile = f.getPath() + getExtensionWorldFile();
693
                        com.iver.andami.Utilities.createTemp(nameWordFile, this.getDataWorldFile(bBox, sz));
694
                        
695
                        if(status!=null && firstLoad){
696
                                status.applyStatus(this);
697
                                firstLoad = false;
698
                        }
699
                        ViewPortData vpData = new ViewPortData(
700
                                vp.getProjection(), new Extent(bBox), sz );
701
                        vpData.setMat(vp.getAffineTransform());
702

    
703
                        rasterProcess(g, vpData, f);
704
                        
705
                } catch (ValidationException e) {
706
                        if (!c.isCanceled())
707
                                throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
708
                } catch (UnsupportedVersionException e) {
709
                        if (!c.isCanceled())
710
                                throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
711
                } catch (IOException e) {
712
                        if (!c.isCanceled())
713
                                throw new DriverException(PluginServices.getText(this, "connect_error"), e);
714
                } catch (WMSException e) {
715
                        if (!c.isCanceled()) {
716
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
717
                                this.setVisible(false);
718
                        }
719
                }
720
                
721
        }
722

    
723
        /**
724
         * Obtiene la extensi?n del fichero de georreferenciaci?n
725
         * @return String con la extensi?n del fichero de georreferenciaci?n dependiendo
726
         * del valor del formato obtenido del servidor. Por defecto asignaremos un .wld 
727
         */
728
        private String getExtensionWorldFile(){
729
                String extWorldFile = ".wld";
730
            if(m_Format.equals("image/tif") || m_Format.equals("image/tiff"))
731
                    extWorldFile = ".tfw";
732
            if(m_Format.equals("image/jpeg"))
733
                    extWorldFile = ".jpgw";
734
            return extWorldFile;
735
        }
736
        
737
        /**
738
         * Calcula el contenido del fichero de georreferenciaci?n de una imagen.
739
         * @param bBox Tama?o y posici?n de la imagen (en coordenadas de usuario)
740
         * @param sz Tama?o de la imagen en pixeles.
741
         * @return el 'WorldFile', como String.
742
         * @throws IOException
743
         */
744
        public String getDataWorldFile(Rectangle2D bBox, Dimension sz) throws IOException {
745
                StringBuffer data = new StringBuffer();
746
            data.append((bBox.getMaxX() - bBox.getMinX())/(sz.getWidth() - 1)+"\n");
747
            data.append("0.0\n");
748
            data.append("0.0\n");
749
            data.append((bBox.getMaxY() - bBox.getMinY())/(sz.getHeight() - 1)+"\n");
750
            data.append(""+bBox.getMinX()+"\n");
751
            data.append(""+bBox.getMinY()+"\n");
752
            return data.toString();
753
        }
754
                
755
        /**
756
         * Dibuja una imagen usando PxRaster
757
         * @param g        Graphics2D en el que hay que dibujar.
758
         * @param vpData Par?metros de visualizaci?n
759
         * @param file La imagen en cuesti?n.
760
         */
761
        private void rasterProcess(Graphics2D g, ViewPortData vpData, File file) {
762
                
763
                //Creamos el PxRaster        
764
                rasterFile = new GdalFile(vpData.getProjection(), file.getAbsolutePath());
765
                raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
766
                
767
                //Recuperamos la pila de filtros si ya hubiese sido cargado antes
768
                if(this.filterStack!=null)
769
                        raster.filterStack = this.filterStack;
770
                
771
                raster.setTransparency(false);
772
                                                
773
                //Asignamos transparencia y orden de bandas
774
                if(this.transparency==-1 && !firstLoad);
775
                else
776
                        raster.setTransparency(this.transparency);
777
                
778
                raster.setBand(GeoRasterFile.RED_BAND,rband);
779
                raster.setBand(GeoRasterFile.GREEN_BAND, gband);
780
                raster.setBand(GeoRasterFile.BLUE_BAND, bband);
781
        
782
                //Despues del primer pxRaster asignamos el stackManager guardado para los siguientes.
783
                //Con esto conseguimos asignar los cambios que se hayan producido desde el cuadro de 
784
                //propiedades cuando creamos un nuevo pxRaster
785
                if(this.stackManager != null)
786
                        raster.setStackManager(this.stackManager); 
787
                
788
                if(visualStatus != null){
789
                        visualStatus.bandCount = raster.getBandCount();
790
                        visualStatus.dataType = raster.getDataType();
791
                }
792

    
793
                raster.draw(g, vpData);
794
                
795
                //En el primer pxRaster de una imagen obtenemos el Stack Manager para poder modificarlo
796
                //si queremos desde las propiedades
797
                
798
                if(this.stackManager == null)
799
                        this.stackManager = raster.getStackManager(); 
800
                
801
                if(this.filterStack == null)
802
                        this.filterStack = raster.filterStack;
803
                
804
                //rasterFile.close();
805
        }
806
        
807
        /**
808
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
809
         *                 com.iver.cit.gvsig.fmap.ViewPort,
810
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
811
         */
812
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
813
                throws DriverException {
814
                if (isVisible() && isWithinScale(scale)){        
815
                isPrinting = true;
816
                if (!mustTilePrint) {
817
                        draw(null, g, viewPort, cancel,scale);
818
                } else {
819
                // Para no pedir imagenes demasiado grandes, vamos
820
                // a hacer lo mismo que hace EcwFile: chunkear.
821
                // Llamamos a drawView con cuadraditos m?s peque?os
822
                // del BufferedImage ni caso, cuando se imprime viene con null
823
                        
824
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
825
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
826
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
827
                            // Parte que dibuja
828
                            try {
829
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
830
                                drawTile(g, vp, cancel);
831
                                } catch (NoninvertibleTransformException e) {
832
                                        e.printStackTrace();
833
                                }
834
                }
835
                }
836
            isPrinting = false;
837
                }
838
        }
839
        
840
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
841
                throws DriverException {
842
                draw(null, g, viewPort, cancel,scale);
843
        }
844

    
845
        /**
846
         * Devuelve el FMapWMSDriver.
847
         *
848
         * @return FMapWMSDriver
849
         *
850
         * @throws IllegalStateException
851
         * @throws ValidationException
852
         * @throws UnsupportedVersionException
853
         * @throws IOException
854
         */
855
        private FMapWMSDriver getDriver()
856
                throws IllegalStateException, ValidationException, 
857
                        UnsupportedVersionException, IOException {
858
                return FMapWMSDriverFactory.getFMapDriverForURL(host);
859
        }
860
        
861
        /**
862
         * Devuelve el FMapWMSDriver.
863
         *
864
         * @return FMapWMSDriver
865
         *
866
         * @throws IllegalStateException
867
         * @throws ValidationException
868
         * @throws UnsupportedVersionException
869
         * @throws IOException
870
         */
871
        public void setDriver(FMapWMSDriver drv) {
872
                wms = drv;
873
        }
874

    
875
        /**
876
         * Devuelve el URL.
877
         *
878
         * @return URL.
879
         */
880
        public URL getHost() {
881
                return host;
882
        }
883

    
884
        /**
885
         * Inserta el URL.
886
         *
887
         * @param host URL.
888
         */
889
        public void setHost(URL host) {
890
                this.host = host;
891
        }
892

    
893
        /**
894
         * Devuelve la informaci?n de la consulta.
895
         *
896
         * @return String.
897
         */
898
        public String getInfoLayerQuery() {
899
                return infoLayerQuery;
900
        }
901

    
902
        /**
903
         * Inserta la informaci?n de la consulta.
904
         *
905
         * @param infoLayerQuery String.
906
         */
907
        public void setInfoLayerQuery(String infoLayerQuery) {
908
                this.infoLayerQuery = infoLayerQuery;
909
        }
910

    
911
        /**
912
         * Devuelve la consulta.
913
         *
914
         * @return String.
915
         */
916
        public String getLayerQuery() {
917
                return layerQuery;
918
        }
919

    
920
        /**
921
         * Inserta la consulta.
922
         *
923
         * @param layerQuery consulta.
924
         */
925
        public void setLayerQuery(String layerQuery) {
926
                this.layerQuery = layerQuery;
927
        }
928

    
929
        /**
930
         * Devuelve el formato.
931
         *
932
         * @return Formato.
933
         */
934
        public String getFormat() {
935
                return m_Format;
936
        }
937

    
938
        /**
939
         * Inserta el formato.
940
         *
941
         * @param format Formato.
942
         */
943
        public void setFormat(String format) {
944
                m_Format = format;
945
        }
946

    
947
        /**
948
         * Devuelve el SRS.
949
         *
950
         * @return SRS.
951
         */
952
        public String getSRS() {
953
                return m_SRS;
954
        }
955

    
956
        /**
957
         * Inserta el SRS.
958
         *
959
         * @param m_srs SRS.
960
         */
961
        public void setSRS(String m_srs) {
962
                m_SRS = m_srs;
963
        }
964

    
965
        /**
966
         * Inserta la extensi?n total de la capa.
967
         *
968
         * @param fullExtent Rect?ngulo.
969
         */
970
        public void setFullExtent(Rectangle2D fullExtent) {
971
                this.fullExtent = fullExtent;
972
        }
973
        
974
        public HashMap getProperties() {
975
                HashMap info = new HashMap();
976
        String[] layerNames = getLayerQuery().split(",");
977
        Vector layers = new Vector(layerNames.length);
978
        try {
979
            if(getDriver().connect(null)){
980
                for (int i = 0; i < layerNames.length; i++) {
981
                    layers.add(i, getDriver().getLayer(layerNames[i]));
982
                }
983
                info.put("name", getName());
984
                info.put("selectedLayers", layers);
985
                info.put("host", getHost());
986
                info.put("srs", getSRS());
987
                info.put("format", getFormat());
988
                info.put("wmsTransparency", new Boolean(wmsTransparency));
989
                info.put("styles", styles);
990
                info.put("dimensions", dimensions);
991
                info.put("fixedSize", fixedSize);
992
                return info;
993
            }
994
        } catch (Exception e) {
995
            e.printStackTrace();
996
        }
997
        return null;
998
        }
999
        
1000
        /**
1001
         * Asignar el estado del raster
1002
         * @param status
1003
         */
1004
        public void setStatus(StatusRasterInterface status){
1005
                this.status = status;
1006
        }
1007
        
1008
        /**
1009
         * Obtiene el estado del raster
1010
         * @return
1011
         */
1012
        public StatusRasterInterface getStatus(){
1013
                return this.status;
1014
        }
1015
        
1016
        /* (non-Javadoc)
1017
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getAttributes()
1018
         */
1019
        public ArrayList getAttributes() {
1020
                if(rasterFile != null){
1021
                        ArrayList attr = new ArrayList();
1022
                        String dataType = "Byte";
1023
                        if (rasterFile.getDataType() == DataBuffer.TYPE_BYTE) dataType = "Byte";
1024
                        else if (visualStatus.dataType == DataBuffer.TYPE_SHORT)
1025
                                dataType = "Short";
1026
                        else if (visualStatus.dataType == DataBuffer.TYPE_USHORT)
1027
                                dataType = "Unsigned Short";
1028
                        else if (visualStatus.dataType == DataBuffer.TYPE_INT)
1029
                                dataType = "Integer";
1030
                        else if (visualStatus.dataType == DataBuffer.TYPE_FLOAT)
1031
                                dataType = "Float";
1032
                        else if (visualStatus.dataType == DataBuffer.TYPE_DOUBLE)
1033
                                dataType = "Double";
1034
                        else
1035
                                dataType = "Unknown";
1036

    
1037
                        Object [][] a = {
1038
                                {"Filename",rasterFile.getName().substring(rasterFile.getName().lastIndexOf("/")+1, rasterFile.getName().length())},
1039
                                {"Filesize",new Long(0)},
1040
                                {"Width",new Integer((int)this.getWidth())},
1041
                                {"Height", new Integer((int)this.getHeight())},
1042
                                {"Bands", new Integer(visualStatus.bandCount)},
1043
                                {"BandDataType", dataType}
1044
                        };
1045
                        for (int i=0; i<a.length; i++)
1046
                                attr.add(a[i]);
1047

    
1048
                        return attr;
1049
                }
1050
                return  null;
1051
        }
1052
        
1053
        /* (non-Javadoc)
1054
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getFilterStack()
1055
         */
1056
        public RasterFilterStack getFilterStack() {
1057
                if(raster!=null)
1058
                        return raster.filterStack;
1059
                return null;
1060
        }
1061
        /* (non-Javadoc)
1062
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getHeight()
1063
         */
1064
        public double getHeight() {
1065
                return visualStatus.height;
1066
        }
1067
        
1068
        /* (non-Javadoc)
1069
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxX()
1070
         */
1071
        public double getMaxX() {
1072
                return visualStatus.maxX;
1073
        }
1074
        
1075
        /* (non-Javadoc)
1076
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxY()
1077
         */
1078
        public double getMaxY() {
1079
                return visualStatus.maxY;
1080
        }
1081
        
1082
        /* (non-Javadoc)
1083
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinX()
1084
         */
1085
        public double getMinX() {
1086
                return visualStatus.minX;
1087
        }
1088
        
1089
        /* (non-Javadoc)
1090
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinY()
1091
         */
1092
        public double getMinY() {
1093
                return visualStatus.minY;
1094
        }
1095
        /* (non-Javadoc)
1096
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getPixel(double, double)
1097
         */
1098
        public int[] getPixel(double wcx, double wcy) {
1099
                if(getPxRaster() != null)
1100
                        return getPxRaster().getPixel(wcx, wcy);
1101
        return null;
1102
        }
1103
        /* (non-Javadoc)
1104
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getSource()
1105
         */
1106
        public RasterAdapter getSource() {
1107
                return null;
1108
        }
1109
        /* (non-Javadoc)
1110
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getWidth()
1111
         */
1112
        public double getWidth() {
1113
                return visualStatus.width;
1114
        }
1115
        /* (non-Javadoc)
1116
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setBand(int, int)
1117
         */
1118
        public void setBand(int flag, int nBand) {
1119
                switch(flag){
1120
                case GeoRasterFile.RED_BAND:setBandR(nBand);break;
1121
                case GeoRasterFile.GREEN_BAND:setBandG(nBand);break;
1122
                case GeoRasterFile.BLUE_BAND:setBandB(nBand);break;
1123
                }
1124
        }
1125
        /* (non-Javadoc)
1126
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setFilterStack(org.cresques.io.raster.RasterFilterStack)
1127
         */
1128
        public void setFilterStack(RasterFilterStack stack) {
1129
                this.filterStack = stack;
1130
        }
1131
        /* (non-Javadoc)
1132
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPos(int, int)
1133
         */
1134
        public void setPos(int x, int y) {
1135
                this.posX = x;
1136
                this.posY = y;
1137
        }
1138
        
1139
        /* (non-Javadoc)
1140
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPosWC(double, double)
1141
         */
1142
        public void setPosWC(double x, double y) {
1143
                this.posXWC = x;
1144
                this.posYWC = y;
1145
        }
1146
        
1147
        /* (non-Javadoc)
1148
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setRGB(int, int, int)
1149
         */
1150
        public void setRGB(int r, int g, int b) {
1151
                this.r = r;
1152
                this.g = g;
1153
                this.b = b;
1154
        }
1155
        /* (non-Javadoc)
1156
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setSource(com.iver.cit.gvsig.fmap.layers.RasterAdapter)
1157
         */
1158
        public void setSource(RasterAdapter ra) {
1159
        }
1160
        /**
1161
         * @return Returns the raster.
1162
         */
1163
        public PxRaster getPxRaster() {
1164
                return raster;
1165
        }
1166
        /**
1167
         * @return Returns the rasterFile.
1168
         */
1169
        public GeoRasterFile getGeoRasterFile() {
1170
                return rasterFile;
1171
        }
1172
        
1173
        public void setTransparency(int trans) {
1174
                this.transparency = trans;
1175
        }
1176
        
1177
        /**
1178
         * Sets the R-band.
1179
         * 
1180
         * Asigna la banda R.
1181
         * @param r
1182
         */
1183
        public void setBandR(int r){
1184
                this.rband = r;
1185
        }
1186
        
1187
        /**
1188
         * Sets the G-band.
1189
         * 
1190
         * Asigna la banda G
1191
         * @param g
1192
         */
1193
        public void setBandG(int g){
1194
                this.gband = g;
1195
        }
1196
        
1197
        /**
1198
         * Sets the B-band.
1199
         * 
1200
         * Asigna la banda B
1201
         * @param b
1202
         */
1203
        public void setBandB(int b){
1204
                this.bband = b;
1205
        }
1206

    
1207
    /**
1208
     * @return Returns the wmsTransparency.
1209
     */
1210
    public boolean isWmsTransparent() {
1211
        return wmsTransparency;
1212
    }
1213

    
1214
    /**
1215
     * @param wmsTransparency The wmsTransparency to set.
1216
     */
1217
    public void setWmsTransparency(boolean wmsTransparency) {
1218
        this.wmsTransparency = wmsTransparency;
1219
    }
1220

    
1221
     /**
1222
     * @param styles
1223
     */
1224
    public void setStyles(Vector styles) {
1225
            //laura:
1226
            //layer query is built with the layer in reverse order
1227
            // so here we build the styles upside-down.
1228
            if (styles != null)
1229
            {
1230
                    this.styles = new Vector();
1231
                    for(int i = styles.size()-1; i>=0; i--)
1232
                    {
1233
                            this.styles.add(styles.elementAt(i));
1234
                    }
1235
            }
1236
    }
1237
    
1238
    /**
1239
     * Sets the dimension vector that is a list of key-value pairs containing
1240
     * the name of the dimension and the value for it
1241
     * @param dimensions
1242
     */
1243
    public void setDimensions(Vector dimensions) {
1244
        this.dimensions = dimensions;
1245
    }
1246

    
1247
    /**
1248
     * Sets the set of URLs that should be accessed for each operation performed
1249
     * to the server.
1250
     * 
1251
     * @param onlineResources
1252
     */
1253
        public void setOnlineResources(Hashtable onlineResources) {
1254
                this.onlineResources = onlineResources;
1255
        }
1256
        
1257
        /**
1258
         * When a server is not fully featured and it only can serve constant map
1259
         * sizes this value must be set. It expresses the size in pixels (width, height)
1260
         * that the map will be requested.
1261
         * @param Dimension sz
1262
         */
1263
        public void setFixedSize(Dimension sz) {
1264
                fixedSize = sz;
1265
        }
1266
        
1267
        /**
1268
         * Tells whether if this layer must deal with the server with the constant-size
1269
         * limitations or not.
1270
         * @return boolean.
1271
         */
1272
        private boolean isSizeFixed() {
1273
                return fixedSize != null && fixedSize.getWidth() > 0 && fixedSize.getHeight() > 0;
1274
        }
1275

    
1276
        /**
1277
         * If it is true, this layer accepts GetFeatureInfo operations. This WMS operations
1278
         * maps to FMap's infoByPoint(p) operation.
1279
         * @param b
1280
         */
1281
        public void setQueryable(boolean b) {
1282
                queryable = b;
1283
        }
1284

    
1285
        /**
1286
         * Creates the part of a OGC's MapContext document that would describe this
1287
         * layer(s).
1288
         * @param version, The desired version of the resulting document. (1.1.0)
1289
         * @return String containing the xml.
1290
         * @throws UnsupportedVersionException 
1291
         */
1292
        public String toMapContext(String mapContextVersion) {
1293
                XmlBuilder xml = new XmlBuilder();
1294
                FMapWMSDriver drv;
1295
                try {
1296
                        drv = getDriver();
1297
                } catch (Exception e) {
1298
                        return xml.toString();
1299
                } 
1300
                String[] layerNames = getLayerQuery().split(",");
1301
                String[] styleNames = (String[]) styles.toArray(new String[0]); 
1302
                for (int i = 0; i < layerNames.length; i++) {
1303
                        WMSLayerNode layer = drv.getLayer(layerNames[i]);
1304
                        HashMap xmlAttrs = new HashMap();
1305
                        
1306
                        // <Layer>
1307
                        xmlAttrs.put(WebMapContextTags.HIDDEN, !isVisible()+"");
1308
                        xmlAttrs.put(WebMapContextTags.QUERYABLE, queryable+"");
1309
                        xml.openTag(WebMapContextTags.LAYER, xmlAttrs);
1310
                        xmlAttrs.clear();
1311
                        if (mapContextVersion.equals("1.1.0") || mapContextVersion.equals("1.0.0")) {
1312
                                // <Server>
1313
                                xmlAttrs.put(WebMapContextTags.SERVICE, WebMapContextTags.WMS);
1314
                                xmlAttrs.put(WebMapContextTags.VERSION, drv.getVersion());
1315
                                xmlAttrs.put(WebMapContextTags.SERVER_TITLE, drv.getServiceTitle());
1316
                                xml.openTag(WebMapContextTags.SERVER, xmlAttrs);
1317
                                xmlAttrs.clear();
1318
                                
1319
                                        // <OnlineResource>
1320
                                        xmlAttrs.put(WebMapContextTags.XLINK_TYPE, "simple");
1321
                                        xmlAttrs.put(WebMapContextTags.XLINK_HREF, getHost().toString());
1322
                                        xml.writeTag(WebMapContextTags.ONLINE_RESOURCE, xmlAttrs);
1323
                                        xmlAttrs.clear();
1324
                                        // </OnlineResource>
1325
                                        
1326
                                xml.closeTag();
1327
                                // </Server>
1328
                                
1329
                                // <Name>
1330
                                xml.writeTag(WebMapContextTags.NAME, layer.getName().trim());
1331
                                // </Name>
1332
                                
1333
                                // <Title>
1334
                                xml.writeTag(WebMapContextTags.TITLE, getName().trim());
1335
                                // </Title>
1336
                                
1337
                                // <Abstract>
1338
                                if (layer.getAbstract() != null)
1339
                                        xml.writeTag(WebMapContextTags.ABSTRACT, layer.getAbstract());
1340
                                // </Abstract>
1341
                                
1342
                                // <SRS> (a list of available SRS for the enclosing layer)
1343
                                String[] strings = (String[]) layer.getAllSrs().toArray(new String[0]);
1344
                                String mySRS = strings[0];
1345
                                for (int j = 1; j < strings.length; j++) {
1346
                                        mySRS += ","+strings[j];
1347
                                }
1348
                                xml.writeTag(WebMapContextTags.SRS, mySRS);
1349
                                // </SRS>
1350
                                
1351
                                // <FormatList>
1352
                                xml.openTag(WebMapContextTags.FORMAT_LIST);
1353
                                        strings = (String[]) drv.getFormats().toArray(new String[0]);
1354
                                        for (int j = 0; j < strings.length; j++) {
1355
                    // <Format>
1356
                                                String str = strings[j].trim();
1357
                                                if (str.equals(getFormat()))
1358
                                                        xml.writeTag(WebMapContextTags.FORMAT, str, WebMapContextTags.CURRENT, "1");
1359
                                                else
1360
                                                        xml.writeTag(WebMapContextTags.FORMAT, str);
1361
                    // </Format>        
1362
                                        }
1363
                                xml.closeTag();
1364
                                // </FormatList>
1365
                                
1366
                                // <StyleList>
1367
                                xml.openTag(WebMapContextTags.STYLE_LIST);
1368
                                
1369
                                        if (layer.getStyles().size()>0) {
1370
                                                for (int j = 0; j < layer.getStyles().size(); j++) {
1371
                                                        // <Style>
1372
                                                        FMapWMSStyle st = (FMapWMSStyle) layer.getStyles().get(j);
1373
                                                        if (st.name.equals(styleNames[i]))
1374
                                                                xmlAttrs.put(WebMapContextTags.CURRENT, "1");
1375
                                                        xml.openTag(WebMapContextTags.STYLE, xmlAttrs);
1376
                                                        xmlAttrs.clear();
1377
                                                        
1378
                                                                // <Name>
1379
                                                                xml.writeTag(WebMapContextTags.NAME, st.name);
1380
                                                                // </Name>
1381
                                                                
1382
                                                                // <Title>
1383
                                                                xml.writeTag(WebMapContextTags.TITLE, st.title);
1384
                                                                // </Title>
1385
                                                                
1386
                                                                // <LegendURL width="180" format="image/gif" height="50">
1387
                                                                        // <OnlineResource xlink:type="simple" xlink:href="http://globe.digitalearth.gov/globe/en/icons/colorbars/NATIONAL.gif"/>
1388
                                                                        // </OnlineResource>
1389
                                                                // </LegendURL>
1390
                                                        xml.closeTag();
1391
                                                        // </Style>
1392
                                                        
1393
                                                }
1394
                                                
1395
                                        } else {
1396
                                                // Create fake style (for compatibility issues)
1397
                                                xmlAttrs.put(WebMapContextTags.CURRENT, "1");
1398
                                                // <Style>
1399
                                                xml.openTag(WebMapContextTags.STYLE, xmlAttrs);
1400
                                                        xmlAttrs.clear();
1401
                                                        // <Name>
1402
                                                        xml.writeTag(WebMapContextTags.NAME, "default");
1403
                                                        // </Name>
1404
                                                        
1405
                                                        // <Title>
1406
                                                        xml.writeTag(WebMapContextTags.TITLE, "default");
1407
                                                        // </Title>
1408
                                                        
1409
//                                                        // <LegendURL width="180" format="image/gif" height="50">
1410
//                                                        xmlAttrs.put(WebMapContextTags.WIDTH, "0");
1411
//                                                        xmlAttrs.put(WebMapContextTags.HEIGHT, "0");
1412
//                                                        xmlAttrs.put(WebMapContextTags.FORMAT.toLowerCase(), "image/gif");
1413
//                                                        xml.openTag(WebMapContextTags.LEGEND_URL, xmlAttrs);
1414
//                                                        xmlAttrs.clear();
1415
//                                                                // <OnlineResource xlink:type="simple" xlink:href="http://globe.digitalearth.gov/globe/en/icons/colorbars/NATIONAL.gif"/>
1416
//                                                                xmlAttrs.put(WebMapContextTags.XLINK_TYPE, "simple");
1417
//                                                                xmlAttrs.put(WebMapContextTags.XLINK_HREF, "http://globe.digitalearth.gov/globe/en/icons/colorbars/NATIONAL.gif");
1418
//                                                                xml.writeTag(WebMapContextTags.ONLINE_RESOURCE, xmlAttrs);
1419
//                                                                // </OnlineResource>
1420
//                                                    // </LegendURL>
1421
//                                                        xml.closeTag();
1422
                                                // </Style>
1423
                                                xml.closeTag();
1424
                                        }
1425
                                // </StyleList>
1426
                                xml.closeTag();
1427
                                if (mapContextVersion.compareTo("1.0.0") > 0) {
1428
                                // <DimensionList>
1429
                                        xml.openTag(WebMapContextTags.DIMENSION_LIST);
1430
                                        // <Dimension>
1431
                                        // </Dimension>
1432
                                        xml.closeTag();
1433
                                // </DimensionList>
1434
                                }
1435
                        } else {
1436
                                xml.writeTag("ERROR", PluginServices.getText(this, "unsupported_map_context_version"));
1437
                        }
1438
                        // </Layer>
1439
                        xml.closeTag();
1440
                }
1441
                return xml.getXML();
1442
        }
1443
}