Statistics
| Revision:

root / trunk / extensions / extWMS / src / com / iver / cit / gvsig / fmap / layers / FLyrWMS.java @ 7713

History | View | Annotate | Download (52.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.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.ImageIcon;
68
import javax.swing.JOptionPane;
69

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

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

    
105

    
106

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

    
125
    public URL                                                         host;
126
    public String                                                 m_Format;
127

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

    
153

    
154
        private class MyCancellable implements ICancellable
155
        {
156

    
157
                private Cancellable original;
158
                public MyCancellable(Cancellable cancelOriginal)
159
                {
160
                        this.original = cancelOriginal;
161
                }
162
                public boolean isCanceled() {
163
                        return original.isCanceled();
164
                }
165

    
166
        }
167

    
168
        public FLyrWMS(){
169
                super();
170
        }
171

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

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

    
197
                        WMSLayerNode wmsNode = drv.getLayer(sLayer);
198

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

    
213

    
214
                this.setFullExtent(fullExtent);
215
                this.setFormat(sFormat);
216
                this.setLayerQuery(sLayer);
217
                this.setInfoLayerQuery("");
218
                this.setSRS(sSRS);
219
                this.setName(sLayer);
220
        }
221

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

    
241
            return (String)formats.get(0);
242
        }
243

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

    
259

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

    
270
                // Full extent
271
                xml.putProperty("fullExtent", StringUtilities.rect2String(fullExtent));
272

    
273
                // Host
274
                xml.putProperty("host", host.toExternalForm());
275

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

    
280
                // Part of the query containing the layer names
281
                xml.putProperty("layerQuery", layerQuery);
282

    
283
                // Format
284
                xml.putProperty("format", m_Format);
285

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

    
295
        // Transparency
296
        xml.putProperty("wms_transparency", wmsTransparency);
297

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

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

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

    
336
        // Queryable
337
        xml.putProperty("queryable", queryable);
338

    
339
        // fixedSize
340
        if (isSizeFixed()) {
341
                xml.putProperty("fixedSize", true);
342
                xml.putProperty("fixedWidth", fixedSize.width);
343
                xml.putProperty("fixedHeight", fixedSize.height);
344
        }
345
        return xml;
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 setXMLEntity03(XMLEntity xml)
358
                throws XMLException {
359
                super.setXMLEntity(xml);
360
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
361
                                        "fullExtent"));
362

    
363
                try {
364
                        host = new URL(xml.getStringProperty("host"));
365
                } catch (MalformedURLException e) {
366
                        throw new XMLException(e);
367
                }
368

    
369
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
370
                layerQuery = xml.getStringProperty("layerQuery");
371
                m_Format = xml.getStringProperty("format");
372
                m_SRS = xml.getStringProperty("srs");
373
        }
374

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

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

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

    
401
                // Part of the query containing the layer names
402
                layerQuery = xml.getStringProperty("layerQuery");
403

    
404
                // Format
405
                m_Format = xml.getStringProperty("format");
406

    
407
                // SRS
408
                m_SRS = xml.getStringProperty("srs");
409

    
410
                String claseStr = StatusLayerRaster.defaultClass;
411
                if (xml.contains("raster.class")) {
412
                        claseStr = xml.getStringProperty("raster.class");
413
                }
414

    
415
                // Transparency
416
        if (xml.contains("wms_transparency"))
417
            wmsTransparency = xml.getBooleanProperty("wms_transparency");
418

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

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

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

    
439
                dimensions.add(dims[i]);
440
            }
441
        }
442

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

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

    
458
        // fixedSize
459
        if (xml.contains("fixedSize")) {
460
                fixedSize = new Dimension(xml.getIntProperty("fixedWidth"),
461
                                                  xml.getIntProperty("fixedHeight"));
462
        }
463

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

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

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

    
508
                                int col = (int) p.getX() / maxTilePrintWidth;
509
                                int row = (int) p.getY() / maxTilePrintHeight;
510
                                int tileIndex = (row*nCols) + col;
