Statistics
| Revision:

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

History | View | Annotate | Download (34.8 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

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

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

    
102

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

    
132
        private URL                                                 host;
133
        private String                                                coverageName;
134
        private Rectangle2D                                        fullExtent;
135
        private String                                                format;
136
        private String                                                srs;
137
        private String                                                time;
138
        private String                                                parameter;
139
        private Point2D                                                maxRes;
140
        private Hashtable                                         onlineResources = new Hashtable();
141

    
142
        private WCSStatus                                        wcsStatus = new WCSStatus();
143

    
144
        private int                                                 posX = 0, posY = 0;
145
        private double                                                 posXWC = 0, posYWC = 0;
146
        private int                                                 r = 0, g = 0, b = 0;
147
        private boolean                                         firstLoad = false;
148
        private VisualStatus                                visualStatus = new VisualStatus();
149

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

    
164
        private class MyCancellable implements ICancellable
165
        {
166

    
167
                private Cancellable original;
168
                public MyCancellable(Cancellable cancelOriginal)
169
                {
170
                        this.original = cancelOriginal;
171
                }
172
                public boolean isCanceled() {
173
                        return original.isCanceled();
174
                }
175
                public Object getID() {
176
                        return this;
177
                }
178

    
179
        }
180

    
181
        public FLyrWCS(){
182
                super();
183
        }
184

    
185
        public FLyrWCS(Map args) throws DriverIOException{
186
                FMapWCSDriver drv = null;
187
                String host = (String)args.get("HOST");
188
                String sCoverage = (String) args.get((String) "COVERAGE");
189

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

    
203
                try{
204
                        if (!drv.connect(false, null)){
205
                                throw new DriverIOException("Can't connect to host '" + host + "'.");
206
                        }
207
                }catch(Exception e){
208
                        throw new DriverIOException("Can't connect to host '" + host + "'.");
209
                }
210

    
211
                WCSLayer wcsNode = drv.getLayer(sCoverage);
212

    
213
                if (wcsNode == null){
214
                        throw new DriverIOException("The server '" + host + "' doesn't has the coverage '" + sCoverage + "'.");
215
                }
216

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

    
229
        }
230

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

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

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

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

    
283
                data += "</file:"+getName().replaceAll("[^a-zA-Z0-9]","")+">\n";
284
                System.out.println(data);
285
                return data;
286
        }
287

    
288
        /**
289
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#getInfo
290
         */
291
        public XMLItem[] getInfo(Point point, double tolerance, Cancellable cancel ) throws ReadDriverException {
292
                return super.getInfo(point, tolerance, cancel);
293
        }
294

    
295
        /*
296
         *  (non-Javadoc)
297
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
298
         */
299
        public Rectangle2D getFullExtent() {
300
                return fullExtent;
301
        }
302

    
303
        /*
304
         *  (non-Javadoc)
305
         * @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)
306
         */
307
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale) throws ReadDriverException {
308
                enableStopped();
309
                // callLegendChanged(null);
310

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

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

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

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

    
429
                if (transparency == null)
430
                        transparency = new GridTransparency(getDataSource().getTransparencyFilesStatus());
431

    
432
                getRender().setLastTransparency(transparency);
433
        }
434

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

    
443
                // Compute the query geometry
444
                // 1. Check if it is within borders
445
                Rectangle2D extent = getFullExtent();
446
                if ((vp.getAdjustedExtent().getMinX() > extent.getMaxX()) ||
447
                                (vp.getAdjustedExtent().getMinY() > extent.getMaxY()) ||
448
                                (vp.getAdjustedExtent().getMaxX() < extent.getMinX()) ||
449
                                (vp.getAdjustedExtent().getMaxY() < extent.getMinY()))
450
                        return false;
451

    
452
                // 2. Compute extent to be requested.
453
                Rectangle2D bBox = new Rectangle2D.Double();
454
                Rectangle2D.intersect(vp.getAdjustedExtent(), extent, bBox);
