Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrRaster.java @ 2859

History | View | Annotate | Download (15.8 KB)

1 1100 fjp
/* 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 214 fernando
package com.iver.cit.gvsig.fmap.layers;
42
43 1040 fjp
import java.awt.Dimension;
44 1034 vcaballero
import java.awt.Graphics2D;
45
import java.awt.Point;
46 1040 fjp
import java.awt.Rectangle;
47
import java.awt.geom.AffineTransform;
48
import java.awt.geom.NoninvertibleTransformException;
49 1034 vcaballero
import java.awt.geom.Rectangle2D;
50
import java.awt.image.BufferedImage;
51 1455 luisw
import java.io.File;
52 2183 fernando
import java.lang.reflect.Constructor;
53
import java.lang.reflect.InvocationTargetException;
54 1680 luisw
import java.util.ArrayList;
55 1034 vcaballero
56 1680 luisw
import org.cresques.cts.IProjection;
57 2623 nacho
import org.cresques.io.raster.RasterFilterStack;
58 1680 luisw
import org.cresques.px.Extent;
59 1034 vcaballero
60 1680 luisw
import com.hardcode.driverManager.Driver;
61
import com.hardcode.driverManager.DriverLoadException;
62
import com.iver.cit.gvsig.fmap.DriverException;
63
import com.iver.cit.gvsig.fmap.ViewPort;
64
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
65
import com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver;
66
import com.iver.cit.gvsig.fmap.operations.Cancellable;
67
import com.iver.utiles.XMLEntity;
68
69
70 526 vcaballero
/**
71 1034 vcaballero
 * Clase b?sica de capa raster.
72 526 vcaballero
 *
73
 * @author Vicente Caballero Navarro
74
 */
