Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1004 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrRaster.java @ 12319

History | View | Annotate | Download (24.7 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.io.File;
53
import java.lang.reflect.Constructor;
54
import java.lang.reflect.InvocationTargetException;
55
import java.util.ArrayList;
56

    
57
import javax.print.attribute.PrintRequestAttributeSet;
58
import javax.swing.ImageIcon;
59

    
60
import org.cresques.cts.IProjection;
61
import org.cresques.filter.RasterFilterStack;
62
import org.cresques.io.data.Grid;
63
import org.cresques.io.data.RasterBuf;
64
import org.cresques.io.datastruct.Statistic;
65
import org.cresques.px.Extent;
66

    
67
import com.hardcode.driverManager.Driver;
68
import com.hardcode.driverManager.DriverLoadException;
69
import com.iver.cit.gvsig.fmap.DriverException;
70
import com.iver.cit.gvsig.fmap.MapControl;
71
import com.iver.cit.gvsig.fmap.ViewPort;
72
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
73
import com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver;
74
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
75
import com.iver.cit.gvsig.fmap.layers.layerOperations.StringXMLItem;
76
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
77
import com.iver.utiles.DoubleUtilities;
78
import com.iver.utiles.XMLEntity;
79
import com.iver.utiles.swing.threads.Cancellable;
80

    
81

    
82
/**
83
 * Clase b?sica de capa raster.
84
 *
85
 * @author Vicente Caballero Navarro
86
 */
87
public class FLyrRaster extends FLyrDefault implements RasterOperations {
88
        private RasterAdapter                                 source;
89
        boolean                                                         isPrinting = false;
90
        boolean                                                         mustTileDraw = false;
91
        boolean                                                         mustTilePrint = true;
92
        private int                                                        maxTileDrawWidth = 200;
93
        private int                                                 maxTileDrawHeight = 200;
94
        int                                                                 maxTilePrintWidth = 1500;
95
        int                                                                 maxTilePrintHeight = 1500;
96
        private StatusRasterInterface                status = null;
97
        private boolean                                                firstLoad = false;
98
        private int                                                 posX = 0, posY = 0;
99
        private double                                                 posXWC = 0, posYWC = 0;
100
        private int                                                 r = 0, g = 0, b = 0;
101
        private boolean                                                removeRasterFlag = true;
102

    
103
                        
104
        /**
105
         * Devuelve el RasterAdapter de la capa.
106
         *
107
         * @return RasterAdapter.
108
         */
109
        public RasterAdapter getSource() {
110
                return source;
111
        }
112

    
113
        /**
114
         *Redefine wakeUp de FLyrDefault
115
         */
116
        public void wakeUp(){
117
                /*setName(getName());
118
                setProjection(getProjection());
119
                try {
120
                        load();
121
                } catch (DriverIOException e) {
122
                        e.printStackTrace();
123
                }*/                        
124
        }
125
        
126
        /**
127
         * Inserta el RasterAdapter.
128
         *
129
         * @param ra RasterAdapter.
130
         */
131
        public void setSource(RasterAdapter ra) {
132
                source = ra;
133
        }
134

    
135
        /**
136
         * Devuelve la pila de filtros aplicada sobre  la capa raster.
137
         *
138
         * @return RasterFilterStack.
139
         */
140
        public RasterFilterStack getFilterStack(){
141
                return source.getFilterStack();
142
        }
143
        
144
        /**
145
         * Asignar el estado del raster
146
         * @param status
147
         */
148
        public void setStatus(StatusRasterInterface status){
149
                this.status = status;
150
        }
151
        
152
        /**
153
         * Asigna la pila de filtros aplicada al raster
154
         * @return
155
         */
156
        public void setFilterStack(RasterFilterStack stack){
157
                source.setFilterStack(stack);                
158
        }
159
        
160
        /**
161
         * Obtiene el estado del raster
162
         * @return
163
         */
164
        public StatusRasterInterface getStatus(){
165
                return this.status;
166
        }
167
        
168
        /**
169
         * Asigna la posici?n en la que se ha hecho click al mostrar
170
         * informaci?n del raster.
171
         * @param x        Posici?n en X
172
         * @param y        Posici?n en Y
173
         */
174
        public void setPos(int x, int y){
175
                this.posX = x;
176
                this.posY = y;
177
        }
178
        
179
        /**
180
         * Asigna la posici?n en coordenadas del mundo real en la que se ha 
181
         * hecho click al mostrar informaci?n del raster.
182
         * @param x        Posici?n en X
183
         * @param y        Posici?n en Y
184
         */
185
        public void setPosWC(double x, double y){
186
                this.posXWC = x;
187
                this.posYWC = y;
188
        }
189
        
190
        /**
191
         * Asigna RGB en la posici?n en la que se ha 
192
         * hecho click al mostrar informaci?n del raster.
193
         * @param r        valor de rojo
194
         * @param g        valor de verde
195
         * @param b        valor de azul
196
         */
197
        public void setRGB(int r, int g, int b){
198
                this.r = r;
199
                this.g = g;
200
                this.b = b;
201
        }
202
        
203
        /*
204
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#load()
205
         */
206
        public void load() throws DriverIOException {
207
                ((RasterFileAdapter) source).start();
208
                ((RasterFileAdapter) source).setTransparency(getTransparency());
209
        }
210

    
211
        /**
212
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
213
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
214
         *                 com.iver.utiles.swing.threads.Cancellable)
215
         */
216
        public void draw(BufferedImage image, Graphics2D g, ViewPort vp,
217
                Cancellable cancel,double scale) throws DriverException {
218
                if (isWithinScale(scale)){        
219
                try {
220
                        if(status!=null && firstLoad){
221
                                if(mustTileDraw){
222
                                        Point2D p = vp.getOffset();
223
                                        Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), vp.getImageWidth(), vp.getImageHeight());
224
                                        Tiling tiles = new Tiling(maxTileDrawWidth, maxTileDrawHeight, r);
225
                                        tiles.setAffineTransform((AffineTransform) vp.getAffineTransform().clone());
226
                                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
227
                                                // drawing part
228
                                                try {
229
                                                        ViewPort vport = tiles.getTileViewPort(vp, tileNr);
230
                                                        //g.setClip(tiles.getClip(tileNr).x, tiles.getClip(tileNr).y, tiles.getClip(tileNr).width - 5, tiles.getClip(tileNr).height);
231
                                                        ((RasterFileAdapter) source).draw(image, g, vport, cancel);
232
                                                } catch (NoninvertibleTransformException e) {
233
                                                        e.printStackTrace();
234
                                                }
235
                                        }
236
                                }else
237
                                        ((RasterFileAdapter) source).draw(image, g, vp, cancel);        
238
                                status.applyStatus(this);
239
                                firstLoad = false;
240
                        }
241
                
242
                        if(mustTileDraw){
243
                                Point2D p = vp.getOffset();
244
                                Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), vp.getImageWidth(), vp.getImageHeight());
245
                                Tiling tiles = new Tiling(maxTileDrawWidth, maxTileDrawHeight, r);
246
                                tiles.setAffineTransform((AffineTransform) vp.getAffineTransform().clone());
247
                                for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
248
                                        // drawing part
249
                                        try {
250
                                                ViewPort vport = tiles.getTileViewPort(vp, tileNr);
251
                                                ((RasterFileAdapter) source).draw(image, g, vport, cancel);
252
                                        } catch (NoninvertibleTransformException e) {
253
                                                e.printStackTrace();
254
                                        }
255
                                }