455

    
456
                // 3. Compute size in pixels
457
                double scalex = vp.getAffineTransform().getScaleX();
458
                double scaley = vp.getAffineTransform().getScaleY();
459
                int wImg = (int) Math.ceil(Math.abs(bBox.getWidth() * scalex) + 1);
460
                int hImg = (int) Math.ceil(Math.abs(bBox.getHeight() * scaley) + 1);
461
                Dimension sz = new Dimension(wImg, hImg);
462

    
463
                if ((wImg <= 0) || (hImg <= 0))
464
                        return false;
465

    
466
                try {
467
                        sz = new Dimension(wImg, hImg);
468

    
469
                        wcsStatus.setCoveraName( coverageName );
470
                        wcsStatus.setExtent( bBox );
471
                        wcsStatus.setFormat( format );
472
                        wcsStatus.setHeight( hImg );
473
                        wcsStatus.setWidth( wImg );
474
                        wcsStatus.setSrs(srs);
475
                        wcsStatus.setParameters( parameter );
476
                        wcsStatus.setTime( time );
477
                        wcsStatus.setOnlineResource((String) onlineResources.get("GetCoverage"));
478

    
479
                        File f = getDriver().getCoverage(wcsStatus, new MyCancellable(cancel));
480
                        if (f == null)
481
                                return false;
482
                        String nameWordFile = f.getPath() + getExtensionWorldFile();
483
                        com.iver.andami.Utilities.createTemp(nameWordFile, this.getDataWorldFile(bBox, sz));
484

    
485
                        IStatusRaster status = super.getStatus();
486
                        if(status!=null && firstLoad){
487
                                status.applyStatus(this);
488
                                firstLoad = false;
489
                        }
490
                        ViewPortData vpData = new ViewPortData(
491
                                vp.getProjection(), new Extent(bBox), sz );
492
                        vpData.setMat(vp.getAffineTransform());
493

    
494
                        String filePath = f.getAbsolutePath();
495
                        visualStatus.fileNames[tile] = filePath;
496

    
497
                        rasterProcess(filePath, g, vp, scale, cancel);
498

    
499
//                        this.getRender().draw(g, vpData);
500
//                        rasterProcess(g, vpData, f);
501

    
502
//                } catch (ValidationException e) {
503
//                        UnknownResponseFormatExceptionType type =
504
//                                new UnknownResponseFormatExceptionType();
505
//                        type.setLayerName(getName());
506
//                        try {
507
//                                type.setDriverName(getDriver().getName());
508
//                        } catch (Exception e1) {
509
//                                e1.printStackTrace();
510
//                        }
511
//                        type.setFormat(format);
512
//                        type.setHost(host);
513
//                        type.setProtocol("WCS");
514
//                        ReadDriverException exception = new ReadDriverException("unknown_response_format",type);
515
//                        throw exception;
516
//        azabala                throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
517
//                }
518
//                catch (UnsupportedVersionLayerException e) {
519
//                        UnsuportedProtocolVersionExceptionType type =
520
//                                new UnsuportedProtocolVersionExceptionType();
521
//                        type.setLayerName(getName());
522
//                        try {
523
//                                type.setDriverName(getDriver().getName());
524
//                        } catch (Exception ex){
525
//                        }
526
//                        type.setUrl(host);
527
//                        throw new ReadDriverException(PluginServices.getText(this, "version_conflict"), e, type);
528

    
529
//        azabala                throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
530
                } catch (IOException e) {
531
//                        ConnectionErrorExceptionType type = new ConnectionErrorExceptionType();
532
//                        type.setLayerName(getName());
533
//                        try {
534
//                                type.setDriverName(getDriver().getName());
535
//                        } catch (Exception e1) {
536
//                        }
537
//                        type.setHost(host);
538
                        throw new ConnectionErrorLayerException(getName(),e);
539
                }
