Statistics
| Revision:

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

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

    
68
import javax.imageio.ImageIO;
69
import javax.swing.ImageIcon;
70
import javax.swing.JOptionPane;
71

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

    
84
import com.iver.andami.PluginServices;
85
import com.iver.andami.messages.NotificationManager;
86
import com.iver.cit.gvsig.fmap.ConnectionErrorExceptionType;
87
import com.iver.cit.gvsig.fmap.DriverException;
88
import com.iver.cit.gvsig.fmap.UnknownResponseFormatExceptionType;
89
import com.iver.cit.gvsig.fmap.UnsuportedProtocolVersionExceptionType;
90
import com.iver.cit.gvsig.fmap.ViewPort;
91
import com.iver.cit.gvsig.fmap.WMSDriverExceptionType;
92
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
93
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException;
94
import com.iver.cit.gvsig.fmap.drivers.wms.FMapWMSDriver;
95
import com.iver.cit.gvsig.fmap.drivers.wms.FMapWMSDriverFactory;
96
import com.iver.cit.gvsig.fmap.drivers.wms.WMSException;
97
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode.FMapWMSStyle;
98
import com.iver.cit.gvsig.fmap.layers.layerOperations.IHasImageLegend;
99
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
100
import com.iver.cit.gvsig.fmap.layers.layerOperations.StringXMLItem;
101
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
102
import com.iver.cit.gvsig.fmap.rendering.XmlBuilder;
103
import com.iver.cit.gvsig.wmc.WebMapContextTags;
104
import com.iver.utiles.StringUtilities;
105
import com.iver.utiles.XMLEntity;
106
import com.iver.utiles.swing.threads.Cancellable;
107

    
108

    
109

    
110
/**
111
* FMap's WMS Layer class.
112
*
113
* @author Jaume Dominguez Faus
114
*                   Nacho Brodin
115
*
116
*/
117
public class FLyrWMS extends FLyrDefault implements InfoByPoint, RasterOperations, IHasImageLegend {
118
        private boolean                                         isPrinting = false;
119
        private boolean                                         mustTileDraw = true;
120
        private boolean                                         mustTilePrint = true;
121
        private final int                                         maxTileDrawWidth = 1023;
122
        private final int                                         maxTileDrawHeight = 1023;
123
        private final int                                         maxTilePrintWidth = 1023;
124
        private final int                                         maxTilePrintHeight = 1023;
125
        private final int                                        minTilePrintWidth = 12;
126
        private final int                                        minTilePrintHeight = 12;
127

    
128
    public URL                                                         host;
129
    public String                                                 m_Format;
130

    
131
        private String                                                 m_SRS;
132
        private String                                                 layerQuery;
133
        private String                                                 infoLayerQuery;
134
        private FMapWMSDriver                                 wms;
135
        private WMSStatus                                         wmsStatus = new WMSStatus();
136
        private Rectangle2D                                 fullExtent;
137
        private boolean                                                wmsTransparency;
138
    private Vector                                                 styles;
139
    private Vector                                                 dimensions;
140
        private StatusRasterInterface                status = null;
141
        private int                                                 posX = 0, posY = 0;
142
        private double                                                 posXWC = 0, posYWC = 0;
143
        private int                                                 r = 0, g = 0, b = 0;
144
        private GeoRasterFile                                 rasterFile = null;
145
        private PxRaster                                         raster = null;
146
        private RasterFilterStack                         filterStack = null;
147
        private boolean                                                firstLoad = false;
148
        private int                                                 transparency = -1;
149
        private int                                                 rband = 0, gband = 1, bband = 2;
150
        private RasterFilterStackManager        stackManager = null;
151
        private Hashtable                                         onlineResources = new Hashtable();
152
        private Dimension                                         fixedSize;
153
        private boolean                                         queryable = true;
154
        private VisualStatusWMS                                visualStatus = new VisualStatusWMS();
155

    
156

    
157
        private class MyCancellable implements ICancellable
158
        {
159
                private Cancellable original;
160
                public MyCancellable(Cancellable cancelOriginal)
161
                {
162
                        this.original = cancelOriginal;
163
                }
164
                public boolean isCanceled() {
165
                        if (original == null) return false;
166
                        return original.isCanceled();
167
                }
168

    
169
        }
170

    
171
        public FLyrWMS(){
172
                super();
173
        }
174

    
175
        public FLyrWMS(Map args) throws DriverIOException{
176
                FMapWMSDriver drv = null;
177
                String host = (String)args.get("host");
178
                String sLayer = (String)args.get("layer");
179
                Rectangle2D fullExtent = (Rectangle2D)args.get("FullExtent");
180
                String sSRS = (String)args.get("SRS");
181
                String sFormat = (String)args.get("Format");
182
                String[] sLayers = sLayer.split(",");
183

    
184
                try {
185
                        this.setHost(new URL(host));
186
                } catch (MalformedURLException e) {
187
                        //e.printStackTrace();
188
                        throw new DriverIOException("Malformed host URL, '" + host + "' (" + e.toString() + ").");
189
                }
190
                try {
191
                        drv = this.getDriver();
192
                } catch (Exception e) {
193
                        // e.printStackTrace();
194
                        throw new DriverIOException("Can't get driver to host '" + host + "' (" + e.toString() + ").");
195
                }
196
                if( sFormat == null || sSRS == null || fullExtent == null ) {
197
                        if (!drv.connect(null))
198
                                throw new DriverIOException("Can't connect to host '" + host + "'.");
199

    
200
                        WMSLayerNode wmsNode = drv.getLayer(sLayer);
201

    
202
                        if (wmsNode == null){
203
                                throw new DriverIOException("The server '" + host + "' doesn't has the layer '" + sLayer + "'.");
204
                        }
205
                        if( sFormat == null ) {
206
                                sFormat = this.getGreatFormat(drv.getFormats());
207
                        }
208
                     if( sSRS == null ) {
209
                             sSRS = (String)wmsNode.getAllSrs().get(0);
210
                     }
211
                        if( fullExtent == null ) {
212
                                fullExtent = drv.getLayersExtent(sLayers,(String)wmsNode.getAllSrs().get(0));
213
                        }
214
                }
215

    
216

    
217
                this.setFullExtent(fullExtent);
218
                this.setFormat(sFormat);
219
                this.setLayerQuery(sLayer);
220
                this.setInfoLayerQuery("");
221
                this.setSRS(sSRS);
222
                this.setName(sLayer);
223
        }
224

    
225
        /**
226
         * It choose the best format to load different maps if the server
227
         * supports it. This format could be png, because it supports
228
         * transparency.
229
         * @param formats
230
         * Arraywith all the formats supported by the server
231
         * @return
232
         */
233
        private String getGreatFormat(Vector formats){
234
            for (int i=0 ; i<formats.size() ; i++){
235
                String format = (String) formats.get(i);
236
                    if (format.equals("image/jpg")){
237
                    return format;
238
                    }
239
                    if (format.equals("image/jpeg")){
240
                    return format;
241
                    }
242
            }
243

    
244
            return (String)formats.get(0);
245
        }
246

    
247
        /**
248
         * Clase que contiene los datos de visualizaci?n de WMS.
249
         * @author Nacho Brodin (brodin_ign@gva.es)
250
         */
251
        private class VisualStatusWMS{
252
                /**
253
                 * Ancho y alto de la imagen o del conjunto de tiles si los tiene. Coincide con
254
                 * el ancho y alto del viewPort
255
                 */
256
                private        int                                                        width = 0, height = 0;
257
                private double                                                minX = 0D, minY = 0D, maxX = 0D, maxY = 0D;
258
                private int                                                 bandCount = 0;
259
                private int                                                        dataType = DataBuffer.TYPE_UNDEFINED;
260
        }
261

    
262

    
263
        /**
264
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir la
265
         * capa.
266
         *
267
         * @return XMLEntity.
268
         * @throws XMLException
269
         */
270
        public XMLEntity getXMLEntity() throws XMLException {
271
                XMLEntity xml = super.getXMLEntity();
272

    
273
                // Full extent
274
                xml.putProperty("fullExtent", StringUtilities.rect2String(fullExtent));
275

    
276
                // Host
277
                xml.putProperty("host", host.toExternalForm());
278

    
279
                // Part of the query that is not the host, or the
280
                // layer names, or other not listed bellow
281
                xml.putProperty("infoLayerQuery", infoLayerQuery);
282

    
283
                // Part of the query containing the layer names
284
                xml.putProperty("layerQuery", layerQuery);
285

    
286
                // Format
287
                xml.putProperty("format", m_Format);
288

    
289
                // SRS
290
                xml.putProperty("srs", m_SRS);
291
                if (status!=null)
292
                        status.getXMLEntity(xml, true, this);
293
                else{
294
                        status = new StatusLayerRaster();
295
                        status.getXMLEntity(xml, true, this);
296
                }
297

    
298
        // Transparency
299
        xml.putProperty("wms_transparency", wmsTransparency);
300

    
301
        // Styles
302
        if (styles!=null){
303
            String stylePr = "";
304
            for (int i = 0; i < styles.size(); i++) {
305
                stylePr += (String) styles.get(i);
306
                if (i<styles.size()-1)
307
                    stylePr += ",";
308
            }
309
            if (stylePr.endsWith(","))
310
                    stylePr += " ";
311
            xml.putProperty("styles", stylePr);
312
        }
313

    
314
        // Dimensions
315
        if (dimensions!=null){
316
            String dim = "";
317
            for (int i = 0; i < dimensions.size(); i++) {
318
                dim += (String) dimensions.get(i);
319
                if (i<dimensions.size()-1)
320
                    dim += ",";
321
            }
322
            if (dim.endsWith(","))
323
                    dim += " ";
324
            xml.putProperty("dimensions", dim);
325
        }
326

    
327
        // OnlineResources
328
        Iterator it = onlineResources.keySet().iterator();
329
        String strOnlines = "";
330
        while (it.hasNext()) {
331
                String key = (String) it.next();
332
                String value = (String) onlineResources.get(key);
333
                strOnlines = key+"~##SEP2##~"+value;
334
                if (it.hasNext())
335
                        strOnlines += "~##SEP1##~";
336
        }
337
        xml.putProperty("onlineResources", strOnlines);
338

    
339
        // Queryable
340
        xml.putProperty("queryable", queryable);
341

    
342
        // fixedSize
343
        if (isSizeFixed()) {
344
                xml.putProperty("fixedSize", true);
345
                xml.putProperty("fixedWidth", fixedSize.width);
346
                xml.putProperty("fixedHeight", fixedSize.height);
347
        }
348
        return xml;
349
        }
350

    
351
        /**
352
         * A partir del XMLEntity reproduce la capa.
353
         *
354
         * @param xml XMLEntity
355
         *
356
         * @throws XMLException
357
         * @throws DriverException
358
         * @throws DriverIOException
359
         */
360
        public void setXMLEntity03(XMLEntity xml)
361
                throws XMLException {
362
                super.setXMLEntity(xml);
363
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
364
                                        "fullExtent"));