256
                        }else
257
                                ((RasterFileAdapter) source).draw(image, g, vp, cancel);
258
                                                                        
259
                } catch (DriverIOException e) {
260
                        throw new DriverException(e);
261
                }
262

    
263
                if (getVirtualLayers() != null) {
264
                        getVirtualLayers().draw(image, g, vp, cancel,scale);
265
                }
266
                }
267
        }
268

    
269
        /**
270
         * Inserta la proyecci?n.
271
         *
272
         * @param proj Proyecci?n.
273
         */
274
        public void setProjection(IProjection proj) {
275
                super.setProjection(proj);
276

    
277
                if (source != null && source.getDriver() != null && source.getDriver() instanceof GeorreferencedRasterDriver) {
278
                        GeorreferencedRasterDriver geoDrv = (GeorreferencedRasterDriver) source.getDriver();
279

    
280
                        if (geoDrv.getProjection() == null) {
281
                                geoDrv.setProjection(proj);
282
                        }
283
                }
284
        }
285
        
286
        public void setTransparency(int trans) {
287
                super.setTransparency(trans);
288
                ((RasterFileAdapter) source).setTransparency(trans);
289
                if (getMapContext() != null){
290
                        getMapContext().invalidate();
291
                }
292
        }
293

    
294
        public int getTransparency() {
295
                return ((RasterFileAdapter) source).getTransparency();
296
        }
