Statistics
| Revision:

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

History | View | Annotate | Download (33.9 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*
19
* For more information, contact:
20
*
21
*  Generalitat Valenciana
22
*   Conselleria d'Infraestructures i Transport
23
*   Av. Blasco Ib??ez, 50
24
*   46010 VALENCIA
25
*   SPAIN
26
*
27
*      +34 963862235
28
*   gvsig@gva.es
29
*      www.gvsig.gva.es
30
*
31
*    or
32
*
33
*   IVER T.I. S.A
34
*   Salamanca 50
35
*   46005 Valencia
36
*   Spain
37
*
38
*   +34 963163400
39
*   dac@iver.es
40
*/
41
package com.iver.cit.gvsig.fmap.layers;
42

    
43
import java.awt.Component;
44
import java.awt.Dimension;
45
import java.awt.Graphics2D;
46
import java.awt.Point;
47
import java.awt.Rectangle;
48
import java.awt.geom.AffineTransform;
49
import java.awt.geom.NoninvertibleTransformException;
50
import java.awt.geom.Point2D;
51
import java.awt.geom.Rectangle2D;
52
import java.awt.image.BufferedImage;
53
import java.io.File;
54
import java.io.IOException;
55
import java.lang.reflect.Constructor;
56
import java.lang.reflect.InvocationTargetException;
57
import java.net.MalformedURLException;
58
import java.net.URL;
59
import java.util.ArrayList;
60
import java.util.HashMap;
61
import java.util.Hashtable;
62
import java.util.Iterator;
63
import java.util.Vector;
64

    
65
import javax.swing.JOptionPane;
66

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

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

    
91

    
92
/**
93
* FMap's WMS Layer class.
94
*
95
* @author Jaume Dominguez Faus
96
*                   Nacho Brodin
97
* 
98
*/
99
public class FLyrWMS extends FLyrDefault implements InfoByPoint, RasterOperations {
100
        private boolean                                         isPrinting = false;
101
        private boolean                                         mustTileDraw = false;
102
        private boolean                                         mustTilePrint = true;
103
        private final int                                         maxTileDrawWidth = -1;
104
        private final int                                         maxTileDrawHeight = -1;
105
        private final int                                         maxTilePrintWidth = 1200;
106
        private final int                                         maxTilePrintHeight = 1200;
107
    
108
    public URL                                                         host;
109
    public String                                                 m_Format;
110
    
111
        private String                                                 m_SRS;
112
        private String                                                 layerQuery;
113
        private String                                                 infoLayerQuery;
114
        private FMapWMSDriver                                 wms;
115
        private WMSStatus                                         wmsStatus = new WMSStatus();
116
        private Rectangle2D                                 fullExtent;
117
        private boolean                                                wmsTransparency;
118
    private Vector                                                 styles;
119
    private Vector                                                 dimensions;
120
        private StatusRasterInterface                status = null;
121
        private int                                                 posX = 0, posY = 0;
122
        private double                                                 posXWC = 0, posYWC = 0;
123
        private int                                                 r = 0, g = 0, b = 0;
124
        private GeoRasterFile                                 rasterFile = null;
125
        private PxRaster                                         raster = null;
126
        private RasterFilterStack                         filterStack = null;
127
        private boolean                                                firstLoad = false;
128
        private int                                                 transparency = -1;
129
        private int                                                 rband = 0, gband = 1, bband = 2;
130
        private RasterFilterStackManager        stackManager = null;
131
        private Hashtable                                         onlineResources = new Hashtable();
132
        private Dimension                                         fixedSize;
133
        private boolean                                         queryable = true;
134
        private VisualStatusWMS                                visualStatus = new VisualStatusWMS();
135
        
136
        /**
137
         * Clase que contiene los datos de visualizaci?n de WMS.
138
         * @author Nacho Brodin (brodin_ign@gva.es)
139
         */
140
        private class VisualStatusWMS{
141
                /**
142
                 * Ancho y alto de la imagen o del conjunto de tiles si los tiene. Coincide con 
143
                 * el ancho y alto del viewPort
144
                 */
145
                private        int                                                        width = 0, height = 0;
146
                private double                                                minX = 0D, minY = 0D, maxX = 0D, maxY = 0D;
147
        }
148
         
149

    
150
        /**
151
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir la
152
         * capa.
153
         *
154
         * @return XMLEntity.
155
         * @throws XMLException
156
         */
157
        public XMLEntity getXMLEntity() throws XMLException {
158
                XMLEntity xml = super.getXMLEntity();
159

    
160
                // Full extent
161
                xml.putProperty("fullExtent", StringUtilities.rect2String(fullExtent));
162
                
163
                // Host
164
                xml.putProperty("host", host.toExternalForm());
165
                
166
                // Part of the query that is not the host, or the
167
                // layer names, or other not listed bellow
168
                xml.putProperty("infoLayerQuery", infoLayerQuery);
169
                
170
                // Part of the query containing the layer names
171
                xml.putProperty("layerQuery", layerQuery);
172
                
173
                // Format
174
                xml.putProperty("format", m_Format);
175
                
176
                // SRS
177
                xml.putProperty("srs", m_SRS);
178
                if (status!=null)
179
                        status.getXMLEntity(xml, true, this);
180
                else{
181
                        status = new StatusLayerRaster();
182
                        status.getXMLEntity(xml, true, this);
183
                }
184
                
185
        // Transparency
186
        xml.putProperty("wms_transparency", wmsTransparency);
187
        
188
        // Styles
189
        if (styles!=null){
190
            String stylePr = "";
191
            for (int i = 0; i < styles.size(); i++) {
192
                stylePr += (String) styles.get(i);
193
                if (i<styles.size()-1)
194
                    stylePr += ",";
195
            }
196
            if (stylePr.endsWith(","))
197
                    stylePr += " ";
198
            xml.putProperty("styles", stylePr);
199
        }
200
        
201
        // Dimensions 
202
        if (dimensions!=null){
203
            String dim = "";
204
            for (int i = 0; i < dimensions.size(); i++) {
205
                dim += (String) dimensions.get(i);
206
                if (i<dimensions.size()-1)
207
                    dim += ",";
208
            }
209
            if (dim.endsWith(","))
210
                    dim += " ";
211
            xml.putProperty("dimensions", dim);
212
        }
213
        
214
        // OnlineResources
215
        Iterator it = onlineResources.keySet().iterator();
216
        String strOnlines = "";
217
        while (it.hasNext()) {
218
                String key = (String) it.next();
219
                String value = (String) onlineResources.get(key);
220
                strOnlines = key+"~##SEP2##~"+value;
221
                if (it.hasNext())
222
                        strOnlines += "~##SEP1##~";
223
        }
224
        xml.putProperty("onlineResources", strOnlines);
225
        
226
        // Queryable
227
        xml.putProperty("queryable", queryable);
228
        
229
        // fixedSize
230
        if (isSizeFixed()) {
231
                xml.putProperty("fixedSize", true);
232
                xml.putProperty("fixedWidth", fixedSize.width);
233
                xml.putProperty("fixedHeight", fixedSize.height);
234
        }
235
        return xml;
236
        }
237

    
238
        /**
239
         * A partir del XMLEntity reproduce la capa.
240
         *
241
         * @param xml XMLEntity
242
         *
243
         * @throws XMLException
244
         * @throws DriverException
245
         * @throws DriverIOException
246
         */
247
        public void setXMLEntity03(XMLEntity xml)
248
                throws XMLException {
249
                super.setXMLEntity(xml);
250
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
251
                                        "fullExtent"));
252

    
253
                try {
254
                        host = new URL(xml.getStringProperty("host"));
255
                } catch (MalformedURLException e) {
256
                        throw new XMLException(e);
257
                }
258

    
259
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
260
                layerQuery = xml.getStringProperty("layerQuery");
261
                m_Format = xml.getStringProperty("format");
262
                m_SRS = xml.getStringProperty("srs");
263
        }
