Statistics
| Revision:

root / trunk / extensions / extWCS / src / com / iver / cit / gvsig / fmap / layers / FLyrWCS.java @ 24160

History | View | Annotate | Download (36.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.Dimension;
44
import java.awt.Graphics2D;
45
import java.awt.Point;
46
import java.awt.Rectangle;
47
import java.awt.geom.AffineTransform;
48
import java.awt.geom.NoninvertibleTransformException;
49
import java.awt.geom.Point2D;
50
import java.awt.geom.Rectangle2D;
51
import java.awt.image.BufferedImage;
52
import java.awt.image.DataBuffer;
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.Hashtable;
61
import java.util.Iterator;
62
import java.util.Map;
63

    
64
import javax.print.attribute.PrintRequestAttributeSet;
65
import javax.swing.ImageIcon;
66

    
67
import org.exolab.castor.xml.ValidationException;
68
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
69
import org.gvsig.fmap.raster.layers.IRasterLayerActions;
70
import org.gvsig.fmap.raster.layers.IStatusRaster;
71
import org.gvsig.fmap.raster.layers.StatusLayerRaster;
72
import org.gvsig.raster.dataset.CompositeDataset;
73
import org.gvsig.raster.dataset.IBuffer;
74
import org.gvsig.raster.dataset.MosaicNotValidException;
75
import org.gvsig.raster.dataset.MultiRasterDataset;
76
import org.gvsig.raster.dataset.NotSupportedExtensionException;
77
import org.gvsig.raster.dataset.io.RasterDriverException;
78
import org.gvsig.raster.datastruct.ColorTable;
79
import org.gvsig.raster.datastruct.Extent;
80
import org.gvsig.raster.datastruct.ViewPortData;
81
import org.gvsig.raster.grid.GridTransparency;
82
import org.gvsig.raster.grid.filter.FilterTypeException;
83
import org.gvsig.raster.grid.filter.RasterFilterList;
84
import org.gvsig.raster.grid.filter.RasterFilterListManager;
85
import org.gvsig.raster.grid.filter.enhancement.LinearEnhancementFilter;
86
import org.gvsig.raster.grid.filter.statistics.TailTrimFilter;
87
import org.gvsig.remoteClient.wcs.WCSStatus;
88
import org.gvsig.remoteClient.wms.ICancellable;
89

    
90
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
91
import com.hardcode.gdbms.engine.data.driver.DriverException;
92
import com.iver.cit.gvsig.exceptions.layers.ConnectionErrorLayerException;
93
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
94
import com.iver.cit.gvsig.exceptions.layers.UnsupportedVersionLayerException;
95
import com.iver.cit.gvsig.fmap.MapControl;
96
import com.iver.cit.gvsig.fmap.ViewPort;
97
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
98
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
99
import com.iver.cit.gvsig.fmap.drivers.wcs.FMapWCSDriver;
100
import com.iver.cit.gvsig.fmap.drivers.wcs.FMapWCSDriverFactory;
101
import com.iver.cit.gvsig.fmap.drivers.wcs.WCSDriverException;
102
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
103
import com.iver.utiles.StringUtilities;
104
import com.iver.utiles.XMLEntity;
105
import com.iver.utiles.swing.threads.Cancellable;
106

    
107

    
108
/**
109
 * Class for the WCS layer.
110
 *
111
 * Capa para el WCS.
112
 *
113
 * Las capas WCS son tileadas para descargarlas del servidor. Esto quiere decir que
114
 * est?n formadas por multiples ficheros raster. Por esto la fuente de datos raster (IRasterDatasource)
115
 * de la capa FLyrWCS es un objeto de tipo CompositeDataset. Este objeto est? compuesto por un array
116
 * bidimensional de MultiRasterDataset. Cada uno de los MultiRasterDataset corresponde con un tile
117
 * salvado en disco. Estos MultiRasterDataset se crean cada vez que se repinta ya que en WCS a cada
118
 * zoom varian los ficheros fuente. La secuencia de creaci?n de un CompositeDataset ser?a la siguiente:
119
 * <UL>
120
 * <LI>Se hace una petici?n de dibujado por parte del usuario llamando al m?todo draw de FLyrWCS</LI>
121
 * <LI>Se tilea la petici?n</LI>
122
 * <LI>Cada tile se dibuja abriendo una FLyrRaster para ese tile</LI>
123
 * <LI>Si es el primer dibujado se guarda una referencia en la capa WMS a las propiedades de renderizado, orden de bandas,
124
 * transparencia, filtros aplicados, ...</LI>
125
 * <LI>Si no es el primer dibujado se asignan las propiedades de renderizado cuya referencia se guarda en la capa WMS</LI>
126
 * <LI>Se guarda el MultiRasterDataset de cada tile</LI>
127
 * <LI>Al acabar todos los tiles creamos un CompositeDataset con los MultiRasterDataset de todos los tiles</LI>
128
 * <LI>Asignamos a la capa la referencia de las propiedades de renderizado que tenemos almacenadas. De esta forma si hay
129
 * alguna modificaci?n desde el cuadro de propiedades ser? efectiva sobre los tiles que se dibujan.</LI>
130
 * </UL>
131
 *
132
 * @author jaume - jaume.dominguez@iver.es
133
 */
134
public class FLyrWCS extends FLyrRasterSE {
135
        private FMapWCSDriver wcs = null;
136

    
137
        private URL                                                 host;
138
        private String                                                coverageName;
139
        private Rectangle2D                                        fullExtent;
140
        private String                                                format;
141
        private String                                                srs;
142
        private String                                                time;
143
        private String                                                parameter;
144
        private Point2D                                                maxRes;
145
        private Hashtable                                         onlineResources = new Hashtable();
146

    
147
        private WCSStatus                                        wcsStatus = new WCSStatus();
148

    
149
        private int                                                 posX = 0, posY = 0;
150
        private double                                                 posXWC = 0, posYWC = 0;
151
        private int                                                 r = 0, g = 0, b = 0;
152
        private boolean                                         firstLoad = false;
153
        private VisualStatus                                visualStatus = new VisualStatus();
154

    
155
        private boolean                                         mustTileDraw = false;
156
        private int                                                 maxTileDrawWidth  = 1023;
157
        private int                                                        maxTileDrawHeight = 1023;
158
        //private int                                                 maxTilePrintWidth  = 250;
159
        //private int                                                        maxTilePrintHeight = 250;
160
        /**
161
         * Lista de filtros aplicada en la renderizaci?n
162
         */
163
        private RasterFilterList            filterList = null;
164
        private GridTransparency                        transparency = null;
165
        private int[]                       renderBands = null;
166
        private FLyrRasterSE                                layerRaster = null;
167
        private ArrayList                   filterArguments = null;
168

    
169
        private class MyCancellable implements ICancellable
170
        {
171

    
172
                private Cancellable original;
173
                public MyCancellable(Cancellable cancelOriginal)
174
                {
175
                        this.original = cancelOriginal;
176
                }
177
                public boolean isCanceled() {
178
                        return original.isCanceled();
179
                }
180
                public Object getID() {
181
                        return this;
182
                }
183

    
184
        }
185

    
186
        public FLyrWCS(){
187
                super();
188
        }
189

    
190
        public FLyrWCS(Map args) throws DriverIOException{
191
                FMapWCSDriver drv = null;
192
                String host = (String)args.get("HOST");
193
                String sCoverage = (String) args.get((String) "COVERAGE");
194

    
195
                try {
196
                        this.setHost(new URL(host));
197
                } catch (MalformedURLException e) {
198
                        //e.printStackTrace();
199
                        throw new DriverIOException("Malformed host URL, '" + host + "' (" + e.toString() + ").");
200
                }
201
                try {
202
                        drv = this.getDriver();
203
                } catch (Exception e) {
204
                        // e.printStackTrace();
205
                        throw new DriverIOException("Can't get driver to host '" + host + "' (" + e.toString() + ").");
206
                }
207

    
208
                try{
209
                        if (!drv.connect(false, null)){
210
                                throw new DriverIOException("Can't connect to host '" + host + "'.");
211
                        }
212
                }catch(Exception e){
213
                        throw new DriverIOException("Can't connect to host '" + host + "'.");
214
                }
215

    
216
                WCSLayer wcsNode = drv.getLayer(sCoverage);
217

    
218
                if (wcsNode == null){
219
                        throw new DriverIOException("The server '" + host + "' doesn't has the coverage '" + sCoverage + "'.");
220
                }
221

    
222
                try{
223
                        this.setFullExtent(drv.getFullExtent(sCoverage,
224
                                        (String) args.get((String) "CRS")));
225
                        this.setFormat((String) args.get((String) "FORMAT"));
226
                        this.setParameter("BANDS=" + (String) args.get((String) "BANDS"));
227
                        this.setSRS((String) args.get((String) "CRS"));
228
                        this.setName(sCoverage);
229
                        this.setCoverageName(sCoverage);
230
                }catch (Exception e){
231
                        throw new DriverIOException("The server '" + host + "' is not able to load the coverage '" + sCoverage + "'.");
232
                }
233

    
234
        }
235

    
236
        /**
237
         * Clase que contiene los datos de visualizaci?n de WCS. Tiene datos que representan al
238
         * raster en la vista. Este raster puede estar compuesto por tiles por lo que valores
239
         * como el ancho total o el m?nimo o m?ximo deben ser calculados a partir de todos los
240
         * tiles visualizados.
241
         * @author Nacho Brodin (brodin_ign@gva.es)
242
         */
243
        private class VisualStatus {
244
                /**
245
                 * Ancho y alto de la imagen o del conjunto de tiles si los tiene. Coincide con
246
                 * el ancho y alto del viewPort
247
                 */
248
                private        int                                                        width = 0, height = 0;
249
                private double                                                minX = 0D, minY = 0D, maxX = 0D, maxY = 0D;
250
                private int                                                 bandCount = 0;
251
                private int                                                        dataType = DataBuffer.TYPE_UNDEFINED;
252

    
253
                /**
254
                 * Ancho y alto total del raster que ser? la suma de todos los tiles.
255
                 */
256
                private        int                                                        rasterWidth = 0, rasterHeight = 0;
257
                private        double                                                rasterMinX = Double.MAX_VALUE, rasterMinY = Double.MAX_VALUE;
258
                private        double                                                rasterMaxX = 0, rasterMaxY = 0;
259
                /**
260
                 * Lista de nombre de fichero que componen toda la visualizaci?n.
261
                 */
262
                private String[]                                        fileNames = null;
263
        }
264

    
265
        /**
266
         * @deprecated
267
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#getInfo
268
         */
269
        public String queryByPoint(Point p) {
270
                String data = "<file:"+getName().replaceAll("[^a-zA-Z0-9]","")+">\n";
271
                ArrayList attr = this.getAttributes();
272
                data += "  <raster\n";
273
                data += "    File=\""+getName()+"\"\n";
274
                for (int i=0; i<attr.size(); i++) {
275
                        Object [] a = (Object []) attr.get(i);
276

    
277
                        data += "    "+a[0].toString()+"=";
278
                        if (a[1].toString() instanceof String)
279
                                data += "\""+a[1].toString()+"\"\n";
280
                        else
281
                                data += a[1].toString()+"\n";
282
                }
283
                data += "    Point=\""+posX+" , "+posY+"\"\n";
284
                data += "    Point_WC=\""+posXWC+" , "+posYWC+"\"\n";
285
                data += "    RGB=\""+r+", "+g+", "+b+"\"\n";
286
                data += "  />\n";
287

    
288
                data += "</file:"+getName().replaceAll("[^a-zA-Z0-9]","")+">\n";
289
                System.out.println(data);
290
                return data;
291
        }
292

    
293
        /**
294
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#getInfo
295
         */
296
        public XMLItem[] getInfo(Point point, double tolerance, Cancellable cancel ) throws ReadDriverException {
297
                return super.getInfo(point, tolerance, cancel);
298
        }
299

    
300
        /*
301
         *  (non-Javadoc)
302
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
303
         */
304
        public Rectangle2D getFullExtent() {
305
                return fullExtent;
306
        }
307

    
308
        /*
309
         *  (non-Javadoc)
310
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.operations.Cancellable, double)
311
         */
312
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale) throws ReadDriverException {
313
                enableStopped();
314
                // callLegendChanged(null);
315

    
316
                if (isWithinScale(scale)) {
317
                        Point2D p = viewPort.getOffset();
318
                        // p will be (0, 0) when drawing a view or other when painting onto
319
                        // the Layout.
320
                        visualStatus.width = viewPort.getImageWidth();
321
                        visualStatus.height = viewPort.getImageHeight();
322
                        visualStatus.minX = viewPort.getAdjustedExtent().getMinX();
323
                        visualStatus.minY = viewPort.getAdjustedExtent().getMinY();
324
                        visualStatus.maxX = viewPort.getAdjustedExtent().getMaxX();
325
                        visualStatus.maxY = viewPort.getAdjustedExtent().getMaxY();
326
                        visualStatus.rasterWidth = 0;
327
                        visualStatus.rasterHeight = 0;
328
                        visualStatus.rasterMinX = Double.MAX_VALUE;
329
                        visualStatus.rasterMinY = Double.MAX_VALUE;
330
                        visualStatus.rasterMaxX = 0;
331
                        visualStatus.rasterMaxY = 0;
332
                        visualStatus.fileNames = new String[1];
333

    
334
                        try {
335
                                if (true) {
336
                                        if (viewPort.getImageWidth() <= maxTileDrawWidth && viewPort.getImageHeight() <= maxTileDrawHeight) {
337
                                                drawTile(g, viewPort, cancel, 0, scale);
338
                                                if (layerRaster == null)
339
                                                        return;
340
                                                dataset = layerRaster.getDataSource();
341
                                                getRender().setLastRenderBuffer(layerRaster.getRender().getLastRenderBuffer());
342
                                                initializeRasterLayer(null, new IBuffer[][] { { layerRaster.getRender().getLastRenderBuffer() } });
343
                                        } else {
344
                                                Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), viewPort.getImageWidth(), viewPort.getImageHeight());
345
                                                Tiling tiles = new Tiling(maxTileDrawWidth, maxTileDrawHeight, r);
346
                                                tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
347
                                                MultiRasterDataset[][] datasets = new MultiRasterDataset[tiles.getNumRows()][tiles.getNumCols()];
348
                                                IBuffer[][] buf = new IBuffer[tiles.getNumRows()][tiles.getNumCols()];
349
                                                visualStatus.fileNames = new String[tiles.getNumTiles()];
350
                                                for (int tileNr = 0; tileNr < tiles.getNumTiles(); tileNr++) {
351
                                                        // drawing part
352
                                                        try {
353
                                                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
354
                                                                boolean painted = drawTile(g, vp, cancel, tileNr, scale);
355
                                                                if (layerRaster != null && painted) {
356
                                                                        datasets[(int) (tileNr / tiles.getNumCols())][tileNr % tiles.getNumCols()] = (MultiRasterDataset) layerRaster.getDataSource().newDataset();
357
                                                                        buf[(int) (tileNr / tiles.getNumCols())][tileNr % tiles.getNumCols()] = layerRaster.getRender().getLastRenderBuffer();
358
                                                                }
359
                                                        } catch (NoninvertibleTransformException e) {
360
                                                                e.printStackTrace();
361
                                                        }
362
                                                }
363
                                                try {
364
                                                        if (datasets != null && datasets[0][0] != null) {
365
                                                                dataset = new CompositeDataset(datasets);
366
                                                                initializeRasterLayer(datasets, buf);
367
                                                        }
368
                                                } catch (MosaicNotValidException e) {
369
                                                        throw new ReadDriverException("No hay continuidad en el mosaico.", e);
370
                                                } catch (LoadLayerException e) {
371
                                                        throw new ReadDriverException("Error inicializando la capa.", e);
372
                                                }
373
                                        }
374
                                } else {
375
                                        drawTile(g, viewPort, cancel, 0, scale);
376
                                        if (layerRaster == null)
377
                                                return;
378
                                        dataset = layerRaster.getDataSource();
379
                                        getRender().setLastRenderBuffer(layerRaster.getRender().getLastRenderBuffer());
380
                                        initializeRasterLayer(null, new IBuffer[][] { { layerRaster.getRender().getLastRenderBuffer() } });
381
                                }
382
                        } catch (ConnectionErrorLayerException e) {
383
                                e.printStackTrace();
384
                        } catch (UnsupportedVersionLayerException e) {
385
                                e.printStackTrace();
386
                        } catch (LoadLayerException e) {
387
                                e.printStackTrace();
388
                        }
389
                }