511

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

    
557
                } catch (UnsupportedVersionException e) {
558
                        UnsuportedProtocolVersionExceptionType type =
559
                                new UnsuportedProtocolVersionExceptionType();
560
                        type.setLayerName(getName());
561
                        try {
562
                                type.setDriverName("WMS Driver");
563
                        } catch (Exception ex){
564
                        }
565
                        type.setUrl(host);
566
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e, type);
567

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

    
585
        /**
586
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
587
         */
588
        public Rectangle2D getFullExtent() {
589
                return fullExtent;
590
        }
591

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

    
614

    
615
                        if (isSizeFixed()) {
616
                                // This condition handles those situations in which the server can
617
                                // only give static extent and resolution maps despite we need
618
                                // a specific BBOX and pixel WIDTH and HEIGHT
619
                                drawFixedSize(g, viewPort, cancel);
620

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

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

    
649

    
650
                // This is the extent that will be requested
651
                Rectangle2D bBox = getFullExtent();
652
                MyCancellable c = new MyCancellable(cancel);
653

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

    
671
                        if(status!=null && firstLoad){
672
                                status.applyStatus(this);
673
                                firstLoad = false;
674
                        }
675

    
676
                        // And finally, obtain the extent intersecting the view and the BBox
677
                        // to draw to.
678
                        Rectangle2D extent = new Rectangle2D.Double();
679
                        Rectangle2D.intersect(vp.getAdjustedExtent(), bBox, extent);
680

    
681
                        ViewPortData vpData = new ViewPortData(
682
                                vp.getProjection(), new Extent(extent), fixedSize );
683
                        vpData.setMat(vp.getAffineTransform());
684

    
685
                        rasterProcess(g, vpData, f);
686

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

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

    
740

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

    
762

    
763
                                } // mess code
764
                        }
765
                }
766
                callCount--; // mess code
767
        }
768

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

    
784
        // 2. Compute extent to be requested.
785
        Rectangle2D bBox = new Rectangle2D.Double();
786
        Rectangle2D.intersect(vp.getExtent(), extent, bBox);
787

    
788
        // 3. Compute size in pixels
789
        double scalex = vp.getAffineTransform().getScaleX();
790
        double scaley = vp.getAffineTransform().getScaleY();
791
        int wImg = (int) Math.ceil(Math.abs(bBox.getWidth() * scalex) + 1);
792
        int hImg = (int) Math.ceil(Math.abs(bBox.getHeight() * scaley) + 1);
793

    
794
        Dimension sz = new Dimension(wImg, hImg);
795

    
796
        if ((wImg <= 0) || (hImg <= 0)) {
797
            return;
798
        }
799
        MyCancellable c = new MyCancellable(cancel);
800

    
801
                try {
802
//                        wImg = vp.getImageWidth();
803
//                        hImg = vp.getImageHeight();
804
                        sz = new Dimension(wImg, hImg);
805
                        Rectangle2D.intersect(vp.getAdjustedExtent(), extent, bBox);
806

    
807

    
808
                        wmsStatus.setExtent( bBox );
809
                        wmsStatus.setFormat(m_Format);
810
                        wmsStatus.setHeight( hImg );
811
                        wmsStatus.setWidth( wImg );
812
                        wmsStatus.setLayerNames(Utilities.createVector(layerQuery,","));
813
                        wmsStatus.setSrs(m_SRS);
814
                        wmsStatus.setStyles(styles);
815
                        wmsStatus.setDimensions(dimensions);
816
                        wmsStatus.setTransparency(wmsTransparency);
817
                        wmsStatus.setOnlineResource((String) onlineResources.get("GetMap"));
818

    
819
                        // begin patch; Avoid to request too small tiles. 
820
                        // This generally occurs when printing 
821

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

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

    
844
                        // end patch
845
                        File f = getDriver().getMap(wmsStatus, c);
846
                        if (f == null)
847
                                return;
848
                        String nameWordFile = f.getPath() + getExtensionWorldFile();
849
                        com.iver.andami.Utilities.createTemp(nameWordFile, this.getDataWorldFile(bBox, sz));
850

    
851
                        ViewPortData vpData = new ViewPortData(
852
                                vp.getProjection(), new Extent(bBox), sz );
853
                        vpData.setMat(vp.getAffineTransform());
854

    
855
                        rasterProcess(g, vpData, f);
856

    
857
                } catch (ValidationException e) {
858
                //azabala
859
//                        if (!c.isCanceled())
860
//                                throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
861

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

    
880
//                        if (!c.isCanceled())
881
//                                throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
882

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

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

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

    
965

    
966
                                } //if
