Statistics
| Revision:

svn-gvsig-desktop / tags / Root_MULTITHREADING_DEVELOPMENT / extensions / extWMS / src / com / iver / cit / gvsig / fmap / layers / FLyrWMS.java @ 5232

History | View | Annotate | Download (40.4 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.WMSStatus;
79

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

    
96

    
97
/**
98
* FMap's WMS Layer class.
99
*
100
* @author Jaume Dominguez Faus
101
*                   Nacho Brodin
102
* 
103
*/
104
public class FLyrWMS extends FLyrDefault implements InfoByPoint, RasterOperations {
105
        private boolean                                         isPrinting = false;
106
        private boolean                                         mustTileDraw = false;
107
        private boolean                                         mustTilePrint = true;
108
        private final int                                         maxTileDrawWidth = -1;
109
        private final int                                         maxTileDrawHeight = -1;
110
        private final int                                         maxTilePrintWidth = 1200;
111
        private final int                                         maxTilePrintHeight = 1200;
112
    
113
    public URL                                                         host;
114
    public String                                                 m_Format;
115
    
116
        private String                                                 m_SRS;
117
        private String                                                 layerQuery;
118
        private String                                                 infoLayerQuery;
119
        private FMapWMSDriver                                 wms;
120
        private WMSStatus                                         wmsStatus = new WMSStatus();
121
        private Rectangle2D                                 fullExtent;
122
        private boolean                                                wmsTransparency;
123
    private Vector                                                 styles;
124
    private Vector                                                 dimensions;
125
        private StatusRasterInterface                status = null;
126
        private int                                                 posX = 0, posY = 0;
127
        private double                                                 posXWC = 0, posYWC = 0;
128
        private int                                                 r = 0, g = 0, b = 0;
129
        private GeoRasterFile                                 rasterFile = null;
130
        private PxRaster                                         raster = null;
131
        private RasterFilterStack                         filterStack = null;
132
        private boolean                                                firstLoad = false;
133
        private int                                                 transparency = -1;
134
        private int                                                 rband = 0, gband = 1, bband = 2;
135
        private RasterFilterStackManager        stackManager = null;
136
        private Hashtable                                         onlineResources = new Hashtable();
137
        private Dimension                                         fixedSize;
138
        private boolean                                         queryable = true;
139
        private VisualStatusWMS                                visualStatus = new VisualStatusWMS();
140
        
141
        public FLyrWMS(){
142
                super();
143
        }
144
        
145
        public FLyrWMS(Map args) throws DriverIOException{
146
                FMapWMSDriver drv = null;
147
                String host = (String)args.get("host");
148
                String sLayer = (String)args.get("layer");
149
                Rectangle2D fullExtent = (Rectangle2D)args.get("FullExtent");
150
                String sSRS = (String)args.get("SRS");
151
                String sFormat = (String)args.get("Format");
152
                String[] sLayers = sLayer.split(",");
153
                
154
                try {
155
                        this.setHost(new URL(host));
156
                } catch (MalformedURLException e) {
157
                        //e.printStackTrace();
158
                        throw new DriverIOException("Malformed host URL, '" + host + "' (" + e.toString() + ").");                        
159
                }
160
                try {
161
                        drv = this.getDriver();
162
                } catch (Exception e) {
163
                        // e.printStackTrace();
164
                        throw new DriverIOException("Can't get driver to host '" + host + "' (" + e.toString() + ").");                        
165
                }
166
                if( sFormat == null || sSRS == null || fullExtent == null ) {
167
                        if (!drv.connect())
168
                                throw new DriverIOException("Can't connect to host '" + host + "'."); 
169
                        
170
                        WMSLayerNode wmsNode = drv.getLayer(sLayer);
171
                                                        
172
                        if (wmsNode == null){
173
                                throw new DriverIOException("The server '" + host + "' doesn't has the layer '" + sLayer + "'.");
174
                        }                
175
                        if( sFormat == null ) {
176
                                sFormat = this.getGreatFormat(drv.getFormats());
177
                        }
178
                     if( sSRS == null ) {
179
                             sSRS = (String)wmsNode.getAllSrs().get(0);
180
                     }
181
                        if( fullExtent == null ) {
182
                                fullExtent = drv.getLayersExtent(sLayers,(String)wmsNode.getAllSrs().get(0));
183
                        }
184
                }
185
                
186
                                
187
                this.setFullExtent(fullExtent);
188
                this.setFormat(sFormat);
189
                this.setLayerQuery(sLayer);
190
                this.setInfoLayerQuery("");
191
                this.setSRS(sSRS);
192
                this.setName(sLayer);
193
        }
194
        
195
        /**
196
         * It choose the best format to load different maps if the server 
197
         * supports it. This format could be png, because it supports 
198
         * transparency.
199
         * @param formats
200
         * Arraywith all the formats supported by the server
201
         * @return
202
         */        
203
        private String getGreatFormat(Vector formats){
204
            for (int i=0 ; i<formats.size() ; i++){
205
                String format = (String) formats.get(i);
206
                    if (format.equals("image/jpg")){
207
                    return format;
208
                    }
209
                    if (format.equals("image/jpeg")){
210
                    return format;
211
                    }
212
            }
213
                    
214
            return (String)formats.get(0);
215
        }
216
        
217
        /**
218
         * Clase que contiene los datos de visualizaci?n de WMS.
219
         * @author Nacho Brodin (brodin_ign@gva.es)
220
         */
221
        private class VisualStatusWMS{
222
                /**
223
                 * Ancho y alto de la imagen o del conjunto de tiles si los tiene. Coincide con 
224
                 * el ancho y alto del viewPort
225
                 */
226
                private        int                                                        width = 0, height = 0;
227
                private double                                                minX = 0D, minY = 0D, maxX = 0D, maxY = 0D;
228
                private int                                                 bandCount = 0;
229
                private int                                                        dataType = DataBuffer.TYPE_UNDEFINED;
230
        }
231
         
232

    
233
        /**
234
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir la
235
         * capa.
236
         *
237
         * @return XMLEntity.
238
         * @throws XMLException
239
         */
240
        public XMLEntity getXMLEntity() throws XMLException {
241
                XMLEntity xml = super.getXMLEntity();
242

    
243
                // Full extent
244
                xml.putProperty("fullExtent", StringUtilities.rect2String(fullExtent));
245
                
246
                // Host
247
                xml.putProperty("host", host.toExternalForm());
248
                
249
                // Part of the query that is not the host, or the
250
                // layer names, or other not listed bellow
251
                xml.putProperty("infoLayerQuery", infoLayerQuery);
252
                
253
                // Part of the query containing the layer names
254
                xml.putProperty("layerQuery", layerQuery);
255
                
256
                // Format
257
                xml.putProperty("format", m_Format);
258
                
259
                // SRS
260
                xml.putProperty("srs", m_SRS);
261
                if (status!=null)
262
                        status.getXMLEntity(xml, true, this);
263
                else{
264
                        status = new StatusLayerRaster();
265
                        status.getXMLEntity(xml, true, this);
266
                }
267
                
268
        // Transparency
269
        xml.putProperty("wms_transparency", wmsTransparency);
270
        
271
        // Styles
272
        if (styles!=null){
273
            String stylePr = "";
274
            for (int i = 0; i < styles.size(); i++) {
275
                stylePr += (String) styles.get(i);
276
                if (i<styles.size()-1)
277
                    stylePr += ",";
278
            }
279
            if (stylePr.endsWith(","))
280
                    stylePr += " ";
281
            xml.putProperty("styles", stylePr);
282
        }
283
        
284
        // Dimensions 
285
        if (dimensions!=null){
286
            String dim = "";
287
            for (int i = 0; i < dimensions.size(); i++) {
288
                dim += (String) dimensions.get(i);
289
                if (i<dimensions.size()-1)
290
                    dim += ",";
291
            }
292
            if (dim.endsWith(","))
293
                    dim += " ";
294
            xml.putProperty("dimensions", dim);
295
        }
296
        
297
        // OnlineResources
298
        Iterator it = onlineResources.keySet().iterator();
299
        String strOnlines = "";
300
        while (it.hasNext()) {
301
                String key = (String) it.next();
302
                String value = (String) onlineResources.get(key);
303
                strOnlines = key+"~##SEP2##~"+value;
304
                if (it.hasNext())
305
                        strOnlines += "~##SEP1##~";
306
        }
307
        xml.putProperty("onlineResources", strOnlines);
308
        
309
        // Queryable
310
        xml.putProperty("queryable", queryable);
311
        
312
        // fixedSize
313
        if (isSizeFixed()) {
314
                xml.putProperty("fixedSize", true);
315
                xml.putProperty("fixedWidth", fixedSize.width);
316
                xml.putProperty("fixedHeight", fixedSize.height);
317
        }
318
        return xml;
319
        }
320

    
321
        /**
322
         * A partir del XMLEntity reproduce la capa.
323
         *
324
         * @param xml XMLEntity
325
         *
326
         * @throws XMLException
327
         * @throws DriverException
328
         * @throws DriverIOException
329
         */
330
        public void setXMLEntity03(XMLEntity xml)
331
                throws XMLException {
332
                super.setXMLEntity(xml);
333
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
334
                                        "fullExtent"));
335

    
336
                try {
337
                        host = new URL(xml.getStringProperty("host"));
338
                } catch (MalformedURLException e) {
339
                        throw new XMLException(e);
340
                }
341

    
342
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
343
                layerQuery = xml.getStringProperty("layerQuery");
344
                m_Format = xml.getStringProperty("format");
345
                m_SRS = xml.getStringProperty("srs");
346
        }