264

    
265
        /**
266
         * A partir del XMLEntity reproduce la capa.
267
         *
268
         * @param xml XMLEntity
269
         *
270
         * @throws XMLException
271
         * @throws DriverException
272
         * @throws DriverIOException
273
         */
274
        public void setXMLEntity(XMLEntity xml)
275
                throws XMLException {
276
                super.setXMLEntity(xml);
277
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty(
278
                                        "fullExtent"));
279
                
280
                // Host
281
                try {
282
                        host = new URL(xml.getStringProperty("host"));
283
                } catch (MalformedURLException e) {
284
                        throw new XMLException(e);
285
                }
286

    
287
                // Part of the query that is not the host, or the
288
                // layer names, or other not listed bellow
289
                infoLayerQuery = xml.getStringProperty("infoLayerQuery");
290

    
291
                // Part of the query containing the layer names
292
                layerQuery = xml.getStringProperty("layerQuery");
293
                
294
                // Format
295
                m_Format = xml.getStringProperty("format");
296
                
297
                // SRS
298
                m_SRS = xml.getStringProperty("srs");
299
                
300
                String claseStr = StatusLayerRaster.defaultClass;
301
                if (xml.contains("raster.class")) {
302
                        claseStr = xml.getStringProperty("raster.class");
303
                }
304
                
305
                // Transparency
306
        if (xml.contains("wms_transparency"))
307
            wmsTransparency = xml.getBooleanProperty("wms_transparency");
308
        
309
        // Styles
310
        if (xml.contains("styles")){
311
            styles = new Vector();
312
            String[] stl = xml.getStringProperty("styles").split(",");
313
            
314
            for (int i = 0; i < stl.length; i++) {
315
                    if (stl[i].equals(" "))
316
                            stl[i]="";
317
                styles.add(stl[i]);
318
            }
319
        }
320
        
321
        // Dimensions
