Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1003 / extensions / extWCS / src / com / iver / cit / gvsig / fmap / layers / FLyrWCS.java @ 12271

History | View | Annotate | Download (34 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.cresques.filter.RasterFilterStack;
67
import org.cresques.filter.RasterFilterStackManager;
68
import org.cresques.geo.ViewPortData;
69
import org.cresques.io.GdalFile;
70
import org.cresques.io.GeoRasterFile;
71
import org.cresques.io.data.Grid;
72
import org.cresques.io.exceptions.NotSupportedExtensionException;
73
import org.cresques.px.Extent;
74
import org.cresques.px.PxRaster;
75
import org.exolab.castor.xml.ValidationException;
76
import org.gvsig.remoteClient.wcs.WCSStatus;
77
import org.gvsig.remoteClient.wms.ICancellable;
78

    
79
import com.iver.andami.PluginServices;
80
import com.iver.cit.gvsig.fmap.ConnectionErrorExceptionType;
81
import com.iver.cit.gvsig.fmap.DriverException;
82
import com.iver.cit.gvsig.fmap.UnknownResponseFormatExceptionType;
83
import com.iver.cit.gvsig.fmap.UnsuportedProtocolVersionExceptionType;
84
import com.iver.cit.gvsig.fmap.ViewPort;
85
import com.iver.cit.gvsig.fmap.WCSDriverExceptionType;
86
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
87
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException;
88
import com.iver.cit.gvsig.fmap.drivers.wcs.FMapWCSDriver;
89
import com.iver.cit.gvsig.fmap.drivers.wcs.FMapWCSDriverFactory;
90
import com.iver.cit.gvsig.fmap.drivers.wcs.WCSException;
91
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
92
import com.iver.cit.gvsig.fmap.layers.layerOperations.StringXMLItem;
93
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
94
import com.iver.utiles.DoubleUtilities;
95
import com.iver.utiles.StringUtilities;
96
import com.iver.utiles.XMLEntity;
97
import com.iver.utiles.swing.threads.Cancellable;
98

    
99

    
100
/**
101
 * Class for the WCS layer.
102
 *
103
 * Capa para el WCS.
104
 *
105
 * @author jaume - jaume.dominguez@iver.es
106
 */
107
public class FLyrWCS extends FLyrDefault implements RasterOperations, InfoByPoint{
108
        private FMapWCSDriver wcs = null;
109

    
110
        private URL                                                 host;
111
        private String                                                coverageName;
112
        private Rectangle2D                                        fullExtent;
113
        private String                                                format;
114
        private String                                                srs;
115
        private String                                                time;
116
        private String                                                parameter;
117
        private Point2D                                                maxRes;
118
        private Hashtable                                         onlineResources = new Hashtable();
119

    
120
        private WCSStatus                                        wcsStatus = new WCSStatus();
121

    
122
        private StatusRasterInterface                status = null;
123
        private int                                                 posX = 0, posY = 0;
124
        private double                                                 posXWC = 0, posYWC = 0;
125
        private int                                                 r = 0, g = 0, b = 0;
126
        private RasterFilterStackManager        stackManager = null;
127
        private PxRaster                                         raster = null;
128
        private GeoRasterFile                                 rasterFile = null;
129
        private RasterFilterStack                         filterStack;
130
        private boolean                                         firstLoad = false;
131
        private int                                                        rband = 0, gband = 1, bband = 2;
132
        private VisualStatus                                visualStatus = new VisualStatus();
133

    
134
        private boolean                                         mustTileDraw = false;
135
        private int                                                 maxTileDrawWidth  = 1023;
136
        private int                                                        maxTileDrawHeight = 1023;
137
        private int                                                 maxTilePrintWidth  = 250;
138
        private int                                                        maxTilePrintHeight = 250;
139

    
140
        private Grid                                                grid = null;
141

    
142
        private class MyCancellable implements ICancellable
143
        {
144

    
145
                private Cancellable original;
146
                public MyCancellable(Cancellable cancelOriginal)
147
                {
148
                        this.original = cancelOriginal;
149
                }
150
                public boolean isCanceled() {
151
                        return original.isCanceled();
152
                }
153

    
154
        }
155
        public FLyrWCS(){
156
                super();
157
        }
158

    
159
        public FLyrWCS(Map args) throws DriverIOException{
160
                FMapWCSDriver drv = null;
161
                String host = (String)args.get("HOST");
162
                String sCoverage = (String) args.get((String) "COVERAGE");
163

    
164
                try {
165
                        this.setHost(new URL(host));
166
                } catch (MalformedURLException e) {
167
                        //e.printStackTrace();
168
                        throw new DriverIOException("Malformed host URL, '" + host + "' (" + e.toString() + ").");
169
                }
170
                try {
171
                        drv = this.getDriver();
172
                } catch (Exception e) {
173
                        // e.printStackTrace();
174
                        throw new DriverIOException("Can't get driver to host '" + host + "' (" + e.toString() + ").");
175
                }
176

    
177
                try{
178
                        if (!drv.connect(false, null)){
179
                                throw new DriverIOException("Can't connect to host '" + host + "'.");
180
                        }
181
                }catch(Exception e){
182
                        throw new DriverIOException("Can't connect to host '" + host + "'.");
183
                }
184

    
185
                WCSLayer wcsNode = drv.getLayer(sCoverage);
186

    
187
                if (wcsNode == null){
188
                        throw new DriverIOException("The server '" + host + "' doesn't has the coverage '" + sCoverage + "'.");
189
                }
190

    
191
                try{
192
                        this.setFullExtent(drv.getFullExtent(sCoverage,
193
                                        (String) args.get((String) "CRS")));
194
                        this.setFormat((String) args.get((String) "FORMAT"));
195
                        this.setParameter("BANDS=" + (String) args.get((String) "BANDS"));
196
                        this.setSRS((String) args.get((String) "CRS"));
197
                        this.setName(sCoverage);
198
                        this.setCoverageName(sCoverage);
199
                }catch (Exception e){
200
                        throw new DriverIOException("The server '" + host + "' is not able to load the coverage '" + sCoverage + "'.");
201
                }
202

    
203
        }
204

    
205
        /**
206
         * Clase que contiene los datos de visualizaci?n de WCS. Tiene datos que representan al
207
         * raster en la vista. Este raster puede estar compuesto por tiles por lo que valores
208
         * como el ancho total o el m?nimo o m?ximo deben ser calculados a partir de todos los
209
         * tiles visualizados.
210
         * @author Nacho Brodin (brodin_ign@gva.es)
211
         */
212
        private class VisualStatus {
213
                /**
214
                 * Ancho y alto de la imagen o del conjunto de tiles si los tiene. Coincide con
215
                 * el ancho y alto del viewPort
216
                 */
217
                private        int                                                        width = 0, height = 0;
218
                private double                                                minX = 0D, minY = 0D, maxX = 0D, maxY = 0D;
219
                private int                                                 bandCount = 0;
220
                private int                                                        dataType = DataBuffer.TYPE_UNDEFINED;
221

    
222
                /**
223
                 * Ancho y alto total del raster que ser? la suma de todos los tiles.
224
                 */
225
                private        int                                                        rasterWidth = 0, rasterHeight = 0;
226
                private        double                                                rasterMinX = Double.MAX_VALUE, rasterMinY = Double.MAX_VALUE;
227
                private        double                                                rasterMaxX = 0, rasterMaxY = 0;
228
                /**
229
                 * Lista de nombre de fichero que componen toda la visualizaci?n.
230
                 */
231
                private String[]                                        fileNames = null;
232
        }
233

    
234
        /*
235
         *  (non-Javadoc)
236
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setRGB(int, int, int)
237
         */
238
        public void setRGB(int r, int g, int b) {
239
                this.r = r;
240
                this.g = g;
241
                this.b = b;
242
        }
243

    
244
        /*
245
         *  (non-Javadoc)
246
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getFilterStack()
247
         */
248
        public RasterFilterStack getFilterStack() {
249
                if(raster!=null)
250
                        return raster.filterStack;
251
                return null;
252
        }
253

    
254
        /*
255
         *  (non-Javadoc)
256
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setFilterStack(org.cresques.io.raster.RasterFilterStack)
257
         */
258
        public void setFilterStack(RasterFilterStack stack) {
259
                this.filterStack = stack;
260
        }
261

    
262
        /*
263
         *  (non-Javadoc)
264
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getSource()
265
         */
266
        public RasterAdapter getSource() {
267
                return null;
268
        }
269

    
270
        /*
271
         *  (non-Javadoc)
272
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setSource(com.iver.cit.gvsig.fmap.layers.RasterAdapter)
273
         */
274
        public void setSource(RasterAdapter ra) {
275
        }
276

    
277
        /*
278
         *  (non-Javadoc)
279
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPos(int, int)
280
         */
281
        public void setPos(int x, int y) {
282
                this.posX = x;
283
                this.posY = y;
284
        }
285

    
286
        /*
287
         *  (non-Javadoc)
288
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setPosWC(double, double)
289
         */
290
        public void setPosWC(double x, double y) {
291
                this.posXWC = x;
292
                this.posYWC = y;
293
        }
294

    
295
        /*
296
         *  (non-Javadoc)
297
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getPixel(double, double)
298
         */
299
        public int[] getPixel(double wcx, double wcy) {
300
                if(getPxRaster() != null)
301
                        return getPxRaster().getPixel(wcx, wcy);
302
        return null;
303
        }
304

    
305
        /*
306
         *  (non-Javadoc)
307
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setBand(int, int)
308
         */
309
        public void setBand(int flag, int nBand) {
310
                switch(flag){
311
                case GeoRasterFile.RED_BAND:setBandR(nBand);break;
312
                case GeoRasterFile.GREEN_BAND:setBandG(nBand);break;
313
                case GeoRasterFile.BLUE_BAND:setBandB(nBand);break;
314
                }
315
        }
316

    
317
        /**
318
         * Sets the R-band.
319
         *
320
         * Asigna la banda R.
321
         * @param r
322
         */
323
        public void setBandR(int r){
324
                this.rband = r;
325
        }
326

    
327
        /**
328
         * Sets the G-band.
329
         *
330
         * Asigna la banda G
331
         * @param g
332
         */
333
        public void setBandG(int g){
334
                this.gband = g;
335
        }
336

    
337
        /**
338
         * Sets the B-band.
339
         *
340
         * Asigna la banda B
341
         * @param b
342
         */
343
        public void setBandB(int b){
344
                this.bband = b;
345
        }
346

    
347
        /*
348
         *  (non-Javadoc)
349
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getAttributes()
350
         */
351
        public ArrayList getAttributes() {
352
                if (rasterFile != null){
353
                        ArrayList attr = new ArrayList();
354
                        String dataType = "Byte";
355
                        if (rasterFile.getDataType() == DataBuffer.TYPE_BYTE) dataType = "Byte";
356
                        else if (visualStatus.dataType == DataBuffer.TYPE_SHORT)
357
                                dataType = "Short";
358
                        else if (visualStatus.dataType == DataBuffer.TYPE_USHORT)
359
                                dataType = "Unsigned Short";
360
                        else if (visualStatus.dataType == DataBuffer.TYPE_INT)
361
                                dataType = "Integer";
362
                        else if (visualStatus.dataType == DataBuffer.TYPE_FLOAT)
363
                                dataType = "Float";
364
                        else if (visualStatus.dataType == DataBuffer.TYPE_DOUBLE)
365
                                dataType = "Double";
366
                        else
367
                                dataType = "Unknown";
368

    
369
                        Object [][] a = {
370
                                {"Filename",rasterFile.getName().substring(rasterFile.getName().lastIndexOf("/")+1, rasterFile.getName().length())},
371
                                {"Filesize",new Long(0)},
372
                                {"Width",new Integer((int)this.getWidth())},
373
                                {"Height", new Integer((int)this.getHeight())},
374
                                {"Bands", new Integer(visualStatus.bandCount)},
375
                                {"BandDataType", dataType}
376
                        };
377
                        for (int i=0; i<a.length; i++)
378
                                attr.add(a[i]);
379

    
380
                        return attr;
381
                }
382
                return  null;
383
        }
384

    
385
        /*
386
         *  (non-Javadoc)
387
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxX()
388
         */
389
        public double getMaxX() {
390
                return visualStatus.maxX;
391
        }
392

    
393
        /*
394
         *  (non-Javadoc)
395
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMaxY()
396
         */
397
        public double getMaxY() {
398
                return visualStatus.maxY;
399
        }
400

    
401
        /*
402
         *  (non-Javadoc)
403
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinX()
404
         */
405
        public double getMinX() {
406
                return visualStatus.minX;
407
        }
408

    
409
        /*
410
         *  (non-Javadoc)
411
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getMinY()
412
         */
413
        public double getMinY() {
414
                return visualStatus.minY;
415
        }
416

    
417
        /*
418
         *  (non-Javadoc)
419
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getWidth()
420
         */
421
        public double getWidth() {
422
                return visualStatus.width;
423
        }
424

    
425
        /*
426
         *  (non-Javadoc)
427
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getHeight()
428
         */
429
        public double getHeight() {
430
                return visualStatus.height;
431
        }
432

    
433
        /**
434
         * @deprecated
435
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#getInfo
436
         */
437
        public String queryByPoint(Point p) throws DriverException {
438
                String data = "<file:"+getName().replaceAll("[^a-zA-Z0-9]","")+">\n";
439
                ArrayList attr = this.getAttributes();
440
                data += "  <raster\n";
441
                data += "    File=\""+getName()+"\"\n";
442
                for (int i=0; i<attr.size(); i++) {
443
                        Object [] a = (Object []) attr.get(i);
444

    
445
                        data += "    "+a[0].toString()+"=";
446
                        if (a[1].toString() instanceof String)
447
                                data += "\""+a[1].toString()+"\"\n";
448
                        else
449
                                data += a[1].toString()+"\n";
450
                }
451
                data += "    Point=\""+posX+" , "+posY+"\"\n";
452
                data += "    Point_WC=\""+posXWC+" , "+posYWC+"\"\n";
453
                data += "    RGB=\""+r+", "+g+", "+b+"\"\n";
454
                data += "  />\n";
455

    
456
                data += "</file:"+getName().replaceAll("[^a-zA-Z0-9]","")+">\n";
457
                System.out.println(data);
458
                return data;
459
        }
460
        /**
461
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#getInfo
462
         */
463
        public XMLItem[] getInfo(Point point, double tolerance, Cancellable cancel ) throws DriverException {
464
                String data = "<file:"+getName().replaceAll("[^a-zA-Z0-9]","")+">\n";
465

    
466
                Point2D pReal = getMapContext().getViewPort().toMapPoint(point);
467
                Point2D px = null;
468
                if(        pReal.getX() > this.getMinX() &&
469
                        pReal.getX() < this.getMaxX() &&
470
                        pReal.getY() > this.getMinY() &&
471
                        pReal.getY() < this.getMaxY()){
472

    
473
                        px = new Point2D.Double();
474
                        double w = (visualStatus.rasterMaxX - visualStatus.rasterMinX);
475
                        double h = (visualStatus.rasterMaxY - visualStatus.rasterMinY);
476
                        px.setLocation(((pReal.getX() - visualStatus.rasterMinX) * visualStatus.rasterWidth) / w,
477
                                                        ((visualStatus.rasterMaxY - pReal.getY()) * visualStatus.rasterHeight) / h);
478
                }
479

    
480
                int[] rgb = this.getPixel(pReal.getX(), pReal.getY());
481

    
482
                StringXMLItem[] item = new StringXMLItem[1];
483

    
484
                data += "  <raster\n";
485
                data += "    View_Point=\""+point.getX()+" , "+point.getY()+"\"\n";
486
                data += "    World_Point=\""+DoubleUtilities.format(pReal.getX(), 3)+" , "+ DoubleUtilities.format(pReal.getY(), 3)+"\"\n";
487
                if(        px == null || px.getX() > visualStatus.rasterWidth || px.getX() < 0 ||
488
                                px.getY() > visualStatus.rasterHeight || px.getY() < 0)
489
                                data += "    Pixel_Point=\"Out\"\n";
490
                        else{
491
                                data += "    Pixel_Point=\""+(int)px.getX()+" , "+(int)px.getY()+"\"\n";
492
                                data += "    Band_Value=\"";
493
                                for(int file = 0; file < visualStatus.fileNames.length; file++ ){
494
                                        GdalFile rf;
495
                                        try {
496
                                                rf = new GdalFile(getMapContext().getViewPort().getProjection(), visualStatus.fileNames[file]);
497
                                        } catch (NotSupportedExtensionException e) {
498
                                                throw new DriverException("Open File Error.");
499
                                        }
500
                                        Extent ex = rf.getExtent();
501

    
502
                                        if(pReal.getX() >= ex.minX() && pReal.getX() <= ex.maxX() && pReal.getY() >= ex.minY() && pReal.getY() <= ex.maxY()){
503
                                                for(int i = 0; i < visualStatus.bandCount; i++){
504
                                                        Object pxData = rf.getData((int)px.getX(), (int)px.getY(), i);
505
                                                        if(pxData != null){
506
                                                                if(visualStatus.dataType >= 0 && visualStatus.dataType <= 3)
507
                                                                        data += ((Integer)pxData).intValue()+"  ";
508

    
509
                                                                if(visualStatus.dataType >= 4)
510
                                                                        data += ((Float)pxData).floatValue()+"  ";
511

    
512
                                                                if(visualStatus.dataType >= 5)
513
                                                                        data += ((Double)pxData).doubleValue()+"  ";
514
                                                        }
515
                                                }
516
                                        }
517
                                        rf.close();
518
                                }
519
                                data += "\"\n";
520
                        }
521

    
522
                        if(rgb != null)
523
                                data += "    RGB=\""+rgb[1]+"  "+rgb[2]+"  "+rgb[3]+"\"\n";
524

    
525
                        data += "  />\n";
526
                        data += "</file:"+getName().replaceAll("[^a-zA-Z0-9]","")+">\n";
527

    
528
                        item[0] = new StringXMLItem(data, this);
529
                        return item;
530
        }
531

    
532
        /*
533
         *  (non-Javadoc)
534
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
535
         */
536
        public Rectangle2D getFullExtent() {
537
                return fullExtent;
538
        }
539

    
540
        /*
541
         *  (non-Javadoc)
542
         * @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)
543
         */
544
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale) throws DriverException {
545

    
546
                if (isWithinScale(scale)){
547
                        Point2D p = viewPort.getOffset();
548
                        // p will be (0, 0) when drawing a view or other when painting onto
549
                        // the Layout.
550
                        visualStatus.width =  viewPort.getImageWidth();
551
                        visualStatus.height =  viewPort.getImageHeight();
552
                        visualStatus.minX = viewPort.getAdjustedExtent().getMinX();
553
                        visualStatus.minY = viewPort.getAdjustedExtent().getMinY();
554
                        visualStatus.maxX = viewPort.getAdjustedExtent().getMaxX();
555
                        visualStatus.maxY = viewPort.getAdjustedExtent().getMaxY();
556
                        visualStatus.rasterWidth = 0;
557
                        visualStatus.rasterHeight = 0;
558
                        visualStatus.rasterMinX = Double.MAX_VALUE;
559
                        visualStatus.rasterMinY = Double.MAX_VALUE;
560
                        visualStatus.rasterMaxX = 0;
561
                        visualStatus.rasterMaxY = 0;
562
                        visualStatus.fileNames = new String[1];
563

    
564
                        if(mustTileDraw){
565
                                if(viewPort.getImageWidth()<= maxTileDrawWidth && viewPort.getImageHeight()<=maxTileDrawHeight)
566
                                        drawTile(g, viewPort, cancel, 0);
567
                                else{
568
                                        Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), viewPort.getImageWidth(), viewPort.getImageHeight());
569
                                        Tiling tiles = new Tiling(maxTileDrawWidth, maxTileDrawHeight, r);
570
                                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
571
                                        visualStatus.fileNames = new String[tiles.getNumTiles()];
572
                                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
573
                                                // drawing part
574
                                                try {
575
                                                        ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
576
                                                        drawTile(g, vp, cancel, tileNr);
577
                                                } catch (NoninvertibleTransformException e) {
578
                                                        e.printStackTrace();
579
                                                }
580
                                        }
581
                                }
582
                        }else