365

    
366
                try {
367
                        host = new URL(xml.getStringProperty("host"));
368
                } catch (MalformedURLException e) {
369
                        throw new XMLException(e);
370
                }
371

    
372
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
373
                layerQuery = xml.getStringProperty("layerQuery");
374
                m_Format = xml.getStringProperty("format");
375
                m_SRS = xml.getStringProperty("srs");
376
        }
377

    
378
        /**
379
         * A partir del XMLEntity reproduce la capa.
380
         *
381
         * @param xml XMLEntity
382
         *
383
         * @throws XMLException
384
         * @throws DriverException
385
         * @throws DriverIOException
386
         */
387
        public void setXMLEntity(XMLEntity xml)
388
                throws XMLException {
389
                super.setXMLEntity(xml);
390
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
391
                                        "fullExtent"));
392

    
393
                // Host
394
                try {
395
                        host = new URL(xml.getStringProperty("host"));
396
                } catch (MalformedURLException e) {
397
                        throw new XMLException(e);
398
                }
399

    
400
                // Part of the query that is not the host, or the
401
                // layer names, or other not listed bellow
402
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
403

    
404
                // Part of the query containing the layer names
405
                layerQuery = xml.getStringProperty("layerQuery");
406

    
407
                // Format
408
                m_Format = xml.getStringProperty("format");
409

    
410
                // SRS
411
                m_SRS = xml.getStringProperty("srs");
412

    
413
                String claseStr = StatusLayerRaster.defaultClass;
414
                if (xml.contains("raster.class")) {
415
                        claseStr = xml.getStringProperty("raster.class");
416
                }
417

    
418
                // Transparency
419
        if (xml.contains("wms_transparency"))
420
            wmsTransparency = xml.getBooleanProperty("wms_transparency");
421

    
422
        // Styles
423
        if (xml.contains("styles")){
424
            styles = new Vector();
425
            String[] stl = xml.getStringProperty("styles").split(",");
426

    
427
            for (int i = 0; i < stl.length; i++) {
428
                    if (stl[i].equals(" "))
429
                            stl[i]="";
430
                styles.add(stl[i]);
431
            }
432
        }
433

    
434
        // Dimensions
435
        if (xml.contains("dimensions")){
436
            dimensions = new Vector();
437
            String[] dims = xml.getStringProperty("dimensions").split(",");
438
            for (int i = 0; i < dims.length; i++){
439
                    if (dims[i].equals(" "))
440
                            dims[i]="";
441

    
442
                dimensions.add(dims[i]);
443
            }
444
        }
445

    
446
        // OnlineResources
447
        if (xml.contains("onlineResources")) {
448
                String[] operations = xml.getStringProperty("onlineResources").split("~##SEP1##~");
449
                for (int i = 0; i < operations.length; i++) {
450
                                String[] resources = operations[i].split("~##SEP2##~");
451
                                if (resources.length==2 && resources[1]!="")
452
                                        onlineResources.put(resources[0], resources[1]);
453
                        }
454
        }
455

    
456
        // Queryable
457
        queryable = true; // let's assume that the layer is queryable by default
458
        if (xml.contains("queryable"))
459
                queryable = xml.getBooleanProperty("queryable");
460

    
461
        // fixedSize
462
        if (xml.contains("fixedSize")) {
463
                fixedSize = new Dimension(xml.getIntProperty("fixedWidth"),
464
                                                  xml.getIntProperty("fixedHeight"));
465
        }
466

    
467
                if(status!=null)
468
                        status.setXMLEntity(xml, this);
469
                else{
470
                        if(claseStr!=null && !claseStr.equals("")){
471
                                try{
472
                                        Class clase = Class.forName(claseStr);
473
                                        Constructor constr = clase.getConstructor(null);
474
                                        status = (StatusRasterInterface)constr.newInstance(null);
475
                                        if(status!=null)
476
                                                status.setXMLEntity(xml, this);
477
                                }catch(ClassNotFoundException exc){
478
                                        exc.printStackTrace();
479
                                }catch(InstantiationException exc){
480
                                        exc.printStackTrace();
481
                                }catch(IllegalAccessException exc){
482
                                        exc.printStackTrace();
483
                                }catch(NoSuchMethodException exc){
484
                                        exc.printStackTrace();
485
                                }catch(InvocationTargetException exc){
486
                                        exc.printStackTrace();
487
                                }
488
                        }
489
                }
490
                firstLoad = true;
491
        }
492

    
493
        /**
494
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(com.iver.cit.gvsig.fmap.operations.QueriedPoint)
495
         */
496
        public XMLItem[] getInfo(Point p, double tolerance, Cancellable cancellable) throws DriverException {
497
                XMLItem[] item =  new XMLItem[1];
498
                try {
499
                        if (queryable)
500
                        {
501
                                //TODO
502
                                // check if there are layers which are not queryable
503
                                ViewPort viewPort = getMapContext().getViewPort();
504

    
505
                                Point tiledPoint = new Point((int) p.getX() % maxTilePrintWidth, (int) p.getY() % maxTilePrintHeight);
506
                                Rectangle rect = new Rectangle(0, 0, viewPort.getImageWidth() - 1, viewPort.getImageHeight() - 1);
507
                                Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, rect);
508
                                tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
509
                                int nCols = tiles.getNumCols();
510

    
511
                                int col = (int) p.getX() / maxTilePrintWidth;
512
                                int row = (int) p.getY() / maxTilePrintHeight;
513
                                int tileIndex = (row*nCols) + col;
514

    
515
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileIndex);
516
                                wmsStatus.setExtent(vp.getAdjustedExtent());
517
                                wmsStatus.setHeight(vp.getImageHeight());
518
                                wmsStatus.setWidth(vp.getImageWidth());
519
                                wmsStatus.setOnlineResource((String) onlineResources.get("GetFeatureInfo"));
520
                                MyCancellable c = new MyCancellable(cancellable);
521
                                item[0] = new StringXMLItem(new String(getDriver()
522
                                                .getFeatureInfo(wmsStatus, (int) tiledPoint.getX(), (int) tiledPoint.getY(), Integer.MAX_VALUE, c)),this);
523
                                return item;
524
                        }