322
        if (xml.contains("dimensions")){
323
            dimensions = new Vector();
324
            String[] dims = xml.getStringProperty("dimensions").split(",");
325
            for (int i = 0; i < dims.length; i++){
326
                    if (dims[i].equals(" "))
327
                            dims[i]="";
328
                
329
                dimensions.add(dims[i]);
330
            }
331
        }
332
        
333
        // OnlineResources
334
        if (xml.contains("onlineResources")) {
335
                String[] operations = xml.getStringProperty("onlineResources").split("~##SEP1##~");
336
                for (int i = 0; i < operations.length; i++) {
337
                                String[] resources = operations[i].split("~##SEP2##~");
338
                                if (resources.length==2 && resources[1]!="")
339
                                        onlineResources.put(resources[0], resources[1]);
340
                        }
341
        }
342
        
343
        // Queryable
344
        queryable = true; // let's assume that the layer is queryable by default
345
        if (xml.contains("queryable"))
346
                queryable = xml.getBooleanProperty("queryable");
347
        
348
        // fixedSize
349
        if (xml.contains("fixedSize")) {
350
                fixedSize = new Dimension(xml.getIntProperty("fixedWidth"), 
351
                                                  xml.getIntProperty("fixedHeight"));
352
        }
353
        
354
                if(status!=null)
355
                        status.setXMLEntity(xml, this);
356
                else{
357
                        if(claseStr!=null && !claseStr.equals("")){
358
                                try{
359
                                        Class clase = Class.forName(claseStr);
360
                                        Constructor constr = clase.getConstructor(null);
361
                                        status = (StatusRasterInterface)constr.newInstance(null);
362
                                        if(status!=null)
363
                                                status.setXMLEntity(xml, this);
364
                                }catch(ClassNotFoundException exc){
365
                                        exc.printStackTrace();
366
                                }catch(InstantiationException exc){
367
                                        exc.printStackTrace();
368
                                }catch(IllegalAccessException exc){
369
                                        exc.printStackTrace();
370
                                }catch(NoSuchMethodException exc){
371
                                        exc.printStackTrace();
372
                                }catch(InvocationTargetException exc){
373
                                        exc.printStackTrace();
374
                                }                                        
375
                        }
376
                }
377
                firstLoad = true;
378
        }
379

    
380
        /**
381
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(com.iver.cit.gvsig.fmap.operations.QueriedPoint)
382
         */
383
        public String queryByPoint(Point p) throws DriverException {
384
                try {
385
                        if (queryable)
386
                        {
387
                                //TODO
388
                                // check if there are layers which are not queryable
389
                                ViewPort viewPort = getFMap().getViewPort();
390

    
391
                                Point tiledPoint = new Point((int) p.getX() % maxTilePrintWidth, (int) p.getY() % maxTilePrintHeight);
392
                                Rectangle rect = new Rectangle(0, 0, viewPort.getImageWidth() - 1, viewPort.getImageHeight() - 1);
393
                                Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, rect);
394
                                tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
395
                                int nCols = tiles.getNumCols();
396

    
397
                                int col = (int) p.getX() / maxTilePrintWidth;
398
                                int row = (int) p.getY() / maxTilePrintHeight;
399
                                int tileIndex = (row*nCols) + col;
400
                                
401
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileIndex);
402
                                wmsStatus.setExtent(vp.getExtent());
403
                                wmsStatus.setHeight(vp.getImageHeight());
404
                                wmsStatus.setWidth(vp.getImageWidth());
405
                                wmsStatus.setOnlineResource((String) onlineResources.get("GetFeatureInfo"));
406
                                return new String(getDriver()
407
                                                .getFeatureInfo(wmsStatus, (int) tiledPoint.getX(), (int) tiledPoint.getY(), Integer.MAX_VALUE));
408
                        }
409
                        else
410
                        {
411
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
412
                                                PluginServices.getText(this, "wms_not_queryable"));
413
                                return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><info></info>";
414
                        }
415
                } catch (WMSException  e) {
416
                        return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><exception>" +
417
                        e.getMessage() + "</exception>";
418
                } catch (ValidationException e) {
419
                        throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
420
                } catch (UnsupportedVersionException e) {
421
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
422
                } catch (IOException e) {
423
                        throw new DriverException(PluginServices.getText(this, "connect_error"), e);
424
                } catch (NoninvertibleTransformException e) {
425
                        NotificationManager.addError("NotinvertibleTransform", e);
426
                }
427
                return null;
428
        }
429

    
430
        /**
431
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
432
         */
433
        public Rectangle2D getFullExtent() {
434
                return fullExtent;
435
        }
436

    
437
        /**
438
         * 
439
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
440
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
441
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
442
         */