347

    
348
        /**
349
         * A partir del XMLEntity reproduce la capa.
350
         *
351
         * @param xml XMLEntity
352
         *
353
         * @throws XMLException
354
         * @throws DriverException
355
         * @throws DriverIOException
356
         */
357
        public void setXMLEntity(XMLEntity xml)
358
                throws XMLException {
359
                super.setXMLEntity(xml);
360
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
361
                                        "fullExtent"));
362
                
363
                // Host
364
                try {
365
                        host = new URL(xml.getStringProperty("host"));
366
                } catch (MalformedURLException e) {
367
                        throw new XMLException(e);
368
                }
369

    
370
                // Part of the query that is not the host, or the
371
                // layer names, or other not listed bellow
372
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
373

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

    
463
        /**
464
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(com.iver.cit.gvsig.fmap.operations.QueriedPoint)
465
         */
466
        public String queryByPoint(Point p) throws DriverException {
467
                try {
468
                        if (queryable)
469
                        {
470
                                //TODO
471
                                // check if there are layers which are not queryable
472
                                ViewPort viewPort = getFMap().getViewPort();
473

    
474
                                Point tiledPoint = new Point((int) p.getX() % maxTilePrintWidth, (int) p.getY() % maxTilePrintHeight);
475
                                Rectangle rect = new Rectangle(0, 0, viewPort.getImageWidth() - 1, viewPort.getImageHeight() - 1);
476
                                Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, rect);
477
                                tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
478
                                int nCols = tiles.getNumCols();
479

    
480
                                int col = (int) p.getX() / maxTilePrintWidth;
481
                                int row = (int) p.getY() / maxTilePrintHeight;
482
                                int tileIndex = (row*nCols) + col;
483
                                
484
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileIndex);
485
                                wmsStatus.setExtent(vp.getExtent());
486
                                wmsStatus.setHeight(vp.getImageHeight());
487
                                wmsStatus.setWidth(vp.getImageWidth());
488
                                wmsStatus.setOnlineResource((String) onlineResources.get("GetFeatureInfo"));
489
                                return new String(getDriver()
490
                                                .getFeatureInfo(wmsStatus, (int) tiledPoint.getX(), (int) tiledPoint.getY(), Integer.MAX_VALUE));
491
                        }