525
                        else
526
                        {
527
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),this.getName() + " " +
528
                                                PluginServices.getText(this,"layer_not_queryable"));
529
                                item[0] =  new StringXMLItem("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><info></info>",this);
530
                                return item;
531
                                //return null;
532
                        }
533
                } catch (WMSException  e) {
534
                        item[0] = new StringXMLItem("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><exception>" +
535
                        e.getMessage() + "</exception>", this);
536
                        return item;
537
                }
538
                /*azabala
539
                catch (ValidationException e) {
540
                        throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
541
                } catch (UnsupportedVersionException e) {
542
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
543
                } catch (IOException e) {
544
                        throw new DriverException(PluginServices.getText(this, "connect_error"), e);
545
                }
546
                */
547
                catch (ValidationException e) {
548
                        UnknownResponseFormatExceptionType type =
549
                                new UnknownResponseFormatExceptionType();
550
                        type.setLayerName(getName());
551
                        try {
552
                                type.setDriverName("WMS Driver");
553
                        } catch (Exception e1) {
554
                                e1.printStackTrace();
555
                        }
556
                        type.setFormat(m_Format);
557
                        type.setHost(host);
558
                        type.setProtocol("WMS");
559
                        DriverException exception = new DriverException("unknown_response_format", type);
560
                        throw exception;
561

    
562
                } catch (UnsupportedVersionException e) {
563
                        UnsuportedProtocolVersionExceptionType type =
564
                                new UnsuportedProtocolVersionExceptionType();
565
                        type.setLayerName(getName());
566
                        try {
567
                                type.setDriverName("WMS Driver");
568
                        } catch (Exception ex){
569
                        }
570
                        type.setUrl(host);
571
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e, type);
572

    
573
                } catch (IOException e) {
574
                        ConnectionErrorExceptionType type = new ConnectionErrorExceptionType();
575
                        type.setLayerName(getName());
576
                        try {
577
                                type.setDriverName("WMS Driver");
578
                        } catch (Exception e1) {
579
                        }
580
                        type.setHost(host);
581
                        throw new DriverException(PluginServices.
582
                                        getText(this, "connect_error"), e, type);
583
                }
584
                catch (NoninvertibleTransformException e) {
585
                        NotificationManager.addError("NotinvertibleTransform", e);
586
                }
587
                return null;
588
        }
589

    
590
        /**
591
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
592
         */
593
        public Rectangle2D getFullExtent() {
594
                return fullExtent;
595
        }
596

    
597
        /**
598
         *
599
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
600
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
601
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
602
         */
603
        private int callCount; // mess code, represents the amount of times the methods drawFixedSize or drawTile where tried for an extent
604
        private static final int MAX_RETRY_TIMES = 5; // mess code, represents the max amount of retries allowed.
605
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
606
                        Cancellable cancel,double scale) throws DriverException {
607
                callCount = 0; // mess code
608
                if (isWithinScale(scale)){
609
                        Point2D p = viewPort.getOffset();
610
                        // p will be (0, 0) when drawing a view or other when painting onto
611
                        // the Layout.
612
                        visualStatus.width =  viewPort.getImageWidth();
613
                        visualStatus.height =  viewPort.getImageHeight();
614
                        visualStatus.minX = viewPort.getAdjustedExtent().getMinX();
615
                        visualStatus.minY = viewPort.getAdjustedExtent().getMinY();
616
                        visualStatus.maxX = viewPort.getAdjustedExtent().getMaxX();
617
                        visualStatus.maxY = viewPort.getAdjustedExtent().getMaxY();
618

    
619

    
620
                        if (isSizeFixed()) {
621
                                // This condition handles those situations in which the server can
622
                                // only give static extent and resolution maps despite we need
623
                                // a specific BBOX and pixel WIDTH and HEIGHT
624
                                drawFixedSize(g, viewPort, cancel);
625

    
626
                        } else {
627
                                if(mustTileDraw){
628
                                        Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), viewPort.getImageWidth(), viewPort.getImageHeight());
629
                                        Tiling tiles = new Tiling(maxTileDrawWidth, maxTileDrawHeight, r);
630
                                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
631
                                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
632
                                                // drawing part
633
                                                try {
634
                                                        ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
635
                                                        drawTile(g, vp, cancel);
636
                                                } catch (NoninvertibleTransformException e) {
637
                                                        e.printStackTrace();
638
                                                }
639
                                        }
640
                                }else
641
                                        drawTile(g, viewPort, cancel);
642
                        }
643
                }
644
//                Runtime r = Runtime.getRuntime();
645
//                long mem = r.totalMemory() - r.freeMemory();
646
//                System.err.println("Memoria total: " + (mem / 1024) +"KB");
647
        }
648

    
649
        private void drawFixedSize(Graphics2D g, ViewPort vp, Cancellable cancel) throws DriverException {
650
                callCount++; // mess code, it is not unusual a wms server to response an error which is completely
651
                                         // temporal and the response is available if we retry requesting.
652
                                         //
653

    
654

    
655
                // This is the extent that will be requested
656
                Rectangle2D bBox = getFullExtent();
657
                MyCancellable c = new MyCancellable(cancel);
658

    
659
                try {
660
                        wmsStatus.setExtent( bBox );
661
                        wmsStatus.setFormat( m_Format );
662
                        wmsStatus.setHeight( fixedSize.height );
663
                        wmsStatus.setWidth( fixedSize.width );
664
                        wmsStatus.setLayerNames(Utilities.createVector(layerQuery,","));
665
                        wmsStatus.setSrs(m_SRS);
666
                        wmsStatus.setStyles(styles);
667
                        wmsStatus.setDimensions(dimensions);
668
                        wmsStatus.setTransparency(wmsTransparency);
669
                        wmsStatus.setOnlineResource((String) onlineResources.get("GetMap"));
670
                        File f = getDriver().getMap(wmsStatus, c);
671
                        if (f == null)
672
                                return;
673
                        String nameWorldFile = f.getPath() + getExtensionWorldFile();
674
                        com.iver.andami.Utilities.createTemp(nameWorldFile, this.getDataWorldFile(bBox, fixedSize));
675

    
676
                        if(status!=null && firstLoad){
677
                                status.applyStatus(this);
678
                                firstLoad = false;
679
                        }
680

    
681
                        // And finally, obtain the extent intersecting the view and the BBox
682
                        // to draw to.
683
                        Rectangle2D extent = new Rectangle2D.Double();
684
                        Rectangle2D.intersect(vp.getAdjustedExtent(), bBox, extent);
685

    
686
                        ViewPortData vpData = new ViewPortData(
687
                                vp.getProjection(), new Extent(extent), fixedSize );
688
                        vpData.setMat(vp.getAffineTransform());
689

    
690
                        rasterProcess(g, vpData, f);
691

    
692
                } catch (ValidationException e) {
693
                        if (!c.isCanceled())
694
                        {
695
                                UnknownResponseFormatExceptionType type =
696
                                        new UnknownResponseFormatExceptionType();
697
                                type.setLayerName(getName());
698
                                try {
699
                                        type.setDriverName("WMS Driver");
700
                                } catch (Exception e1) {
701
                                        e1.printStackTrace();
702
                                }
703
                                type.setFormat(m_Format);
704
                                type.setHost(host);
705
                                type.setProtocol("WMS");
706
                                DriverException exception = new DriverException("unknown_response_format", type);
707
                                throw exception;
708
                        }
709
//        azabala                        throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
710
                } catch (UnsupportedVersionException e) {
711
                        if (!c.isCanceled()){
712
                                UnsuportedProtocolVersionExceptionType type =
713
                                        new UnsuportedProtocolVersionExceptionType();
714
                                type.setLayerName(getName());
715
                                try {
716
                                        type.setDriverName("WMS Driver");
717
                                } catch (Exception ex){
718
                                }
719
                                type.setUrl(host);
720
                                throw new DriverException(PluginServices.getText(this, "version_conflict"), e, type);
721
                        }
722
//        azabala                throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
723
                } catch (IOException e) {
724
                        if (!c.isCanceled())
725
                                if (callCount<MAX_RETRY_TIMES) { // mess code
726
                                        NotificationManager.addWarning("\n[ FLyrWMS.drawFixedSize() ]  Failed in trying " + callCount + "/" + MAX_RETRY_TIMES + ")\n", null); // mess code
727
                                        // I'll try again requesting up to MAX_RETRY_TIMES times before throw an error // mess code
728
                                        // (this is mess code, should be replaced by a layer status handler which is scheduled for version > 1.0) // mess code
729
                                        drawFixedSize(g, vp, cancel); // mess code
730
                                } // mess code
731

    
732
                                if (callCount == 1) { // mess code
733
                                        ConnectionErrorExceptionType type = new ConnectionErrorExceptionType();
734
                                        type.setLayerName(getName());
735
                                        try {
736
                                                type.setDriverName("WMS Driver");
737
                                        } catch (Exception e1) {
738
                                        }
739
                                        type.setHost(host);
740
                                        throw new DriverException(PluginServices.
741
                                                        getText(this, "connect_error"), e, type);
742
//        azabala        throw new DriverException(PluginServices.getText(this, "connect_error"), e);
743
                                } // mess code
744

    
745

    
746
                } catch (WMSException e) {
747
                        if (!c.isCanceled()) {
748
                                if (callCount<MAX_RETRY_TIMES) { // mess code
749
                                        NotificationManager.addWarning("\n[ FLyrWMS.drawFixedSize() ]  Failed in trying " + callCount + "/" + MAX_RETRY_TIMES + ")\n", null); // mess code
750
                                        // I'll try again requesting up to MAX_RETRY_TIMES times before throw an error // mess code
751
                                        // (this is mess code, should be replaced by a layer status handler which is scheduled for version > 1.0) // mess code
752
                                        drawFixedSize(g, vp, cancel); // mess code
753
                                } // mess code
754
                                if (callCount == 1) { // mess code
755
//                azabala                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
756
                                        WMSDriverExceptionType type = new WMSDriverExceptionType();
757
                                        type.setLayerName(getName());
758
                                        try {
759
                                                type.setDriverName("WMS Driver");
760
                                        } catch (Exception e1) {
761
                                        }
762
                                        type.setWcsStatus(this.wmsStatus);
763
                                        if (!isPrinting)
764
                                                this.setVisible(false);
765
                                        throw new DriverException("Error WMS", e,  type);
766

    
767

    
768
                                } // mess code
769
                        }
770
                }
771
                callCount--; // mess code
772
        }