297
        
298
        /*
299
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
300
         */
301
        public Rectangle2D getFullExtent() throws DriverException {
302
                return ((RasterFileAdapter) source).getFullExtent();
303
        }
304

    
305
        /**
306
         * Obtiene el valor del pixel del Image en la posici?n x,y
307
         * @param x Posici?n x
308
         * @param y Posici?n y
309
         * @return valor de pixel
310
         */
311
        public int[] getPixel(double wcx, double wcy){
312
                return ((RasterFileAdapter) source).getPixel(wcx, wcy);
313
        }
314
        
315
        public double getMaxX(){
316
                try {
317
                        return this.getFullExtent().getMaxX();
318
                }catch(DriverException e){
319
                        return 0D;
320
                }
321
        }
322
        
323
        public double getMaxY(){
324
                try {
325
                        return this.getFullExtent().getMaxY();
326
                }catch(DriverException e){
327
                        return 0D;
328
                }
329
        }
330
        
331
        public double getMinX(){
332
                try {
333
                        return this.getFullExtent().getMinX();
334
                }catch(DriverException e){
335
                        return 0D;
336
                }
337
        }
338
        
339
        public double getMinY(){
340
                try {
341
                        return this.getFullExtent().getMinY();
342
                }catch(DriverException e){
343
                        return 0D;
344
                }
345
        }
346
        
347
        public double getHeight(){
348
                try {
349
                        return this.getFullExtent().getHeight();
350
                }catch(DriverException e){
351
                        return 0D;
352
                }
353
        }
354
        
355
        public double getWidth(){
356
                try {
357
                        return this.getFullExtent().getWidth();
358
                }catch(DriverException e){
359
                        return 0D;
360
                }
361
        }
362

    
363
        
364
        /* (non-Javadoc)
365
         * @deprecated. See String getInfo(Point p) throws DriverException
366
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(java.awt.Point)
367
         */
368
        public String queryByPoint(Point p) throws DriverException {
369
                String data = "<file:"+normalizeAsXMLTag(getName())+">\n";
370

    
371
                ArrayList attr = source.getAttributes();
372
                data += "  <raster\n";
373
                data += "    File=\""+((RasterFileAdapter) source).getFile()+"\"\n";
374
                for (int i=0; i<attr.size(); i++) {
375
                        Object [] a = (Object []) attr.get(i);
376

    
377
                        data += "    "+a[0].toString()+"=";
378
                        if (a[1].toString() instanceof String)
379
                                data += "\""+a[1].toString()+"\"\n";
380
                        else
381
                                data += a[1].toString()+"\n";
382
                }
383
                data += "    Point=\""+posX+" , "+posY+"\"\n";
384
                data += "    Point_WC=\""+posXWC+" , "+posYWC+"\"\n";
385
                data += "    RGB=\""+r+", "+g+", "+b+"\"\n";
386
                data += "  />\n";
387

    
388
                data += "</file:"+normalizeAsXMLTag(getName())+">\n";
389
                //System.out.println(data);
390
                return data;
391
        }
392
        