390
                disableStopped();
391
                // callLegendChanged(null);
392
                Runtime r = Runtime.getRuntime();
393
                long mem = r.totalMemory() - r.freeMemory();
394
                System.err.println("Memoria total: " + (mem / 1024) +"KB");
395
        }
396

    
397
        /**
398
         * Acciones que se realizan despu?s de asignar la fuente de datos a
399
         * la capa raster.
400
         *
401
         * @throws LoadLayerException
402
         */
403
        private void initializeRasterLayer(MultiRasterDataset[][] datasets, IBuffer[][] buf) throws LoadLayerException {
404
                if(this.filterList != null)
405
                        getRender().setFilterList(filterList);
406

    
407
/*
408
                if(this.transparency != null)
409
                        getRender().setLastTransparency(transparency);
410
*/
411
                if(this.renderBands != null)
412
                        getRender().setRenderBands(renderBands);
413
                if(datasets != null) {
414
                        String[][] names = new String[datasets.length][datasets[0].length];
415
                        for (int i = 0; i < datasets.length; i++) {
416
                                for (int j = 0; j < datasets[i].length; j++) {
417
                                        if(datasets[i][j] != null)
418
                                                names[i][j] = datasets[i][j].getDataset(0)[0].getFName();
419
                                }
420
                        }
421
                        super.setLoadParams(names);
422
                }
423
                super.init();
424
                if(buf != null) {
425
                        int drawablesBandCount = layerRaster.getDataSource().getBands().getDrawableBandsCount();
426
                        IBuffer buff = null;
427
                        if(dataset instanceof CompositeDataset)
428
                                buff = ((CompositeDataset)dataset).generateBuffer(buf, drawablesBandCount);
429
                        else
430
                                buff = buf[0][0];
431
                        getRender().setLastRenderBuffer(buff);
432
                }
433

    
434
                if (transparency == null)
435
                        transparency = new GridTransparency(getDataSource().getTransparencyFilesStatus());
436

    
437
                getRender().setLastTransparency(transparency);
438
        }