492
                        else
493
                        {
494
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
495
                                                PluginServices.getText(this, "wms_not_queryable"));
496
                                return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><info></info>";
497
                        }
498
                } catch (WMSException  e) {
499
                        return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><exception>" +
500
                        e.getMessage() + "</exception>";
501
                } catch (ValidationException e) {
502
                        throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
503
                } catch (UnsupportedVersionException e) {
504
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
505
                } catch (IOException e) {
506
                        throw new DriverException(PluginServices.getText(this, "connect_error"), e);
507
                } catch (NoninvertibleTransformException e) {
508
                        NotificationManager.addError("NotinvertibleTransform", e);
509
                }
510
                return null;
511
        }
512

    
513
        /**
514
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
515
         */
516
        public Rectangle2D getFullExtent() {
517
                return fullExtent;
518
        }
519

    
520
        /**
521
         * 
522
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
523
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
524
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
525
         */
526
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
527
                        Cancellable cancel,double scale) throws DriverException {
528
                
529
                if (isWithinScale(scale)){
530
                        Point2D p = viewPort.getOffset();
531
                        // p will be (0, 0) when drawing a view or other when painting onto
532
                        // the Layout.
533
                        visualStatus.width =  viewPort.getImageWidth();
534
                        visualStatus.height =  viewPort.getImageHeight();
535
                        visualStatus.minX = viewPort.getAdjustedExtent().getMinX();
536
                        visualStatus.minY = viewPort.getAdjustedExtent().getMinY();
537
                        visualStatus.maxX = viewPort.getAdjustedExtent().getMaxX();
538
                        visualStatus.maxY = viewPort.getAdjustedExtent().getMaxY();
539
                        if (isSizeFixed()) {
540
                                // This condition handles those situations in which the server can
541
                                // only give static extent and resolution maps despite we need
542
                                // a specific BBOX and pixel WIDTH and HEIGHT
543
                                drawFixedSize(g, viewPort, cancel);
544

    
545
                        } else {
546
                                Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), viewPort.getImageWidth() - 1, viewPort.getImageHeight() - 1);
547
                                Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, r);
548
                                tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
549
                                for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
550
                                        // drawing part
551
                                        try {
552
                                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
553
                                                drawTile(g, vp, cancel);
554
                                        } catch (NoninvertibleTransformException e) {
555
                                                e.printStackTrace();
556
                                        }
557
                                }
558
                        }
559
                }
560
//                Runtime r = Runtime.getRuntime();
561
//                long mem = r.totalMemory() - r.freeMemory();
562
//                System.err.println("Memoria total: " + (mem / 1024) +"KB");
563
        }
564
        
565
        private void drawFixedSize(Graphics2D g, ViewPort vp, Cancellable cancel) throws DriverException {
566
                // This is the extent that will be requested
567
                Rectangle2D bBox = getFullExtent();
568
                
569
                try {                        
570
                        wmsStatus.setExtent( bBox );
571
                        wmsStatus.setFormat( m_Format );
572
                        wmsStatus.setHeight( fixedSize.height );
573
                        wmsStatus.setWidth( fixedSize.width );
574
                        wmsStatus.setLayerNames(Utilities.createVector(layerQuery,","));
575
                        wmsStatus.setSrs(m_SRS);
576
                        wmsStatus.setStyles(styles);
577
                        wmsStatus.setDimensions(dimensions);
578
                        wmsStatus.setTransparency(wmsTransparency);
579
                        wmsStatus.setOnlineResource((String) onlineResources.get("GetMap"));
580
                        
581
                        File f = getDriver().getMap(wmsStatus);
582
                        String nameWorldFile = f.getPath() + getExtensionWorldFile();
583
                        com.iver.andami.Utilities.createTemp(nameWorldFile, this.getDataWorldFile(bBox, fixedSize));
584
                        
585
                        if(status!=null && firstLoad){
586
                                status.applyStatus(this);
587
                                firstLoad = false;
588
                        }
589
                        
590
                        // And finally, obtain the extent intersecting the view and the BBox
591
                        // to draw to.
592
                        Rectangle2D extent = new Rectangle2D.Double();
593
                        Rectangle2D.intersect(vp.getAdjustedExtent(), bBox, extent);
594
                        
595
                        ViewPortData vpData = new ViewPortData(
596
                                vp.getProjection(), new Extent(extent), fixedSize );
597
                        vpData.setMat(vp.getAffineTransform());
598

    
599
                        rasterProcess(g, vpData, f);
600
                        
601
                } catch (ValidationException e) {
602
                        throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
603
                } catch (UnsupportedVersionException e) {
604
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
605
                } catch (IOException e) {
606
                        throw new DriverException(PluginServices.getText(this, "connect_error"), e);
607
                } catch (WMSException e) {
608
            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
609
                        this.setVisible(false);
610
                }
611
                
612
                
613
        }