967
                        }//if
968
                }//catch
969
                callCount--;
970
        }
971

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

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

    
1004
        /**
1005
         * Dibuja una imagen usando PxRaster
1006
         * @param g        Graphics2D en el que hay que dibujar.
1007
         * @param vpData Par?metros de visualizaci?n
1008
         * @param file La imagen en cuesti?n.
1009
         */
1010
        private void rasterProcess(Graphics2D g, ViewPortData vpData, File file) {
1011

    
1012
                //Creamos el PxRaster
1013
                rasterFile = new GdalFile(vpData.getProjection(), file.getAbsolutePath());
1014
                raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
1015

    
1016
                if(status!=null && firstLoad){
1017
                        status.applyStatus(this);
1018
                        firstLoad = false;
1019
                }
1020

    
1021
                //Recuperamos la pila de filtros si ya hubiese sido cargado antes
1022
                if(this.filterStack!=null)
1023
                        raster.filterStack = this.filterStack;
1024

    
1025
                raster.setTransparency(false);
1026

    
1027
                //Asignamos transparencia y orden de bandas
1028
                if(this.transparency==-1 && !firstLoad);
1029
                else
1030
                        raster.setTransparency(this.transparency);
1031

    
1032
                raster.setBand(GeoRasterFile.RED_BAND,rband);
1033
                raster.setBand(GeoRasterFile.GREEN_BAND, gband);
1034
                raster.setBand(GeoRasterFile.BLUE_BAND, bband);
1035

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

    
1042
                if(visualStatus != null){
1043
                        visualStatus.bandCount = raster.getBandCount();
1044
                        visualStatus.dataType = raster.getDataType();
1045
                }
1046

    
1047
                raster.draw(g, vpData);
1048

    
1049
                //En el primer pxRaster de una imagen obtenemos el Stack Manager para poder modificarlo
1050
                //si queremos desde las propiedades
1051

    
1052
                if(this.stackManager == null)
1053
                        this.stackManager = raster.getStackManager();
1054

    
1055
                if(this.filterStack == null)
1056
                        this.filterStack = raster.filterStack;
1057

    
1058
                //rasterFile.close();
1059
        }
1060

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

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

    
1094
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
1095
                throws DriverException {
1096
                draw(null, g, viewPort, cancel,scale);
1097
        }
1098

    
1099
        /**
1100
         * Devuelve el FMapWMSDriver.
1101
         *
1102
         * @return FMapWMSDriver
1103
         *
1104
         * @throws IllegalStateException
1105
         * @throws ValidationException
1106
         * @throws UnsupportedVersionException
1107
         * @throws IOException
1108
         */
1109
        private FMapWMSDriver getDriver()
1110
                throws IllegalStateException, ValidationException,
1111
                        UnsupportedVersionException, IOException {
1112
                return FMapWMSDriverFactory.getFMapDriverForURL(host);
1113
        }
1114

    
1115
        /**
1116
         * Devuelve el FMapWMSDriver.
1117
         *
1118
         * @return FMapWMSDriver
1119
         *
1120
         * @throws IllegalStateException
1121
         * @throws ValidationException
1122
         * @throws UnsupportedVersionException
1123
         * @throws IOException
1124
         */
1125
        public void setDriver(FMapWMSDriver drv) {
1126
                wms = drv;
1127
        }
1128

    
1129
        /**
1130
         * Devuelve el URL.
1131
         *
1132
         * @return URL.
1133
         */