439

    
440
        /**
441
         * This is the method used to draw a tile in a WCS mosaic layer.
442
         * @param tile Tile number to draw
443
         * @throws ReadDriverException
444
         * @return true when a tile has been painted
445
         */
446
        private boolean drawTile(Graphics2D g, ViewPort vp, Cancellable cancel, int tile, double scale) throws LoadLayerException, ReadDriverException {
447

    
448
                // Compute the query geometry
449
                // 1. Check if it is within borders
450
                Rectangle2D extent = getFullExtent();
451
                if ((vp.getAdjustedExtent().getMinX() > extent.getMaxX()) ||
452
                                (vp.getAdjustedExtent().getMinY() > extent.getMaxY()) ||
453
                                (vp.getAdjustedExtent().getMaxX() < extent.getMinX()) ||
454
                                (vp.getAdjustedExtent().getMaxY() < extent.getMinY()))
455
                        return false;
456

    
457
                // 2. Compute extent to be requested.
458
                Rectangle2D bBox = new Rectangle2D.Double();
459
                Rectangle2D.intersect(vp.getAdjustedExtent(), extent, bBox);
460

    
461
                // 3. Compute size in pixels
462
                double scalex = vp.getAffineTransform().getScaleX();
463
                double scaley = vp.getAffineTransform().getScaleY();
464
                int wImg = (int) Math.ceil(Math.abs(bBox.getWidth() * scalex) + 1);
465
                int hImg = (int) Math.ceil(Math.abs(bBox.getHeight() * scaley) + 1);
466
                Dimension sz = new Dimension(wImg, hImg);
467

    
468
                if ((wImg <= 0) || (hImg <= 0))
469
                        return false;
470

    
471
                try {
472
                        sz = new Dimension(wImg, hImg);
473

    
474
                        wcsStatus.setCoveraName( coverageName );
475
                        wcsStatus.setExtent( bBox );
476
                        wcsStatus.setFormat( format );
477
                        wcsStatus.setHeight( hImg );
478
                        wcsStatus.setWidth( wImg );
479
                        wcsStatus.setSrs(srs);
480
                        wcsStatus.setParameters( parameter );
481
                        wcsStatus.setTime( time );
482
                        wcsStatus.setOnlineResource((String) onlineResources.get("GetCoverage"));
483

    
484
                        File f = getDriver().getCoverage(wcsStatus, new MyCancellable(cancel));
485
                        if (f == null)
486
                                return false;
487
                        String nameWordFile = f.getPath() + getExtensionWorldFile();
488
                        com.iver.andami.Utilities.createTemp(nameWordFile, this.getDataWorldFile(bBox, sz));
489

    
490
                        IStatusRaster status = super.getStatus();
491
                        if(status!=null && firstLoad){
492
                                try {
493
                                        status.applyStatus(this);
494
                                } catch (NotSupportedExtensionException e) {
495
                                        throw new ReadDriverException("", e);
496
                                } catch (RasterDriverException e) {
497
                                        throw new ReadDriverException("", e);
498
                                } catch (FilterTypeException e) {
499
                                        throw new ReadDriverException("", e);
500
                                }
501
                                firstLoad = false;
502
                        }
503
                        ViewPortData vpData = new ViewPortData(
504
                                vp.getProjection(), new Extent(bBox), sz );
505
                        vpData.setMat(vp.getAffineTransform());
506

    
507
                        String filePath = f.getAbsolutePath();
508
                        visualStatus.fileNames[tile] = filePath;
509

    
510
                        try {
511
                                rasterProcess(filePath, g, vp, scale, cancel);
512
                        } catch (FilterTypeException e) {
513
                        }
514

    
515
//                        this.getRender().draw(g, vpData);
516
//                        rasterProcess(g, vpData, f);
517

    
518
//                } catch (ValidationException e) {
519
//                        UnknownResponseFormatExceptionType type =
520
//                                new UnknownResponseFormatExceptionType();
521
//                        type.setLayerName(getName());
522
//                        try {
523
//                                type.setDriverName(getDriver().getName());
524
//                        } catch (Exception e1) {
525
//                                e1.printStackTrace();
526
//                        }
527
//                        type.setFormat(format);
528
//                        type.setHost(host);
529
//                        type.setProtocol("WCS");
530
//                        ReadDriverException exception = new ReadDriverException("unknown_response_format",type);
531
//                        throw exception;
532
//        azabala                throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
533
//                }
534
//                catch (UnsupportedVersionLayerException e) {
535
//                        UnsuportedProtocolVersionExceptionType type =
536
//                                new UnsuportedProtocolVersionExceptionType();
537
//                        type.setLayerName(getName());
538
//                        try {
539
//                                type.setDriverName(getDriver().getName());
540
//                        } catch (Exception ex){
541
//                        }
542
//                        type.setUrl(host);
543
//                        throw new ReadDriverException(PluginServices.getText(this, "version_conflict"), e, type);
544

    
545
//        azabala                throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
546
                } catch (IOException e) {
547
//                        ConnectionErrorExceptionType type = new ConnectionErrorExceptionType();
548
//                        type.setLayerName(getName());
549
//                        try {
550
//                                type.setDriverName(getDriver().getName());
551
//                        } catch (Exception e1) {
552
//                        }
553
//                        type.setHost(host);
554
                        throw new ConnectionErrorLayerException(getName(),e);
555
                }
556
//                catch (WCSLayerException e) {
557
////azabala: la capturamos y la convertimos en DriverException
558
//                        WCSDriverExceptionType type = new WCSDriverExceptionType();
559
//                        type.setLayerName(getName());
560
//                        try {
561
//                                type.setDriverName(getDriver().getName());
562
//                        } catch (Exception e1) {
563
//                        }
564
//                        type.setWcsStatus(wcsStatus);
565
//                        this.setVisible(false);
566
//                        throw new WDriverException("Error WCS", e,  type);
567
//
568
////            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
569
//
570
//        }//
571
                catch (WCSDriverException e) {
572
                        throw new LoadLayerException(getName(),e);
573
                } catch (IllegalStateException e) {
574
                        throw new LoadLayerException(getName(),e);
575
                }
576
                return true;
577
        }