583
                                drawTile(g, viewPort, cancel, 0);
584
                }
585
                Runtime r = Runtime.getRuntime();
586
                long mem = r.totalMemory() - r.freeMemory();
587
                System.err.println("Memoria total: " + (mem / 1024) +"KB");
588
        }
589

    
590
        /**
591
         * This is the method used to draw a tile in a WMS mosaic layer.
592
         * @param tile Tile number to draw
593
         */
594
        private void drawTile(Graphics2D g, ViewPort vp, Cancellable cancel, int tile) throws DriverException {
595

    
596
                // Compute the query geometry
597
                // 1. Check if it is within borders
598
                Rectangle2D extent = getFullExtent();
599
        if ((vp.getAdjustedExtent().getMinX() > extent.getMaxX()) ||
600
                (vp.getAdjustedExtent().getMinY() > extent.getMaxY()) ||
601
                (vp.getAdjustedExtent().getMaxX() < extent.getMinX()) ||
602
                (vp.getAdjustedExtent().getMaxY() < extent.getMinY())) {
603
            return;
604
        }
605

    
606
        // 2. Compute extent to be requested.
607
        Rectangle2D bBox = new Rectangle2D.Double();
608
        Rectangle2D.intersect(vp.getAdjustedExtent(), extent, bBox);
609

    
610
        // 3. Compute size in pixels
611
        double scalex = vp.getAffineTransform().getScaleX();
612
        double scaley = vp.getAffineTransform().getScaleY();
613
        int wImg = (int) Math.ceil(Math.abs(bBox.getWidth() * scalex) + 1);
614
        int hImg = (int) Math.ceil(Math.abs(bBox.getHeight() * scaley) + 1);
615
        Dimension sz = new Dimension(wImg, hImg);
616

    
617
        if ((wImg <= 0) || (hImg <= 0)) {
618
            return;
619
        }
620

    
621
                try {
622
                        sz = new Dimension(wImg, hImg);
623
                        Rectangle2D.intersect(vp.getAdjustedExtent(), extent, bBox);
624

    
625
                        wcsStatus.setCoveraName( coverageName );
626
                        wcsStatus.setExtent( bBox );
627
                        wcsStatus.setFormat( format );
628
                        wcsStatus.setHeight( hImg );
629
                        wcsStatus.setWidth( wImg );
630
                        wcsStatus.setSrs(srs);
631
                        wcsStatus.setParameters( parameter );
632
                        wcsStatus.setTime( time );
633
                        wcsStatus.setOnlineResource((String) onlineResources.get("GetCoverage"));
634

    
635
                        File f = getDriver().getCoverage(wcsStatus, new MyCancellable(cancel));
636
                        if (f == null)
637
                                return;
638
                        String nameWordFile = f.getPath() + getExtensionWorldFile();
639
                        com.iver.andami.Utilities.createTemp(nameWordFile, this.getDataWorldFile(bBox, sz));
640

    
641
                        /*if(status!=null && firstLoad){
642
                                status.applyStatus(this);
643
                                firstLoad = false;
644
                        }*/
645
                        ViewPortData vpData = new ViewPortData(
646
                                vp.getProjection(), new Extent(bBox), sz );
647
                        vpData.setMat(vp.getAffineTransform());
648

    
649
                        visualStatus.fileNames[tile] = f.getAbsolutePath();
650
                        rasterProcess(g, vpData, f);
651

    
652
                } catch (ValidationException e) {
653
                        UnknownResponseFormatExceptionType type =
654
                                new UnknownResponseFormatExceptionType();
655
                        type.setLayerName(getName());
656
                        try {
657
                                type.setDriverName(getDriver().getName());
658
                        } catch (Exception e1) {
659
                                e1.printStackTrace();
660
                        }
661
                        type.setFormat(format);
662
                        type.setHost(host);
663
                        type.setProtocol("WCS");
664
                        DriverException exception = new DriverException("unknown_response_format", type);
665
                        throw exception;
666
//        azabala                throw new DriverException(PluginServices.getText(this, "unknown_response_format"), e);
667
                }
668
                catch (UnsupportedVersionException e) {
669
                        UnsuportedProtocolVersionExceptionType type =
670
                                new UnsuportedProtocolVersionExceptionType();
671
                        type.setLayerName(getName());
672
                        try {
673
                                type.setDriverName(getDriver().getName());
674
                        } catch (Exception ex){
675
                        }
676
                        type.setUrl(host);
677
                        throw new DriverException(PluginServices.getText(this, "version_conflict"), e, type);
678

    
679
//        azabala                throw new DriverException(PluginServices.getText(this, "version_conflict"), e);
680
                } catch (IOException e) {
681
                        ConnectionErrorExceptionType type = new ConnectionErrorExceptionType();
682
                        type.setLayerName(getName());
683
                        try {
684
                                type.setDriverName(getDriver().getName());
685
                        } catch (Exception e1) {
686
                        }
687
                        type.setHost(host);
688
                        throw new DriverException(PluginServices.
689
                                        getText(this, "connect_error"), e, type);
690
                } catch (WCSException e) {
691
//azabala: la capturamos y la convertimos en DriverException
692
                        WCSDriverExceptionType type = new WCSDriverExceptionType();
693
                        type.setLayerName(getName());
694
                        try {
695
                                type.setDriverName(getDriver().getName());
696
                        } catch (Exception e1) {
697
                        }
698
                        type.setWcsStatus(wcsStatus);
699
                        this.setVisible(false);
700
                        throw new DriverException("Error WCS", e,  type);
701

    
702
//            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), e.getMessage());
703

    
704
        }//catch
705
        }