614
        
615
        /**
616
         * This is the method used to draw a tile in a WMS mosaic layer.
617
         */
618
        private void drawTile(Graphics2D g, ViewPort vp, Cancellable cancel) throws DriverException {
619

    
620
                // Compute the query geometry 
621
                // 1. Check if it is within borders
622
                Rectangle2D extent = getFullExtent();
623
        if ((vp.getExtent().getMinX() > extent.getMaxX()) ||
624
                (vp.getExtent().getMinY() > extent.getMaxY()) ||
625
                (vp.getExtent().getMaxX() < extent.getMinX()) ||
626
                (vp.getExtent().getMaxY() < extent.getMinY())) {
627
            return;
628
        }
629
        
630
        // 2. Compute extent to be requested.
631
        Rectangle2D bBox = new Rectangle2D.Double();
632
        Rectangle2D.intersect(vp.getExtent(), extent, bBox);
633
        
634
        // 3. Compute size in pixels
635
        double scalex = vp.getAffineTransform().getScaleX(); 
636
        double scaley = vp.getAffineTransform().getScaleY(); 
637
        int wImg = (int) Math.ceil(Math.abs(bBox.getWidth() * scalex) + 1);
638
        int hImg = (int) Math.ceil(Math.abs(bBox.getHeight() * scaley) + 1);
639
        Dimension sz = new Dimension(wImg, hImg);
640

    
641
        if ((wImg <= 0) || (hImg <= 0)) {
642
            return;
643
        }
644
                
645
                try {                        
646
                        wmsStatus.setExtent( bBox );
647
                        wmsStatus.setFormat(m_Format);
648
                        wmsStatus.setHeight( hImg );
649
                        wmsStatus.setWidth( wImg );
650
                        wmsStatus.setLayerNames(Utilities.createVector(layerQuery,","));
651
                        wmsStatus.setSrs(m_SRS);
652
                        wmsStatus.setStyles(styles);
653
                        wmsStatus.setDimensions(dimensions);
654
                        wmsStatus.setTransparency(wmsTransparency);
655
                        wmsStatus.setOnlineResource((String) onlineResources.get("GetMap"));
656
                        
657
                        File f = getDriver().getMap(wmsStatus);
658
                        String nameWordFile = f.getPath() + getExtensionWorldFile();
659
                        com.iver.andami.Utilities.createTemp(nameWordFile, this.getDataWorldFile(bBox, sz));
660
                        
661
                        if(status!=null && firstLoad){
662
                                status.applyStatus(this);
663
                                firstLoad = false;
664
                        }
665
                        ViewPortData vpData = new ViewPortData(
666
                                vp.getProjection(), new Extent(bBox), sz );
667
                        vpData.setMat(vp.getAffineTransform());
668

    
669
                        rasterProcess(g, vpData, f);
670
                        
671
                } catch (ValidationException e) {
672
                        throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
673
                } catch (UnsupportedVersionException e) {
674
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
675
                } catch (IOException e) {
676
                        throw new DriverException(PluginServices.getText(this, "connect_error"), e);
677
                } catch (WMSException e) {
678
            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
679
                        this.setVisible(false);
680
                }
681
                
682
        }
683

    
684
        /**
685
         * Obtiene la extensi?n del fichero de georreferenciaci?n
686
         * @return String con la extensi?n del fichero de georreferenciaci?n dependiendo
687
         * del valor del formato obtenido del servidor. Por defecto asignaremos un .wld 
688
         */
689
        private String getExtensionWorldFile(){
690
                String extWorldFile = ".wld";
691
            if(m_Format.equals("image/tif") || m_Format.equals("image/tiff"))
692
                    extWorldFile = ".tfw";
693
            if(m_Format.equals("image/jpeg"))
694
                    extWorldFile = ".jpgw";
695
            return extWorldFile;
696
        }
697
        
698
        /**
699
         * Calcula el contenido del fichero de georreferenciaci?n de una imagen.
700
         * @param bBox Tama?o y posici?n de la imagen (en coordenadas de usuario)
701
         * @param sz Tama?o de la imagen en pixeles.
702
         * @return el 'WorldFile', como String.
703
         * @throws IOException
704
         */
705
        public String getDataWorldFile(Rectangle2D bBox, Dimension sz) throws IOException {
706
                StringBuffer data = new StringBuffer();
707
            data.append((bBox.getMaxX() - bBox.getMinX())/(sz.getWidth() - 1)+"\n");
708
            data.append("0.0\n");
709
            data.append("0.0\n");
710
            data.append((bBox.getMaxY() - bBox.getMinY())/(sz.getHeight() - 1)+"\n");
711
            data.append(""+bBox.getMinX()+"\n");
712
            data.append(""+bBox.getMinY()+"\n");
713
            return data.toString();
714
        }
715
                
716
        /**
717
         * Dibuja una imagen usando PxRaster
718
         * @param g        Graphics2D en el que hay que dibujar.
719
         * @param vpData Par?metros de visualizaci?n
720
         * @param file La imagen en cuesti?n.
721
         */