393
        public XMLItem[] getInfo(Point p, double tolerance, Cancellable cancel) throws DriverException {
394
                
395
                Point2D pReal = getMapContext().getViewPort().toMapPoint(p);
396
                Point2D px = null;
397
                if(        pReal.getX() > this.getMinX() && 
398
                        pReal.getX() < this.getMaxX() && 
399
                        pReal.getY() > this.getMinY() && 
400
                        pReal.getY() < this.getMaxY()){
401
                        ArrayList attr = source.getAttributes();
402
                        int w = 0, h = 0;
403
                        for (int i=0; i<attr.size(); i++) {
404
                                Object [] a = (Object []) attr.get(i);
405
                                if(a[0].toString().equals("Width"))
406
                                        w = ((Integer)a[1]).intValue();
407
                                if(a[0].toString().equals("Height"))
408
                                        h = ((Integer)a[1]).intValue();
409
                        }        
410
                        px = new Point2D.Double();
411
                        px.setLocation( ((pReal.getX() - this.getMinX()) * w) / getWidth(),
412
                                                        ((this.getMaxY() - pReal.getY()) * h) / getHeight());
413
                }
414
                        
415
                int[] rgb = this.getPixel(pReal.getX(), pReal.getY());
416
                                
417
                StringXMLItem[] item = new StringXMLItem[1]; 
418
                String data = "<file:"+normalizeAsXMLTag(getName())+">\n";
419
                
420
                data += "  <raster\n";
421
                data += "    View_Point=\""+p.getX()+" , "+p.getY()+"\"\n";
422
                data += "    World_Point=\""+DoubleUtilities.format(pReal.getX(), 5)+" , "+DoubleUtilities.format(pReal.getY(), 5)+"\"\n";
423
                if(px == null)
424
                        data += "    Pixel_Point=\"Out\"\n";
425
                else{
426
                        data += "    Pixel_Point=\""+(int)px.getX()+" , "+(int)px.getY()+"\"\n";
427
                        data += "    Band_Value=\"";
428

    
429
                        for(int i = 0; i < this.getSource().getNumBands(); i++){
430
                                Object pxData = ((RasterDriver)this.getSource().getDriver()).getData((int)px.getX(), (int)px.getY(), i);
431
                                if(pxData != null){
432
                                        if(this.getSource().getDataType() >= 0 && this.getSource().getDataType() <= 3)        
433
                                                        data += ((Integer)pxData).intValue()+"  ";
434
                                        
435
                                        if(this.getSource().getDataType() >= 4)
436
                                                        data += ((Float)pxData).floatValue()+"  ";
437
                                        
438
                                        if(this.getSource().getDataType() >= 5)
439
                                                        data += ((Double)pxData).doubleValue()+"  ";
440
                                }
441
                        }
442
                        data += "\"\n";
443
                }
444

    
445
                data += "    RGB=\""+rgb[1]+"  "+rgb[2]+"  "+rgb[3]+"\"\n";
446
                
447
                data += "  />\n";
448
                data += "</file:"+normalizeAsXMLTag(getName())+">\n";
449
                
450
                item[0] = new StringXMLItem(data, this); 
451
                return item;
452
                
453
        }        
454
        
455
        /**
456
         * Filters a string for being suitable as XML Tag, erasing
457
         * all not alphabetic or numeric characters.
458
         * @param s
459
         * @return string normalized
460
         */
461
        public String normalizeAsXMLTag(String s) {
462
                return s.replaceAll("[^a-zA-Z0-9]","");
463
        }
464

    
465
        /**
466
         * Obtiene atributos a partir de un georasterfile
467
         * @return
468
         */
469
        public ArrayList getAttributes() {
470
                return source.getAttributes();
471
        }
472
        
473
        /**
474
         * @throws XMLException
475
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
476
         */
477
        public XMLEntity getXMLEntity() throws XMLException {
478
                XMLEntity xml = super.getXMLEntity();
479
        
480
                if (source instanceof RasterFileAdapter) {
481
                        xml.putProperty("file", ((RasterFileAdapter) source).getFile());
482
                }
483
        
484
                xml.putProperty("driverName", getSource().getDriver().getName());
485
                
486
                //Si no hay ning?n Status aplicamos el StatusLayerRaster que se usa por defecto
487
                if(status == null)
488
                        status = new StatusLayerRaster();
489
                
490
                status.getXMLEntity(xml, true, this);
491
                
492
        
493
                return xml;
494
        }
495
        
496
        public void setXMLEntity03(XMLEntity xml)
497
        throws XMLException {
498
                super.setXMLEntity(xml);
499
                try {
500
                        Driver d = LayerFactory.getDM().getDriver(
501
                                xml.getStringProperty("driverName"));
502
                        File f = new File(xml.getStringProperty("file"));
503
                        RasterAdapter adapter = new RasterFileAdapter(f);
504
                        adapter.setDriver(d);
505
                        setSource(adapter);
506
                        // Para notificar al adapter-driver cual es la proyecci?n.
507
                        setProjection(super.getProjection());
508
                } catch (DriverLoadException e) {
509
                        throw new XMLException(e);
510
                }
511
        }
512
        
513
        public void setXMLEntity(XMLEntity xml)