706

    
707
        /**
708
         * Devuelve el FMapWMSDriver.
709
         *
710
         * @return FMapWMSDriver
711
         *
712
         * @throws IllegalStateException
713
         * @throws ValidationException
714
         * @throws UnsupportedVersionException
715
         * @throws IOException
716
         */
717
        private FMapWCSDriver getDriver() throws IllegalStateException, ValidationException, UnsupportedVersionException, IOException {
718
                if (wcs == null) {
719
                        wcs = FMapWCSDriverFactory.getFMapDriverForURL(host);
720
                }
721
                return wcs;
722
        }
723

    
724
        /**
725
         * Calcula el contenido del fichero de georreferenciaci?n de una imagen.
726
         * @param bBox Tama?o y posici?n de la imagen (en coordenadas de usuario)
727
         * @param sz Tama?o de la imagen en pixeles.
728
         * @return el 'WorldFile', como String.
729
         * @throws IOException
730
         */
731
        public String getDataWorldFile(Rectangle2D bBox, Dimension sz) throws IOException {
732
                StringBuffer data = new StringBuffer();
733
            data.append((bBox.getMaxX() - bBox.getMinX())/(sz.getWidth() - 1)+"\n");
734
            data.append("0.0\n");
735
            data.append("0.0\n");
736
            data.append("-"+(bBox.getMaxY() - bBox.getMinY())/(sz.getHeight() - 1)+"\n");
737
            data.append(""+bBox.getMinX()+"\n");
738
            data.append(""+bBox.getMaxY()+"\n");
739
            return data.toString();
740
        }