540
//                catch (WCSLayerException e) {
541
////azabala: la capturamos y la convertimos en DriverException
542
//                        WCSDriverExceptionType type = new WCSDriverExceptionType();
543
//                        type.setLayerName(getName());
544
//                        try {
545
//                                type.setDriverName(getDriver().getName());
546
//                        } catch (Exception e1) {
547
//                        }
548
//                        type.setWcsStatus(wcsStatus);
549
//                        this.setVisible(false);
550
//                        throw new WDriverException("Error WCS", e,  type);
551
//
552
////            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
553
//
554
//        }//
555
                catch (WCSDriverException e) {
556
                        throw new LoadLayerException(getName(),e);
557
                } catch (IllegalStateException e) {
558
                        throw new LoadLayerException(getName(),e);
559
                }
560
                return true;
561
        }
562

    
563
        /**
564
         * Devuelve el FMapWMSDriver.
565
         *
566
         * @return FMapWMSDriver
567
         *
568
         * @throws IllegalStateException
569
         * @throws ValidationException
570
         * @throws UnsupportedVersionLayerException
571
         * @throws IOException
572
         */
573
        private FMapWCSDriver getDriver() throws IllegalStateException, IOException {
574
                if (wcs == null) {
575
                        wcs = FMapWCSDriverFactory.getFMapDriverForURL(host);
576
                }
577
                return wcs;
578
        }
579

    
580
        /**
581
         * Calcula el contenido del fichero de georreferenciaci?n de una imagen.
582
         * @param bBox Tama?o y posici?n de la imagen (en coordenadas de usuario)
583
         * @param sz Tama?o de la imagen en pixeles.
584
         * @return el 'WorldFile', como String.
585
         * @throws IOException
586
         */
587
        public String getDataWorldFile(Rectangle2D bBox, Dimension sz) throws IOException {
588
                StringBuffer data = new StringBuffer();
589
                data.append((bBox.getMaxX() - bBox.getMinX())/(sz.getWidth() - 1)+"\n");
590
                data.append("0.0\n");
591
                data.append("0.0\n");
592
                data.append("-"+(bBox.getMaxY() - bBox.getMinY())/(sz.getHeight() - 1)+"\n");
593
                data.append(""+bBox.getMinX()+"\n");
594
                data.append(""+bBox.getMaxY()+"\n");
595
                return data.toString();
596
        }
597

    
598
        /**
599
         * Carga y dibuja el raster usando la librer?a
600
         * @param filePath Ruta al fichero en disco
601
         * @param g Graphics2D
602
         * @param vp ViewPort
603
         * @param scale Escala para el draw
604
         * @param cancel Cancelaci?n para el draw
605
         * @throws ReadDriverException
606
         * @throws LoadLayerException
607
         */
