Statistics
| Revision:

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

History | View | Annotate | Download (22.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.datastruct.Statistic;
64
import org.cresques.px.Extent;
65

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

    
80

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
770
        /*
771
         *  (non-Javadoc)
772
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#isTiled()
773
         */
774
        public boolean isTiled() {
775
                return mustTileDraw;
776
        }
777
        
778
        /**
779
         * Obtiene el flag que dice si la imagen est? o no georreferenciada
780
         * @return true si est? georreferenciada y false si no lo est?.
781
         */
782
        public boolean isGeoreferenced() {
783
                return getSource().isGeoreferenced();
784
        }
785
        
786
        /**
787
         * Obtiene el grid asociado
788
         * @return grid
789
         */
790
        public Grid getGrid(){
791
                return getSource().getGrid();
792
        }
793
        
794
        /**
795
         * Obtiene el tipo de dato de la capa raster
796
         * @return Entero que representa el tipo de dato de la capa raster.
797
         */
798
        public int getDataType() {
799
                return getSource().getDataType();
800
        }
801
}