741

    
742
        /**
743
         * Dibuja una imagen usando PxRaster
744
         * @param g        Graphics2D en el que hay que dibujar.
745
         * @param vpData Par?metros de visualizaci?n
746
         * @param file La imagen en cuesti?n.
747
         */
748
        private void rasterProcess(Graphics2D g, ViewPortData vpData, File file)throws DriverException {
749

    
750
                //Creamos el PxRaster
751
                try{
752
                        rasterFile = new GdalFile(vpData.getProjection(), file.getAbsolutePath());
753
                } catch (NotSupportedExtensionException e) {
754
                        throw new DriverException("Open File Error");
755
                }
756
                raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
757

    
758
                if(status!=null && firstLoad){
759
                        status.applyStatus(this);
760
                        firstLoad = false;
761
                }
762

    
763
                grid = new Grid(rasterFile);
764
                grid.addRenderizer(raster);
765

    
766
                visualStatus.rasterWidth += rasterFile.getWidth();
767
                visualStatus.rasterHeight += rasterFile.getHeight();
768
                if(raster.getExtent().getMin().getX() < visualStatus.rasterMinX)
769
                        visualStatus.rasterMinX = raster.getExtent().getMin().getX();
770
                if(raster.getExtent().getMin().getY() < visualStatus.rasterMinY)
771
                        visualStatus.rasterMinY = raster.getExtent().getMin().getY();
772
                if(raster.getExtent().getMax().getX() > visualStatus.rasterMaxX)
773
                        visualStatus.rasterMaxX = raster.getExtent().getMax().getX();
774
                if(raster.getExtent().getMax().getY() > visualStatus.rasterMaxY)
775
                        visualStatus.rasterMaxY = raster.getExtent().getMax().getY();
776

    
777
                //Recuperamos la pila de filtros si ya hubiese sido cargado antes
778
                if (this.filterStack!=null)
779
                        raster.filterStack = this.filterStack;
780

    
781
                raster.setTransparency(false);
782

    
783
                //Asignamos transparencia y orden de bandas
784
                if (getTransparency() == -1 && !firstLoad);
785
                else
786
                        raster.setTransparency(getTransparency());
787

    
788
                raster.setBand(GeoRasterFile.RED_BAND,rband);
789
                raster.setBand(GeoRasterFile.GREEN_BAND, gband);
790
                raster.setBand(GeoRasterFile.BLUE_BAND, bband);
791

    
792
                //Despues del primer pxRaster asignamos el stackManager guardado para los siguientes.
793
                //Con esto conseguimos asignar los cambios que se hayan producido desde el cuadro de
794
                //propiedades cuando creamos un nuevo pxRaster
795
                if (this.stackManager != null)
796
                        raster.setStackManager(this.stackManager);
797

    
798
                if (visualStatus != null){
799
                        visualStatus.bandCount = raster.getBandCount();
800
                        visualStatus.dataType = raster.getDataType();
801
                }
802

    
803
                raster.draw(g, vpData);
804

    
805
                //En el primer pxRaster de una imagen obtenemos el Stack Manager para poder modificarlo
806
                //si queremos desde las propiedades
807

    
808
                if (this.stackManager == null)
809
                        this.stackManager = raster.getStackManager();
810

    
811
                if (this.filterStack == null)
812
                        this.filterStack = raster.filterStack;
813

    
814
                rasterFile.close();
815
        }