608
        private void rasterProcess(String filePath, Graphics2D g, ViewPort vp, double scale, Cancellable cancel) throws ReadDriverException, LoadLayerException {
609
                //Cerramos el dataset asociado a la capa si est? abierto.
610
                if(layerRaster != null) {
611
                        layerRaster.setRemoveRasterFlag(true);
612
                        layerRaster.getDataSource().close();
613
                }
614

    
615
                //Cargamos el dataset con el raster de disco.
616
                layerRaster = FLyrRasterSE.createLayer("", filePath, vp.getProjection());
617
                layerRaster.getRender().setBufferFactory(layerRaster.getBufferFactory());
618

    
619
                if(visualStatus.dataType == IBuffer.TYPE_UNDEFINED && layerRaster.getDataType() != null)
620
                        visualStatus.dataType = layerRaster.getDataType()[0];
621
                if(visualStatus.bandCount == 0 && layerRaster.getBandCount() != 0)
622
                        visualStatus.bandCount = layerRaster.getBandCount();
623

    
624
                if (getLegend() == null)
625
                        lastLegend = layerRaster.getLegend();
626

    
627
                //En caso de cargar un proyecto con XMLEntity se crean los filtros
628
                if(filterArguments != null) {
629
                        RasterFilterList fl = new RasterFilterList();
630
                        fl.addEnvParam("IStatistics", layerRaster.getDataSource().getStatistics());
631
                        fl.addEnvParam("MultiRasterDataset", layerRaster.getDataSource());
632
                        fl.setInitDataType(layerRaster.getDataType()[0]);
633
                        RasterFilterListManager filterListManager = new RasterFilterListManager(fl);
634
                        filterListManager.createFilterListFromStrings(filterArguments);
635
                        if (fl.move(TailTrimFilter.class, 0))
636
                                fl.move(LinearEnhancementFilter.class, 1);
637
                        else
638
                                fl.move(LinearEnhancementFilter.class, 0);
639
                        fl.controlTypes();
640
                        filterArguments = null;
641
                        filterList = fl;
642
                }
643

    
644
                //Como el raster se carga a cada zoom el render se crea nuevamente y la lista de
645
                //filtros siempre estar? vacia a cada visualizaci?n. Para evitarlo tenemos que
646
                //guardar la lista de filtro aplicada en la visualizaci?n anterior.
647
                if (filterList != null)
648
                        layerRaster.getRender().setFilterList(filterList);
649
                if (transparency == null)
650
                        transparency = layerRaster.getRender().getLastTransparency();
651
                if (transparency != null)
652
                        layerRaster.getRender().setLastTransparency(transparency);
653
                if (renderBands != null)
654
                        layerRaster.getRender().setRenderBands(renderBands);
655

    
656
                //Dibujamos
657
                layerRaster.draw(null, g, vp, cancel, scale);
658

    
659
                //La primera vez asignamos la lista de filtros asociada al renderizador. Guardamos una referencia
660
                //en esta clase para que a cada zoom no se pierda.
661
                if (filterList == null)
662
                        filterList = layerRaster.getRender().getFilterList();
663
                if (renderBands == null)
664
                        renderBands = layerRaster.getRender().getRenderBands();
665
        }
666

    
667
        /*
668
         * (non-Javadoc)
669
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#cloneLayer()
670
         */
671
        public FLayer cloneLayer() throws Exception {
672
                Object par = null;
673
                if (dataset instanceof CompositeDataset)
674
                        par = ((CompositeDataset) dataset).getFileNames();
675
                else
676
                        if (layerRaster != null)
677
                                par = layerRaster.getLoadParams();
678

    
679
                FLyrRasterSE newLayer = FLyrRasterSE.createLayer(this.getName(), par, this.getProjection());
680

    
681
                ArrayList filters = getRender().getFilterList().getStatusCloned();
682
                newLayer.getRender().getFilterList().setStatus(filters);
683

    
684
                return newLayer;
685
        }
686

    
687
        /*
688
         * (non-Javadoc)
689
         * @see org.gvsig.fmap.raster.IRasterRendering#getRenderFilterList()
690
         */
691
        public RasterFilterList getRenderFilterList(){
692
                return (filterList != null) ? filterList : getRender().getFilterList();
693
        }
694

    
695
        /*
696
         * (non-Javadoc)
697
         * @see org.gvsig.raster.hierarchy.IRasterRendering#setRenderFilterList(org.gvsig.raster.grid.filter.RasterFilterList)
698
         */
699
        public void setRenderFilterList(RasterFilterList filterList) {
700
                this.filterList = filterList;
701
                super.getRender().setFilterList(filterList);
702
        }
703

    
704
        /*
705
         * (non-Javadoc)
706
         * @see org.gvsig.fmap.raster.IRasterRendering#getRenderTransparency()
707
         */
708
        public GridTransparency getRenderTransparency() {
709
                return getRender().getLastTransparency();
710
//                return (transparency != null) ? transparency : getRender().getLastTransparency();
711
        }
712

    
713
        /*
714
         * (non-Javadoc)
715
         * @see org.gvsig.raster.hierarchy.IRasterRendering#getRenderBands()
716
         */