578

    
579
        /**
580
         * Devuelve el FMapWMSDriver.
581
         *
582
         * @return FMapWMSDriver
583
         *
584
         * @throws IllegalStateException
585
         * @throws ValidationException
586
         * @throws UnsupportedVersionLayerException
587
         * @throws IOException
588
         */
589
        private FMapWCSDriver getDriver() throws IllegalStateException, IOException {
590
                if (wcs == null) {
591
                        wcs = FMapWCSDriverFactory.getFMapDriverForURL(host);
592
                }
593
                return wcs;
594
        }
595

    
596
        /**
597
         * Calcula el contenido del fichero de georreferenciaci?n de una imagen.
598
         * @param bBox Tama?o y posici?n de la imagen (en coordenadas de usuario)
599
         * @param sz Tama?o de la imagen en pixeles.
600
         * @return el 'WorldFile', como String.
601
         * @throws IOException
602
         */
603
        public String getDataWorldFile(Rectangle2D bBox, Dimension sz) throws IOException {
604
                StringBuffer data = new StringBuffer();
605
                data.append((bBox.getMaxX() - bBox.getMinX())/(sz.getWidth() - 1)+"\n");
606
                data.append("0.0\n");
607
                data.append("0.0\n");
608
                data.append("-"+(bBox.getMaxY() - bBox.getMinY())/(sz.getHeight() - 1)+"\n");
609
                data.append(""+bBox.getMinX()+"\n");
610
                data.append(""+bBox.getMaxY()+"\n");
611
                return data.toString();
612
        }