443
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
444
                        Cancellable cancel,double scale) throws DriverException {
445
                
446
                if (isWithinScale(scale)){
447
                        Point2D p = viewPort.getOffset();
448
                        // p will be (0, 0) when drawing a view or other when painting onto
449
                        // the Layout.
450
                        visualStatus.width =  viewPort.getImageWidth();
451
                        visualStatus.height =  viewPort.getImageHeight();
452
                        visualStatus.minX = viewPort.getAdjustedExtent().getMinX();
453
                        visualStatus.minY = viewPort.getAdjustedExtent().getMinY();
454
                        visualStatus.maxX = viewPort.getAdjustedExtent().getMaxX();
455
                        visualStatus.maxY = viewPort.getAdjustedExtent().getMaxY();
456
                        if (isSizeFixed()) {
457
                                // This condition handles those situations in which the server can
458
                                // only give static extent and resolution maps despite we need
459
                                // a specific BBOX and pixel WIDTH and HEIGHT
460
                                drawFixedSize(g, viewPort, cancel);
461

    
462
                        } else {
463
                                Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), viewPort.getImageWidth() - 1, viewPort.getImageHeight() - 1);
464
                                Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, r);
465
                                tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
466
                                for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
467
                                        // drawing part
468
                                        try {
469
                                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
470
                                                drawTile(g, vp, cancel);
471
                                        } catch (NoninvertibleTransformException e) {
472
                                                e.printStackTrace();
473
                                        }
474
                                }
475
                        }
476
                }
477
                Runtime r = Runtime.getRuntime();
478
                long mem = r.totalMemory() - r.freeMemory();
479
                System.err.println("Memoria total: " + (mem / 1024) +"KB");
480
        }
481
        
482
        private void drawFixedSize(Graphics2D g, ViewPort vp, Cancellable cancel) throws DriverException {
483
                // This is the extent that will be requested
484
                Rectangle2D bBox = getFullExtent();
485
                
486
                try {                        
487
                        wmsStatus.setExtent( bBox );
488
                        wmsStatus.setFormat( m_Format );
489
                        wmsStatus.setHeight( fixedSize.height );
490
                        wmsStatus.setWidth( fixedSize.width );
491
                        wmsStatus.setLayerNames(Utilities.createVector(layerQuery,","));
492
                        wmsStatus.setSrs(m_SRS);
493
                        wmsStatus.setStyles(styles);
494
                        wmsStatus.setDimensions(dimensions);
495
                        wmsStatus.setTransparency(wmsTransparency);
496
                        wmsStatus.setOnlineResource((String) onlineResources.get("GetMap"));
497
                        
498
                        File f = getDriver().getMap(wmsStatus);
499
                        String nameWorldFile = f.getPath() + getExtensionWorldFile();
500
                        com.iver.andami.Utilities.createTemp(nameWorldFile, this.getDataWorldFile(bBox, fixedSize));
501
                        
502
                        if(status!=null && firstLoad){
503
                                status.applyStatus(this);
504
                                firstLoad = false;
505
                        }
506
                        
507
                        // And finally, obtain the extent intersecting the view and the BBox
508
                        // to draw to.
509
                        Rectangle2D extent = new Rectangle2D.Double();
510
                        Rectangle2D.intersect(vp.getAdjustedExtent(), bBox, extent);
511
                        
512
                        ViewPortData vpData = new ViewPortData(
513
                                vp.getProjection(), new Extent(extent), fixedSize );
514
                        vpData.setMat(vp.getAffineTransform());
515

    
516
                        rasterProcess(g, vpData, f);
517
                        
518
                } catch (ValidationException e) {
519
                        throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
520
                } catch (UnsupportedVersionException e) {
521
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
522
                } catch (IOException e) {
523
                        throw new DriverException(PluginServices.getText(this, "connect_error"), e);
524
                } catch (WMSException e) {
525
            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
526
                        this.setVisible(false);
527
                }
528
                
529
                
530
        }
531
        
532
        /**
533
         * This is the method used to draw a tile in a WMS mosaic layer.
534
         */