816

    
817
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties) throws DriverException {
818
                draw(null, g, viewPort, cancel, scale);
819
        }
820

    
821
        /**
822
         * @return Returns the raster.
823
         */
824
        public PxRaster getPxRaster() {
825
                return raster;
826
        }
827

    
828
        /**
829
         * Returns the XMLEntity containing the necessary info for reproduce
830
         * the layer.
831
         *
832
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir
833
         * la capa.
834
         *
835
         * @return XMLEntity.
836
         * @throws XMLException
837
         */
838
        public XMLEntity getXMLEntity() throws XMLException {
839
                XMLEntity xml = super.getXMLEntity();
840

    
841
                xml.putProperty("wcs.host", getHost());
842
                xml.putProperty("wcs.fullExtent", StringUtilities.rect2String( fullExtent ));
843
                xml.putProperty("wcs.layerQuery", coverageName );
844
                xml.putProperty("wcs.format", format );
845
                xml.putProperty("wcs.srs", srs );
846
                xml.putProperty("wcs.time", time );
847
                xml.putProperty("wcs.parameter", parameter );
848
                xml.putProperty("wcs.coverageName", coverageName );
849
                xml.putProperty("wcs.maxResX", maxRes.getX());
850
                xml.putProperty("wcs.maxResY", maxRes.getY());
851

    
852
                 Iterator it = onlineResources.keySet().iterator();
853
                 String strOnlines = "";
854
                 while (it.hasNext()) {
855
                         String key = (String) it.next();
856
                         String value = (String) onlineResources.get(key);
857
                         strOnlines += key+"~##SEP2##~"+value;
858
                         if (it.hasNext())
859
                                 strOnlines += "~##SEP1##~";
860
                 }
861
                 xml.putProperty("onlineResources", strOnlines);
862

    
863
                if (status!=null)
864
                        status.getXMLEntity(xml, true, this);
865
                else{
866
                        status = new StatusLayerRaster();
867
                        status.getXMLEntity(xml, true, this);
868
                }
869
                return xml;
870
        }