75 567 luisw
public class FLyrRaster extends FLyrDefault implements RasterOperations {
76 2183 fernando
        private RasterAdapter                                 source;
77
        boolean                                                         isPrinting = false;
78
        boolean                                                         mustTileDraw = false;
79
        boolean                                                         mustTilePrint = true;
80
        int                                                                 maxTileDrawWidth = -1;
81
        int                                                                 maxTileDrawHeight = -1;
82
        int                                                                 maxTilePrintWidth = 1500;
83
        int                                                                 maxTilePrintHeight = 1500;
84
        private StatusRasterInterface                status = null;
85
        private boolean                                                firstLoad = false;
86
        private int                                                 posX = 0, posY = 0;
87
        private double                                                 posXWC = 0, posYWC = 0;
88 2322 igbrotru
        private int                                                 r = 0, g = 0, b = 0;
89 2647 nacho
90 1034 vcaballero
        /**
91
         * Devuelve el RasterAdapter de la capa.
92
         *
93
         * @return RasterAdapter.
94
         */
95
        public RasterAdapter getSource() {
96
                return source;
97
        }
98 567 luisw
99 1034 vcaballero
        /**
100
         * Inserta el RasterAdapter.
101
         *
102
         * @param ra RasterAdapter.
103
         */
104
        public void setSource(RasterAdapter ra) {
105
                source = ra;
106
        }
107 567 luisw
108 2183 fernando
        /**
109 2623 nacho
         * Devuelve la pila de filtros aplicada sobre  la capa raster.
110
         *
111
         * @return RasterFilterStack.
112
         */
113
        public RasterFilterStack getFilterStack(){
114
                return source.getFilterStack();
115
        }
116
117
        /**
118 2183 fernando
         * Asignar el estado del raster
119
         * @param status
120
         */
121
        public void setStatus(StatusRasterInterface status){
122
                this.status = status;
123
        }
124
125
        /**
126 2623 nacho
         * Asigna la pila de filtros aplicada al raster
127
         * @return
128
         */
129
        public void setFilterStack(RasterFilterStack stack){
130
                source.setFilterStack(stack);
131
        }
132
133
        /**
134 2183 fernando
         * Obtiene el estado del raster
135
         * @return
136
         */
137
        public StatusRasterInterface getStatus(){
138
                return this.status;
139
        }
140
141
        /**
142
         * Asigna la posici?n en la que se ha hecho click al mostrar
143
         * informaci?n del raster.
144
         * @param x        Posici?n en X
145
         * @param y        Posici?n en Y
146
         */
147
        public void setPos(int x, int y){
148
                this.posX = x;
149
                this.posY = y;
150
        }
151
152
        /**
153
         * Asigna la posici?n en coordenadas del mundo real en la que se ha
154
         * hecho click al mostrar informaci?n del raster.
155
         * @param x        Posici?n en X
156
         * @param y        Posici?n en Y
157
         */
158
        public void setPosWC(double x, double y){
159
                this.posXWC = x;
160
                this.posYWC = y;
161
        }
162
163 2322 igbrotru
        /**
164
         * Asigna RGB en la posici?n en la que se ha
165
         * hecho click al mostrar informaci?n del raster.
166
         * @param r        valor de rojo
167
         * @param g        valor de verde
168
         * @param b        valor de azul
169
         */
170
        public void setRGB(int r, int g, int b){
171
                this.r = r;
172
                this.g = g;
173
                this.b = b;
174
        }
175
176 567 luisw
        /*
177
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#load()
178
         */
179 1034 vcaballero
        public void load() throws DriverIOException {
180
                ((RasterFileAdapter) source).start();
181 1455 luisw
                ((RasterFileAdapter) source).setTransparency(getTransparency());
182 567 luisw
        }
183 1034 vcaballero
184 526 vcaballero
        /**
185
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
186
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
187
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
188
         */
189 659 luisw
        public void draw(BufferedImage image, Graphics2D g, ViewPort vp,
190 2531 caballero
                Cancellable cancel,double scale) throws DriverException {
191
                if (isWithinScale(scale)){
192 671 luisw
                try {
193 2183 fernando
                        if(status!=null && firstLoad){
194
                                ((RasterFileAdapter) source).draw(image, g, vp, cancel);
195 2623 nacho
                                status.applyStatus(this);
196 2183 fernando
                                firstLoad = false;
197
                        }
198
199 1034 vcaballero
                        ((RasterFileAdapter) source).draw(image, g, vp, cancel);
200 2183 fernando
201 671 luisw
                } catch (DriverIOException e) {
202
                        throw new DriverException(e);
203
                }
204 1034 vcaballero
205
                if (getVirtualLayers() != null) {
206 2531 caballero
                        getVirtualLayers().draw(image, g, vp, cancel,scale);
207 1034 vcaballero
                }
208 2531 caballero
                }
209 526 vcaballero
        }
210 1034 vcaballero
211
        /**
212
         * Inserta la proyecci?n.
213
         *
214
         * @param proj Proyecci?n.
215
         */
216 567 luisw
        public void setProjection(IProjection proj) {
217
                super.setProjection(proj);
218 1034 vcaballero
219 1466 luisw
                if (source != null && source.getDriver() != null && source.getDriver() instanceof GeorreferencedRasterDriver) {
220 567 luisw
                        GeorreferencedRasterDriver geoDrv = (GeorreferencedRasterDriver) source.getDriver();
221 1034 vcaballero
222
                        if (geoDrv.getProjection() == null) {
223 567 luisw
                                geoDrv.setProjection(proj);
224 1034 vcaballero
                        }
225 567 luisw
                }
226 562 fernando
        }
227 1455 luisw
228
        public void setTransparency(int trans) {
229
                super.setTransparency(trans);
230
                ((RasterFileAdapter) source).setTransparency(trans);
231
                // TODO LWS Guarradita para actualizar el MapControl con ApplyButton
232 2183 fernando
                if (getFMap() != null)
233
                        getFMap().invalidate();
234 1455 luisw
        }
235 562 fernando
236 2183 fernando
        public int getTransparency() {
237 1455 luisw
                return ((RasterFileAdapter) source).getTransparency();
238
        }
239 2183 fernando
240 567 luisw
        /*
241 526 vcaballero
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
242
         */
243 651 fernando
        public Rectangle2D getFullExtent() throws DriverException {
244 567 luisw
                return ((RasterFileAdapter) source).getFullExtent();
245 526 vcaballero
        }
246 303 fernando
247 2322 igbrotru
        /**
248
         * Obtiene el valor del pixel del Image en la posici?n x,y
249
         * @param x Posici?n x
250
         * @param y Posici?n y
251
         * @return valor de pixel
252
         */
253 2347 igbrotru
        public int[] getPixel(double wcx, double wcy){
254
                return ((RasterFileAdapter) source).getPixel(wcx, wcy);
255 2322 igbrotru
        }
256
257 2396 nacho
        public double getMaxX(){
258
                try {
259
                        return this.getFullExtent().getMaxX();
260
                }catch(DriverException e){
261
                        return 0D;
262
                }
263
        }
264
265
        public double getMaxY(){
266
                try {
267
                        return this.getFullExtent().getMaxY();
268
                }catch(DriverException e){
269
                        return 0D;
270
                }
271
        }
272
273
        public double getMinX(){
274
                try {
275
                        return this.getFullExtent().getMinX();
276
                }catch(DriverException e){
277
                        return 0D;
278
                }
279
        }
280
281
        public double getMinY(){
282
                try {
283
                        return this.getFullExtent().getMinY();
284
                }catch(DriverException e){
285
                        return 0D;
286
                }
287
        }
288
289
        public double getHeight(){
290
                try {
291
                        return this.getFullExtent().getHeight();
292
                }catch(DriverException e){
293
                        return 0D;
294
                }
295
        }
296
297
        public double getWidth(){
298
                try {
299
                        return this.getFullExtent().getWidth();
300
                }catch(DriverException e){
301
                        return 0D;
302
                }
303
        }
304
305
306 1680 luisw
        /* (non-Javadoc)
307
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(java.awt.Point)
308 526 vcaballero
         */
309 1680 luisw
        public String queryByPoint(Point p) throws DriverException {
310 2347 igbrotru
                String data = "<file:"+getName()+">\n";
311 1680 luisw
312
                ArrayList attr = source.getAttributes();
313 1717 luisw
                data += "  <raster\n";
314
                data += "    File=\""+((RasterFileAdapter) source).getFile()+"\"\n";
315 1680 luisw
                for (int i=0; i<attr.size(); i++) {
316
                        Object [] a = (Object []) attr.get(i);
317
318 1717 luisw
                        data += "    "+a[0].toString()+"=";
319
                        if (a[1].toString() instanceof String)
320
                                data += "\""+a[1].toString()+"\"\n";
321
                        else
322
                                data += a[1].toString()+"\n";
323 1680 luisw
                }
324 2183 fernando
                data += "    Point=\""+posX+" , "+posY+"\"\n";
325
                data += "    Point_WC=\""+posXWC+" , "+posYWC+"\"\n";
326 2322 igbrotru
                data += "    RGB=\""+r+", "+g+", "+b+"\"\n";
327 1717 luisw
                data += "  />\n";
328 1680 luisw
329 2347 igbrotru
                data += "</file:"+getName()+">\n";
330 2183 fernando
                //System.out.println(data);
331 1717 luisw
                return data;
332 526 vcaballero
        }
333 435 vcaballero
334 526 vcaballero
        /**
335 2396 nacho
         * Obtiene atributos a partir de un georasterfile
336
         * @return
337
         */
338
        public ArrayList getAttributes() {
339
                return source.getAttributes();
340
        }
341
342
        /**
343 1828 fernando
         * @throws XMLException
344 526 vcaballero
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
345
         */
346 1828 fernando
        public XMLEntity getXMLEntity() throws XMLException {
347 526 vcaballero
                XMLEntity xml = super.getXMLEntity();
348 2647 nacho
349 895 vcaballero
                if (source instanceof RasterFileAdapter) {
350
                        xml.putProperty("file", ((RasterFileAdapter) source).getFile());
351 1034 vcaballero
                }
352 2647 nacho
353 895 vcaballero
                xml.putProperty("driverName", getSource().getDriver().getName());
354 2183 fernando
                //xml.putProperty("raster.opacityLevel", ""+getTransparency());
355
                if(status!=null)
356
                        status.getXMLEntity(xml, true, this);
357 2647 nacho
358 526 vcaballero
                return xml;
359
        }
360 1455 luisw
361 2183 fernando
        public void setXMLEntity03(XMLEntity xml)
362
        throws XMLException {
363
                super.setXMLEntity(xml);
364
                try {
365
                        Driver d = LayerFactory.getDM().getDriver(
366
                                xml.getStringProperty("driverName"));
367
                        File f = new File(xml.getStringProperty("file"));
368
                        RasterAdapter adapter = new RasterFileAdapter(f);
369
                        adapter.setDriver(d);
370
                        setSource(adapter);
371
                        // Para notificar al adapter-driver cual es la proyecci?n.
372
                        setProjection(super.getProjection());
373
                } catch (DriverLoadException e) {
374
                        throw new XMLException(e);
375
                }
376
        }
377
378 1455 luisw
        public void setXMLEntity(XMLEntity xml)
379
        throws XMLException {
380
                super.setXMLEntity(xml);
381
                try {
382
                        Driver d = LayerFactory.getDM().getDriver(
383
                                xml.getStringProperty("driverName"));
384
                        File f = new File(xml.getStringProperty("file"));
385
                        RasterAdapter adapter = new RasterFileAdapter(f);
386
                        adapter.setDriver(d);
387
                        setSource(adapter);
388 1466 luisw
                        // Para notificar al adapter-driver cual es la proyecci?n.
389
                        setProjection(super.getProjection());
390 2183 fernando
                        String claseStr = null;
391
                        if (xml.contains("raster.class")) {
392
                                claseStr = xml.getStringProperty("raster.class");
393
                        }
394
                        if(status!=null)
395
                                status.setXMLEntity(xml, this);
396
                        else{
397
398
                                //Cuando cargamos un proyecto
399
400
                                if(claseStr!=null && !claseStr.equals("")){
401
                                        try{
402
                                                Class clase = Class.forName(claseStr);
403
                                                Constructor constr = clase.getConstructor(null);
404
                                                status = (StatusRasterInterface)constr.newInstance(null);
405
                                                if(status!=null)
406
                                                        status.setXMLEntity(xml, this);
407
                                        }catch(ClassNotFoundException exc){
408
                                                exc.printStackTrace();
409
                                        }catch(InstantiationException exc){
410
                                                exc.printStackTrace();
411
                                        }catch(IllegalAccessException exc){
412
                                                exc.printStackTrace();
413
                                        }catch(NoSuchMethodException exc){
414
                                                exc.printStackTrace();
415
                                        }catch(InvocationTargetException exc){
416
                                                exc.printStackTrace();
417
                                        }
418
                                }
419
                        }
420
                        firstLoad = true;
421
422
423 1455 luisw
                } catch (DriverLoadException e) {
424 1472 luisw
                        throw new XMLException(e);
425 1455 luisw
                }
426
        }
427 435 vcaballero
428 569 luisw
        /* (non-Javadoc)
429
         * @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)
430
         */
431 2531 caballero
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
432 1488 luisw
                throws DriverException {
433 2531 caballero
                if (isVisible() && isWithinScale(scale)){
434 1488 luisw
                isPrinting = true;
435
                if (!mustTilePrint) {
436 2531 caballero
                        draw(null, g, viewPort, cancel,scale);
437 1488 luisw
                } else {
438
                // Para no pedir imagenes demasiado grandes, vamos
439
                // a hacer lo mismo que hace EcwFile: chunkear.
440
                // Llamamos a drawView con cuadraditos m?s peque?os
441
                // del BufferedImage ni caso, cuando se imprime viene con null
442
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
443
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
444
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
445
                            // Parte que dibuja
446
                            try {
447
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
448 2531 caballero
                                draw(null, g, vp, cancel,scale);
449 1488 luisw
                                } catch (NoninvertibleTransformException e) {
450
                                        // TODO Auto-generated catch block
451
                                        e.printStackTrace();
452
                                }
453
                }
454
                }
455
            isPrinting = false;
456 2531 caballero
                }
457 1488 luisw
        }