722
        private void rasterProcess(Graphics2D g, ViewPortData vpData, File file) {
723
                
724
                //Creamos el PxRaster        
725
                rasterFile = new GdalFile(vpData.getProjection(), file.getAbsolutePath());
726
                raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
727
                
728
                //Recuperamos la pila de filtros si ya hubiese sido cargado antes
729
                if(this.filterStack!=null)
730
                        raster.filterStack = this.filterStack;
731
                
732
                raster.setTransparency(false);
733
                                                
734
                //Asignamos transparencia y orden de bandas
735
                if(this.transparency==-1 && !firstLoad);
736
                else
737
                        raster.setTransparency(this.transparency);
738
                
739
                raster.setBand(GeoRasterFile.RED_BAND,rband);
740
                raster.setBand(GeoRasterFile.GREEN_BAND, gband);
741
                raster.setBand(GeoRasterFile.BLUE_BAND, bband);
742
        
743
                //Despues del primer pxRaster asignamos el stackManager guardado para los siguientes.
744
                //Con esto conseguimos asignar los cambios que se hayan producido desde el cuadro de 
745
                //propiedades cuando creamos un nuevo pxRaster
746
                if(this.stackManager != null)
747
                        raster.setStackManager(this.stackManager); 
748
                
749
                if(visualStatus != null){
750
                        visualStatus.bandCount = raster.getBandCount();
751
                        visualStatus.dataType = raster.getDataType();
752
                }
753

    
754
                raster.draw(g, vpData);
755
                
756
                //En el primer pxRaster de una imagen obtenemos el Stack Manager para poder modificarlo
757
                //si queremos desde las propiedades
758
                
759
                if(this.stackManager == null)
760
                        this.stackManager = raster.getStackManager(); 
761
                
762
                if(this.filterStack == null)
763
                        this.filterStack = raster.filterStack;
764
                
765
                //rasterFile.close();
766
        }
767
        
768
        /**
769
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
770
         *                 com.iver.cit.gvsig.fmap.ViewPort,
771
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
772
         */
773
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
774
                throws DriverException {
775
                if (isVisible() && isWithinScale(scale)){        
776
                isPrinting = true;
777
                if (!mustTilePrint) {
778
                        draw(null, g, viewPort, cancel,scale);
779
                } else {
780
                // Para no pedir imagenes demasiado grandes, vamos
781
                // a hacer lo mismo que hace EcwFile: chunkear.
782
                // Llamamos a drawView con cuadraditos m?s peque?os
783
                // del BufferedImage ni caso, cuando se imprime viene con null
784
                        
785
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
786
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
787
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
788
                            // Parte que dibuja
789
                            try {
790
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
791
                                drawTile(g, vp, cancel);
792
                                } catch (NoninvertibleTransformException e) {
793
                                        e.printStackTrace();
794
                                }
795
                }
796
                }
797
            isPrinting = false;
798
                }
799
        }
800
        
801
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
802
                throws DriverException {
803
                draw(null, g, viewPort, cancel,scale);
804
        }
805

    
806
        /**
807
         * Devuelve el FMapWMSDriver.
808
         *
809
         * @return FMapWMSDriver
810
         *
811
         * @throws IllegalStateException
812
         * @throws ValidationException
813
         * @throws UnsupportedVersionException
814
         * @throws IOException
815
         */
816
        private FMapWMSDriver getDriver()
817
                throws IllegalStateException, ValidationException, 
818
                        UnsupportedVersionException, IOException {
819
                if (wms == null) {
820
                        //wmsClient = WMSClientFactory.getClient(host);
821
                        wms = new FMapWMSDriver();
822
            wms.createClient(host);
823
            
824
                }
825

    
826
                return wms;
827
        }
828
        
829
        /**
830
         * Devuelve el FMapWMSDriver.
831
         *
832
         * @return FMapWMSDriver
833
         *
834
         * @throws IllegalStateException
835
         * @throws ValidationException
836
         * @throws UnsupportedVersionException
837
         * @throws IOException
838
         */
839
        public void setDriver(FMapWMSDriver drv) {
840
                wms = drv;
841
        }
842

    
843
        /**
844
         * Devuelve el URL.
845
         *
846
         * @return URL.
847
         */
848
        public URL getHost() {
849
                return host;
850
        }
851

    
852
        /**
853
         * Inserta el URL.
854
         *
855
         * @param host URL.
856
         */
857
        public void setHost(URL host) {
858
                this.host = host;
859
        }
860

    
861
        /**
862
         * Devuelve la informaci?n de la consulta.
863
         *
864
         * @return String.
865
         */
866
        public String getInfoLayerQuery() {
867
                return infoLayerQuery;
868
        }
869

    
870
        /**
871
         * Inserta la informaci?n de la consulta.
872
         *
873
         * @param infoLayerQuery String.
874
         */
875
        public void setInfoLayerQuery(String infoLayerQuery) {
876
                this.infoLayerQuery = infoLayerQuery;
877
        }
878

    
879
        /**
880
         * Devuelve la consulta.
881
         *
882
         * @return String.
883
         */
884
        public String getLayerQuery() {
885
                return layerQuery;
886
        }
887

    
888
        /**
889
         * Inserta la consulta.
890
         *
891
         * @param layerQuery consulta.
892
         */
893
        public void setLayerQuery(String layerQuery) {
894
                this.layerQuery = layerQuery;
895
        }
896

    
897
        /**
898
         * Devuelve el formato.
899
         *
900
         * @return Formato.
901
         */
902
        public String getFormat() {
903
                return m_Format;
904
        }
905

    
906
        /**
907
         * Inserta el formato.
908
         *
909
         * @param format Formato.
910
         */
911
        public void setFormat(String format) {
912
                m_Format = format;
913
        }
914

    
915
        /**
916
         * Devuelve el SRS.
917
         *
918
         * @return SRS.
919
         */
920
        public String getSRS() {
921
                return m_SRS;
922
        }
923

    
924
        /**
925
         * Inserta el SRS.
926
         *
927
         * @param m_srs SRS.
928
         */