514
        throws XMLException {
515
                super.setXMLEntity(xml);
516
                try {
517
                        Driver d = LayerFactory.getDM().getDriver(
518
                                xml.getStringProperty("driverName"));
519
                        File f = new File(xml.getStringProperty("file"));
520
                        RasterAdapter adapter = new RasterFileAdapter(f);
521
                        adapter.setDriver(d);
522
                        setSource(adapter);
523
                        // Para notificar al adapter-driver cual es la proyecci?n.
524
                        setProjection(super.getProjection());
525
                        
526
                        //Inicializamos la clase a la que se usa por defecto para
527
                        //compatibilidad con proyectos antiguos
528
                        String claseStr = StatusLayerRaster.defaultClass;
529
                        if (xml.contains("raster.class")) {
530
                                claseStr = xml.getStringProperty("raster.class");
531
                        }
532
                        if(status!=null)
533
                                status.setXMLEntity(xml, this);
534
                        else{
535
                                
536
                                //Cuando cargamos un proyecto 
537
                                
538
                                if(claseStr!=null && !claseStr.equals("")){
539
                                        try{
540
                                                Class clase = Class.forName(claseStr);
541
                                                Constructor constr = clase.getConstructor(null);
542
                                                status = (StatusRasterInterface)constr.newInstance(null);
543
                                                if(status!=null)
544
                                                        status.setXMLEntity(xml, this);
545
                                        }catch(ClassNotFoundException exc){
546
                                                exc.printStackTrace();
547
                                        }catch(InstantiationException exc){
548
                                                exc.printStackTrace();
549
                                        }catch(IllegalAccessException exc){
550
                                                exc.printStackTrace();
551
                                        }catch(NoSuchMethodException exc){
552
                                                exc.printStackTrace();
553
                                        }catch(InvocationTargetException exc){
554
                                                exc.printStackTrace();
555
                                        }                                        
556
                                }
557
                        }
558
                        firstLoad = true;
559
                        
560
                        
561
                } catch (DriverLoadException e) {
562
                        throw new XMLException(e);
563
                }
564
        }
565

    
566
        /* (non-Javadoc)
567
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.operations.Cancellable)
568
         */
569
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet propeties)
570
                throws DriverException {
571
                
572
                if (isVisible() && isWithinScale(scale)){        
573
                isPrinting = true;
574
                if (!mustTilePrint) {
575
                        draw(null, g, viewPort, cancel,scale);
576
                } else {
577
                // Para no pedir imagenes demasiado grandes, vamos
578
                // a hacer lo mismo que hace EcwFile: chunkear.
579
                // Llamamos a drawView con cuadraditos m?s peque?os
580
                // del BufferedImage ni caso, cuando se imprime viene con null
581
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
582
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
583
                        
584
                        //Si es la primera lectura salvamos los valores de m?ximo y m?nimo para la aplicaci?n
585
                        //de realce si la imagen es de 16 bits.
586
                        
587
                        Statistic stats = getSource().getFilterStack().getStats();
588
                        try{
589
                                if(stats != null)
590
                                        stats.history.add(stats.new History(getName(), stats.minBandValue, stats.maxBandValue, stats.secondMinBandValue, stats.secondMaxBandValue));
591
                        }catch(NoSuchFieldError e){
592
                                e.printStackTrace();
593
                        }
594
                                        
595
                        
596
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
597
                            // Parte que dibuja
598
                            try {
599
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
600
                                draw(null, g, vp, cancel,scale);
601
                                } catch (NoninvertibleTransformException e) {
602
                                        // TODO Auto-generated catch block
603
                                        e.printStackTrace();
604
                                }
605
                }
606
                        
607
                        if(stats != null){
608
                                getSource().getFilterStack().getStats().history.clear();
609
                                stats = getSource().getFilterStack().getStats();
610
                        }
611
                                                
612
                }
613
            isPrinting = false;
614
                }
615
        }