613

    
614
        /**
615
         * Carga y dibuja el raster usando la librer?a
616
         * @param filePath Ruta al fichero en disco
617
         * @param g Graphics2D
618
         * @param vp ViewPort
619
         * @param scale Escala para el draw
620
         * @param cancel Cancelaci?n para el draw
621
         * @throws ReadDriverException
622
         * @throws LoadLayerException
623
         */
624
        private void rasterProcess(String filePath, Graphics2D g, ViewPort vp, double scale, Cancellable cancel) throws ReadDriverException, LoadLayerException, FilterTypeException {
625
                //Cerramos el dataset asociado a la capa si est? abierto.
626
                if(layerRaster != null) {
627
                        layerRaster.setRemoveRasterFlag(true);
628
                        layerRaster.getDataSource().close();
629
                }
630

    
631
                //Cargamos el dataset con el raster de disco.
632
                layerRaster = FLyrRasterSE.createLayer("", filePath, vp.getProjection());
633
                layerRaster.getRender().setBufferFactory(layerRaster.getBufferFactory());
634

    
635
                if(visualStatus.dataType == IBuffer.TYPE_UNDEFINED && layerRaster.getDataType() != null)
636
                        visualStatus.dataType = layerRaster.getDataType()[0];
637
                if(visualStatus.bandCount == 0 && layerRaster.getBandCount() != 0)
638
                        visualStatus.bandCount = layerRaster.getBandCount();
639

    
640
                if (getLegend() == null)
641
                        lastLegend = layerRaster.getLegend();
642

    
643
                //En caso de cargar un proyecto con XMLEntity se crean los filtros
644
                if(filterArguments != null) {
645
                        RasterFilterList fl = new RasterFilterList();
646
                        fl.addEnvParam("IStatistics", layerRaster.getDataSource().getStatistics());
647
                        fl.addEnvParam("MultiRasterDataset", layerRaster.getDataSource());
648
                        fl.setInitDataType(layerRaster.getDataType()[0]);
649
                        RasterFilterListManager filterListManager = new RasterFilterListManager(fl);
650
                        filterListManager.createFilterListFromStrings(filterArguments);
651
                        if (fl.move(TailTrimFilter.class, 0))
652
                                fl.move(LinearEnhancementFilter.class, 1);
653
                        else
654
                                fl.move(LinearEnhancementFilter.class, 0);
655
                        fl.controlTypes();
656
                        filterArguments = null;
657
                        filterList = fl;
658
                }
659

    
660
                //Como el raster se carga a cada zoom el render se crea nuevamente y la lista de
661
                //filtros siempre estar? vacia a cada visualizaci?n. Para evitarlo tenemos que
662
                //guardar la lista de filtro aplicada en la visualizaci?n anterior.
663
                if (filterList != null)
664
                        layerRaster.getRender().setFilterList(filterList);
665
                if (transparency == null)
666
                        transparency = layerRaster.getRender().getLastTransparency();
667
                if (transparency != null)
668
                        layerRaster.getRender().setLastTransparency(transparency);
669
                if (renderBands != null)
670
                        layerRaster.getRender().setRenderBands(renderBands);
671

    
672
                //Dibujamos
673
                layerRaster.draw(null, g, vp, cancel, scale);
674

    
675
                //La primera vez asignamos la lista de filtros asociada al renderizador. Guardamos una referencia
676
                //en esta clase para que a cada zoom no se pierda.
677
                if (filterList == null)
678
                        filterList = layerRaster.getRender().getFilterList();
679
                if (renderBands == null)
680
                        renderBands = layerRaster.getRender().getRenderBands();
681
        }