929
        public void setSRS(String m_srs) {
930
                m_SRS = m_srs;
931
        }
932

    
933
        /**
934
         * Inserta la extensi?n total de la capa.
935
         *
936
         * @param fullExtent Rect?ngulo.
937
         */
938
        public void setFullExtent(Rectangle2D fullExtent) {
939
                this.fullExtent = fullExtent;
940
        }
941
        
942
        public HashMap getProperties() {
943
                HashMap info = new HashMap();
944
        String[] layerNames = getLayerQuery().split(",");
945
        Vector layers = new Vector(layerNames.length);
946
        try {
947
            if(getDriver().connect()){
948
                for (int i = 0; i < layerNames.length; i++) {
949
                    layers.add(i, wms.getLayer(layerNames[i]));
950
                }
951
                info.put("name", getName());
952
                info.put("selectedLayers", layers);
953
                info.put("host", getHost());
954
                info.put("srs", getSRS());
955
                info.put("format", getFormat());
956
                info.put("wmsTransparency", new Boolean(wmsTransparency));
957
                info.put("styles", styles);
958
                info.put("dimensions", dimensions);
959
                info.put("fixedSize", fixedSize);
960
                return info;
961
            }
962
        } catch (Exception e) {
963
            e.printStackTrace();
964
        }
965
        return null;
966
        }
967
        
968
        /**
969
         * Asignar el estado del raster
970
         * @param status
971
         */
972
        public void setStatus(StatusRasterInterface status){
973
                this.status = status;
974
        }
975
        
976
        /**
977
         * Obtiene el estado del raster
978
         * @return
979
         */
980
        public StatusRasterInterface getStatus(){
981
                return this.status;
982
        }
983
        
984
        /* (non-Javadoc)
985
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getAttributes()
986
         */
987
        public ArrayList getAttributes() {
988
                if(rasterFile != null){
989
                        ArrayList attr = new ArrayList();
990
                        String dataType = "Byte";
991
                        if (rasterFile.getDataType() == DataBuffer.TYPE_BYTE) dataType = "Byte";
992
                        else if (visualStatus.dataType == DataBuffer.TYPE_SHORT)
993
                                dataType = "Short";
994
                        else if (visualStatus.dataType == DataBuffer.TYPE_USHORT)
995
                                dataType = "Unsigned Short";
996
                        else if (visualStatus.dataType == DataBuffer.TYPE_INT)
997
                                dataType = "Integer";
998
                        else if (visualStatus.dataType == DataBuffer.TYPE_FLOAT)
999
                                dataType = "Float";
1000
                        else if (visualStatus.dataType == DataBuffer.TYPE_DOUBLE)
1001
                                dataType = "Double";
1002
                        else
1003
                                dataType = "Unknown";
1004

    
1005
                        Object [][] a = {
1006
                                {"Filename",rasterFile.getName().substring(rasterFile.getName().lastIndexOf("/")+1, rasterFile.getName().length())},
1007
                                {"Filesize",new Long(0)},
1008
                                {"Width",new Integer((int)this.getWidth())},
1009
                                {"Height", new Integer((int)this.getHeight())},
1010
                                {"Bands", new Integer(visualStatus.bandCount)},
1011
                                {"BandDataType", dataType}
1012
                        };
1013
                        for (int i=0; i<a.length; i++)
1014
                                attr.add(a[i]);
1015

    
1016
                        return attr;
1017
                }
1018
                return  null;
1019
        }
1020
        
1021
        /* (non-Javadoc)
1022
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getFilterStack()
1023
         */
1024
        public RasterFilterStack getFilterStack() {
1025
                if(raster!=null)
1026
                        return raster.filterStack;
1027
                return null;
1028
        }
1029
        /* (non-Javadoc)
1030
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getHeight()
1031
         */
1032
        public double getHeight() {
1033
                return visualStatus.height;
1034
        }
1035
        
1036
        /* (non-Javadoc)
1037
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxX()
1038
         */
1039
        public double getMaxX() {
1040
                return visualStatus.maxX;
1041
        }
1042
        
1043
        /* (non-Javadoc)
1044
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxY()
1045
         */
1046
        public double getMaxY() {
1047
                return visualStatus.maxY;
1048
        }
1049
        
1050
        /* (non-Javadoc)
1051
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinX()
1052
         */
1053
        public double getMinX() {
1054
                return visualStatus.minX;
1055
        }
1056
        
1057
        /* (non-Javadoc)
1058
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinY()
1059
         */
1060
        public double getMinY() {
1061
                return visualStatus.minY;
1062
        }
1063
        /* (non-Javadoc)
1064
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getPixel(double, double)
1065
         */
1066
        public int[] getPixel(double wcx, double wcy) {
1067
                if(getPxRaster() != null)
1068
                        return getPxRaster().getPixel(wcx, wcy);
1069
        return null;
1070
        }
1071
        /* (non-Javadoc)
1072
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getSource()
1073
         */
1074
        public RasterAdapter getSource() {
1075
                return null;
1076
        }
1077
        /* (non-Javadoc)
1078
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getWidth()
1079
         */
1080
        public double getWidth() {
1081
                return visualStatus.width;
1082
        }
1083
        /* (non-Javadoc)
1084
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setBand(int, int)
1085
         */
1086
        public void setBand(int flag, int nBand) {
1087
                switch(flag){
1088
                case GeoRasterFile.RED_BAND:setBandR(nBand);break;
1089
                case GeoRasterFile.GREEN_BAND:setBandG(nBand);break;
1090
                case GeoRasterFile.BLUE_BAND:setBandB(nBand);break;
1091
                }
1092
        }
