Statistics
| Revision:

root / branches / FMap04 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrRaster.java @ 3071

History | View | Annotate | Download (16.2 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.Rectangle2D;
50
import java.awt.image.BufferedImage;
51
import java.io.File;
52
import java.lang.reflect.Constructor;
53
import java.lang.reflect.InvocationTargetException;
54
import java.util.ArrayList;
55

    
56
import org.cresques.cts.IProjection;
57
import org.cresques.io.raster.RasterFilterStack;
58
import org.cresques.io.raster.RasterStats;
59
import org.cresques.io.raster.RasterStats.History;
60
import org.cresques.px.Extent;
61

    
62
import com.hardcode.driverManager.Driver;
63
import com.hardcode.driverManager.DriverLoadException;
64
import com.iver.cit.gvsig.fmap.DriverException;
65
import com.iver.cit.gvsig.fmap.ViewPort;
66
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
67
import com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver;
68
import com.iver.cit.gvsig.fmap.operations.Cancellable;
69
import com.iver.utiles.XMLEntity;
70

    
71

    
72
/**
73
 * Clase b?sica de capa raster.
74
 *
75
 * @author Vicente Caballero Navarro
76
 */
77
public class FLyrRaster extends FLyrDefault implements RasterOperations {
78
        private RasterAdapter                                 source;
79
        boolean                                                         isPrinting = false;
80
        boolean                                                         mustTileDraw = false;
81
        boolean                                                         mustTilePrint = true;
82
        int                                                                 maxTileDrawWidth = -1;
83
        int                                                                 maxTileDrawHeight = -1;
84
        int                                                                 maxTilePrintWidth = 1500;
85
        int                                                                 maxTilePrintHeight = 1500;
86
        private StatusRasterInterface                status = null;
87
        private boolean                                                firstLoad = false;
88
        private int                                                 posX = 0, posY = 0;
89
        private double                                                 posXWC = 0, posYWC = 0;
90
        private int                                                 r = 0, g = 0, b = 0;
91
                
92
        /**
93
         * Devuelve el RasterAdapter de la capa.
94
         *
95
         * @return RasterAdapter.
96
         */
97
        public RasterAdapter getSource() {
98
                return source;
99
        }
100

    
101
        /**
102
         * Inserta el RasterAdapter.
103
         *
104
         * @param ra RasterAdapter.
105
         */
106
        public void setSource(RasterAdapter ra) {
107
                source = ra;
108
        }
109

    
110
        /**
111
         * Devuelve la pila de filtros aplicada sobre  la capa raster.
112
         *
113
         * @return RasterFilterStack.
114
         */
115
        public RasterFilterStack getFilterStack(){
116
                return source.getFilterStack();
117
        }
118
        
119
        /**
120
         * Asignar el estado del raster
121
         * @param status
122
         */
123
        public void setStatus(StatusRasterInterface status){
124
                this.status = status;
125
        }
126
        
127
        /**
128
         * Asigna la pila de filtros aplicada al raster
129
         * @return
130
         */
131
        public void setFilterStack(RasterFilterStack stack){
132
                source.setFilterStack(stack);                
133
        }
134
        
135
        /**
136
         * Obtiene el estado del raster
137
         * @return
138
         */
139
        public StatusRasterInterface getStatus(){
140
                return this.status;
141
        }
142
        
143
        /**
144
         * Asigna la posici?n en la que se ha hecho click al mostrar
145
         * informaci?n del raster.
146
         * @param x        Posici?n en X
147
         * @param y        Posici?n en Y
148
         */
149
        public void setPos(int x, int y){
150
                this.posX = x;
151
                this.posY = y;
152
        }
153
        
154
        /**
155
         * Asigna la posici?n en coordenadas del mundo real en la que se ha 
156
         * hecho click al mostrar informaci?n del raster.
157
         * @param x        Posici?n en X
158
         * @param y        Posici?n en Y
159
         */
160
        public void setPosWC(double x, double y){
161
                this.posXWC = x;
162
                this.posYWC = y;
163
        }
164
        
165
        /**
166
         * Asigna RGB en la posici?n en la que se ha 
167
         * hecho click al mostrar informaci?n del raster.
168
         * @param r        valor de rojo
169
         * @param g        valor de verde
170
         * @param b        valor de azul
171
         */
172
        public void setRGB(int r, int g, int b){
173
                this.r = r;
174
                this.g = g;
175
                this.b = b;
176
        }
177
        
178
        /*
179
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#load()
180
         */
181
        public void load() throws DriverIOException {
182
                ((RasterFileAdapter) source).start();
183
                ((RasterFileAdapter) source).setTransparency(getTransparency());
184
        }
185

    
186
        /**
187
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
188
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
189
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
190
         */
191
        public void draw(BufferedImage image, Graphics2D g, ViewPort vp,
192
                Cancellable cancel,double scale) throws DriverException {
193
                if (isWithinScale(scale)){        
194
                try {
195
                        if(status!=null && firstLoad){
196
                                ((RasterFileAdapter) source).draw(image, g, vp, cancel);        
197
                                status.applyStatus(this);
198
                                firstLoad = false;
199
                        }
200
                
201
                        ((RasterFileAdapter) source).draw(image, g, vp, cancel);
202
                                                                        
203
                } catch (DriverIOException e) {
204
                        throw new DriverException(e);
205
                }
206

    
207
                if (getVirtualLayers() != null) {
208
                        getVirtualLayers().draw(image, g, vp, cancel,scale);
209
                }
210
                }
211
        }
212

    
213
        /**
214
         * Inserta la proyecci?n.
215
         *
216
         * @param proj Proyecci?n.
217
         */
218
        public void setProjection(IProjection proj) {
219
                super.setProjection(proj);
220

    
221
                if (source != null && source.getDriver() != null && source.getDriver() instanceof GeorreferencedRasterDriver) {
222
                        GeorreferencedRasterDriver geoDrv = (GeorreferencedRasterDriver) source.getDriver();
223

    
224
                        if (geoDrv.getProjection() == null) {
225
                                geoDrv.setProjection(proj);
226
                        }
227
                }
228
        }
229
        
230
        public void setTransparency(int trans) {
231
                super.setTransparency(trans);
232
                ((RasterFileAdapter) source).setTransparency(trans);
233
                // TODO LWS Guarradita para actualizar el MapControl con ApplyButton
234
                if (getFMap() != null)
235
                        getFMap().invalidate();
236
        }
237

    
238
        public int getTransparency() {
239
                return ((RasterFileAdapter) source).getTransparency();
240
        }
241
        
242
        /*
243
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
244
         */
245
        public Rectangle2D getFullExtent() throws DriverException {
246
                return ((RasterFileAdapter) source).getFullExtent();
247
        }
248

    
249
        /**
250
         * Obtiene el valor del pixel del Image en la posici?n x,y
251
         * @param x Posici?n x
252
         * @param y Posici?n y
253
         * @return valor de pixel
254
         */
255
        public int[] getPixel(double wcx, double wcy){
256
                return ((RasterFileAdapter) source).getPixel(wcx, wcy);
257
        }
258
        
259
        public double getMaxX(){
260
                try {
261
                        return this.getFullExtent().getMaxX();
262
                }catch(DriverException e){
263
                        return 0D;
264
                }
265
        }
266
        
267
        public double getMaxY(){
268
                try {
269
                        return this.getFullExtent().getMaxY();
270
                }catch(DriverException e){
271
                        return 0D;
272
                }
273
        }
274
        
275
        public double getMinX(){
276
                try {
277
                        return this.getFullExtent().getMinX();
278
                }catch(DriverException e){
279
                        return 0D;
280
                }
281
        }
282
        
283
        public double getMinY(){
284
                try {
285
                        return this.getFullExtent().getMinY();
286
                }catch(DriverException e){
287
                        return 0D;
288
                }
289
        }
290
        
291
        public double getHeight(){
292
                try {
293
                        return this.getFullExtent().getHeight();
294
                }catch(DriverException e){
295
                        return 0D;
296
                }
297
        }
298
        
299
        public double getWidth(){
300
                try {
301
                        return this.getFullExtent().getWidth();
302
                }catch(DriverException e){
303
                        return 0D;
304
                }
305
        }
306

    
307
        
308
        /* (non-Javadoc)
309
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(java.awt.Point)
310
         */
311
        public String queryByPoint(Point p) throws DriverException {
312
                String data = "<file:"+getName()+">\n";
313

    
314
                ArrayList attr = source.getAttributes();
315
                data += "  <raster\n";
316
                data += "    File=\""+((RasterFileAdapter) source).getFile()+"\"\n";
317
                for (int i=0; i<attr.size(); i++) {
318
                        Object [] a = (Object []) attr.get(i);
319

    
320
                        data += "    "+a[0].toString()+"=";
321
                        if (a[1].toString() instanceof String)
322
                                data += "\""+a[1].toString()+"\"\n";
323
                        else
324
                                data += a[1].toString()+"\n";
325
                }
326
                data += "    Point=\""+posX+" , "+posY+"\"\n";
327
                data += "    Point_WC=\""+posXWC+" , "+posYWC+"\"\n";
328
                data += "    RGB=\""+r+", "+g+", "+b+"\"\n";
329
                data += "  />\n";
330

    
331
                data += "</file:"+getName()+">\n";
332
                //System.out.println(data);
333
                return data;
334
        }
335

    
336
        /**
337
         * Obtiene atributos a partir de un georasterfile
338
         * @return
339
         */
340
        public ArrayList getAttributes() {
341
                return source.getAttributes();
342
        }
343
        
344
        /**
345
         * @throws XMLException
346
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
347
         */
348
        public XMLEntity getXMLEntity() throws XMLException {
349
                XMLEntity xml = super.getXMLEntity();
350
        
351
                if (source instanceof RasterFileAdapter) {
352
                        xml.putProperty("file", ((RasterFileAdapter) source).getFile());
353
                }
354
        
355
                xml.putProperty("driverName", getSource().getDriver().getName());
356
                //xml.putProperty("raster.opacityLevel", ""+getTransparency());
357
                if(status!=null)
358
                        status.getXMLEntity(xml, true, this);
359
        
360
                return xml;
361
        }
362
        
363
        public void setXMLEntity03(XMLEntity xml)
364
        throws XMLException {
365
                super.setXMLEntity(xml);
366
                try {
367
                        Driver d = LayerFactory.getDM().getDriver(
368
                                xml.getStringProperty("driverName"));
369
                        File f = new File(xml.getStringProperty("file"));
370
                        RasterAdapter adapter = new RasterFileAdapter(f);
371
                        adapter.setDriver(d);
372
                        setSource(adapter);
373
                        // Para notificar al adapter-driver cual es la proyecci?n.
374
                        setProjection(super.getProjection());
375
                } catch (DriverLoadException e) {
376
                        throw new XMLException(e);
377
                }
378
        }
379
        
380
        public void setXMLEntity(XMLEntity xml)
381
        throws XMLException {
382
                super.setXMLEntity(xml);
383
                try {
384
                        Driver d = LayerFactory.getDM().getDriver(
385
                                xml.getStringProperty("driverName"));
386
                        File f = new File(xml.getStringProperty("file"));
387
                        RasterAdapter adapter = new RasterFileAdapter(f);
388
                        adapter.setDriver(d);
389
                        setSource(adapter);
390
                        // Para notificar al adapter-driver cual es la proyecci?n.
391
                        setProjection(super.getProjection());
392
                        String claseStr = null;
393
                        if (xml.contains("raster.class")) {
394
                                claseStr = xml.getStringProperty("raster.class");
395
                        }
396
                        if(status!=null)
397
                                status.setXMLEntity(xml, this);
398
                        else{
399
                                
400
                                //Cuando cargamos un proyecto 
401
                                
402
                                if(claseStr!=null && !claseStr.equals("")){
403
                                        try{
404
                                                Class clase = Class.forName(claseStr);
405
                                                Constructor constr = clase.getConstructor(null);
406
                                                status = (StatusRasterInterface)constr.newInstance(null);
407
                                                if(status!=null)
408
                                                        status.setXMLEntity(xml, this);
409
                                        }catch(ClassNotFoundException exc){
410
                                                exc.printStackTrace();
411
                                        }catch(InstantiationException exc){
412
                                                exc.printStackTrace();
413
                                        }catch(IllegalAccessException exc){
414
                                                exc.printStackTrace();
415
                                        }catch(NoSuchMethodException exc){
416
                                                exc.printStackTrace();
417
                                        }catch(InvocationTargetException exc){
418
                                                exc.printStackTrace();
419
                                        }                                        
420
                                }
421
                        }
422
                        firstLoad = true;
423
                        
424
                        
425
                } catch (DriverLoadException e) {
426
                        throw new XMLException(e);
427
                }
428
        }
429

    
430
        /* (non-Javadoc)
431
         * @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)
432
         */
433
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
434
                throws DriverException {
435
                if (isVisible() && isWithinScale(scale)){        
436
                isPrinting = true;
437
                if (!mustTilePrint) {
438
                        draw(null, g, viewPort, cancel,scale);
439
                } else {
440
                // Para no pedir imagenes demasiado grandes, vamos
441
                // a hacer lo mismo que hace EcwFile: chunkear.
442
                // Llamamos a drawView con cuadraditos m?s peque?os
443
                // del BufferedImage ni caso, cuando se imprime viene con null
444
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
445
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
446
                        
447
                        //Si es la primera lectura salvamos los valores de m?ximo y m?nimo para la aplicaci?n
448
                        //de realce si la imagen es de 16 bits.
449
                        
450
                        RasterStats stats = getSource().getFilterStack().getStats();
451
                        if(stats != null)
452
                                stats.history.add(stats.new History(getName(), stats.minBandValue, stats.maxBandValue, stats.secondMinBandValue, stats.secondMaxBandValue));        
453
                                        
454
                        
455
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
456
                            // Parte que dibuja
457
                            try {
458
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
459
                                draw(null, g, vp, cancel,scale);
460
                                } catch (NoninvertibleTransformException e) {
461
                                        // TODO Auto-generated catch block
462
                                        e.printStackTrace();
463
                                }
464
                }
465
                        
466
                        if(stats != null){
467
                                getSource().getFilterStack().getStats().history.clear();
468
                                stats = getSource().getFilterStack().getStats();
469
                        }
470
                }
471
            isPrinting = false;
472
                }
473
        }
474

    
475
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
476
                throws DriverException {                
477
                // Para no pedir imagenes demasiado grandes, vamos
478
                // a hacer lo mismo que hace EcwFile: chunkear.
479
                // Llamamos a drawView con cuadraditos m?s peque?os
480
                // del BufferedImage ni caso, cuando se imprime viene con null
481
                int numW, numH;
482
                int stepX, stepY;
483
                int xProv, yProv, wProv, hProv;
484
                double xProvD, yProvD, wProvD, hProvD;
485
                int A = 1500; 
486
                int H = 1500;
487
                int altoAux, anchoAux;
488
                
489
                AffineTransform mat = (AffineTransform) viewPort.getAffineTransform().clone();
490
                
491
                // Vamos a hacerlo en trozos de AxH
492
                Rectangle r = g.getClipRect();
493
                numW = (int) (r.width) / A;
494
                numH = (int) (r.height) / H;                
495
                
496
                
497
                double[] srcPts = new double[8];
498
                double[] dstPts= new double[8];
499

    
500
                yProv = (int) r.y;
501
                for (stepY=0; stepY < numH+1; stepY++)
502
                {
503
                        if ((yProv + H) > r.getMaxY()) 
504
                                altoAux = (int) r.getMaxY() - yProv;
505
                        else
506
                                altoAux = H;
507
                                                
508
                        xProv = (int) r.x;
509
                        for (stepX=0; stepX < numW+1; stepX++)                                
510
                        {                        
511
                                    if ((xProv + A) > r.getMaxX()) 
512
                                            anchoAux = (int) r.getMaxX() - xProv;
513
                                    else
514
                                            anchoAux = A;
515
                                
516
                                Rectangle newRect = new Rectangle(xProv, yProv, anchoAux, altoAux);
517
                                
518
                                // Parte que dibuja
519
                                srcPts[0] = xProv;
520
                                srcPts[1] = yProv;
521
                                srcPts[2] = xProv + anchoAux+1;
522
                                srcPts[3] = yProv;
523
                                srcPts[4] = xProv + anchoAux+1;
524
                                srcPts[5] = yProv + altoAux+1;
525
                                srcPts[6] = xProv;
526
                                srcPts[7] = yProv + altoAux+1;
527
                                
528
                                try {
529
                                                mat.inverseTransform(srcPts, 0, dstPts, 0, 4);
530
                                        Rectangle2D.Double rectCuadricula = new Rectangle2D.Double(
531
                                                        dstPts[0], dstPts[1],
532
                                                                dstPts[2] - dstPts[0], dstPts[5]-dstPts[3]); 
533
                                        Extent extent = new Extent(rectCuadricula);
534
                                        
535
                                        Dimension tam = new Dimension(anchoAux+1, altoAux+1);
536
                                        ViewPort vp = viewPort.cloneViewPort();
537
                                        vp.setImageSize(tam);
538
                                        vp.setExtent(rectCuadricula);
539
                                        vp.setAffineTransform(mat);
540
                                        // ViewPortData vp = new ViewPortData(getProjection(), extent, tam);
541
                                        // vp.setMat(mat);
542
                                        // rasterList.draw(g2, vp);
543
                                                                                
544
                                        /*System.out.println("FLyrRaster.print(): fila "+stepX+" de "
545
                                                + numW + " , col "+stepY+" de " + numH + 
546
                                                "\n, Extent = "+vp.getExtent() + " imageSize: "
547
                                                + tam);*/
548
                                        draw(null, g, vp, cancel,scale);
549
                                                
550
                                        } catch (NoninvertibleTransformException e) {
551
                                                // TODO Auto-generated catch block
552
                                                e.printStackTrace();
553
                                        }
554
                                // Fin parte que dibuja
555
                                        xProv = xProv + A;        
556
                        }                        
557
                        yProv = yProv + H;
558
                }  
559
                
560
        }
561
        
562
        /**
563
         * A?ade un fichero a la capa raster
564
         * @param fileName Nombre del fichero
565
         */
566
        public void addFiles(String fileName){
567
                ((RasterFileAdapter) source).addFile(fileName);
568
        }
569
        
570
        /**
571
         * Elimina un fichero a la capa raster
572
         * @param fileName Nombre del fichero
573
         */
574
        public void delFile(String fileName){
575
                ((RasterFileAdapter) source).delFile(fileName);
576
        }
577
        
578
        /* (non-Javadoc)
579
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setBand(int, int)
580
         */
581
        public void setBand(int flag, int nBand) {
582
                this.getSource().setBand(flag, nBand);
583
        }
584
}