682

    
683
        /*
684
         * (non-Javadoc)
685
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#cloneLayer()
686
         */
687
        public FLayer cloneLayer() throws Exception {
688
                Object par = null;
689
                if (dataset instanceof CompositeDataset)
690
                        par = ((CompositeDataset) dataset).getFileNames();
691
                else
692
                        if (layerRaster != null)
693
                                par = layerRaster.getLoadParams();
694

    
695
                FLyrRasterSE newLayer = FLyrRasterSE.createLayer(this.getName(), par, this.getProjection());
696

    
697
                ArrayList filters = getRender().getFilterList().getStatusCloned();
698
                newLayer.getRender().getFilterList().setStatus(filters);
699

    
700
                return newLayer;
701
        }
702

    
703
        /*
704
         * (non-Javadoc)
705
         * @see org.gvsig.fmap.raster.IRasterRendering#getRenderFilterList()
706
         */
707
        public RasterFilterList getRenderFilterList(){
708
                return (filterList != null) ? filterList : getRender().getFilterList();
709
        }
710

    
711
        /*
712
         * (non-Javadoc)
713
         * @see org.gvsig.raster.hierarchy.IRasterRendering#setRenderFilterList(org.gvsig.raster.grid.filter.RasterFilterList)
714
         */
715
        public void setRenderFilterList(RasterFilterList filterList) {
716
                this.filterList = filterList;
717
                super.getRender().setFilterList(filterList);
718
        }
719

    
720
        /*
721
         * (non-Javadoc)
722
         * @see org.gvsig.fmap.raster.IRasterRendering#getRenderTransparency()
723
         */
724
        public GridTransparency getRenderTransparency() {
725
                return getRender().getLastTransparency();
726
//                return (transparency != null) ? transparency : getRender().getLastTransparency();
727
        }
728

    
729
        /*
730
         * (non-Javadoc)
731
         * @see org.gvsig.raster.hierarchy.IRasterRendering#getRenderBands()
732
         */
733
        public int[] getRenderBands() {
734
                return (renderBands != null) ? renderBands : getRender().getRenderBands();
735
        }
736

    
737
        /*
738
         * (non-Javadoc)
739
         * @see org.gvsig.raster.hierarchy.IRasterRendering#setRenderBands(int[])
740
         */
741
        public void setRenderBands(int[] renderBands) {
742
                this.renderBands = renderBands;
743
                getRender().setRenderBands(renderBands);
744
        }
745

    
746
        /*
747
         * (non-Javadoc)
748
         * @see org.gvsig.fmap.raster.layers.FLyrRasterSE#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.utiles.swing.threads.Cancellable, double, javax.print.attribute.PrintRequestAttributeSet)
749
         */
750
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties) throws ReadDriverException {
751
                if (isVisible() && isWithinScale(scale)){
752
                        draw(null, g, viewPort, cancel, scale);
753
                }
754
        }
755

    
756
        /**
757
         * Returns the XMLEntity containing the necessary info for reproduce
758
         * the layer.
759
         *
760
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir
761
         * la capa.
762
         *
763
         * @return XMLEntity.
764
         * @throws XMLException
765
         */
766
        public XMLEntity getXMLEntity() throws XMLException {
767
                XMLEntity xml = super.getXMLEntity();
768

    
769
                xml.putProperty("wcs.host", getHost());
770
                xml.putProperty("wcs.fullExtent", StringUtilities.rect2String( fullExtent ));
771
                xml.putProperty("wcs.layerQuery", coverageName );
772
                xml.putProperty("wcs.format", format );
773
                xml.putProperty("wcs.srs", srs );
774
                xml.putProperty("wcs.time", time );
775
                xml.putProperty("wcs.parameter", parameter );
776
                xml.putProperty("wcs.coverageName", coverageName );
777
                xml.putProperty("wcs.maxResX", maxRes.getX());
778
                xml.putProperty("wcs.maxResY", maxRes.getY());
779

    
780
                Iterator it = onlineResources.keySet().iterator();
781
                String strOnlines = "";
782
                while (it.hasNext()) {
783
                        String key = (String) it.next();
784
                        String value = (String) onlineResources.get(key);
785
                        strOnlines += key+"~##SEP2##~"+value;
786
                        if (it.hasNext())
787
                                strOnlines += "~##SEP1##~";
788
                }
789
                xml.putProperty("onlineResources", strOnlines);
790

    
791
                IStatusRaster status = super.getStatus();
792
                if (status!=null)
793
                        status.getXMLEntity(xml, true, this);
794
                else{
795
                        status = new StatusLayerRaster();
796
                        status.getXMLEntity(xml, true, this);
797
                }
798
                return xml;
799
        }
800

    
801
        /**
802
         * Reproduces the layer from an XMLEntity.
803
         *
804
         * A partir del XMLEntity reproduce la capa.
805
         *
806
                * @param xml XMLEntity
807
         *
808
         * @throws XMLException
809
         * @throws DriverException
810
         * @throws DriverIOException
811
         */