1134
        public URL getHost() {
1135
                return host;
1136
        }
1137

    
1138
        /**
1139
         * Inserta el URL.
1140
         *
1141
         * @param host URL.
1142
         */
1143
        public void setHost(URL host) {
1144
                this.host = host;
1145
        }
1146

    
1147
        /**
1148
         * Devuelve la informaci?n de la consulta.
1149
         *
1150
         * @return String.
1151
         */
1152
        public String getInfoLayerQuery() {
1153
                return infoLayerQuery;
1154
        }
1155

    
1156
        /**
1157
         * Inserta la informaci?n de la consulta.
1158
         *
1159
         * @param infoLayerQuery String.
1160
         */
1161
        public void setInfoLayerQuery(String infoLayerQuery) {
1162
                this.infoLayerQuery = infoLayerQuery;
1163
        }
1164

    
1165
        /**
1166
         * Devuelve la consulta.
1167
         *
1168
         * @return String.
1169
         */
1170
        public String getLayerQuery() {
1171
                return layerQuery;
1172
        }
1173

    
1174
        /**
1175
         * Inserta la consulta.
1176
         *
1177
         * @param layerQuery consulta.
1178
         */
1179
        public void setLayerQuery(String layerQuery) {
1180
                this.layerQuery = layerQuery;
1181
        }
1182

    
1183
        /**
1184
         * Devuelve el formato.
1185
         *
1186
         * @return Formato.
1187
         */
1188
        public String getFormat() {
1189
                return m_Format;
1190
        }
1191

    
1192
        /**
1193
         * Inserta el formato.
1194
         *
1195
         * @param format Formato.
1196
         */
1197
        public void setFormat(String format) {
1198
                m_Format = format;
1199
        }
1200

    
1201
        /**
1202
         * Devuelve el SRS.
1203
         *
1204
         * @return SRS.
1205
         */
1206
        public String getSRS() {
1207
                return m_SRS;
1208
        }
1209

    
1210
        /**
1211
         * Inserta el SRS.
1212
         *
1213
         * @param m_srs SRS.
1214
         */
1215
        public void setSRS(String m_srs) {
1216
                m_SRS = m_srs;
1217
        }
1218

    
1219
        /**
1220
         * Inserta la extensi?n total de la capa.
1221
         *
1222
         * @param fullExtent Rect?ngulo.
1223
         */
1224
        public void setFullExtent(Rectangle2D fullExtent) {
1225
                this.fullExtent = fullExtent;
1226
        }
1227

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

    
1254
        /**
1255
         * Asignar el estado del raster
1256
         * @param status
1257
         */
1258
        public void setStatus(StatusRasterInterface status){
1259
                this.status = status;
1260
        }
1261

    
1262
        /**
1263
         * Obtiene el estado del raster
1264
         * @return
1265
         */
1266
        public StatusRasterInterface getStatus(){
1267
                return this.status;
1268
        }
1269

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

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

    
1302
                        return attr;
1303
                }
1304
                return  null;
1305
        }
1306

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

    
1322
        /* (non-Javadoc)
1323
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxX()
1324
         */
1325
        public double getMaxX() {
1326
                return visualStatus.maxX;
1327
        }
1328

    
1329
        /* (non-Javadoc)
1330
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxY()
1331
         */
1332
        public double getMaxY() {
1333
                return visualStatus.maxY;
1334
        }
1335

    
1336
        /* (non-Javadoc)
1337
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinX()
1338
         */
1339
        public double getMinX() {
1340
                return visualStatus.minX;
1341
        }
1342

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

    
1393
        /* (non-Javadoc)
1394
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPosWC(double, double)
1395
         */
1396
        public void setPosWC(double x, double y) {
1397
                this.posXWC = x;
1398
                this.posYWC = y;
1399
        }
1400

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

    
1427
        public void setTransparency(int trans) {
1428
                this.transparency = trans;
1429
        }