1093
        /* (non-Javadoc)
1094
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setFilterStack(org.cresques.io.raster.RasterFilterStack)
1095
         */
1096
        public void setFilterStack(RasterFilterStack stack) {
1097
                this.filterStack = stack;
1098
        }
1099
        /* (non-Javadoc)
1100
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPos(int, int)
1101
         */
1102
        public void setPos(int x, int y) {
1103
                this.posX = x;
1104
                this.posY = y;
1105
        }
1106
        
1107
        /* (non-Javadoc)
1108
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPosWC(double, double)
1109
         */
1110
        public void setPosWC(double x, double y) {
1111
                this.posXWC = x;
1112
                this.posYWC = y;
1113
        }
1114
        
1115
        /* (non-Javadoc)
1116
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setRGB(int, int, int)
1117
         */
1118
        public void setRGB(int r, int g, int b) {
1119
                this.r = r;
1120
                this.g = g;
1121
                this.b = b;
1122
        }
1123
        /* (non-Javadoc)
1124
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setSource(com.iver.cit.gvsig.fmap.layers.RasterAdapter)
1125
         */
1126
        public void setSource(RasterAdapter ra) {
1127
        }
1128
        /**
1129
         * @return Returns the raster.
1130
         */
1131
        public PxRaster getPxRaster() {
1132
                return raster;
1133
        }
1134
        /**
1135
         * @return Returns the rasterFile.
1136
         */
1137
        public GeoRasterFile getGeoRasterFile() {
1138
                return rasterFile;
1139
        }
1140
        
1141
        public void setTransparency(int trans) {
1142
                this.transparency = trans;
1143
        }
1144
        
1145
        /**
1146
         * Sets the R-band.
1147
         * 
1148
         * Asigna la banda R.
1149
         * @param r
1150
         */
1151
        public void setBandR(int r){
1152
                this.rband = r;
1153
        }
1154
        
1155
        /**
1156
         * Sets the G-band.
1157
         * 
1158
         * Asigna la banda G
1159
         * @param g
1160
         */
1161
        public void setBandG(int g){
1162
                this.gband = g;
1163
        }
1164
        
1165
        /**
1166
         * Sets the B-band.
1167
         * 
1168
         * Asigna la banda B
1169
         * @param b
1170
         */
1171
        public void setBandB(int b){
1172
                this.bband = b;
1173
        }
1174

    
1175
    /**
1176
     * @return Returns the wmsTransparency.
1177
     */
1178
    public boolean isWmsTransparent() {
1179
        return wmsTransparency;
1180
    }
1181

    
1182
    /**
1183
     * @param wmsTransparency The wmsTransparency to set.
1184
     */
1185
    public void setWmsTransparency(boolean wmsTransparency) {
1186
        this.wmsTransparency = wmsTransparency;
1187
    }
1188

    
1189
     /**
1190
     * @param styles
1191
     */
1192
    public void setStyles(Vector styles) {
1193
            //laura:
1194
            //layer query is built with the layer in reverse order
1195
            // so here we build the styles upside-down.
1196
            if (styles != null)
1197
            {
1198
                    this.styles = new Vector();
1199
                    for(int i = styles.size()-1; i>=0; i--)
1200
                    {
1201
                            this.styles.add(styles.elementAt(i));
1202
                    }
1203
            }
1204
    }
1205
    
1206
    /**
1207
     * Sets the dimension vector that is a list of key-value pairs containing
1208
     * the name of the dimension and the value for it
1209
     * @param dimensions
1210
     */
1211
    public void setDimensions(Vector dimensions) {
1212
        this.dimensions = dimensions;
1213
    }
1214

    
1215
    /**
1216
     * Sets the set of URLs that should be accessed for each operation performed
1217
     * to the server.
1218
     * 
1219
     * @param onlineResources
1220
     */
1221
        public void setOnlineResources(Hashtable onlineResources) {
1222
                this.onlineResources = onlineResources;
1223
        }
1224
        
1225
        /**
1226
         * When a server is not fully featured and it only can serve constant map
1227
         * sizes this value must be set. It expresses the size in pixels (width, height)
1228
         * that the map will be requested.
1229
         * @param Dimension sz
1230
         */
1231
        public void setFixedSize(Dimension sz) {
1232
                fixedSize = sz;
1233
        }
1234
        
1235
        /**
1236
         * Tells whether if this layer must deal with the server with the constant-size
1237
         * limitations or not.
1238
         * @return boolean.
1239
         */
1240
        private boolean isSizeFixed() {
1241
                return fixedSize != null && fixedSize.getWidth() > 0 && fixedSize.getHeight() > 0;
1242
        }
1243

    
1244
        /**
1245
         * If it is true, this layer accepts GetFeatureInfo operations. This WMS operations
1246
         * maps to FMap's infoByPoint(p) operation.
1247
         * @param b
1248
         */
1249
        public void setQueryable(boolean b) {
1250
                queryable = b;
1251
        }
1252

    
1253
        /**
1254
         * Creates the part of a OGC's MapContext document that would describe this
1255
         * layer(s).
1256
         * @param version, The desired version of the resulting document. (1.1.0)
1257
         * @return String containing the xml.
1258
         * @throws UnsupportedVersionException 
1259
         */