616

    
617
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
618
                throws DriverException {                
619
                // Para no pedir imagenes demasiado grandes, vamos
620
                // a hacer lo mismo que hace EcwFile: chunkear.
621
                // Llamamos a drawView con cuadraditos m?s peque?os
622
                // del BufferedImage ni caso, cuando se imprime viene con null
623
                
624
                int numW, numH;
625
                int stepX, stepY;
626
                int xProv, yProv, wProv, hProv;
627
                double xProvD, yProvD, wProvD, hProvD;
628
                int A = 1500; 
629
                int H = 1500;
630
                int altoAux, anchoAux;
631
                
632
                AffineTransform mat = (AffineTransform) viewPort.getAffineTransform().clone();
633
                
634
                // Vamos a hacerlo en trozos de AxH
635
                Rectangle r = g.getClipRect();
636
                numW = (int) (r.width) / A;
637
                numH = (int) (r.height) / H;                
638
                
639
                
640
                double[] srcPts = new double[8];
641
                double[] dstPts= new double[8];
642

    
643
                yProv = (int) r.y;
644
                for (stepY=0; stepY < numH+1; stepY++)
645
                {
646
                        if ((yProv + H) > r.getMaxY()) 
647
                                altoAux = (int) r.getMaxY() - yProv;
648
                        else
649
                                altoAux = H;
650
                                                
651
                        xProv = (int) r.x;
652
                        for (stepX=0; stepX < numW+1; stepX++)                                
653
                        {                        
654
                                    if ((xProv + A) > r.getMaxX()) 
655
                                            anchoAux = (int) r.getMaxX() - xProv;
656
                                    else
657
                                            anchoAux = A;
658
                                
659
                                Rectangle newRect = new Rectangle(xProv, yProv, anchoAux, altoAux);
660
                                
661
                                // Parte que dibuja
662
                                srcPts[0] = xProv;
663
                                srcPts[1] = yProv;
664
                                srcPts[2] = xProv + anchoAux+1;
665
                                srcPts[3] = yProv;
666
                                srcPts[4] = xProv + anchoAux+1;
667
                                srcPts[5] = yProv + altoAux+1;
668
                                srcPts[6] = xProv;
669
                                srcPts[7] = yProv + altoAux+1;
670
                                
671
                                try {
672
                                                mat.inverseTransform(srcPts, 0, dstPts, 0, 4);
673
                                        Rectangle2D.Double rectCuadricula = new Rectangle2D.Double(
674
                                                        dstPts[0], dstPts[1],
675
                                                                dstPts[2] - dstPts[0], dstPts[5]-dstPts[3]); 
676
                                        Extent extent = new Extent(rectCuadricula);
677
                                        
678
                                        Dimension tam = new Dimension(anchoAux+1, altoAux+1);
679
                                        ViewPort vp = viewPort.cloneViewPort();
680
                                        vp.setImageSize(tam);
681
                                        vp.setExtent(rectCuadricula);
682
                                        vp.setAffineTransform(mat);
683
                                        // ViewPortData vp = new ViewPortData(getProjection(), extent, tam);
684
                                        // vp.setMat(mat);
685
                                        // rasterList.draw(g2, vp);
686
                                                                                
687
                                        /*System.out.println("FLyrRaster.print(): fila "+stepX+" de "
688
                                                + numW + " , col "+stepY+" de " + numH + 
689
                                                "\n, Extent = "+vp.getExtent() + " imageSize: "
690
                                                + tam);*/
691
                                        draw(null, g, vp, cancel,scale);
692
                                                
693
                                        } catch (NoninvertibleTransformException e) {
694
                                                // TODO Auto-generated catch block
695
                                                e.printStackTrace();
696
                                        }
697
                                // Fin parte que dibuja
698
                                        xProv = xProv + A;        
699
                        }                        
700
                        yProv = yProv + H;
701
                }  
702
                
703
        }
704
        
705
        /**
706
         * A?ade un fichero a la capa raster
707
         * @param fileName Nombre del fichero
708
         */
709
        public void addFiles(String fileName){
710
                ((RasterFileAdapter) source).addFile(fileName);
711
        }
712
        
713
        /**
714
         * Elimina un fichero a la capa raster
715
         * @param fileName Nombre del fichero
716
         */
717
        public void delFile(String fileName){
718
                ((RasterFileAdapter) source).delFile(fileName);
719
        }
720
        
721
        /* (non-Javadoc)
722
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setBand(int, int)
723
         */
724
        public void setBand(int flag, int nBand) {
725
                this.getSource().setBand(flag, nBand);
726
        }
727
        
728
        /**
729
         * Borra de la lista de listeners el que se pasa como par?metro.
730
         *
731
         * @param o LayerListener a borrar.
732
         *
733
         * @return True si ha sido correcto el borrado del Listener.
734
         */
735
        public boolean removeLayerListener(LayerListener o) {
736
                if(this.isRemoveRasterFlag()){
737
                        /*try{
738
                                ((RasterFileAdapter) source).stop();
739
                        }catch(DriverIOException exc){
740
                                
741
                        }*/
742
                        this.setRemoveRasterFlag(true);
743
                }
744
                return super.layerListeners.remove(o);
745
        }
746
        
747
        /**
748
         * @return Returns the removeRasterFlag.
749
         */