871

    
872
        /**
873
         * Reproduces the layer from an XMLEntity.
874
         *
875
         * A partir del XMLEntity reproduce la capa.
876
         *
877
          * @param xml XMLEntity
878
         *
879
         * @throws XMLException
880
         * @throws DriverException
881
         * @throws DriverIOException
882
         */
883
        public void setXMLEntity(XMLEntity xml) throws XMLException {
884
                super.setXMLEntity(xml);
885

    
886
                // host
887
                try {
888
                        host = new URL(xml.getStringProperty("wcs.host"));
889
                } catch (MalformedURLException e) {
890
                        throw new XMLException(e);
891
                }
892

    
893
                // full extent
894
                fullExtent = StringUtilities.string2Rect(xml.getStringProperty("wcs.fullExtent"));
895

    
896
                // coverageQuery
897
                coverageName = xml.getStringProperty("wcs.layerQuery");
898

    
899
                // format
900
                format = xml.getStringProperty("wcs.format");
901

    
902
                // srs
903
                srs = xml.getStringProperty("wcs.srs");
904

    
905
                // time
906
                time = xml.getStringProperty("wcs.time");
907

    
908
                // parameter
909
                parameter = xml.getStringProperty("wcs.parameter");
910

    
911
                // coverage name
912
                coverageName = xml.getStringProperty("wcs.coverageName");
913

    
914
                // max resolution
915
                if (xml.contains("wcs.maxRes"))
916
                        maxRes = new Point2D.Double(xml.getDoubleProperty("wcs.maxRes"), xml.getDoubleProperty("wcs.maxRes"));
917
                else if (xml.contains("wcs.maxResX") && xml.contains("wcs.maxResY"))
918
                        maxRes = new Point2D.Double(xml.getDoubleProperty("wcs.maxResX"), xml.getDoubleProperty("wcs.maxResY"));
919

    
920
                // OnlineResources
921
        if (xml.contains("onlineResources")) {
922
                String[] operations = xml.getStringProperty("onlineResources").split("~##SEP1##~");
923
                for (int i = 0; i < operations.length; i++) {
924
                                String[] resources = operations[i].split("~##SEP2##~");
925
                                if (resources.length==2 && resources[1]!="")
926
                                        onlineResources.put(resources[0], resources[1]);
927
                        }
928
        }
929
                String claseStr = null;
930
                if (xml.contains("raster.class")) {
931
                        claseStr = xml.getStringProperty("raster.class");
932
                }
933
                if (status!=null)
934
                        status.setXMLEntity(xml, this);
935
                else {
936
                        //Cuando cargamos un proyecto
937

    
938
                        if(claseStr!=null && !claseStr.equals("")){
939
                                try{
940
                                        Class clase = Class.forName(claseStr);
941
                                        Constructor constr = clase.getConstructor(null);
942
                                        status = (StatusRasterInterface)constr.newInstance(null);
943
                                        if(status!=null)
944
                                                status.setXMLEntity(xml, this);
945
                                } catch(ClassNotFoundException exc) {
946
                                        exc.printStackTrace();
947
                                } catch(InstantiationException exc) {
948
                                        exc.printStackTrace();
949
                                } catch(IllegalAccessException exc) {
950
                                        exc.printStackTrace();
951
                                } catch(NoSuchMethodException exc) {
952
                                        exc.printStackTrace();
953
                                } catch(InvocationTargetException exc) {
954
                                        exc.printStackTrace();
955
                                }
956
                        }
957
                }
958
                firstLoad = true;
959
        }