1260
        public String toMapContext(String mapContextVersion) {
1261
                XmlBuilder xml = new XmlBuilder();
1262
                FMapWMSDriver drv;
1263
                try {
1264
                        drv = getDriver();
1265
                } catch (Exception e) {
1266
                        return xml.toString();
1267
                } 
1268
                String[] layerNames = getLayerQuery().split(",");
1269
                for (int i = 0; i < layerNames.length; i++) {
1270
                        WMSLayerNode layer = drv.getLayer(layerNames[i]);
1271
                        HashMap xmlAttrs = new HashMap();
1272
                        
1273
                        // <Layer>
1274
                        xmlAttrs.put(WebMapContextTags.HIDDEN, !isVisible()+"");
1275
                        xmlAttrs.put(WebMapContextTags.QUERYABLE, queryable+"");
1276
                        xml.openTag(WebMapContextTags.LAYER);
1277
                        if (mapContextVersion.equals("1.1.0") || mapContextVersion.equals("1.0.0")) {
1278
                                // <Server>
1279
                                xmlAttrs.put(WebMapContextTags.SERVICE, WebMapContextTags.WMS);
1280
                                xmlAttrs.put(WebMapContextTags.VERSION, drv.getVersion());
1281
                                xmlAttrs.put(WebMapContextTags.SERVER_TITLE, drv.getServiceTitle());
1282
                                xml.openTag(WebMapContextTags.SERVER, xmlAttrs);
1283
                                xmlAttrs.clear();
1284
                                
1285
                                        // <OnlineResource>
1286
                                        xmlAttrs.put(WebMapContextTags.XLINK_TYPE, "simple");
1287
                                        xmlAttrs.put(WebMapContextTags.XLINK_HREF, getHost().toString());
1288
                                        xml.writeTag(WebMapContextTags.ONLINE_RESOURCE, xmlAttrs);
1289
                                        xmlAttrs.clear();
1290
                                        // </OnlineResource>
1291
                                        
1292
                                xml.closeTag();
1293
                                // </Server>
1294
                                
1295
                                // <Name>
1296
                                xml.writeTag(WebMapContextTags.NAME, layer.getName().trim());
1297
                                // </Name>
1298
                                
1299
                                // <Title>
1300
                                xml.writeTag(WebMapContextTags.TITLE, getName().trim());
1301
                                // </Title>
1302
                                
1303
                                // <Abstract>
1304
                                if (layer.getAbstract() != null)
1305
                                        xml.writeTag(WebMapContextTags.ABSTRACT, layer.getAbstract());
1306
                                // </Abstract>
1307
                                
1308
                                // <SRS> (a list of available SRS for the enclosing layer)
1309
                                String[] strings = (String[]) layer.getAllSrs().toArray(new String[0]);
1310
                                String mySRS = strings[0];
1311
                                for (int j = 1; j < strings.length; j++) {
1312
                                        mySRS += ","+strings[j];
1313
                                }
1314
                                xml.writeTag(WebMapContextTags.SRS, mySRS);
1315
                                // </SRS>
1316
                                
1317
                                // <FormatList>
1318
                                xml.openTag(WebMapContextTags.FORMAT_LIST);
1319
                                        strings = (String[]) drv.getFormats().toArray(new String[0]);
1320
                                        for (int j = 0; j < strings.length; j++) {
1321
                    // <Format>
1322
                                                String str = strings[j].trim();
1323
                                                if (str.equals(getFormat()))
1324
                                                        xml.writeTag(WebMapContextTags.FORMAT, str, WebMapContextTags.CURRENT, "1");
1325
                                                else
1326
                                                        xml.writeTag(WebMapContextTags.FORMAT, str);
1327
                    // </Format>        
1328
                                        }
1329
                                xml.closeTag();
1330
                                // </FormatList>
1331
                                
1332
                                // <StyleList>
1333
                                if (layer.getStyles().size()>0) {
1334
                                        xml.openTag(WebMapContextTags.STYLE_LIST);
1335
                                        for (int j = 0; j < layer.getStyles().size(); j++) {
1336
                                                // <Style>
1337
                                                FMapWMSStyle st = (FMapWMSStyle) layer.getStyles().get(i);
1338
                                                if (st.equals(layer.getSelectedStyle()))
1339
                                                        xmlAttrs.put(WebMapContextTags.CURRENT, "1");
1340
                                                xml.openTag(WebMapContextTags.STYLE, xmlAttrs);
1341
                                                
1342
                                                        // <Name>
1343
                                                        xml.writeTag(WebMapContextTags.NAME, st.name);
1344
                                                        // </Name>
1345
                                                        
1346
                                                        // <Title>
1347
                                                        xml.writeTag(WebMapContextTags.TITLE, st.title);
1348
                                                        // </Title>
1349
                                                        
1350
                                                        // <LegendURL width="180" format="image/gif" height="50">
1351
                                                                // <OnlineResource xlink:type="simple" xlink:href="http://globe.digitalearth.gov/globe/en/icons/colorbars/NATIONAL.gif"/>
1352
                                                                // </OnlineResource>
1353
                                                        // </LegendURL>
1354
                                                xml.closeTag();
1355
                                                // </Style>
1356
                                                
1357
                                        }
1358
                                        xml.closeTag();
1359
                                }
1360
                                // </StyleList>
1361
                                if (mapContextVersion.compareTo("1.0.0") > 0) {
1362
                                // <DimensionList>
1363
                                        xml.openTag(WebMapContextTags.DIMENSION_LIST);
1364
                                        // <Dimension>
1365
                                        // </Dimension>
1366
                                        xml.closeTag();
1367
                                // </DimensionList>
1368
                                }
1369
                        } else {
1370
                                xml.writeTag("ERROR", PluginServices.getText(this, "unsupported_map_context_version"));
1371
                        }
1372
                        // </Layer>
1373
                        xml.closeTag();
1374
                }
1375
                return xml.getXML();
1376
        }
1377
}