773

    
774
        /**
775
         * This is the method used to draw a tile in a WMS mosaic layer.
776
         */
777
        private void drawTile(Graphics2D g, ViewPort vp, Cancellable cancel) throws DriverException {
778
                callCount++;
779
                // Compute the query geometry
780
                // 1. Check if it is within borders
781
                Rectangle2D extent = getFullExtent();
782
        if ((vp.getAdjustedExtent().getMinX() > extent.getMaxX()) ||
783
                (vp.getAdjustedExtent().getMinY() > extent.getMaxY()) ||
784
                (vp.getAdjustedExtent().getMaxX() < extent.getMinX()) ||
785
                (vp.getAdjustedExtent().getMaxY() < extent.getMinY())) {
786
            return;
787
        }
788

    
789
        // 2. Compute extent to be requested.
790
        Rectangle2D bBox = new Rectangle2D.Double();
791
        Rectangle2D.intersect(vp.getAdjustedExtent(), extent, bBox);
792

    
793
        // 3. Compute size in pixels
794
        double scalex = vp.getAffineTransform().getScaleX();
795
        double scaley = vp.getAffineTransform().getScaleY();
796
        int wImg = (int) Math.ceil(Math.abs(bBox.getWidth() * scalex) + 1);
797
        int hImg = (int) Math.ceil(Math.abs(bBox.getHeight() * scaley) + 1);
798

    
799
        Dimension sz = new Dimension(wImg, hImg);
800

    
801
        if ((wImg <= 0) || (hImg <= 0)) {
802
            return;
803
        }
804
        MyCancellable c = new MyCancellable(cancel);
805

    
806
                try {
807
//                        wImg = vp.getImageWidth();
808
//                        hImg = vp.getImageHeight();
809
                        sz = new Dimension(wImg, hImg);
810
                        Rectangle2D.intersect(vp.getAdjustedExtent(), extent, bBox);
811

    
812

    
813
                        wmsStatus.setExtent( bBox );
814
                        wmsStatus.setFormat(m_Format);
815
                        wmsStatus.setHeight( hImg );
816
                        wmsStatus.setWidth( wImg );
817
                        wmsStatus.setLayerNames(Utilities.createVector(layerQuery,","));
818
                        wmsStatus.setSrs(m_SRS);
819
                        wmsStatus.setStyles(styles);
820
                        wmsStatus.setDimensions(dimensions);
821
                        wmsStatus.setTransparency(wmsTransparency);
822
                        wmsStatus.setOnlineResource((String) onlineResources.get("GetMap"));
823

    
824
                        // begin patch; Avoid to request too small tiles.
825
                        // This generally occurs when printing
826

    
827
                        if (wImg < minTilePrintWidth) {
828
                                double wScale = (double) minTilePrintWidth / wImg;
829
                                wmsStatus.setWidth(minTilePrintWidth);
830
                                Rectangle2D sExtent = wmsStatus.getExtent();
831
                                Point2D initialPoint = new Point2D.Double(sExtent.getX(), sExtent.getY());
832
                                sExtent.setRect(sExtent.getX()*wScale, sExtent.getY(), sExtent.getWidth()*wScale, sExtent.getHeight());
833
                                if (!bBox.contains(initialPoint)) {
834
                                        sExtent.setRect(sExtent.getX() - initialPoint.getX(), sExtent.getY(), sExtent.getWidth(), sExtent.getHeight());
835
                                }
836
                        }
837

    
838
                        if (hImg < minTilePrintHeight) {
839
                                double hScale = (double) minTilePrintHeight / hImg;
840
                                wmsStatus.setHeight(minTilePrintHeight);
841
                                Rectangle2D sExtent = wmsStatus.getExtent();
842
                                Point2D initialPoint = new Point2D.Double(sExtent.getX(), sExtent.getY());
843
                                sExtent.setRect(sExtent.getX(), sExtent.getY()*hScale, sExtent.getWidth(), sExtent.getHeight()*hScale);
844
                                if (!bBox.contains(initialPoint)) {
845
                                        sExtent.setRect(sExtent.getX(), sExtent.getY() - initialPoint.getY(), sExtent.getWidth(), sExtent.getHeight());
846
                                }
847
                        }
848

    
849
                        // end patch
850
                        File f = getDriver().getMap(wmsStatus, c);
851
                        if (f == null)
852
                                return;
853
                        String nameWordFile = f.getPath() + getExtensionWorldFile();
854
                        com.iver.andami.Utilities.createTemp(nameWordFile, this.getDataWorldFile(bBox, sz));
855

    
856
                        ViewPortData vpData = new ViewPortData(
857
                                vp.getProjection(), new Extent(bBox), sz );
858
                        vpData.setMat(vp.getAffineTransform());
859

    
860
                        rasterProcess(g, vpData, f);
861

    
862
                } catch (ValidationException e) {
863
                //azabala
864
//                        if (!c.isCanceled())
865
//                                throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
866

    
867
                        if (!c.isCanceled())
868
                        {
869
                                UnknownResponseFormatExceptionType type =
870
                                        new UnknownResponseFormatExceptionType();
871
                                type.setLayerName(getName());
872
                                try {
873
                                        type.setDriverName("WMS Driver");
874
                                } catch (Exception e1) {
875
                                        e1.printStackTrace();
876
                                }
877
                                type.setFormat(m_Format);
878
                                type.setHost(host);
879
                                type.setProtocol("WMS");
880
                                DriverException exception = new DriverException("unknown_response_format", type);
881
                                throw exception;
882
                        }
883
                } catch (UnsupportedVersionException e) {
884

    
885
//                        if (!c.isCanceled())
886
//                                throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
887

    
888
                        if (!c.isCanceled()){
889
                                UnsuportedProtocolVersionExceptionType type =
890
                                        new UnsuportedProtocolVersionExceptionType();
891
                                type.setLayerName(getName());
892
                                try {
893
                                        type.setDriverName("WMS Driver");
894
                                } catch (Exception ex){
895
                                }
896
                                type.setUrl(host);
897
                                throw new DriverException(PluginServices.getText(this, "version_conflict"), e, type);
898
                        }
899

    
900
                } catch (IOException e) {
901
                        /*azabala
902
                        if (!c.isCanceled())
903
                                if (callCount<MAX_RETRY_TIMES) {
904
                                        NotificationManager.addWarning("\n[ FLyrWMS.drawFixedSize() ]  Failed in trying " + callCount + "/" + MAX_RETRY_TIMES + ")\n", null);
905
                                        // I'll try again requesting up to MAX_RETRY_TIMES times before throw an error
906
                                        // (this is mess code, should be replaced by a layer status handler which is scheduled for version > 1.0)
907
                                        drawTile(g, vp, cancel);
908
                                }
909
                                if (callCount == 1) {
910
                                        throw new DriverException(PluginServices.getText(this, "connect_error"), e);
911
                                }
912
                        */
913
                        if (!c.isCanceled()){
914
                                if (callCount<MAX_RETRY_TIMES) { // mess code
915
                                        NotificationManager.addWarning("\n[ FLyrWMS.drawFixedSize() ]  Failed in trying " + callCount + "/" + MAX_RETRY_TIMES + ")\n", null); // mess code
916
                                        // I'll try again requesting up to MAX_RETRY_TIMES times before throw an error // mess code
917
                                        // (this is mess code, should be replaced by a layer status handler which is scheduled for version > 1.0) // mess code
918

    
919
                                        //FIXME Aqu? deberiamos llamar a drawTile o a drawFixedSize????
920
                                        drawFixedSize(g, vp, cancel); // mess code
921
                                } // mess code
922
                        }
923
                        if (callCount == 1) { // mess code
924
                                ConnectionErrorExceptionType type = new ConnectionErrorExceptionType();
925
                                type.setLayerName(getName());
926
                                try {
927
                                        type.setDriverName("WMS Driver");
928
                                } catch (Exception e1) {}
929
                                type.setHost(host);
930
                                throw new DriverException(PluginServices.
931
                                                        getText(this, "connect_error"), e, type);
932
                        }//if
933
                } catch (WMSException e) {
934
                        /*azabala
935
                        if (!c.isCanceled()) {
936
                                if (callCount<MAX_RETRY_TIMES) {
937
                                        NotificationManager.addWarning("\n[ FLyrWMS.drawTile() ] Failed in trying " + callCount + "/" + MAX_RETRY_TIMES +" \n", null);
938
                                        // I'll try again requesting up to MAX_RETRY_TIMES times before throw an error
939
                                        // (this is mess code, should be replaced by a layer status handler which is scheduled for version > 1.0)
940
                                        drawTile(g, vp, cancel);
941
                                }
942
                                if (callCount == 1) {
943
                                        if (!isPrinting) {
944
                                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
945
                                                this.setVisible(false);
946
                                        }
947
                                }
948
                        }
949
                        */
950
                        if (!c.isCanceled()) {
951
                                if (callCount<MAX_RETRY_TIMES) { // mess code
952
                                        NotificationManager.addWarning("\n[ FLyrWMS.drawFixedSize() ]  Failed in trying " + callCount + "/" + MAX_RETRY_TIMES + ")\n", null); // mess code
953
                                        // I'll try again requesting up to MAX_RETRY_TIMES times before throw an error // mess code
954
                                        // (this is mess code, should be replaced by a layer status handler which is scheduled for version > 1.0) // mess code
955
                                        drawTile(g, vp, cancel);
956
                                } // mess code
957
                                if (callCount == 1) { // mess code
958
//                azabala                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
959
                                        WMSDriverExceptionType type = new WMSDriverExceptionType();
960
                                        type.setLayerName(getName());
961
                                        try {
962
                                                type.setDriverName("WMS Driver");
963
                                        } catch (Exception e1) {
964
                                        }
965
                                        type.setWcsStatus(this.wmsStatus);
966
                                        if (!isPrinting)
967
                                                this.setVisible(false);
968
                                        throw new DriverException("Error WMS", e,  type);
969

    
970

    
971
                                } //if
972
                        }//if
973
                }//catch
974
                callCount--;
975
        }