1430

    
1431
        /**
1432
         * Sets the R-band.
1433
         *
1434
         * Asigna la banda R.
1435
         * @param r
1436
         */
1437
        public void setBandR(int r){
1438
                this.rband = r;
1439
        }
1440

    
1441
        /**
1442
         * Sets the G-band.
1443
         *
1444
         * Asigna la banda G
1445
         * @param g
1446
         */
1447
        public void setBandG(int g){
1448
                this.gband = g;
1449
        }
1450

    
1451
        /**
1452
         * Sets the B-band.
1453
         *
1454
         * Asigna la banda B
1455
         * @param b
1456
         */
1457
        public void setBandB(int b){
1458
                this.bband = b;
1459
        }
1460

    
1461
    /**
1462
     * @return Returns the wmsTransparency.
1463
     */
1464
    public boolean isWmsTransparent() {
1465
        return wmsTransparency;
1466
    }
1467

    
1468
    /**
1469
     * @param wmsTransparency The wmsTransparency to set.
1470
     */
1471
    public void setWmsTransparency(boolean wmsTransparency) {
1472
        this.wmsTransparency = wmsTransparency;
1473
    }
1474

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

    
1492
    /**
1493
     * Sets the dimension vector that is a list of key-value pairs containing
1494
     * the name of the dimension and the value for it
1495
     * @param dimensions
1496
     */
1497
    public void setDimensions(Vector dimensions) {
1498
        this.dimensions = dimensions;
1499
    }
1500

    
1501
    /**
1502
     * Sets the set of URLs that should be accessed for each operation performed
1503
     * to the server.
1504
     *
1505
     * @param onlineResources
1506
     */
1507
        public void setOnlineResources(Hashtable onlineResources) {
1508
                this.onlineResources = onlineResources;
1509
        }
1510

    
1511
        /**
1512
         * When a server is not fully featured and it only can serve constant map
1513
         * sizes this value must be set. It expresses the size in pixels (width, height)
1514
         * that the map will be requested.
1515
         * @param Dimension sz
1516
         */
1517
        public void setFixedSize(Dimension sz) {
1518
                fixedSize = sz;
1519
        }
1520

    
1521
        /**
1522
         * Tells whether if this layer must deal with the server with the constant-size
1523
         * limitations or not.
1524
         * @return boolean.
1525
         */
1526
        private boolean isSizeFixed() {
1527
                return fixedSize != null && fixedSize.getWidth() > 0 && fixedSize.getHeight() > 0;
1528
        }
1529

    
1530
        /**
1531
         * If it is true, this layer accepts GetFeatureInfo operations. This WMS operations
1532
         * maps to FMap's infoByPoint(p) operation.
1533
         * @param b
1534
         */
1535
        public void setQueryable(boolean b) {
1536
                queryable = b;
1537
        }
1538

    
1539
        /**
1540
         * Creates the part of a OGC's MapContext document that would describe this
1541
         * layer(s).
1542
         * @param version, The desired version of the resulting document. (1.1.0)
1543
         * @return String containing the xml.
1544
         * @throws UnsupportedVersionException
1545
         */