960

    
961
        public void setCoverageName(String coverageName) {
962
                this.coverageName = coverageName;
963
        }
964

    
965
        public void setParameter(String parametersString) {
966
                this.parameter = parametersString;
967
        }
968

    
969
        public void setTime(String time) {
970
                this.time = time;
971
        }
972

    
973
        public void setSRS(String srs) {
974
                this.srs = srs;
975
        }
976

    
977
        public void setFormat(String format) {
978
                this.format = format;
979
        }
980

    
981

    
982
        /**
983
         * Inserta el URL.
984
         *
985
         * @param host String.
986
         * @throws MalformedURLException
987
         */
988
        public void setHost(String host) {
989
                try {
990
                        setHost(new URL(host));
991
                } catch (MalformedURLException e) {
992

    
993
                }
994
        }
995

    
996
        /**
997
         * Inserta el URL.
998
         *
999
         * @param host URL.
1000
         */
1001
        public void setHost(URL host) {
1002
                this.host = host;
1003
        }
1004

    
1005
        /**
1006
         * Sets the layer's full extent.
1007
         *
1008
         * Establece la extensi?n m?xima de la capa.
1009
         *
1010
         * @param rect
1011
         */
1012
        public void setFullExtent(Rectangle2D rect) {
1013
                this.fullExtent = rect;
1014
        }