717
        public int[] getRenderBands() {
718
                return (renderBands != null) ? renderBands : getRender().getRenderBands();
719
        }
720

    
721
        /*
722
         * (non-Javadoc)
723
         * @see org.gvsig.raster.hierarchy.IRasterRendering#setRenderBands(int[])
724
         */
725
        public void setRenderBands(int[] renderBands) {
726
                this.renderBands = renderBands;
727
                getRender().setRenderBands(renderBands);
728
        }
729

    
730
        /*
731
         * (non-Javadoc)
732
         * @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)
733
         */
734
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties) throws ReadDriverException {
735
                if (isVisible() && isWithinScale(scale)){
736
                        draw(null, g, viewPort, cancel, scale);
737
                }
738
        }
739

    
740
        /**
741
         * Returns the XMLEntity containing the necessary info for reproduce
742
         * the layer.
743
         *
744
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir
745
         * la capa.
746
         *
747
         * @return XMLEntity.
748
         * @throws XMLException
749
         */
750
        public XMLEntity getXMLEntity() throws XMLException {
751
                XMLEntity xml = super.getXMLEntity();
752

    
753
                xml.putProperty("wcs.host", getHost());
754
                xml.putProperty("wcs.fullExtent", StringUtilities.rect2String( fullExtent ));
755
                xml.putProperty("wcs.layerQuery", coverageName );
756
                xml.putProperty("wcs.format", format );
757
                xml.putProperty("wcs.srs", srs );
758
                xml.putProperty("wcs.time", time );
759
                xml.putProperty("wcs.parameter", parameter );
760
                xml.putProperty("wcs.coverageName", coverageName );
761
                xml.putProperty("wcs.maxResX", maxRes.getX());
762
                xml.putProperty("wcs.maxResY", maxRes.getY());
763

    
764
                Iterator it = onlineResources.keySet().iterator();
765
                String strOnlines = "";
766
                while (it.hasNext()) {
767
                        String key = (String) it.next();
768
                        String value = (String) onlineResources.get(key);
769
                        strOnlines += key+"~##SEP2##~"+value;
770
                        if (it.hasNext())
771
                                strOnlines += "~##SEP1##~";
772
                }
773
                xml.putProperty("onlineResources", strOnlines);
774

    
775
                IStatusRaster status = super.getStatus();
776
                if (status!=null)
777
                        status.getXMLEntity(xml, true, this);
778
                else{
779
                        status = new StatusLayerRaster();
780
                        status.getXMLEntity(xml, true, this);
781
                }
782
                return xml;
783
        }
784

    
785
        /**
786
         * Reproduces the layer from an XMLEntity.
787
         *
788
         * A partir del XMLEntity reproduce la capa.
789
         *
790
                * @param xml XMLEntity
791
         *
792
         * @throws XMLException
793
         * @throws DriverException
794
         * @throws DriverIOException
795
         */
796
        public void setXMLEntity(XMLEntity xml) throws XMLException {
797
                super.setXMLEntity(xml);
798

    
799
                // host
800
                try {
801
                        host = new URL(xml.getStringProperty("wcs.host"));
802
                } catch (MalformedURLException e) {
803
                        throw new XMLException(e);
804
                }
805

    
806
                // full extent
807
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty("wcs.fullExtent"));
808

    
809
                // coverageQuery
810
                coverageName = xml.getStringProperty("wcs.layerQuery");
811

    
812
                // format
813
                format = xml.getStringProperty("wcs.format");
814

    
815
                // srs
816
                srs = xml.getStringProperty("wcs.srs");
817

    
818
                // time
819
                time = xml.getStringProperty("wcs.time");
820

    
821
                // parameter
822
                parameter = xml.getStringProperty("wcs.parameter");
823

    
824
                // coverage name
825
                coverageName = xml.getStringProperty("wcs.coverageName");
826

    
827
                // max resolution
828
                if (xml.contains("wcs.maxRes"))