976

    
977
        /**
978
         * Obtiene la extensi?n del fichero de georreferenciaci?n
979
         * @return String con la extensi?n del fichero de georreferenciaci?n dependiendo
980
         * del valor del formato obtenido del servidor. Por defecto asignaremos un .wld
981
         */
982
        private String getExtensionWorldFile(){
983
                String extWorldFile = ".wld";
984
            if(m_Format.equals("image/tif") || m_Format.equals("image/tiff"))
985
                    extWorldFile = ".tfw";
986
            if(m_Format.equals("image/jpeg"))
987
                    extWorldFile = ".jpgw";
988
            return extWorldFile;
989
        }
990

    
991
        /**
992
         * Calcula el contenido del fichero de georreferenciaci?n de una imagen.
993
         * @param bBox Tama?o y posici?n de la imagen (en coordenadas de usuario)
994
         * @param sz Tama?o de la imagen en pixeles.
995
         * @return el 'WorldFile', como String.
996
         * @throws IOException
997
         */
998
        public String getDataWorldFile(Rectangle2D bBox, Dimension sz) throws IOException {
999
                StringBuffer data = new StringBuffer();
1000
            data.append((bBox.getMaxX() - bBox.getMinX())/(sz.getWidth() - 1)+"\n");
1001
            data.append("0.0\n");
1002
            data.append("0.0\n");
1003
            data.append("-"+(bBox.getMaxY() - bBox.getMinY())/(sz.getHeight() - 1)+"\n");
1004
            data.append(""+bBox.getMinX()+"\n");
1005
            data.append(""+bBox.getMaxY()+"\n");
1006
            return data.toString();
1007
        }
1008

    
1009
        /**
1010
         * Dibuja una imagen usando PxRaster
1011
         * @param g        Graphics2D en el que hay que dibujar.
1012
         * @param vpData Par?metros de visualizaci?n
1013
         * @param file La imagen en cuesti?n.
1014
         */
1015
        private void rasterProcess(Graphics2D g, ViewPortData vpData, File file) {
1016

    
1017
                //Creamos el PxRaster
1018
                rasterFile = new GdalFile(vpData.getProjection(), file.getAbsolutePath());
1019
                raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
1020

    
1021
                if(status!=null && firstLoad){
1022
                        status.applyStatus(this);
1023
                        firstLoad = false;
1024
                }
1025

    
1026
                //Recuperamos la pila de filtros si ya hubiese sido cargado antes
1027
                if(this.filterStack!=null)
1028
                        raster.filterStack = this.filterStack;
1029

    
1030
                raster.setTransparency(false);
1031

    
1032
                //Asignamos transparencia y orden de bandas
1033
                if(this.transparency==-1 && !firstLoad);
1034
                else
1035
                        raster.setTransparency(this.transparency);
1036

    
1037
                raster.setBand(GeoRasterFile.RED_BAND,rband);
1038
                raster.setBand(GeoRasterFile.GREEN_BAND, gband);
1039
                raster.setBand(GeoRasterFile.BLUE_BAND, bband);
1040

    
1041
                //Despues del primer pxRaster asignamos el stackManager guardado para los siguientes.
1042
                //Con esto conseguimos asignar los cambios que se hayan producido desde el cuadro de
1043
                //propiedades cuando creamos un nuevo pxRaster
1044
                if(this.stackManager != null)
1045
                        raster.setStackManager(this.stackManager);
1046

    
1047
                if(visualStatus != null){
1048
                        visualStatus.bandCount = raster.getBandCount();
1049
                        visualStatus.dataType = raster.getDataType();
1050
                }
1051

    
1052
                raster.draw(g, vpData);
1053

    
1054
                //En el primer pxRaster de una imagen obtenemos el Stack Manager para poder modificarlo
1055
                //si queremos desde las propiedades
1056

    
1057
                if(this.stackManager == null)
1058
                        this.stackManager = raster.getStackManager();
1059

    
1060
                if(this.filterStack == null)
1061
                        this.filterStack = raster.filterStack;
1062

    
1063
                //rasterFile.close();
1064
        }