535
        private void drawTile(Graphics2D g, ViewPort vp, Cancellable cancel) throws DriverException {
536

    
537
                // Compute the query geometry 
538
                // 1. Check if it is within borders
539
                Rectangle2D extent = getFullExtent();
540
        if ((vp.getExtent().getMinX() > extent.getMaxX()) ||
541
                (vp.getExtent().getMinY() > extent.getMaxY()) ||
542
                (vp.getExtent().getMaxX() < extent.getMinX()) ||
543
                (vp.getExtent().getMaxY() < extent.getMinY())) {
544
            return;
545
        }
546
        
547
        // 2. Compute extent to be requested.
548
        Rectangle2D bBox = new Rectangle2D.Double();
549
        Rectangle2D.intersect(vp.getExtent(), extent, bBox);
550
        
551
        // 3. Compute size in pixels
552
        double scalex = vp.getAffineTransform().getScaleX(); 
553
        double scaley = vp.getAffineTransform().getScaleY(); 
554
        int wImg = (int) Math.ceil(Math.abs(bBox.getWidth() * scalex) + 1);
555
        int hImg = (int) Math.ceil(Math.abs(bBox.getHeight() * scaley) + 1);
556
        Dimension sz = new Dimension(wImg, hImg);
557

    
558
        if ((wImg <= 0) || (hImg <= 0)) {
559
            return;
560
        }
561
                
562
                try {                        
563
                        wmsStatus.setExtent( bBox );
564
                        wmsStatus.setFormat(m_Format);
565
                        wmsStatus.setHeight( hImg );
566
                        wmsStatus.setWidth( wImg );
567
                        wmsStatus.setLayerNames(Utilities.createVector(layerQuery,","));
568
                        wmsStatus.setSrs(m_SRS);
569
                        wmsStatus.setStyles(styles);
570
                        wmsStatus.setDimensions(dimensions);
571
                        wmsStatus.setTransparency(wmsTransparency);
572
                        wmsStatus.setOnlineResource((String) onlineResources.get("GetMap"));
573
                        
574
                        File f = getDriver().getMap(wmsStatus);
575
                        String nameWordFile = f.getPath() + getExtensionWorldFile();
576
                        com.iver.andami.Utilities.createTemp(nameWordFile, this.getDataWorldFile(bBox, sz));
577
                        
578
                        if(status!=null && firstLoad){
579
                                status.applyStatus(this);
580
                                firstLoad = false;
581
                        }
582
                        ViewPortData vpData = new ViewPortData(
583
                                vp.getProjection(), new Extent(bBox), sz );
584
                        vpData.setMat(vp.getAffineTransform());
585

    
586
                        rasterProcess(g, vpData, f);
587
                        
588
                } catch (ValidationException e) {
589
                        throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
590
                } catch (UnsupportedVersionException e) {
591
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
592
                } catch (IOException e) {
593
                        throw new DriverException(PluginServices.getText(this, "connect_error"), e);
594
                } catch (WMSException e) {
595
            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
596
                        this.setVisible(false);
597
                }
598
                
599
        }
600

    
601
        /**
602
         * Obtiene la extensi?n del fichero de georreferenciaci?n
603
         * @return String con la extensi?n del fichero de georreferenciaci?n dependiendo
604
         * del valor del formato obtenido del servidor. Por defecto asignaremos un .wld 
605
         */
606
        private String getExtensionWorldFile(){
607
                String extWorldFile = ".wld";
608
            if(m_Format.equals("image/tif") || m_Format.equals("image/tiff"))
609
                    extWorldFile = ".tfw";
610
            if(m_Format.equals("image/jpeg"))
611
                    extWorldFile = ".jpgw";
612
            return extWorldFile;
613
        }
614
        
615
        /**
616
         * Calcula el contenido del fichero de georreferenciaci?n de una imagen.
617
         * @param bBox Tama?o y posici?n de la imagen (en coordenadas de usuario)
618
         * @param sz Tama?o de la imagen en pixeles.
619
         * @return el 'WorldFile', como String.
620
         * @throws IOException
621
         */
622
        public String getDataWorldFile(Rectangle2D bBox, Dimension sz) throws IOException {
623
                StringBuffer data = new StringBuffer();
624
            data.append((bBox.getMaxX() - bBox.getMinX())/(sz.getWidth() - 1)+"\n");
625
            data.append("0.0\n");
626
            data.append("0.0\n");
627
            data.append((bBox.getMaxY() - bBox.getMinY())/(sz.getHeight() - 1)+"\n");
628
            data.append(""+bBox.getMinX()+"\n");
629
            data.append(""+bBox.getMinY()+"\n");
630
            return data.toString();
631
        }
632
                
633
        /**
634
         * Dibuja una imagen usando PxRaster
635
         * @param g        Graphics2D en el que hay que dibujar.
636
         * @param vpData Par?metros de visualizaci?n
637
         * @param file La imagen en cuesti?n.
638
         */
639
        private void rasterProcess(Graphics2D g, ViewPortData vpData, File file) {
640
                
641
                //Creamos el PxRaster        
642
                rasterFile = new GdalFile(vpData.getProjection(), file.getAbsolutePath());
643
                raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
644
                
645
                //Recuperamos la pila de filtros si ya hubiese sido cargado antes
646
                if(this.filterStack!=null)
647
                        raster.filterStack = this.filterStack;
648
                
649
                raster.setTransparency(false);
650
                                                
651
                //Asignamos transparencia y orden de bandas
652
                if(this.transparency==-1 && !firstLoad);
653
                else
654
                        raster.setTransparency(this.transparency);
655
                
656
                raster.setBand(GeoRasterFile.RED_BAND,rband);
657
                raster.setBand(GeoRasterFile.GREEN_BAND, gband);
658
                raster.setBand(GeoRasterFile.BLUE_BAND, bband);
659
        
660
                //Despues del primer pxRaster asignamos el stackManager guardado para los siguientes.
661
                //Con esto conseguimos asignar los cambios que se hayan producido desde el cuadro de 
662
                //propiedades cuando creamos un nuevo pxRaster
663
                if(this.stackManager != null)
664
                        raster.setStackManager(this.stackManager); 
665

    
666
                raster.draw(g, vpData);
667
                
668
                //En el primer pxRaster de una imagen obtenemos el Stack Manager para poder modificarlo
669
                //si queremos desde las propiedades
670
                
671
                if(this.stackManager == null)
672
                        this.stackManager = raster.getStackManager(); 
673
                
674
                if(this.filterStack == null)
675
                        this.filterStack = raster.filterStack;
676
                
677
                rasterFile.close();
678
        }