829
                        maxRes = new Point2D.Double(xml.getDoubleProperty("wcs.maxRes"), xml.getDoubleProperty("wcs.maxRes"));
830
                else if (xml.contains("wcs.maxResX") && xml.contains("wcs.maxResY"))
831
                        maxRes = new Point2D.Double(xml.getDoubleProperty("wcs.maxResX"), xml.getDoubleProperty("wcs.maxResY"));
832

    
833
                // OnlineResources
834
                                if (xml.contains("onlineResources")) {
835
                                        String[] operations = xml.getStringProperty("onlineResources").split("~##SEP1##~");
836
                                        for (int i = 0; i < operations.length; i++) {
837
                                String[] resources = operations[i].split("~##SEP2##~");
838
                                if (resources.length==2 && resources[1]!="")
839
                                        onlineResources.put(resources[0], resources[1]);
840
                        }
841
                                }
842
                String claseStr = null;
843
                if (xml.contains("raster.class")) {
844
                        claseStr = xml.getStringProperty("raster.class");
845
                }
846

    
847
                IStatusRaster status = super.getStatus();
848
                if (status!=null)
849
                        status.setXMLEntity(xml, this);
850
                else {
851
                        //Cuando cargamos un proyecto
852

    
853
                        if(claseStr!=null && !claseStr.equals("")){
854
                                try{
855
                                        Class clase = LayerFactory.getLayerClassForLayerClassName(claseStr);
856
                                        Constructor constr = clase.getConstructor(null);
857
                                        status = (IStatusRaster)constr.newInstance(null);
858
                                        if(status != null) {
859
                                                ((StatusLayerRaster)status).setNameClass(claseStr);
860
                                                status.setXMLEntity(xml, this);
861
                                                filterArguments = status.getFilterArguments();
862
//                                                transparency = status.getTransparency();
863
                                                renderBands = status.getRenderBands();
864
                                                ColorTable ct = status.getColorTable();
865
                                                if(ct != null)
866
                                                        setLastLegend(ct);
867
                                        }
868
                                } catch(ClassNotFoundException exc) {
869
                                        exc.printStackTrace();
870
                                } catch(InstantiationException exc) {
871
                                        exc.printStackTrace();
872
                                } catch(IllegalAccessException exc) {
873
                                        exc.printStackTrace();
874
                                } catch(NoSuchMethodException exc) {
875
                                        exc.printStackTrace();
876
                                } catch(InvocationTargetException exc) {
877
                                        exc.printStackTrace();
878
                                }
879
                        }
880
                }
881
                firstLoad = true;
882
        }
883

    
884
        public void setCoverageName(String coverageName) {
885
                this.coverageName = coverageName;
886
        }
887

    
888
        public void setParameter(String parametersString) {
889
                this.parameter = parametersString;
890
        }
891

    
892
        public void setTime(String time) {
893
                this.time = time;
894
        }
895

    
896
        public void setSRS(String srs) {
897
                this.srs = srs;
898
                setProjection(CRSFactory.getCRS(srs));
899
        }
900

    
901
        public void setFormat(String format) {
902
                this.format = format;
903
        }
904

    
905

    
906
        /**
907
         * Inserta el URL.
908
         *
909
         * @param host String.
910
         * @throws MalformedURLException
911
         */
912
        public void setHost(String host) {
913
                try {
914
                        setHost(new URL(host));
915
                } catch (MalformedURLException e) {
916

    
917
                }
918
        }
919

    
920
        /**
921
         * Inserta el URL.
922
         *
923
         * @param host URL.
924
         */
925
        public void setHost(URL host) {
926
                this.host = host;
927
        }
928

    
929
        /**
930
         * Sets the layer's full extent.
931
         *
932
         * Establece la extensi?n m?xima de la capa.
933
         *
934
         * @param rect
935
         */
936
        public void setFullExtent(Rectangle2D rect) {
937
                this.fullExtent = rect;
938
        }