1065

    
1066
        /**
1067
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
1068
         *                 com.iver.cit.gvsig.fmap.ViewPort,
1069
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
1070
         */
1071
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
1072
                throws DriverException {
1073
                if (isVisible() && isWithinScale(scale)){
1074
                isPrinting = true;
1075
                if (!mustTilePrint) {
1076
                        draw(null, g, viewPort, cancel,scale);
1077
                } else {
1078
                // Para no pedir imagenes demasiado grandes, vamos
1079
                // a hacer lo mismo que hace EcwFile: chunkear.
1080
                // Llamamos a drawView con cuadraditos m?s peque?os
1081
                // del BufferedImage ni caso, cuando se imprime viene con null
1082

    
1083
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipBounds());
1084
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
1085
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
1086
                            // Parte que dibuja
1087
                            try {
1088
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
1089
                                drawTile(g, vp, cancel);
1090
                                } catch (NoninvertibleTransformException e) {
1091
                                        e.printStackTrace();
1092
                                }
1093
                }
1094
                }
1095
            isPrinting = false;
1096
                }
1097
        }
1098

    
1099
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
1100
                throws DriverException {
1101
                draw(null, g, viewPort, cancel,scale);
1102
        }
1103

    
1104
        /**
1105
         * Devuelve el FMapWMSDriver.
1106
         *
1107
         * @return FMapWMSDriver
1108
         *
1109
         * @throws IllegalStateException
1110
         * @throws ValidationException
1111
         * @throws UnsupportedVersionException
1112
         * @throws IOException
1113
         */
1114
        private FMapWMSDriver getDriver()
1115
                throws IllegalStateException, ValidationException,
1116
                        UnsupportedVersionException, IOException {
1117
                return FMapWMSDriverFactory.getFMapDriverForURL(host);
1118
        }
1119

    
1120
        /**
1121
         * Devuelve el FMapWMSDriver.
1122
         *
1123
         * @return FMapWMSDriver
1124
         *
1125
         * @throws IllegalStateException
1126
         * @throws ValidationException
1127
         * @throws UnsupportedVersionException
1128
         * @throws IOException
1129
         */
1130
        public void setDriver(FMapWMSDriver drv) {
1131
                wms = drv;
1132
        }
1133

    
1134
        /**
1135
         * Devuelve el URL.
1136
         *
1137
         * @return URL.
1138
         */
1139
        public URL getHost() {
1140
                return host;
1141
        }
1142

    
1143
        /**
1144
         * Inserta el URL.
1145
         *
1146
         * @param host URL.
1147
         */
1148
        public void setHost(URL host) {
1149
                this.host = host;
1150
        }
1151

    
1152
        /**
1153
         * Devuelve la informaci?n de la consulta.
1154
         *
1155
         * @return String.
1156
         */
1157
        public String getInfoLayerQuery() {
1158
                return infoLayerQuery;
1159
        }
1160

    
1161
        /**
1162
         * Inserta la informaci?n de la consulta.
1163
         *
1164
         * @param infoLayerQuery String.
1165
         */
1166
        public void setInfoLayerQuery(String infoLayerQuery) {
1167
                this.infoLayerQuery = infoLayerQuery;
1168
        }
1169

    
1170
        /**
1171
         * Devuelve la consulta.
1172
         *
1173
         * @return String.
1174
         */
1175
        public String getLayerQuery() {
1176
                return layerQuery;
1177
        }
1178

    
1179
        /**
1180
         * Inserta la consulta.
1181
         *
1182
         * @param layerQuery consulta.
1183
         */
1184
        public void setLayerQuery(String layerQuery) {
1185
                this.layerQuery = layerQuery;
1186
        }
1187

    
1188
        /**
1189
         * Devuelve el formato.
1190
         *
1191
         * @return Formato.
1192
         */
1193
        public String getFormat() {
1194
                return m_Format;
1195
        }
1196

    
1197
        /**
1198
         * Inserta el formato.
1199
         *
1200
         * @param format Formato.
1201
         */
1202
        public void setFormat(String format) {
1203
                m_Format = format;
1204
        }
1205

    
1206
        /**
1207
         * Devuelve el SRS.
1208
         *
1209
         * @return SRS.
1210
         */
1211
        public String getSRS() {
1212
                return m_SRS;
1213
        }
1214

    
1215
        /**
1216
         * Inserta el SRS.
1217
         *
1218
         * @param m_srs SRS.
1219
         */
1220
        public void setSRS(String m_srs) {
1221
                m_SRS = m_srs;
1222
        }
1223

    
1224
        /**
1225
         * Inserta la extensi?n total de la capa.
1226
         *
1227
         * @param fullExtent Rect?ngulo.
1228
         */
1229
        public void setFullExtent(Rectangle2D fullExtent) {
1230
                this.fullExtent = fullExtent;
1231
        }
1232

    
1233
        public HashMap getProperties() {
1234
                HashMap info = new HashMap();
1235
        String[] layerNames = getLayerQuery().split(",");
1236
        Vector layers = new Vector(layerNames.length);
1237
        try {
1238
            if(getDriver().connect(null)){
1239
                for (int i = 0; i < layerNames.length; i++) {
1240
                    layers.add(i, getDriver().getLayer(layerNames[i]));
1241
                }
1242
                info.put("name", getName());
1243
                info.put("selectedLayers", layers);
1244
                info.put("host", getHost());
1245
                info.put("srs", getSRS());
1246
                info.put("format", getFormat());
1247
                info.put("wmsTransparency", new Boolean(wmsTransparency));
1248
                info.put("styles", styles);
1249
                info.put("dimensions", dimensions);
1250
                info.put("fixedSize", fixedSize);
1251
                return info;
1252
            }
1253
        } catch (Exception e) {
1254
            e.printStackTrace();
1255
        }
1256
        return null;
1257
        }
1258

    
1259
        /**
1260
         * Asignar el estado del raster
1261
         * @param status
1262
         */
1263
        public void setStatus(StatusRasterInterface status){
1264
                this.status = status;
1265
        }
1266

    
1267
        /**
1268
         * Obtiene el estado del raster
1269
         * @return
1270
         */
1271
        public StatusRasterInterface getStatus(){
1272
                return this.status;
1273
        }
1274

    
1275
        /* (non-Javadoc)
1276
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getAttributes()
1277
         */
1278
        public ArrayList getAttributes() {
1279
                if(rasterFile != null){
1280
                        ArrayList attr = new ArrayList();
1281
                        String dataType = "Byte";
1282
                        if (rasterFile.getDataType() == DataBuffer.TYPE_BYTE) dataType = "Byte";
1283
                        else if (visualStatus.dataType == DataBuffer.TYPE_SHORT)
1284
                                dataType = "Short";
1285
                        else if (visualStatus.dataType == DataBuffer.TYPE_USHORT)
1286
                                dataType = "Unsigned Short";
1287
                        else if (visualStatus.dataType == DataBuffer.TYPE_INT)
1288
                                dataType = "Integer";
1289
                        else if (visualStatus.dataType == DataBuffer.TYPE_FLOAT)
1290
                                dataType = "Float";
1291
                        else if (visualStatus.dataType == DataBuffer.TYPE_DOUBLE)
1292
                                dataType = "Double";
1293
                        else
1294
                                dataType = "Unknown";
1295

    
1296
                        Object [][] a = {
1297
                                {"Filename",rasterFile.getName().substring(rasterFile.getName().lastIndexOf("/")+1, rasterFile.getName().length())},
1298
                                {"Filesize",new Long(0)},
1299
                                {"Width",new Integer((int)this.getWidth())},
1300
                                {"Height", new Integer((int)this.getHeight())},
1301
                                {"Bands", new Integer(visualStatus.bandCount)},
1302
                                {"BandDataType", dataType}
1303
                        };
1304
                        for (int i=0; i<a.length; i++)
1305
                                attr.add(a[i]);
1306

    
1307
                        return attr;
1308
                }
1309
                return  null;
1310
        }
1311

    
1312
        /* (non-Javadoc)
1313
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getFilterStack()
1314
         */
1315
        public RasterFilterStack getFilterStack() {
1316
                if(raster!=null)
1317
                        return raster.filterStack;
1318
                return null;
1319
        }
1320
        /* (non-Javadoc)
1321
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getHeight()
1322
         */
1323
        public double getHeight() {
1324
                return visualStatus.height;
1325
        }