1015

    
1016
        /**
1017
         * Devuelve el URL.
1018
         *
1019
         * @return URL.
1020
         */
1021
        public URL getHost() {
1022
                return host;
1023
        }
1024

    
1025
        /**
1026
         * Remote source layers have a bunch of properties that are required for get them from
1027
         * the servers. This method supplies a hash table containing any needed field. This hash
1028
         * table may be used to let the client to connect to a server and restore a previously saved
1029
         * layer. So, the layer itself may not be saved to the disk since the actual saved
1030
         * info is just its properties.
1031
         *
1032
         * @return Returns a hash table containing all the required information for
1033
         * set up a wms layer
1034
         */
1035
        public Hashtable getProperties(){
1036
                Hashtable info = new Hashtable();
1037
                info.put(   "name", coverageName);
1038
                info.put(   "host", getHost());
1039
                info.put(    "crs", srs);
1040
                info.put( "format", format);
1041
                String str = time;
1042
                if (str==null)
1043
                        str = "";
1044
                info.put(   "time", str);
1045
                str = parameter;
1046
                if (str==null)
1047
                        str = "";
1048
                info.put("parameter", str);
1049

    
1050
                return info;
1051
        }
1052

    
1053
        /**
1054
         * Obtiene la extensi?n del fichero de georreferenciaci?n
1055
         * @return String con la extensi?n del fichero de georreferenciaci?n dependiendo
1056
         * del valor del formato obtenido del servidor. Por defecto asignaremos un .wld
1057
         */
1058
        private String getExtensionWorldFile(){
1059
                String extWorldFile = ".wld";
1060
            if (format.equals("image/tif") || format.equals("image/tiff"))
1061
                    extWorldFile = ".tfw";
1062
            if (format.equals("image/jpeg"))
1063
                    extWorldFile = ".jpgw";
1064
            return extWorldFile;
1065
        }
1066

    
1067
        public void setMaxResolution(Point2D maxResolution) {
1068
                this.maxRes = maxResolution;
1069
        }
1070

    
1071
        /**
1072
         * Gets the last open GeoRasterFile against the temp file received
1073
         *
1074
         * Obtiene el ?ltimo GeoRasterFile abierto sobre el temporal recibido
1075
         * @return
1076
         */
1077
        public GeoRasterFile getGeoRasterFile(){
1078
                return rasterFile;
1079
        }
1080

    
1081

    
1082
        /**
1083
         * Asignar el estado del raster
1084
         * @param status
1085
         */
1086
        public void setStatus(StatusRasterInterface status){
1087
                this.status = status;
1088
        }
1089

    
1090
        /**
1091
         * Obtiene el estado del raster
1092
         * @return
1093
         */
1094
        public StatusRasterInterface getStatus(){
1095
                return this.status;
1096
        }
1097

    
1098
        /**
1099
         * Gets the max resolution allowed by the coverage. Requesting a higher resolution
1100
         * than this value does not cause any error, but the info responsed is just an
1101
         * interpolation.
1102
         *
1103
         * In exchange for obtaining a greater file and without additional information,
1104
         * we can easily connect it at the View.
1105
         *
1106
         *
1107
         * Obtiene la resoluci?n m?xima soportada por la cobertura. La petici?n
1108
         * de una resoluci?n superior a la soportada no provoca ning?n error, aunque
1109
         * la informaci?n obtenida s?lo es una mera interpolaci?n de informaci?n.
1110
         *
1111
         * A cambio de obtener un archivo mayor y sin informaci?n adicional, podemos
1112
         * f?cilmente acoplarlo a la vista.
1113
         *
1114
         * @return double
1115
         */
1116
        public Point2D getMaxResolution() {
1117
                if (maxRes==null)
1118
                        maxRes = wcs.getMaxResolution(coverageName);
1119
                return maxRes;
1120
        }
1121

    
1122

    
1123
        public void setDriver(FMapWCSDriver driver) {
1124
                this.wcs = driver;
1125
        }
1126

    
1127
        /*
1128
         *  (non-Javadoc)
1129
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getTileSize()
1130
         */
1131
        public int[] getTileSize() {
1132
                int[] size = {maxTileDrawWidth, maxTileDrawHeight};
1133
                return size;
1134
        }
1135

    
1136
        /*
1137
         *  (non-Javadoc)
1138
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#isTiled()
1139
         */
1140
        public boolean isTiled() {
1141
                return mustTileDraw;
1142
        }
1143

    
1144
        /**
1145
         * Obtiene el tipo de dato de la capa raster
1146
         * @return Entero que representa el tipo de dato de la capa raster.
1147
         */
1148
        public int getDataType() {
1149
                return rasterFile.getDataType();
1150
        }
1151

    
1152
        /**
1153
         * Obtiene el grid asociado al raster que hay cargado en ese momento
1154
         * @return grid
1155
         */
1156
        public Grid getGrid(){
1157
                return grid;
1158
        }
1159
}