939

    
940
        /**
941
         * Devuelve el URL.
942
         *
943
         * @return URL.
944
         */
945
        public URL getHost() {
946
                return host;
947
        }
948

    
949
        /**
950
         * Remote source layers have a bunch of properties that are required for get them from
951
         * the servers. This method supplies a hash table containing any needed field. This hash
952
         * table may be used to let the client to connect to a server and restore a previously saved
953
         * layer. So, the layer itself may not be saved to the disk since the actual saved
954
         * info is just its properties.
955
         *
956
         * @return Returns a hash table containing all the required information for
957
         * set up a wms layer
958
         */
959
        public Hashtable getProperties(){
960
                Hashtable info = new Hashtable();
961
                info.put(   "name", coverageName);
962
                info.put(   "host", getHost());
963
                info.put(    "crs", srs);
964
                info.put( "format", format);
965
                String str = time;
966
                if (str==null)
967
                        str = "";
968
                info.put(   "time", str);
969
                str = parameter;
970
                if (str==null)
971
                        str = "";
972
                info.put("parameter", str);
973

    
974
                return info;
975
        }
976

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

    
991
        public void setMaxResolution(Point2D maxResolution) {
992
                this.maxRes = maxResolution;
993
        }
994

    
995
        /**
996
         * <p>
997
         * Gets the max resolution allowed by the coverage. Requesting a higher resolution
998
         * than this value does not cause any error, but the info responsed is just an
999
         * interpolation. <br>
1000
         * </p>
1001
         *
1002
         * <p>
1003
         * In exchange for obtaining a greater file and without additional information,
1004
         * we can easily fit it into the View. <br>
1005
         * </p>
1006
         *
1007
         * <p>
1008
         * Obtiene la resoluci?n m?xima soportada por la cobertura. La petici?n
1009
         * de una resoluci?n superior a la soportada no provoca ning?n error, aunque
1010
         * la informaci?n obtenida s?lo es una mera interpolaci?n de informaci?n. <br>
1011
         * </p>
1012
         *
1013
         * <p>
1014
         * A cambio de obtener un archivo mayor y sin informaci?n adicional, podemos
1015
         * f?cilmente acoplarlo a la vista. <br>
1016
         * </p>
1017
         *
1018
         * @return double
1019
         */
1020
        public Point2D getMaxResolution() {
1021
                if (maxRes==null)
1022
                        maxRes = wcs.getMaxResolution(coverageName);
1023
                return maxRes;
1024
        }
1025

    
1026

    
1027
        public void setDriver(FMapWCSDriver driver) {
1028
                this.wcs = driver;
1029
        }
1030

    
1031
        /*
1032
         *  (non-Javadoc)
1033
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getTileSize()
1034
         */
1035
        public int[] getTileSize() {
1036
                int[] size = {maxTileDrawWidth, maxTileDrawHeight};
1037
                return size;
1038
        }
1039

    
1040
        /*
1041
         *  (non-Javadoc)
1042
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#isTiled()
1043
         */
1044
        public boolean isTiled() {
1045
                return mustTileDraw;
1046
        }
1047

    
1048
        /*
1049
         * (non-Javadoc)
1050
         * @see org.gvsig.fmap.raster.layers.FLyrRasterSE#isActionEnabled(int)
1051
         */
1052
        public boolean isActionEnabled(int action) {
1053
                switch (action) {
1054
                        case IRasterLayerActions.ZOOM_PIXEL_RESOLUTION:
1055
                        case IRasterLayerActions.FLYRASTER_BAR_TOOLS:
1056
                        case IRasterLayerActions.BANDS_FILE_LIST:
1057
                        case IRasterLayerActions.GEOLOCATION:
1058
                        case IRasterLayerActions.PANSHARPENING:
1059
                                return false;
1060
                        case IRasterLayerActions.BANDS_RGB:
1061
                                return true;
1062
                }
1063

    
1064
                return super.isActionEnabled(action);
1065
        }
1066

    
1067
}