1546
        public String toMapContext(String mapContextVersion) {
1547
                XmlBuilder xml = new XmlBuilder();
1548
                FMapWMSDriver drv;
1549
                try {
1550
                        drv = getDriver();
1551
                } catch (Exception e) {
1552
                        return xml.toString();
1553
                }
1554
                String[] layerNames = getLayerQuery().split(",");
1555
                String[] styleNames = (String[]) styles.toArray(new String[0]);
1556
                for (int i = 0; i < layerNames.length; i++) {
1557
                        WMSLayerNode layer = drv.getLayer(layerNames[i]);
1558
                        HashMap xmlAttrs = new HashMap();
1559

    
1560
                        // <Layer>
1561
                        xmlAttrs.put(WebMapContextTags.HIDDEN, !isVisible()+"");
1562
                        xmlAttrs.put(WebMapContextTags.QUERYABLE, queryable+"");
1563
                        xml.openTag(WebMapContextTags.LAYER, xmlAttrs);
1564
                        xmlAttrs.clear();
1565
                        if (mapContextVersion.equals("1.1.0") || mapContextVersion.equals("1.0.0")) {
1566
                                // <Server>
1567
                                xmlAttrs.put(WebMapContextTags.SERVICE, WebMapContextTags.WMS);
1568
                                xmlAttrs.put(WebMapContextTags.VERSION, drv.getVersion());
1569
                                xmlAttrs.put(WebMapContextTags.SERVER_TITLE, drv.getServiceTitle());
1570
                                xml.openTag(WebMapContextTags.SERVER, xmlAttrs);
1571
                                xmlAttrs.clear();
1572

    
1573
                                        // <OnlineResource>
1574
                                        xmlAttrs.put(WebMapContextTags.XLINK_TYPE, "simple");
1575
                                        xmlAttrs.put(WebMapContextTags.XLINK_HREF, getHost().toString());
1576
                                        xml.writeTag(WebMapContextTags.ONLINE_RESOURCE, xmlAttrs);
1577
                                        xmlAttrs.clear();
1578
                                        // </OnlineResource>
1579

    
1580
                                xml.closeTag();
1581
                                // </Server>
1582

    
1583
                                // <Name>
1584
                                xml.writeTag(WebMapContextTags.NAME, layer.getName().trim());
1585
                                // </Name>
1586

    
1587
                                // <Title>
1588
                                xml.writeTag(WebMapContextTags.TITLE, layer.getTitle().trim());
1589
                                //?xml.writeTag(WebMapContextTags.TITLE, getName().trim());
1590
                                // </Title>
1591

    
1592
                                // <Abstract>
1593
                                if (layer.getAbstract() != null)
1594
                                        xml.writeTag(WebMapContextTags.ABSTRACT, layer.getAbstract());
1595
                                // </Abstract>
1596

    
1597
                                // <SRS> (a list of available SRS for the enclosing layer)
1598
                                String[] strings = (String[]) layer.getAllSrs().toArray(new String[0]);
1599
                                String mySRS = strings[0];
1600
                                for (int j = 1; j < strings.length; j++) {
1601
                                        mySRS += ","+strings[j];
1602
                                }
1603
                                xml.writeTag(WebMapContextTags.SRS, mySRS);
1604
                                // </SRS>
1605

    
1606
                                // <FormatList>
1607
                                xml.openTag(WebMapContextTags.FORMAT_LIST);
1608
                                        strings = (String[]) drv.getFormats().toArray(new String[0]);
1609
                                        for (int j = 0; j < strings.length; j++) {
1610
                    // <Format>
1611
                                                String str = strings[j].trim();
1612
                                                if (str.equals(getFormat()))
1613
                                                        xml.writeTag(WebMapContextTags.FORMAT, str, WebMapContextTags.CURRENT, "1");
1614
                                                else
1615
                                                        xml.writeTag(WebMapContextTags.FORMAT, str);
1616
                    // </Format>
1617
                                        }
1618
                                xml.closeTag();
1619
                                // </FormatList>
1620

    
1621
                                // <StyleList>
1622
                                xml.openTag(WebMapContextTags.STYLE_LIST);
1623

    
1624
                                        if (layer.getStyles().size()>0) {
1625
                                                for (int j = 0; j < layer.getStyles().size(); j++) {
1626
                                                        // <Style>
1627
                                                        FMapWMSStyle st = (FMapWMSStyle) layer.getStyles().get(j);
1628
                                                        if (st.name.equals(styleNames[i]))
1629
                                                                xmlAttrs.put(WebMapContextTags.CURRENT, "1");
1630
                                                        xml.openTag(WebMapContextTags.STYLE, xmlAttrs);
1631
                                                        xmlAttrs.clear();
1632

    
1633
                                                                // <Name>
1634
                                                                xml.writeTag(WebMapContextTags.NAME, st.name);
1635
                                                                // </Name>
1636

    
1637
                                                                // <Title>
1638
                                                                xml.writeTag(WebMapContextTags.TITLE, st.title);
1639
                                                                // </Title>
1640

    
1641
                                                                // <LegendURL width="180" format="image/gif" height="50">
1642
                                                                        // <OnlineResource xlink:type="simple" xlink:href="http://globe.digitalearth.gov/globe/en/icons/colorbars/NATIONAL.gif"/>
1643
                                                                        // </OnlineResource>
1644
                                                                // </LegendURL>
1645
                                                        xml.closeTag();
1646
                                                        // </Style>
1647

    
1648
                                                }
1649

    
1650
                                        } else {
1651
                                                // Create fake style (for compatibility issues)
1652
                                                xmlAttrs.put(WebMapContextTags.CURRENT, "1");
1653
                                                // <Style>
1654
                                                xml.openTag(WebMapContextTags.STYLE, xmlAttrs);
1655
                                                        xmlAttrs.clear();
1656
                                                        // <Name>
1657
                                                        xml.writeTag(WebMapContextTags.NAME, "default");
1658
                                                        // </Name>
1659

    
1660
                                                        // <Title>
1661
                                                        xml.writeTag(WebMapContextTags.TITLE, "default");
1662
                                                        // </Title>
1663

    
1664
//                                                        // <LegendURL width="180" format="image/gif" height="50">
1665
//                                                        xmlAttrs.put(WebMapContextTags.WIDTH, "0");
1666
//                                                        xmlAttrs.put(WebMapContextTags.HEIGHT, "0");
1667
//                                                        xmlAttrs.put(WebMapContextTags.FORMAT.toLowerCase(), "image/gif");
1668
//                                                        xml.openTag(WebMapContextTags.LEGEND_URL, xmlAttrs);
1669
//                                                        xmlAttrs.clear();
1670
//                                                                // <OnlineResource xlink:type="simple" xlink:href="http://globe.digitalearth.gov/globe/en/icons/colorbars/NATIONAL.gif"/>
1671
//                                                                xmlAttrs.put(WebMapContextTags.XLINK_TYPE, "simple");
1672
//                                                                xmlAttrs.put(WebMapContextTags.XLINK_HREF, "http://globe.digitalearth.gov/globe/en/icons/colorbars/NATIONAL.gif");
1673
//                                                                xml.writeTag(WebMapContextTags.ONLINE_RESOURCE, xmlAttrs);
1674
//                                                                // </OnlineResource>
1675
//                                                    // </LegendURL>
1676
//                                                        xml.closeTag();
1677
                                                // </Style>
1678
                                                xml.closeTag();
1679
                                        }
1680
                                // </StyleList>
1681
                                xml.closeTag();
1682
                                if (mapContextVersion.compareTo("1.0.0") > 0) {
1683
                                // <DimensionList>
1684
                                        xml.openTag(WebMapContextTags.DIMENSION_LIST);
1685
                                        // <Dimension>
1686
                                        // </Dimension>
1687
                                        xml.closeTag();
1688
                                // </DimensionList>
1689
                                }
1690
                        } else {
1691
                                xml.writeTag("ERROR", PluginServices.getText(this, "unsupported_map_context_version"));
1692
                        }
1693
                        // </Layer>
1694
                        xml.closeTag();
1695
                }
1696
                return xml.getXML();
1697
        }
1698

    
1699
        public ImageIcon getTocImageIcon() {
1700
                return new ImageIcon(PluginServices.getPluginServices("com.iver.cit.gvsig.wms").getClassLoader().getResource("images/icoLayer.png"));
1701
        }
1702

    
1703
        /*
1704
         *  (non-Javadoc)
1705
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getTileSize()
1706
         */
1707
        public int[] getTileSize() {
1708
                int[] size = {maxTileDrawWidth, maxTileDrawHeight};
1709
                return size;
1710
        }
1711

    
1712
        /*
1713
         *  (non-Javadoc)
1714
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#isTiled()
1715
         */
1716
        public boolean isTiled() {
1717
                return mustTileDraw;
1718
        }
1719

    
1720

    
1721
}