679
        
680
        /**
681
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
682
         *                 com.iver.cit.gvsig.fmap.ViewPort,
683
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
684
         */
685
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
686
                throws DriverException {
687
                if (isVisible() && isWithinScale(scale)){        
688
                isPrinting = true;
689
                if (!mustTilePrint) {
690
                        draw(null, g, viewPort, cancel,scale);
691
                } else {
692
                // Para no pedir imagenes demasiado grandes, vamos
693
                // a hacer lo mismo que hace EcwFile: chunkear.
694
                // Llamamos a drawView con cuadraditos m?s peque?os
695
                // del BufferedImage ni caso, cuando se imprime viene con null
696
                        
697
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
698
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
699
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
700
                            // Parte que dibuja
701
                            try {
702
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
703
                                drawTile(g, vp, cancel);
704
                                } catch (NoninvertibleTransformException e) {
705
                                        e.printStackTrace();
706
                                }
707
                }
708
                }
709
            isPrinting = false;
710
                }
711
        }
712
        
713
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
714
                throws DriverException {
715
                draw(null, g, viewPort, cancel,scale);
716
        }
717

    
718
        /**
719
         * Devuelve el FMapWMSDriver.
720
         *
721
         * @return FMapWMSDriver
722
         *
723
         * @throws IllegalStateException
724
         * @throws ValidationException
725
         * @throws UnsupportedVersionException
726
         * @throws IOException
727
         */
728
        private FMapWMSDriver getDriver()
729
                throws IllegalStateException, ValidationException, 
730
                        UnsupportedVersionException, IOException {
731
                if (wms == null) {
732
                        //wmsClient = WMSClientFactory.getClient(host);
733
                        wms = new FMapWMSDriver();
734
            wms.createClient(host);
735
            
736
                }
737

    
738
                return wms;
739
        }
740
        
741
        /**
742
         * Devuelve el FMapWMSDriver.
743
         *
744
         * @return FMapWMSDriver
745
         *
746
         * @throws IllegalStateException
747
         * @throws ValidationException
748
         * @throws UnsupportedVersionException
749
         * @throws IOException
750
         */
751
        public void setDriver(FMapWMSDriver drv) {
752
                wms = drv;
753
        }
754

    
755
        /**
756
         * Devuelve el URL.
757
         *
758
         * @return URL.
759
         */
760
        public URL getHost() {
761
                return host;
762
        }
763

    
764
        /**
765
         * Inserta el URL.
766
         *
767
         * @param host URL.
768
         */
769
        public void setHost(URL host) {
770
                this.host = host;
771
        }
772

    
773
        /**
774
         * Devuelve la informaci?n de la consulta.
775
         *
776
         * @return String.
777
         */
778
        public String getInfoLayerQuery() {
779
                return infoLayerQuery;
780
        }
781

    
782
        /**
783
         * Inserta la informaci?n de la consulta.
784
         *
785
         * @param infoLayerQuery String.
786
         */
787
        public void setInfoLayerQuery(String infoLayerQuery) {
788
                this.infoLayerQuery = infoLayerQuery;
789
        }
790

    
791
        /**
792
         * Devuelve la consulta.
793
         *
794
         * @return String.
795
         */
796
        public String getLayerQuery() {
797
                return layerQuery;
798
        }
799

    
800
        /**
801
         * Inserta la consulta.
802
         *
803
         * @param layerQuery consulta.
804
         */
805
        public void setLayerQuery(String layerQuery) {
806
                this.layerQuery = layerQuery;
807
        }
808

    
809
        /**
810
         * Devuelve el formato.
811
         *
812
         * @return Formato.
813
         */
814
        public String getFormat() {
815
                return m_Format;
816
        }
817

    
818
        /**
819
         * Inserta el formato.
820
         *
821
         * @param format Formato.
822
         */
823
        public void setFormat(String format) {
824
                m_Format = format;
825
        }
826

    
827
        /**
828
         * Devuelve el SRS.
829
         *
830
         * @return SRS.
831
         */
832
        public String getSRS() {
833
                return m_SRS;
834
        }
835

    
836
        /**
837
         * Inserta el SRS.
838
         *
839
         * @param m_srs SRS.
840
         */
841
        public void setSRS(String m_srs) {
842
                m_SRS = m_srs;
843
        }
844

    
845
        /**
846
         * Inserta la extensi?n total de la capa.
847
         *
848
         * @param fullExtent Rect?ngulo.
849
         */
850
        public void setFullExtent(Rectangle2D fullExtent) {
851
                this.fullExtent = fullExtent;
852
        }
853
        