750
        public boolean isRemoveRasterFlag() {
751
                return removeRasterFlag;
752
        }
753
        
754
        /**
755
         * Asigna el valor del flag que dice si destruimos la memoria del raster
756
         * al eliminarlo del TOC o  no.
757
         * @param removeRasterFlag The removeRasterFlag to set.
758
         */
759
        public void setRemoveRasterFlag(boolean removeRasterFlag) {
760
                this.removeRasterFlag = removeRasterFlag;
761
        }
762
        
763
        public ImageIcon getTocImageIcon() {                        
764
                return new ImageIcon(MapControl.class.getResource("images/icolayerRaster.PNG"));
765
        }
766
        
767
        /*
768
         *  (non-Javadoc)
769
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getTileSize()
770
         */
771
        public int[] getTileSize() {
772
                int[] size = {maxTileDrawWidth, maxTileDrawHeight}; 
773
                return size;
774
        }
775

    
776
        /*
777
         *  (non-Javadoc)
778
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#isTiled()
779
         */
780
        public boolean isTiled() {
781
                return mustTileDraw;
782
        }
783
        
784
        /**
785
         * Obtiene el flag que dice si la imagen est? o no georreferenciada
786
         * @return true si est? georreferenciada y false si no lo est?.
787
         */
788
        public boolean isGeoreferenced() {
789
                return getSource().isGeoreferenced();
790
        }
791
        
792
        /**
793
         * Obtiene el grid asociado
794
         * @return grid
795
         */
796
        public Grid getGrid(){
797
                return getSource().getGrid();
798
        }
799
        
800
        /**
801
         * Obtiene el tipo de dato de la capa raster
802
         * @return Entero que representa el tipo de dato de la capa raster.
803
         */
804
        public int getDataType() {
805
                return getSource().getDataType();
806
        }
807
        
808
        /*
809
         *  (non-Javadoc)
810
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#cloneLayer()
811
         */
812
        public FLayer cloneLayer() throws Exception {
813
                FLyrRaster clonedLayer = new FLyrRaster();
814
                
815
                File f = new File(getSource().getFiles()[0].getName());
816
                RasterAdapter adapter = new RasterFileAdapter(f);
817
                com.hardcode.driverManager.Driver driver = LayerFactory.getDM().getDriver("gvSIG Image Driver");
818
                adapter.setDriver(driver);
819

    
820
                clonedLayer.setName(getName());
821
                clonedLayer.setVisible(isVisible());
822
                clonedLayer.setSource(adapter);
823
                clonedLayer.setProjection(getProjection());
824
                                        
825
                clonedLayer.setXMLEntity(getXMLEntity());
826
                
827
                clonedLayer.load();
828
                
829
                return clonedLayer;
830
        }
831
        
832
        /**
833
         * Set data buffer to create a memory raster driver
834
         * @param buf data buffer
835
         */
836
        public void setBuffer(RasterBuf buf){
837
                source.setBuffer(buf);
838
        }
839

    
840
        /**
841
         * Set extent to create a memory raster driver
842
         * @param ext Extent
843
         */
844
        public void setExtent(Extent ext){
845
                source.setExtent(ext);
846
        }
847
        
848
        public boolean isReprojectable() {
849
                return false;
850
        }
851

    
852
        public boolean reProject(MapControl mapC) {
853

    
854
                //Descomentar cuando se valla a habilitar la reproyecci?n de raster
855
                
856
                //                ViewPort vPort = mapC.getViewPort();
857
//                ICoordTrans ct = this.getProjection().getCT(vPort.getProjection());
858
//                GeoRasterFile grf = ((CmsRasterDriver)this.getSource().getDriver()).getRasterFile();
859
//                this.setCoordTrans(ct);
860
//                if(grf instanceof GdalFile){
861
//                        if((((GdalFile)grf).isGeoreferenced()) && (((GdalFile)grf).hasProjection())){
862
//                                ReprojectRaster repRaster = new ReprojectRaster(this, file, mapC);
863
//                                repRaster.warp(vPort.getProjection());
864
//                                return false;
865
//                        }
866
//                }
867
//                System.err.println("coordTrans = " + this.getProjection().getAbrev() + " "
868
//                                + vPort.getProjection().getAbrev());
869
                //Si no termina el m?todo por el anterior return es que no se ha reproyectado por lo que es AddLayer el
870
                //que tiene que cargar la capa.
871
                return true;
872
        }
873
        
874
}