458
459 2531 caballero
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
460 1040 fjp
                throws DriverException {
461
                // Para no pedir imagenes demasiado grandes, vamos
462
                // a hacer lo mismo que hace EcwFile: chunkear.
463
                // Llamamos a drawView con cuadraditos m?s peque?os
464
                // del BufferedImage ni caso, cuando se imprime viene con null
465
                int numW, numH;
466
                int stepX, stepY;
467
                int xProv, yProv, wProv, hProv;
468
                double xProvD, yProvD, wProvD, hProvD;
469 1152 fjp
                int A = 1500;
470
                int H = 1500;
471 1040 fjp
                int altoAux, anchoAux;
472
473 1152 fjp
                AffineTransform mat = (AffineTransform) viewPort.getAffineTransform().clone();
474 1040 fjp
475
                // Vamos a hacerlo en trozos de AxH
476
                Rectangle r = g.getClipRect();
477
                numW = (int) (r.width) / A;
478
                numH = (int) (r.height) / H;
479
480
481
                double[] srcPts = new double[8];
482
                double[] dstPts= new double[8];
483
484
                yProv = (int) r.y;
485
                for (stepY=0; stepY < numH+1; stepY++)
486
                {
487
                        if ((yProv + H) > r.getMaxY())
488
                                altoAux = (int) r.getMaxY() - yProv;
489
                        else
490
                                altoAux = H;
491
492
                        xProv = (int) r.x;
493
                        for (stepX=0; stepX < numW+1; stepX++)
494
                        {
495
                                    if ((xProv + A) > r.getMaxX())
496
                                            anchoAux = (int) r.getMaxX() - xProv;
497
                                    else
498
                                            anchoAux = A;
499
500
                                Rectangle newRect = new Rectangle(xProv, yProv, anchoAux, altoAux);
501
502
                                // Parte que dibuja
503
                                srcPts[0] = xProv;
504
                                srcPts[1] = yProv;
505
                                srcPts[2] = xProv + anchoAux+1;
506
                                srcPts[3] = yProv;
507
                                srcPts[4] = xProv + anchoAux+1;
508
                                srcPts[5] = yProv + altoAux+1;
509
                                srcPts[6] = xProv;
510
                                srcPts[7] = yProv + altoAux+1;
511
512
                                try {
513
                                                mat.inverseTransform(srcPts, 0, dstPts, 0, 4);
514
                                        Rectangle2D.Double rectCuadricula = new Rectangle2D.Double(
515
                                                        dstPts[0], dstPts[1],
516
                                                                dstPts[2] - dstPts[0], dstPts[5]-dstPts[3]);
517
                                        Extent extent = new Extent(rectCuadricula);
518
519
                                        Dimension tam = new Dimension(anchoAux+1, altoAux+1);
520
                                        ViewPort vp = viewPort.cloneViewPort();
521 1152 fjp
                                        vp.setImageSize(tam);
522 1040 fjp
                                        vp.setExtent(rectCuadricula);
523 1152 fjp
                                        vp.setAffineTransform(mat);
524 1040 fjp
                                        // ViewPortData vp = new ViewPortData(getProjection(), extent, tam);
525
                                        // vp.setMat(mat);
526
                                        // rasterList.draw(g2, vp);
527 1152 fjp
528 2396 nacho
                                        /*System.out.println("FLyrRaster.print(): fila "+stepX+" de "
529 1152 fjp
                                                + numW + " , col "+stepY+" de " + numH +
530
                                                "\n, Extent = "+vp.getExtent() + " imageSize: "
531 2396 nacho
                                                + tam);*/
532 2531 caballero
                                        draw(null, g, vp, cancel,scale);
533 1040 fjp
534
                                        } catch (NoninvertibleTransformException e) {
535
                                                // TODO Auto-generated catch block
536
                                                e.printStackTrace();
537
                                        }
538
                                // Fin parte que dibuja
539
                                        xProv = xProv + A;
540
                        }
541
                        yProv = yProv + H;
542
                }
543
544 569 luisw
        }
545 2183 fernando
546
        /**
547
         * A?ade un fichero a la capa raster
548
         * @param fileName Nombre del fichero
549
         */
550
        public void addFiles(String fileName){
551
                ((RasterFileAdapter) source).addFile(fileName);
552
        }
553
554
        /**
555
         * Elimina un fichero a la capa raster
556
         * @param fileName Nombre del fichero
557
         */
558
        public void delFile(String fileName){
559
                ((RasterFileAdapter) source).delFile(fileName);
560
        }
561 2623 nacho
562
        /* (non-Javadoc)
563
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setBand(int, int)
564
         */
565
        public void setBand(int flag, int nBand) {
566
                this.getSource().setBand(flag, nBand);
567
        }
568 2850 nacho
569
        /**
570
         * Asina el extent de la vista para imagenes sin informaci?n de
571
         * georeferenciaci?n.
572
         * @param extent        Asigna el extent
573
         */
574
        public void setTempExtent(ViewPort vp){
575
                ((RasterFileAdapter) source).setTempExtent(vp);
576
        }
577 214 fernando
}