854
        public HashMap getProperties() {
855
                HashMap info = new HashMap();
856
        String[] layerNames = getLayerQuery().split(",");
857
        Vector layers = new Vector(layerNames.length);
858
        try {
859
            if(getDriver().connect()){
860
                for (int i = 0; i < layerNames.length; i++) {
861
                    layers.add(i, wms.getLayer(layerNames[i]));
862
                }
863
                info.put("name", getName());
864
                info.put("selectedLayers", layers);
865
                info.put("host", getHost());
866
                info.put("srs", getSRS());
867
                info.put("format", getFormat());
868
                info.put("wmsTransparency", new Boolean(wmsTransparency));
869
                info.put("styles", styles);
870
                info.put("dimensions", dimensions);
871
                info.put("fixedSize", fixedSize);
872
                return info;
873
            }
874
        } catch (Exception e) {
875
            e.printStackTrace();
876
        }
877
        return null;
878
        }
879
        
880
        /**
881
         * Asignar el estado del raster
882
         * @param status
883
         */
884
        public void setStatus(StatusRasterInterface status){
885
                this.status = status;
886
        }
887
        
888
        /**
889
         * Obtiene el estado del raster
890
         * @return
891
         */
892
        public StatusRasterInterface getStatus(){
893
                return this.status;
894
        }
895
        
896
        /* (non-Javadoc)
897
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getAttributes()
898
         */
899
        public ArrayList getAttributes() {
900
                if(rasterFile != null){
901
                        ArrayList attr = new ArrayList();
902
/*                        String dataType = "Byte";
903
                        if (rasterFile.getDataType() == DataBuffer.TYPE_BYTE) dataType = "Byte";
904
                        else if (rasterFile.getDataType() == DataBuffer.TYPE_SHORT)
905
                                dataType = "Short";
906
                        else if (rasterFile.getDataType() == DataBuffer.TYPE_USHORT)
907
                                dataType = "Unsigned Short";
908
                        else if (rasterFile.getDataType() == DataBuffer.TYPE_INT)
909
                                dataType = "Integer";
910
                        else if (rasterFile.getDataType() == DataBuffer.TYPE_FLOAT)
911
                                dataType = "Float";
912
                        else if (rasterFile.getDataType() == DataBuffer.TYPE_DOUBLE)
913
                                dataType = "Double";
914
                        else
915
                                dataType = "Unknown";
916
*/
917
                        Object [][] a = {
918
                                {"Filename",rasterFile.getName().substring(rasterFile.getName().lastIndexOf("/")+1, rasterFile.getName().length())},
919
                                {"Filesize",new Long(rasterFile.getFileSize())},
920
                                {"Width",new Integer(rasterFile.getWidth())},
921
                                {"Height", new Integer(rasterFile.getHeight())},
922
                                {"Bands", new Integer(rasterFile.getBandCount())}
923
                                //{"BandDataType", dataType}
924
                        };
925
                        for (int i=0; i<a.length; i++){
926
                                System.out.println("===> "+a[i][0]+" "+a[i][1]);
927
                                attr.add(a[i]);
928
                        }
929
                        return attr;
930
                }
931
                return  null;
932
        }
933
        
934
        /* (non-Javadoc)
935
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getFilterStack()
936
         */
937
        public RasterFilterStack getFilterStack() {
938
                if(raster!=null)
939
                        return raster.filterStack;
940
                return null;
941
        }
942
        /* (non-Javadoc)
943
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getHeight()
944
         */
945
        public double getHeight() {
946
                return visualStatus.height;
947
        }
948
        
949
        /* (non-Javadoc)
950
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxX()
951
         */
952
        public double getMaxX() {
953
                return visualStatus.maxX;
954
        }
955
        
956
        /* (non-Javadoc)
957
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxY()
958
         */
959
        public double getMaxY() {
960
                return visualStatus.maxY;
961
        }
962
        
963
        /* (non-Javadoc)
964
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinX()
965
         */
966
        public double getMinX() {
967
                return visualStatus.minX;
968
        }
969
        
970
        /* (non-Javadoc)
971
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinY()
972
         */
973
        public double getMinY() {
974
                return visualStatus.minY;
975
        }
976
        /* (non-Javadoc)
977
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getPixel(double, double)
978
         */
979
        public int[] getPixel(double wcx, double wcy) {
980
                if(getPxRaster() != null)
981
                        return getPxRaster().getPixel(wcx, wcy);
982
        return null;
983
        }
984
        /* (non-Javadoc)
985
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getSource()
986
         */
987
        public RasterAdapter getSource() {
988
                return null;
989
        }
990
        /* (non-Javadoc)
991
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getWidth()
992
         */
993
        public double getWidth() {
994
                return visualStatus.width;
995
        }
996
        /* (non-Javadoc)
997
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setBand(int, int)
998
         */
999
        public void setBand(int flag, int nBand) {
1000
                switch(flag){
1001
                case GeoRasterFile.RED_BAND:setBandR(nBand);break;
1002
                case GeoRasterFile.GREEN_BAND:setBandG(nBand);break;
1003
                case GeoRasterFile.BLUE_BAND:setBandB(nBand);break;
1004
                }
1005
        }