812
        public void setXMLEntity(XMLEntity xml) throws XMLException {
813
                super.setXMLEntity(xml);
814

    
815
                // host
816
                try {
817
                        host = new URL(xml.getStringProperty("wcs.host"));
818
                } catch (MalformedURLException e) {
819
                        throw new XMLException(e);
820
                }
821

    
822
                // full extent
823
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty("wcs.fullExtent"));
824

    
825
                // coverageQuery
826
                coverageName = xml.getStringProperty("wcs.layerQuery");
827

    
828
                // format
829
                format = xml.getStringProperty("wcs.format");
830

    
831
                // srs
832
                srs = xml.getStringProperty("wcs.srs");
833

    
834
                // time
835
                time = xml.getStringProperty("wcs.time");
836

    
837
                // parameter
838
                parameter = xml.getStringProperty("wcs.parameter");
839

    
840
                // coverage name
841
                coverageName = xml.getStringProperty("wcs.coverageName");
842

    
843
                // max resolution
844
                if (xml.contains("wcs.maxRes"))
845
                        maxRes = new Point2D.Double(xml.getDoubleProperty("wcs.maxRes"), xml.getDoubleProperty("wcs.maxRes"));
846
                else if (xml.contains("wcs.maxResX") && xml.contains("wcs.maxResY"))
847
                        maxRes = new Point2D.Double(xml.getDoubleProperty("wcs.maxResX"), xml.getDoubleProperty("wcs.maxResY"));
848

    
849
                // OnlineResources
850
                                if (xml.contains("onlineResources")) {
851
                                        String[] operations = xml.getStringProperty("onlineResources").split("~##SEP1##~");
852
                                        for (int i = 0; i < operations.length; i++) {
853
                                String[] resources = operations[i].split("~##SEP2##~");
854
                                if (resources.length==2 && resources[1]!="")
855
                                        onlineResources.put(resources[0], resources[1]);
856
                        }
857
                                }
858
                String claseStr = null;
859
                if (xml.contains("raster.class")) {
860
                        claseStr = xml.getStringProperty("raster.class");
861
                }
862

    
863
                IStatusRaster status = super.getStatus();
864
                if (status!=null)
865
                        status.setXMLEntity(xml, this);
866
                else {
867
                        //Cuando cargamos un proyecto
868

    
869
                        if(claseStr!=null && !claseStr.equals("")){
870
                                try{
871
                                        Class clase = LayerFactory.getLayerClassForLayerClassName(claseStr);
872
                                        Constructor constr = clase.getConstructor(null);
873
                                        status = (IStatusRaster)constr.newInstance(null);
874
                                        if(status != null) {
875
                                                ((StatusLayerRaster)status).setNameClass(claseStr);
876
                                                status.setXMLEntity(xml, this);
877
                                                filterArguments = status.getFilterArguments();
878
//                                                transparency = status.getTransparency();
879
                                                renderBands = status.getRenderBands();
880
                                                ColorTable ct = status.getColorTable();
881
                                                if(ct != null)
882
                                                        setLastLegend(ct);
883
                                        }
884
                                } catch(ClassNotFoundException exc) {
885
                                        exc.printStackTrace();
886
                                } catch(InstantiationException exc) {
887
                                        exc.printStackTrace();
888
                                } catch(IllegalAccessException exc) {
889
                                        exc.printStackTrace();
890
                                } catch(NoSuchMethodException exc) {
891
                                        exc.printStackTrace();
892
                                } catch(InvocationTargetException exc) {
893
                                        exc.printStackTrace();
894
                                } catch (FilterTypeException exc) {
895
                                        exc.printStackTrace();
896
                                }
897
                        }
898
                }
899
                firstLoad = true;
900
        }
901

    
902
        public void setCoverageName(String coverageName) {
903
                this.coverageName = coverageName;
904
        }
905

    
906
        public void setParameter(String parametersString) {
907
                if (this.parameter == parametersString){
908
                        return;
909
                }
910
                if (this.parameter != null && this.parameter.equals(parametersString)){
911
                        return;
912
                }
913
                this.parameter = parametersString;
914
                this.updateDrawVersion();
915
        }
916

    
917
        public void setTime(String time) {
918
                if (this.time == time){
919
                        return;
920
                }
921
                if (this.time != null && this.time.equals(time)){
922
                        return;
923
                }
924
                this.time = time;
925
                this.updateDrawVersion();
926
        }
927

    
928
        public void setSRS(String srs) {
929
                if (this.srs == srs){
930
                        return;
931
                }
932
                if (this.srs != null && this.srs.equals(srs)){
933
                        return;
934
                }
935
                this.srs = srs;
936
                this.updateDrawVersion();
937
                setProjection(CRSFactory.getCRS(srs));
938
        }
939

    
940
        public void setFormat(String format) {
941
                if (this.format == format){
942
                        return;
943
                }
944
                if (this.format != null && this.format.equals(format)){
945
                        return;
946
                }
947
                this.format = format;
948
                this.updateDrawVersion();
949
        }
950

    
951

    
952
        /**
953
         * Inserta el URL.
954
         *
955
         * @param host String.
956
         * @throws MalformedURLException
957
         */
958
        public void setHost(String host) {
959
                try {
960
                        setHost(new URL(host));
961
                } catch (MalformedURLException e) {
962

    
963
                }
964
        }
965

    
966
        /**
967
         * Inserta el URL.
968
         *
969
         * @param host URL.
970
         */
971
        public void setHost(URL host) {
972
                if (this.host == host){
973
                        return;
974
                }
975
                if (this.host != null && this.host.equals(host)){
976
                        return;
977
                }
978
                this.host = host;
979
                this.updateDrawVersion();
980
        }
981

    
982
        /**
983
         * Sets the layer's full extent.
984
         *
985
         * Establece la extensi?n m?xima de la capa.
986
         *
987
         * @param rect
988
         */