1326

    
1327
        /* (non-Javadoc)
1328
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxX()
1329
         */
1330
        public double getMaxX() {
1331
                return visualStatus.maxX;
1332
        }
1333

    
1334
        /* (non-Javadoc)
1335
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxY()
1336
         */
1337
        public double getMaxY() {
1338
                return visualStatus.maxY;
1339
        }
1340

    
1341
        /* (non-Javadoc)
1342
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinX()
1343
         */
1344
        public double getMinX() {
1345
                return visualStatus.minX;
1346
        }
1347

    
1348
        /* (non-Javadoc)
1349
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinY()
1350
         */
1351
        public double getMinY() {
1352
                return visualStatus.minY;
1353
        }
1354
        /* (non-Javadoc)
1355
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getPixel(double, double)
1356
         */
1357
        public int[] getPixel(double wcx, double wcy) {
1358
                if(getPxRaster() != null)
1359
                        return getPxRaster().getPixel(wcx, wcy);
1360
        return null;
1361
        }
1362
        /* (non-Javadoc)
1363
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getSource()
1364
         */
1365
        public RasterAdapter getSource() {
1366
                return null;
1367
        }
1368
        /* (non-Javadoc)
1369
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getWidth()
1370
         */
1371
        public double getWidth() {
1372
                return visualStatus.width;
1373
        }
1374
        /* (non-Javadoc)
1375
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setBand(int, int)
1376
         */
1377
        public void setBand(int flag, int nBand) {
1378
                switch(flag){
1379
                case GeoRasterFile.RED_BAND:setBandR(nBand);break;
1380
                case GeoRasterFile.GREEN_BAND:setBandG(nBand);break;
1381
                case GeoRasterFile.BLUE_BAND:setBandB(nBand);break;
1382
                }
1383
        }
1384
        /* (non-Javadoc)
1385
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setFilterStack(org.cresques.io.raster.RasterFilterStack)
1386
         */
1387
        public void setFilterStack(RasterFilterStack stack) {
1388
                this.filterStack = stack;
1389
        }
1390
        /* (non-Javadoc)
1391
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPos(int, int)
1392
         */
1393
        public void setPos(int x, int y) {
1394
                this.posX = x;
1395
                this.posY = y;
1396
        }
1397

    
1398
        /* (non-Javadoc)
1399
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPosWC(double, double)
1400
         */
1401
        public void setPosWC(double x, double y) {
1402
                this.posXWC = x;
1403
                this.posYWC = y;
1404
        }
1405

    
1406
        /* (non-Javadoc)
1407
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setRGB(int, int, int)
1408
         */
1409
        public void setRGB(int r, int g, int b) {
1410
                this.r = r;
1411
                this.g = g;
1412
                this.b = b;
1413
        }
1414
        /* (non-Javadoc)
1415
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setSource(com.iver.cit.gvsig.fmap.layers.RasterAdapter)
1416
         */
1417
        public void setSource(RasterAdapter ra) {
1418
        }
1419
        /**
1420
         * @return Returns the raster.
1421
         */
1422
        public PxRaster getPxRaster() {
1423
                return raster;
1424
        }
1425
        /**
1426
         * @return Returns the rasterFile.
1427
         */
1428
        public GeoRasterFile getGeoRasterFile() {
1429
                return rasterFile;
1430
        }
1431

    
1432
        public void setTransparency(int trans) {
1433
                this.transparency = trans;
1434
        }
1435

    
1436
        /**
1437
         * Sets the R-band.
1438
         *
1439
         * Asigna la banda R.
1440
         * @param r
1441
         */
1442
        public void setBandR(int r){
1443
                this.rband = r;
1444
        }
1445

    
1446
        /**
1447
         * Sets the G-band.
1448
         *
1449
         * Asigna la banda G
1450
         * @param g
1451
         */
1452
        public void setBandG(int g){
1453
                this.gband = g;
1454
        }
1455

    
1456
        /**
1457
         * Sets the B-band.
1458
         *
1459
         * Asigna la banda B
1460
         * @param b
1461
         */
1462
        public void setBandB(int b){
1463
                this.bband = b;
1464
        }
1465

    
1466
    /**
1467
     * @return Returns the wmsTransparency.
1468
     */
1469
    public boolean isWmsTransparent() {
1470
        return wmsTransparency;
1471
    }
1472

    
1473
    /**
1474
     * @param wmsTransparency The wmsTransparency to set.
1475
     */
1476
    public void setWmsTransparency(boolean wmsTransparency) {
1477
        this.wmsTransparency = wmsTransparency;
1478
    }
1479

    
1480
     /**
1481
     * @param styles
1482
     */
1483
    public void setStyles(Vector styles) {
1484
            //laura:
1485
            //layer query is built with the layer in reverse order
1486
            // so here we build the styles upside-down.
1487
            if (styles != null)
1488
            {
1489
                    this.styles = new Vector();
1490
                    for(int i = styles.size()-1; i>=0; i--)
1491
                    {
1492
                            this.styles.add(styles.elementAt(i));
1493
                    }
1494
            }
1495
    }
1496

    
1497
    /**
1498
     * Sets the dimension vector that is a list of key-value pairs containing
1499
     * the name of the dimension and the value for it
1500
     * @param dimensions
1501
     */
1502
    public void setDimensions(Vector dimensions) {
1503
        this.dimensions = dimensions;
1504
    }
1505

    
1506
    /**
1507
     * Sets the set of URLs that should be accessed for each operation performed
1508
     * to the server.
1509
     *
1510
     * @param onlineResources
1511
     */
1512
        public void setOnlineResources(Hashtable onlineResources) {
1513
                this.onlineResources = onlineResources;
1514
        }
1515

    
1516
    /**
1517
     * Gets the URL that should be accessed for an operation performed
1518
     * to the server.
1519
     *
1520
     * @param onlineResources
1521
     */
1522
        public String getOnlineResource(String operation) {
1523
                return ((String) onlineResources.get(operation));
1524
        }
1525

    
1526
        /**
1527
         * When a server is not fully featured and it only can serve constant map
1528
         * sizes this value must be set. It expresses the size in pixels (width, height)
1529
         * that the map will be requested.
1530
         * @param Dimension sz
1531
         */
1532
        public void setFixedSize(Dimension sz) {
1533
                fixedSize = sz;
1534
        }
1535

    
1536
        /**
1537
         * Tells whether if this layer must deal with the server with the constant-size
1538
         * limitations or not.
1539
         * @return boolean.
1540
         */
1541
        private boolean isSizeFixed() {
1542
                return fixedSize != null && fixedSize.getWidth() > 0 && fixedSize.getHeight() > 0;
1543
        }
1544

    
1545
        /**
1546
         * If it is true, this layer accepts GetFeatureInfo operations. This WMS operations
1547
         * maps to FMap's infoByPoint(p) operation.
1548
         * @param b
1549
         */
1550
        public void setQueryable(boolean b) {
1551
                queryable = b;
1552
        }
1553

    
1554
        /**
1555
         * Creates the part of a OGC's MapContext document that would describe this
1556
         * layer(s).
1557
         * @param version, The desired version of the resulting document. (1.1.0)
1558
         * @return String containing the xml.
1559
         * @throws UnsupportedVersionException
1560
         */