1006
        /* (non-Javadoc)
1007
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setFilterStack(org.cresques.io.raster.RasterFilterStack)
1008
         */
1009
        public void setFilterStack(RasterFilterStack stack) {
1010
                this.filterStack = stack;
1011
        }
1012
        /* (non-Javadoc)
1013
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPos(int, int)
1014
         */
1015
        public void setPos(int x, int y) {
1016
                this.posX = x;
1017
                this.posY = y;
1018
        }
1019
        
1020
        /* (non-Javadoc)
1021
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPosWC(double, double)
1022
         */
1023
        public void setPosWC(double x, double y) {
1024
                this.posXWC = x;
1025
                this.posYWC = y;
1026
        }
1027
        
1028
        /* (non-Javadoc)
1029
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setRGB(int, int, int)
1030
         */
1031
        public void setRGB(int r, int g, int b) {
1032
                this.r = r;
1033
                this.g = g;
1034
                this.b = b;
1035
        }
1036
        /* (non-Javadoc)
1037
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setSource(com.iver.cit.gvsig.fmap.layers.RasterAdapter)
1038
         */
1039
        public void setSource(RasterAdapter ra) {
1040
        }
1041
        /**
1042
         * @return Returns the raster.
1043
         */
1044
        public PxRaster getPxRaster() {
1045
                return raster;
1046
        }
1047
        /**
1048
         * @return Returns the rasterFile.
1049
         */
1050
        public GeoRasterFile getGeoRasterFile() {
1051
                return rasterFile;
1052
        }
1053
        
1054
        public void setTransparency(int trans) {
1055
                this.transparency = trans;
1056
        }
1057
        
1058
        /**
1059
         * Sets the R-band.
1060
         * 
1061
         * Asigna la banda R.
1062
         * @param r
1063
         */
1064
        public void setBandR(int r){
1065
                this.rband = r;
1066
        }
1067
        
1068
        /**
1069
         * Sets the G-band.
1070
         * 
1071
         * Asigna la banda G
1072
         * @param g
1073
         */
1074
        public void setBandG(int g){
1075
                this.gband = g;
1076
        }
1077
        
1078
        /**
1079
         * Sets the B-band.
1080
         * 
1081
         * Asigna la banda B
1082
         * @param b
1083
         */
1084
        public void setBandB(int b){
1085
                this.bband = b;
1086
        }
1087

    
1088
    /**
1089
     * @return Returns the wmsTransparency.
1090
     */
1091
    public boolean isWmsTransparent() {
1092
        return wmsTransparency;
1093
    }
1094

    
1095
    /**
1096
     * @param wmsTransparency The wmsTransparency to set.
1097
     */
1098
    public void setWmsTransparency(boolean wmsTransparency) {
1099
        this.wmsTransparency = wmsTransparency;
1100
    }
1101

    
1102
     /**
1103
     * @param styles
1104
     */
1105
    public void setStyles(Vector styles) {
1106
            //laura:
1107
            //layer query is built with the layer in reverse order
1108
            // so here we build the styles upside-down.
1109
            if (styles != null)
1110
            {
1111
                    this.styles = new Vector();
1112
                    for(int i = styles.size()-1; i>=0; i--)
1113
                    {
1114
                            this.styles.add(styles.elementAt(i));
1115
                    }
1116
            }
1117
    }
1118
    
1119
    /**
1120
     * Sets the dimension vector that is a list of key-value pairs containing
1121
     * the name of the dimension and the value for it
1122
     * @param dimensions
1123
     */
1124
    public void setDimensions(Vector dimensions) {
1125
        this.dimensions = dimensions;
1126
    }
1127

    
1128
    /**
1129
     * Sets the set of URLs that should be accessed for each operation performed
1130
     * to the server.
1131
     * 
1132
     * @param onlineResources
1133
     */
1134
        public void setOnlineResources(Hashtable onlineResources) {
1135
                this.onlineResources = onlineResources;
1136
        }
1137
        
1138
        /**
1139
         * When a server is not fully featured and it only can serve constant map
1140
         * sizes this value must be set. It expresses the size in pixels (width, height)
1141
         * that the map will be requested.
1142
         * @param Dimension sz
1143
         */
1144
        public void setFixedSize(Dimension sz) {
1145
                fixedSize = sz;
1146
        }
1147
        
1148
        /**
1149
         * Tells whether if this layer must deal with the server with the constant-size
1150
         * limitations or not.
1151
         * @return boolean.
1152
         */
1153
        private boolean isSizeFixed() {
1154
                return fixedSize != null && fixedSize.getWidth() > 0 && fixedSize.getHeight() > 0;
1155
        }
1156

    
1157
        /**
1158
         * If it is true, this layer accepts GetFeatureInfo operations. This WMS operations
1159
         * maps to FMap's infoByPoint(p) operation.
1160
         * @param b
1161
         */
1162
        public void setQueryable(boolean b) {
1163
                queryable = b;
1164
        }
1165
        
1166
}