989
        public void setFullExtent(Rectangle2D rect) {
990
                if (this.fullExtent == rect){
991
                        return;
992
                }
993
                if (this.fullExtent != null && this.fullExtent.equals(rect)){
994
                        return;
995
                }
996

    
997
                this.fullExtent = rect;
998
                this.updateDrawVersion();
999
        }
1000

    
1001
        /**
1002
         * Devuelve el URL.
1003
         *
1004
         * @return URL.
1005
         */
1006
        public URL getHost() {
1007
                return host;
1008
        }
1009

    
1010
        /**
1011
         * Remote source layers have a bunch of properties that are required for get them from
1012
         * the servers. This method supplies a hash table containing any needed field. This hash
1013
         * table may be used to let the client to connect to a server and restore a previously saved
1014
         * layer. So, the layer itself may not be saved to the disk since the actual saved
1015
         * info is just its properties.
1016
         *
1017
         * @return Returns a hash table containing all the required information for
1018
         * set up a wms layer
1019
         */
1020
        public Hashtable getProperties(){
1021
                Hashtable info = new Hashtable();
1022
                info.put(   "name", coverageName);
1023
                info.put(   "host", getHost());
1024
                info.put(    "crs", srs);
1025
                info.put( "format", format);
1026
                String str = time;
1027
                if (str==null)
1028
                        str = "";
1029
                info.put(   "time", str);
1030
                str = parameter;
1031
                if (str==null)
1032
                        str = "";
1033
                info.put("parameter", str);
1034

    
1035
                return info;
1036
        }
1037

    
1038
        /**
1039
         * Obtiene la extensi?n del fichero de georreferenciaci?n
1040
         * @return String con la extensi?n del fichero de georreferenciaci?n dependiendo
1041
         * del valor del formato obtenido del servidor. Por defecto asignaremos un .wld
1042
         */
1043
        private String getExtensionWorldFile(){
1044
                String extWorldFile = ".wld";
1045
                        if (format.equals("image/tif") || format.equals("image/tiff"))
1046
                                extWorldFile = ".tfw";
1047
                        if (format.equals("image/jpeg"))
1048
                                extWorldFile = ".jpgw";
1049
                        return extWorldFile;
1050
        }
1051

    
1052
        public void setMaxResolution(Point2D maxResolution) {
1053
                if (this.maxRes == maxResolution){
1054
                        return;
1055
                }
1056
                if (this.maxRes != null && this.maxRes.equals(maxResolution)){
1057
                        return;
1058
                }
1059
                this.maxRes = maxResolution;
1060
                this.updateDrawVersion();
1061
        }
1062

    
1063
        /**
1064
         * <p>
1065
         * Gets the max resolution allowed by the coverage. Requesting a higher resolution
1066
         * than this value does not cause any error, but the info responsed is just an
1067
         * interpolation. <br>
1068
         * </p>
1069
         *
1070
         * <p>
1071
         * In exchange for obtaining a greater file and without additional information,
1072
         * we can easily fit it into the View. <br>
1073
         * </p>
1074
         *
1075
         * <p>
1076
         * Obtiene la resoluci?n m?xima soportada por la cobertura. La petici?n
1077
         * de una resoluci?n superior a la soportada no provoca ning?n error, aunque
1078
         * la informaci?n obtenida s?lo es una mera interpolaci?n de informaci?n. <br>
1079
         * </p>
1080
         *
1081
         * <p>
1082
         * A cambio de obtener un archivo mayor y sin informaci?n adicional, podemos
1083
         * f?cilmente acoplarlo a la vista. <br>
1084
         * </p>
1085
         *
1086
         * @return double
1087
         */
1088
        public Point2D getMaxResolution() {
1089
                if (maxRes==null)
1090
                        maxRes = wcs.getMaxResolution(coverageName);
1091
                return maxRes;
1092
        }
1093

    
1094

    
1095
        public void setDriver(FMapWCSDriver driver) {
1096
                if (driver == this.wcs){
1097
                        return;
1098
                }
1099
                this.wcs = driver;
1100
                this.updateDrawVersion();
1101
        }
1102

    
1103
        /*
1104
         *  (non-Javadoc)
1105
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getTileSize()
1106
         */
1107
        public int[] getTileSize() {
1108
                int[] size = {maxTileDrawWidth, maxTileDrawHeight};
1109
                return size;
1110
        }
1111

    
1112
        /*
1113
         * (non-Javadoc)
1114
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getTocImageIcon()
1115
         */
1116
        public ImageIcon getTocImageIcon() {
1117
                return new ImageIcon(getClass().getResource("image/icoLayer.png"));
1118
        }
1119

    
1120
        /*
1121
         *  (non-Javadoc)
1122
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#isTiled()
1123
         */
1124
        public boolean isTiled() {
1125
                return mustTileDraw;
1126
        }
1127

    
1128
        /*
1129
         * (non-Javadoc)
1130
         * @see org.gvsig.fmap.raster.layers.FLyrRasterSE#isActionEnabled(int)
1131
         */
1132
        public boolean isActionEnabled(int action) {
1133
                switch (action) {
1134
                        case IRasterLayerActions.ZOOM_PIXEL_RESOLUTION:
1135
                        case IRasterLayerActions.FLYRASTER_BAR_TOOLS:
1136
                        case IRasterLayerActions.BANDS_FILE_LIST:
1137
                        case IRasterLayerActions.GEOLOCATION:
1138
                        case IRasterLayerActions.PANSHARPENING:
1139
                                return false;
1140
                        case IRasterLayerActions.BANDS_RGB:
1141
                                return true;
1142
                }
1143

    
1144
                return super.isActionEnabled(action);
1145
        }
1146

    
1147
        /*
1148
         * (non-Javadoc)
1149
         * @see org.gvsig.fmap.raster.layers.FLyrRasterSE#overviewsSupport()
1150
         */
1151
        public boolean overviewsSupport() {
1152
                return false;
1153
        }
1154
}