1561
        public String toMapContext(String mapContextVersion) {
1562
                XmlBuilder xml = new XmlBuilder();
1563
                FMapWMSDriver drv;
1564
                try {
1565
                        drv = getDriver();
1566
                } catch (Exception e) {
1567
                        return xml.toString();
1568
                }
1569
                String[] layerNames = getLayerQuery().split(",");
1570
                String[] styleNames = (String[]) styles.toArray(new String[0]);
1571
                for (int i = 0; i < layerNames.length; i++) {
1572
                        WMSLayerNode layer = drv.getLayer(layerNames[i]);
1573
                        HashMap xmlAttrs = new HashMap();
1574

    
1575
                        // <Layer>
1576
                        xmlAttrs.put(WebMapContextTags.HIDDEN, !isVisible()+"");
1577
                        xmlAttrs.put(WebMapContextTags.QUERYABLE, queryable+"");
1578
                        xml.openTag(WebMapContextTags.LAYER, xmlAttrs);
1579
                        xmlAttrs.clear();
1580
                        if (mapContextVersion.equals("1.1.0") || mapContextVersion.equals("1.0.0")) {
1581
                                // <Server>
1582
                                xmlAttrs.put(WebMapContextTags.SERVICE, WebMapContextTags.WMS);
1583
                                xmlAttrs.put(WebMapContextTags.VERSION, drv.getVersion());
1584
                                xmlAttrs.put(WebMapContextTags.SERVER_TITLE, drv.getServiceTitle());
1585
                                xml.openTag(WebMapContextTags.SERVER, xmlAttrs);
1586
                                xmlAttrs.clear();
1587

    
1588
                                        // <OnlineResource>
1589
                                        xmlAttrs.put(WebMapContextTags.XLINK_TYPE, "simple");
1590
                                        xmlAttrs.put(WebMapContextTags.XLINK_HREF, getHost().toString());
1591
                                        xml.writeTag(WebMapContextTags.ONLINE_RESOURCE, xmlAttrs);
1592
                                        xmlAttrs.clear();
1593
                                        // </OnlineResource>
1594

    
1595
                                xml.closeTag();
1596
                                // </Server>
1597

    
1598
                                // <Name>
1599
                                xml.writeTag(WebMapContextTags.NAME, layer.getName().trim());
1600
                                // </Name>
1601

    
1602
                                // <Title>
1603
                                xml.writeTag(WebMapContextTags.TITLE, layer.getTitle().trim());
1604
                                //?xml.writeTag(WebMapContextTags.TITLE, getName().trim());
1605
                                // </Title>
1606

    
1607
                                // <Abstract>
1608
                                if (layer.getAbstract() != null)
1609
                                        xml.writeTag(WebMapContextTags.ABSTRACT, layer.getAbstract());
1610
                                // </Abstract>
1611

    
1612
                                // <SRS> (a list of available SRS for the enclosing layer)
1613
                                String[] strings = (String[]) layer.getAllSrs().toArray(new String[0]);
1614
                                String mySRS = strings[0];
1615
                                for (int j = 1; j < strings.length; j++) {
1616
                                        mySRS += ","+strings[j];
1617
                                }
1618
                                xml.writeTag(WebMapContextTags.SRS, mySRS);
1619
                                // </SRS>
1620

    
1621
                                // <FormatList>
1622
                                xml.openTag(WebMapContextTags.FORMAT_LIST);
1623
                                        strings = (String[]) drv.getFormats().toArray(new String[0]);
1624
                                        for (int j = 0; j < strings.length; j++) {
1625
                    // <Format>
1626
                                                String str = strings[j].trim();
1627
                                                if (str.equals(getFormat()))
1628
                                                        xml.writeTag(WebMapContextTags.FORMAT, str, WebMapContextTags.CURRENT, "1");
1629
                                                else
1630
                                                        xml.writeTag(WebMapContextTags.FORMAT, str);
1631
                    // </Format>
1632
                                        }
1633
                                xml.closeTag();
1634
                                // </FormatList>
1635

    
1636
                                // <StyleList>
1637
                                xml.openTag(WebMapContextTags.STYLE_LIST);
1638

    
1639
                                        if (layer.getStyles().size()>0) {
1640
                                                for (int j = 0; j < layer.getStyles().size(); j++) {
1641
                                                        // <Style>
1642
                                                        FMapWMSStyle st = (FMapWMSStyle) layer.getStyles().get(j);
1643
                                                        if (st.name.equals(styleNames[i]))
1644
                                                                xmlAttrs.put(WebMapContextTags.CURRENT, "1");
1645
                                                        xml.openTag(WebMapContextTags.STYLE, xmlAttrs);
1646
                                                        xmlAttrs.clear();
1647

    
1648
                                                                // <Name>
1649
                                                                xml.writeTag(WebMapContextTags.NAME, st.name);
1650
                                                                // </Name>
1651

    
1652
                                                                // <Title>
1653
                                                                xml.writeTag(WebMapContextTags.TITLE, st.title);
1654
                                                                // </Title>
1655

    
1656
                                                                // <LegendURL width="180" format="image/gif" height="50">
1657
                                                                        // <OnlineResource xlink:type="simple" xlink:href="http://globe.digitalearth.gov/globe/en/icons/colorbars/NATIONAL.gif"/>
1658
                                                                        // </OnlineResource>
1659
                                                                // </LegendURL>
1660
                                                        xml.closeTag();
1661
                                                        // </Style>
1662

    
1663
                                                }
1664

    
1665
                                        } else {
1666
                                                // Create fake style (for compatibility issues)
1667
                                                xmlAttrs.put(WebMapContextTags.CURRENT, "1");
1668
                                                // <Style>
1669
                                                xml.openTag(WebMapContextTags.STYLE, xmlAttrs);
1670
                                                        xmlAttrs.clear();
1671
                                                        // <Name>
1672
                                                        xml.writeTag(WebMapContextTags.NAME, "default");
1673
                                                        // </Name>
1674

    
1675
                                                        // <Title>
1676
                                                        xml.writeTag(WebMapContextTags.TITLE, "default");
1677
                                                        // </Title>
1678

    
1679
//                                                        // <LegendURL width="180" format="image/gif" height="50">
1680
//                                                        xmlAttrs.put(WebMapContextTags.WIDTH, "0");
1681
//                                                        xmlAttrs.put(WebMapContextTags.HEIGHT, "0");
1682
//                                                        xmlAttrs.put(WebMapContextTags.FORMAT.toLowerCase(), "image/gif");
1683
//                                                        xml.openTag(WebMapContextTags.LEGEND_URL, xmlAttrs);
1684
//                                                        xmlAttrs.clear();
1685
//                                                                // <OnlineResource xlink:type="simple" xlink:href="http://globe.digitalearth.gov/globe/en/icons/colorbars/NATIONAL.gif"/>
1686
//                                                                xmlAttrs.put(WebMapContextTags.XLINK_TYPE, "simple");
1687
//                                                                xmlAttrs.put(WebMapContextTags.XLINK_HREF, "http://globe.digitalearth.gov/globe/en/icons/colorbars/NATIONAL.gif");
1688
//                                                                xml.writeTag(WebMapContextTags.ONLINE_RESOURCE, xmlAttrs);
1689
//                                                                // </OnlineResource>
1690
//                                                    // </LegendURL>
1691
//                                                        xml.closeTag();
1692
                                                // </Style>
1693
                                                xml.closeTag();
1694
                                        }
1695
                                // </StyleList>
1696
                                xml.closeTag();
1697
                                if (mapContextVersion.compareTo("1.0.0") > 0) {
1698
                                // <DimensionList>
1699
                                        xml.openTag(WebMapContextTags.DIMENSION_LIST);
1700
                                        // <Dimension>
1701
                                        // </Dimension>
1702
                                        xml.closeTag();
1703
                                // </DimensionList>
1704
                                }
1705
                        } else {
1706
                                xml.writeTag("ERROR", PluginServices.getText(this, "unsupported_map_context_version"));
1707
                        }
1708
                        // </Layer>
1709
                        xml.closeTag();
1710
                }
1711
                return xml.getXML();
1712
        }
1713

    
1714
        public ImageIcon getTocImageIcon() {
1715
                return new ImageIcon(PluginServices.getPluginServices("com.iver.cit.gvsig.wms").getClassLoader().getResource("images/icoLayer.png"));
1716
        }
1717

    
1718
        /*
1719
         *  (non-Javadoc)
1720
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getTileSize()
1721
         */
1722
        public int[] getTileSize() {
1723
                int[] size = {maxTileDrawWidth, maxTileDrawHeight};
1724
                return size;
1725
        }
1726

    
1727
        /*
1728
         *  (non-Javadoc)
1729
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#isTiled()
1730
         */
1731
        public boolean isTiled() {
1732
                return mustTileDraw;
1733
        }
1734

    
1735
        public Image getImageLegend() {
1736
                try {
1737
                        if (wms.hasLegendGraphic() ){
1738
                                wmsStatus.setOnlineResource((String) onlineResources.get("GetLegendGraphic"));
1739
                                File legend = getDriver().getLegendGraphic(wmsStatus, layerQuery, null);
1740
                                Image img = null;
1741
                                if ((legend!= null) && (legend.length() > 0))
1742
                                        img = ImageIO.read(legend);
1743
                                return img;
1744
                        }else{
1745
                                return null;
1746
                        }
1747
                }catch(Exception e){
1748
                        e.printStackTrace();
1749
                        return null;
1750
                }
1751
        }
1752

    
1753

    
1754
}