Statistics
| Revision:

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

History | View | Annotate | Download (23.7 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 5979 nacho
import java.awt.geom.Point2D;
50 1034 vcaballero
import java.awt.geom.Rectangle2D;
51
import java.awt.image.BufferedImage;
52 1455 luisw
import java.io.File;
53 2183 fernando
import java.lang.reflect.Constructor;
54
import java.lang.reflect.InvocationTargetException;
55 1680 luisw
import java.util.ArrayList;
56 1034 vcaballero
57 9013 caballero
import javax.print.attribute.PrintRequestAttributeSet;
58 5761 jmvivo
import javax.swing.ImageIcon;
59
60 1680 luisw
import org.cresques.cts.IProjection;
61 5979 nacho
import org.cresques.filter.RasterFilterStack;
62 9056 nacho
import org.cresques.io.data.Grid;
63 10195 nacho
import org.cresques.io.data.RasterBuf;
64 9056 nacho
import org.cresques.io.datastruct.Statistic;
65 1680 luisw
import org.cresques.px.Extent;
66 9056 nacho
67 1680 luisw
import com.hardcode.driverManager.Driver;
68
import com.hardcode.driverManager.DriverLoadException;
69
import com.iver.cit.gvsig.fmap.DriverException;
70 5761 jmvivo
import com.iver.cit.gvsig.fmap.MapControl;
71 1680 luisw
import com.iver.cit.gvsig.fmap.ViewPort;
72
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
73
import com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver;
74 8765 jjdelcerro
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
75 5057 ldiaz
import com.iver.cit.gvsig.fmap.layers.layerOperations.StringXMLItem;
76 5200 ldiaz
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
77 9056 nacho
import com.iver.utiles.DoubleUtilities;
78 1680 luisw
import com.iver.utiles.XMLEntity;
79 5317 fjp
import com.iver.utiles.swing.threads.Cancellable;
80 1680 luisw
81
82 526 vcaballero
/**
83 1034 vcaballero
 * Clase b?sica de capa raster.
84 526 vcaballero
 *
85
 * @author Vicente Caballero Navarro
86
 */
87 567 luisw
public class FLyrRaster extends FLyrDefault implements RasterOperations {
88 2183 fernando
        private RasterAdapter                                 source;
89
        boolean                                                         isPrinting = false;
90
        boolean                                                         mustTileDraw = false;
91
        boolean                                                         mustTilePrint = true;
92 5979 nacho
        private int                                                        maxTileDrawWidth = 200;
93
        private int                                                 maxTileDrawHeight = 200;
94 2183 fernando
        int                                                                 maxTilePrintWidth = 1500;
95
        int                                                                 maxTilePrintHeight = 1500;
96
        private StatusRasterInterface                status = null;
97
        private boolean                                                firstLoad = false;
98
        private int                                                 posX = 0, posY = 0;
99
        private double                                                 posXWC = 0, posYWC = 0;
100 2322 igbrotru
        private int                                                 r = 0, g = 0, b = 0;
101 3410 nacho
        private boolean                                                removeRasterFlag = true;
102 9056 nacho
103 1034 vcaballero
        /**
104
         * Devuelve el RasterAdapter de la capa.
105
         *
106
         * @return RasterAdapter.
107
         */
108
        public RasterAdapter getSource() {
109
                return source;
110
        }
111 567 luisw
112 1034 vcaballero
        /**
113 4376 nacho
         *Redefine wakeUp de FLyrDefault
114
         */
115
        public void wakeUp(){
116 4404 nacho
                /*setName(getName());
117 4376 nacho
                setProjection(getProjection());
118
                try {
119
                        load();
120
                } catch (DriverIOException e) {
121
                        e.printStackTrace();
122 9056 nacho
                }*/
123 4376 nacho
        }
124 9056 nacho
125 4376 nacho
        /**
126 1034 vcaballero
         * Inserta el RasterAdapter.
127
         *
128
         * @param ra RasterAdapter.
129
         */
130
        public void setSource(RasterAdapter ra) {
131
                source = ra;
132
        }
133 567 luisw
134 2183 fernando
        /**
135 2623 nacho
         * Devuelve la pila de filtros aplicada sobre  la capa raster.
136
         *
137
         * @return RasterFilterStack.
138
         */
139
        public RasterFilterStack getFilterStack(){
140
                return source.getFilterStack();
141
        }
142 9056 nacho
143 2623 nacho
        /**
144 2183 fernando
         * Asignar el estado del raster
145
         * @param status
146
         */
147
        public void setStatus(StatusRasterInterface status){
148
                this.status = status;
149
        }
150 9056 nacho
151 2183 fernando
        /**
152 2623 nacho
         * Asigna la pila de filtros aplicada al raster
153
         * @return
154
         */
155
        public void setFilterStack(RasterFilterStack stack){
156 9056 nacho
                source.setFilterStack(stack);
157 2623 nacho
        }
158 9056 nacho
159 2623 nacho
        /**
160 2183 fernando
         * Obtiene el estado del raster
161
         * @return
162
         */
163
        public StatusRasterInterface getStatus(){
164
                return this.status;
165
        }
166 9056 nacho
167 2183 fernando
        /**
168
         * Asigna la posici?n en la que se ha hecho click al mostrar
169
         * informaci?n del raster.
170
         * @param x        Posici?n en X
171
         * @param y        Posici?n en Y
172
         */
173
        public void setPos(int x, int y){
174
                this.posX = x;
175
                this.posY = y;
176
        }
177 9056 nacho
178 2183 fernando
        /**
179 9056 nacho
         * Asigna la posici?n en coordenadas del mundo real en la que se ha
180 2183 fernando
         * hecho click al mostrar informaci?n del raster.
181
         * @param x        Posici?n en X
182
         * @param y        Posici?n en Y
183
         */
184
        public void setPosWC(double x, double y){
185
                this.posXWC = x;
186
                this.posYWC = y;
187
        }
188 9056 nacho
189 2322 igbrotru
        /**
190 9056 nacho
         * Asigna RGB en la posici?n en la que se ha
191 2322 igbrotru
         * hecho click al mostrar informaci?n del raster.
192
         * @param r        valor de rojo
193
         * @param g        valor de verde
194
         * @param b        valor de azul
195
         */
196
        public void setRGB(int r, int g, int b){
197
                this.r = r;
198
                this.g = g;
199
                this.b = b;
200
        }
201 9056 nacho
202 567 luisw
        /*
203
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#load()
204
         */
205 1034 vcaballero
        public void load() throws DriverIOException {
206
                ((RasterFileAdapter) source).start();
207 1455 luisw
                ((RasterFileAdapter) source).setTransparency(getTransparency());
208 567 luisw
        }
209 1034 vcaballero
210 526 vcaballero
        /**
211
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
212
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
213 5317 fjp
         *                 com.iver.utiles.swing.threads.Cancellable)
214 526 vcaballero
         */
215 659 luisw
        public void draw(BufferedImage image, Graphics2D g, ViewPort vp,
216 2531 caballero
                Cancellable cancel,double scale) throws DriverException {
217 9056 nacho
                if (isWithinScale(scale)){
218 671 luisw
                try {
219 2183 fernando
                        if(status!=null && firstLoad){
220 5979 nacho
                                if(mustTileDraw){
221
                                        Point2D p = vp.getOffset();
222
                                        Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), vp.getImageWidth(), vp.getImageHeight());
223
                                        Tiling tiles = new Tiling(maxTileDrawWidth, maxTileDrawHeight, r);
224
                                        tiles.setAffineTransform((AffineTransform) vp.getAffineTransform().clone());
225
                                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
226
                                                // drawing part
227
                                                try {
228
                                                        ViewPort vport = tiles.getTileViewPort(vp, tileNr);
229
                                                        //g.setClip(tiles.getClip(tileNr).x, tiles.getClip(tileNr).y, tiles.getClip(tileNr).width - 5, tiles.getClip(tileNr).height);
230
                                                        ((RasterFileAdapter) source).draw(image, g, vport, cancel);
231
                                                } catch (NoninvertibleTransformException e) {
232
                                                        e.printStackTrace();
233
                                                }
234
                                        }
235
                                }else
236 9056 nacho
                                        ((RasterFileAdapter) source).draw(image, g, vp, cancel);
237 2623 nacho
                                status.applyStatus(this);
238 2183 fernando
                                firstLoad = false;
239
                        }
240 9056 nacho
241 5979 nacho
                        if(mustTileDraw){
242
                                Point2D p = vp.getOffset();
243
                                Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), vp.getImageWidth(), vp.getImageHeight());
244
                                Tiling tiles = new Tiling(maxTileDrawWidth, maxTileDrawHeight, r);
245
                                tiles.setAffineTransform((AffineTransform) vp.getAffineTransform().clone());
246
                                for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
247
                                        // drawing part
248
                                        try {
249
                                                ViewPort vport = tiles.getTileViewPort(vp, tileNr);
250
                                                ((RasterFileAdapter) source).draw(image, g, vport, cancel);
251
                                        } catch (NoninvertibleTransformException e) {
252
                                                e.printStackTrace();
253
                                        }
254
                                }
255
                        }else
256
                                ((RasterFileAdapter) source).draw(image, g, vp, cancel);
257 9056 nacho
258 671 luisw
                } catch (DriverIOException e) {
259
                        throw new DriverException(e);
260
                }
261 1034 vcaballero
262
                if (getVirtualLayers() != null) {
263 2531 caballero
                        getVirtualLayers().draw(image, g, vp, cancel,scale);
264 1034 vcaballero
                }
265 2531 caballero
                }
266 526 vcaballero
        }
267 1034 vcaballero
268
        /**
269
         * Inserta la proyecci?n.
270
         *
271
         * @param proj Proyecci?n.
272
         */
273 567 luisw
        public void setProjection(IProjection proj) {
274
                super.setProjection(proj);
275 1034 vcaballero
276 1466 luisw
                if (source != null && source.getDriver() != null && source.getDriver() instanceof GeorreferencedRasterDriver) {
277 567 luisw
                        GeorreferencedRasterDriver geoDrv = (GeorreferencedRasterDriver) source.getDriver();
278 1034 vcaballero
279
                        if (geoDrv.getProjection() == null) {
280 567 luisw
                                geoDrv.setProjection(proj);
281 1034 vcaballero
                        }
282 567 luisw
                }
283 562 fernando
        }
284 9056 nacho
285 1455 luisw
        public void setTransparency(int trans) {
286
                super.setTransparency(trans);
287
                ((RasterFileAdapter) source).setTransparency(trans);
288 6881 cesar
                if (getMapContext() != null){
289
                        getMapContext().invalidate();
290 6007 nacho
                }
291 1455 luisw
        }
292 562 fernando
293 2183 fernando
        public int getTransparency() {
294 1455 luisw
                return ((RasterFileAdapter) source).getTransparency();
295
        }
296 9056 nacho
297 567 luisw
        /*
298 526 vcaballero
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
299
         */
300 651 fernando
        public Rectangle2D getFullExtent() throws DriverException {
301 567 luisw
                return ((RasterFileAdapter) source).getFullExtent();
302 526 vcaballero
        }
303 303 fernando
304 2322 igbrotru
        /**
305
         * Obtiene el valor del pixel del Image en la posici?n x,y
306
         * @param x Posici?n x
307
         * @param y Posici?n y
308
         * @return valor de pixel
309
         */
310 2347 igbrotru
        public int[] getPixel(double wcx, double wcy){
311
                return ((RasterFileAdapter) source).getPixel(wcx, wcy);
312 2322 igbrotru
        }
313 9056 nacho
314 2396 nacho
        public double getMaxX(){
315
                try {
316
                        return this.getFullExtent().getMaxX();
317
                }catch(DriverException e){
318
                        return 0D;
319
                }
320
        }
321 9056 nacho
322 2396 nacho
        public double getMaxY(){
323
                try {
324
                        return this.getFullExtent().getMaxY();
325
                }catch(DriverException e){
326
                        return 0D;
327
                }
328
        }
329 9056 nacho
330 2396 nacho
        public double getMinX(){
331
                try {
332
                        return this.getFullExtent().getMinX();
333
                }catch(DriverException e){
334
                        return 0D;
335
                }
336
        }
337 9056 nacho
338 2396 nacho
        public double getMinY(){
339
                try {
340
                        return this.getFullExtent().getMinY();
341
                }catch(DriverException e){
342
                        return 0D;
343
                }
344
        }
345 9056 nacho
346 2396 nacho
        public double getHeight(){
347
                try {
348
                        return this.getFullExtent().getHeight();
349
                }catch(DriverException e){
350
                        return 0D;
351
                }
352
        }
353 9056 nacho
354 2396 nacho
        public double getWidth(){
355
                try {
356
                        return this.getFullExtent().getWidth();
357
                }catch(DriverException e){
358
                        return 0D;
359
                }
360
        }
361
362 9056 nacho
363 1680 luisw
        /* (non-Javadoc)
364 5048 ldiaz
         * @deprecated. See String getInfo(Point p) throws DriverException
365 1680 luisw
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(java.awt.Point)
366 526 vcaballero
         */
367 1680 luisw
        public String queryByPoint(Point p) throws DriverException {
368 3242 luisw2
                String data = "<file:"+normalizeAsXMLTag(getName())+">\n";
369 1680 luisw
370
                ArrayList attr = source.getAttributes();
371 1717 luisw
                data += "  <raster\n";
372
                data += "    File=\""+((RasterFileAdapter) source).getFile()+"\"\n";
373 1680 luisw
                for (int i=0; i<attr.size(); i++) {
374
                        Object [] a = (Object []) attr.get(i);
375
376 1717 luisw
                        data += "    "+a[0].toString()+"=";
377
                        if (a[1].toString() instanceof String)
378
                                data += "\""+a[1].toString()+"\"\n";
379
                        else
380
                                data += a[1].toString()+"\n";
381 1680 luisw
                }
382 2183 fernando
                data += "    Point=\""+posX+" , "+posY+"\"\n";
383
                data += "    Point_WC=\""+posXWC+" , "+posYWC+"\"\n";
384 2322 igbrotru
                data += "    RGB=\""+r+", "+g+", "+b+"\"\n";
385 1717 luisw
                data += "  />\n";
386 1680 luisw
387 3242 luisw2
                data += "</file:"+normalizeAsXMLTag(getName())+">\n";
388 2183 fernando
                //System.out.println(data);
389 1717 luisw
                return data;
390 526 vcaballero
        }
391 9056 nacho
392 8765 jjdelcerro
        public XMLItem[] getInfo(Point p, double tolerance, Cancellable cancel) throws DriverException {
393 9056 nacho
394 6881 cesar
                Point2D pReal = getMapContext().getViewPort().toMapPoint(p);
395 6030 nacho
                Point2D px = null;
396 9056 nacho
                if(        pReal.getX() > this.getMinX() &&
397
                        pReal.getX() < this.getMaxX() &&
398
                        pReal.getY() > this.getMinY() &&
399 6030 nacho
                        pReal.getY() < this.getMaxY()){
400
                        ArrayList attr = source.getAttributes();
401
                        int w = 0, h = 0;
402
                        for (int i=0; i<attr.size(); i++) {
403
                                Object [] a = (Object []) attr.get(i);
404
                                if(a[0].toString().equals("Width"))
405
                                        w = ((Integer)a[1]).intValue();
406
                                if(a[0].toString().equals("Height"))
407
                                        h = ((Integer)a[1]).intValue();
408 9056 nacho
                        }
409 8765 jjdelcerro
                        px = new Point2D.Double();
410 6030 nacho
                        px.setLocation( ((pReal.getX() - this.getMinX()) * w) / getWidth(),
411
                                                        ((this.getMaxY() - pReal.getY()) * h) / getHeight());
412
                }
413 9056 nacho
414 6030 nacho
                int[] rgb = this.getPixel(pReal.getX(), pReal.getY());
415 9056 nacho
416
                StringXMLItem[] item = new StringXMLItem[1];
417 5048 ldiaz
                String data = "<file:"+normalizeAsXMLTag(getName())+">\n";
418 9056 nacho
419 5048 ldiaz
                data += "  <raster\n";
420 6030 nacho
                data += "    View_Point=\""+p.getX()+" , "+p.getY()+"\"\n";
421 9056 nacho
                data += "    World_Point=\""+DoubleUtilities.format(pReal.getX(), 5)+" , "+DoubleUtilities.format(pReal.getY(), 5)+"\"\n";
422 6030 nacho
                if(px == null)
423
                        data += "    Pixel_Point=\"Out\"\n";
424 9056 nacho
                else{
425 8765 jjdelcerro
                        data += "    Pixel_Point=\""+(int)px.getX()+" , "+(int)px.getY()+"\"\n";
426 9056 nacho
                        data += "    Band_Value=\"";
427
428
                        for(int i = 0; i < this.getSource().getNumBands(); i++){
429
                                Object pxData = ((RasterDriver)this.getSource().getDriver()).getData((int)px.getX(), (int)px.getY(), i);
430
                                if(pxData != null){
431
                                        if(this.getSource().getDataType() >= 0 && this.getSource().getDataType() <= 3)
432
                                                        data += ((Integer)pxData).intValue()+"  ";
433
434
                                        if(this.getSource().getDataType() >= 4)
435
                                                        data += ((Float)pxData).floatValue()+"  ";
436
437
                                        if(this.getSource().getDataType() >= 5)
438
                                                        data += ((Double)pxData).doubleValue()+"  ";
439
                                }
440
                        }
441
                        data += "\"\n";
442
                }
443
444 8765 jjdelcerro
                data += "    RGB=\""+rgb[1]+"  "+rgb[2]+"  "+rgb[3]+"\"\n";
445 9056 nacho
446 5048 ldiaz
                data += "  />\n";
447
                data += "</file:"+normalizeAsXMLTag(getName())+">\n";
448 9056 nacho
449
                item[0] = new StringXMLItem(data, this);
450 5057 ldiaz
                return item;
451 9056 nacho
452
        }
453
454 3242 luisw2
        /**
455
         * Filters a string for being suitable as XML Tag, erasing
456
         * all not alphabetic or numeric characters.
457
         * @param s
458
         * @return string normalized
459
         */
460
        public String normalizeAsXMLTag(String s) {
461
                return s.replaceAll("[^a-zA-Z0-9]","");
462
        }
463 435 vcaballero
464 526 vcaballero
        /**
465 2396 nacho
         * Obtiene atributos a partir de un georasterfile
466
         * @return
467
         */
468
        public ArrayList getAttributes() {
469
                return source.getAttributes();
470
        }
471 9056 nacho
472 2396 nacho
        /**
473 1828 fernando
         * @throws XMLException
474 526 vcaballero
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
475
         */
476 1828 fernando
        public XMLEntity getXMLEntity() throws XMLException {
477 526 vcaballero
                XMLEntity xml = super.getXMLEntity();
478 9056 nacho
479 895 vcaballero
                if (source instanceof RasterFileAdapter) {
480
                        xml.putProperty("file", ((RasterFileAdapter) source).getFile());
481 1034 vcaballero
                }
482 9056 nacho
483 895 vcaballero
                xml.putProperty("driverName", getSource().getDriver().getName());
484 9056 nacho
485 4234 nacho
                //Si no hay ning?n Status aplicamos el StatusLayerRaster que se usa por defecto
486
                if(status == null)
487
                        status = new StatusLayerRaster();
488 9056 nacho
489 4234 nacho
                status.getXMLEntity(xml, true, this);
490 9056 nacho
491
492 526 vcaballero
                return xml;
493
        }
494 9056 nacho
495 2183 fernando
        public void setXMLEntity03(XMLEntity xml)
496
        throws XMLException {
497
                super.setXMLEntity(xml);
498
                try {
499
                        Driver d = LayerFactory.getDM().getDriver(
500
                                xml.getStringProperty("driverName"));
501
                        File f = new File(xml.getStringProperty("file"));
502
                        RasterAdapter adapter = new RasterFileAdapter(f);
503
                        adapter.setDriver(d);
504
                        setSource(adapter);
505
                        // Para notificar al adapter-driver cual es la proyecci?n.
506
                        setProjection(super.getProjection());
507
                } catch (DriverLoadException e) {
508
                        throw new XMLException(e);
509
                }
510
        }
511 9056 nacho
512 1455 luisw
        public void setXMLEntity(XMLEntity xml)
513
        throws XMLException {
514
                super.setXMLEntity(xml);
515
                try {
516
                        Driver d = LayerFactory.getDM().getDriver(
517
                                xml.getStringProperty("driverName"));
518
                        File f = new File(xml.getStringProperty("file"));
519
                        RasterAdapter adapter = new RasterFileAdapter(f);
520
                        adapter.setDriver(d);
521
                        setSource(adapter);
522 1466 luisw
                        // Para notificar al adapter-driver cual es la proyecci?n.
523
                        setProjection(super.getProjection());
524 9056 nacho
525 4234 nacho
                        //Inicializamos la clase a la que se usa por defecto para
526
                        //compatibilidad con proyectos antiguos
527
                        String claseStr = StatusLayerRaster.defaultClass;
528 2183 fernando
                        if (xml.contains("raster.class")) {
529
                                claseStr = xml.getStringProperty("raster.class");
530
                        }
531
                        if(status!=null)
532
                                status.setXMLEntity(xml, this);
533
                        else{
534 9056 nacho
535
                                //Cuando cargamos un proyecto
536
537 2183 fernando
                                if(claseStr!=null && !claseStr.equals("")){
538
                                        try{
539
                                                Class clase = Class.forName(claseStr);
540
                                                Constructor constr = clase.getConstructor(null);
541
                                                status = (StatusRasterInterface)constr.newInstance(null);
542
                                                if(status!=null)
543
                                                        status.setXMLEntity(xml, this);
544
                                        }catch(ClassNotFoundException exc){
545
                                                exc.printStackTrace();
546
                                        }catch(InstantiationException exc){
547
                                                exc.printStackTrace();
548
                                        }catch(IllegalAccessException exc){
549
                                                exc.printStackTrace();
550
                                        }catch(NoSuchMethodException exc){
551
                                                exc.printStackTrace();
552
                                        }catch(InvocationTargetException exc){
553
                                                exc.printStackTrace();
554 9056 nacho
                                        }
555 2183 fernando
                                }
556
                        }
557
                        firstLoad = true;
558 9056 nacho
559
560 1455 luisw
                } catch (DriverLoadException e) {
561 1472 luisw
                        throw new XMLException(e);
562 1455 luisw
                }
563
        }
564 435 vcaballero
565 569 luisw
        /* (non-Javadoc)
566
         * @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)
567
         */
568 9013 caballero
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet propeties)
569 1488 luisw
                throws DriverException {
570 9056 nacho
571
                if (isVisible() && isWithinScale(scale)){
572 1488 luisw
                isPrinting = true;
573
                if (!mustTilePrint) {
574 2531 caballero
                        draw(null, g, viewPort, cancel,scale);
575 1488 luisw
                } else {
576
                // Para no pedir imagenes demasiado grandes, vamos
577
                // a hacer lo mismo que hace EcwFile: chunkear.
578
                // Llamamos a drawView con cuadraditos m?s peque?os
579
                // del BufferedImage ni caso, cuando se imprime viene con null
580
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
581
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
582 9056 nacho
583 3084 nacho
                        //Si es la primera lectura salvamos los valores de m?ximo y m?nimo para la aplicaci?n
584
                        //de realce si la imagen es de 16 bits.
585 9056 nacho
586
                        Statistic stats = getSource().getFilterStack().getStats();
587 3084 nacho
                        if(stats != null)
588 9056 nacho
                                stats.history.add(stats.new History(getName(), stats.minBandValue, stats.maxBandValue, stats.secondMinBandValue, stats.secondMaxBandValue));
589
590
591 1488 luisw
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
592
                            // Parte que dibuja
593
                            try {
594
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
595 2531 caballero
                                draw(null, g, vp, cancel,scale);
596 1488 luisw
                                } catch (NoninvertibleTransformException e) {
597
                                        // TODO Auto-generated catch block
598
                                        e.printStackTrace();
599
                                }
600
                }
601 9056 nacho
602 3084 nacho
                        if(stats != null){
603
                                getSource().getFilterStack().getStats().history.clear();
604
                                stats = getSource().getFilterStack().getStats();
605
                        }
606 9056 nacho
607 1488 luisw
                }
608
            isPrinting = false;
609 2531 caballero
                }
610 1488 luisw
        }
611
612 2531 caballero
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
613 9056 nacho
                throws DriverException {
614 1040 fjp
                // Para no pedir imagenes demasiado grandes, vamos
615
                // a hacer lo mismo que hace EcwFile: chunkear.
616
                // Llamamos a drawView con cuadraditos m?s peque?os
617
                // del BufferedImage ni caso, cuando se imprime viene con null
618 9056 nacho
619 1040 fjp
                int numW, numH;
620
                int stepX, stepY;
621
                int xProv, yProv, wProv, hProv;
622
                double xProvD, yProvD, wProvD, hProvD;
623 9056 nacho
                int A = 1500;
624 1152 fjp
                int H = 1500;
625 1040 fjp
                int altoAux, anchoAux;
626 9056 nacho
627 1152 fjp
                AffineTransform mat = (AffineTransform) viewPort.getAffineTransform().clone();
628 9056 nacho
629 1040 fjp
                // Vamos a hacerlo en trozos de AxH
630
                Rectangle r = g.getClipRect();
631
                numW = (int) (r.width) / A;
632 9056 nacho
                numH = (int) (r.height) / H;
633
634
635 1040 fjp
                double[] srcPts = new double[8];
636
                double[] dstPts= new double[8];
637
638
                yProv = (int) r.y;
639
                for (stepY=0; stepY < numH+1; stepY++)
640
                {
641 9056 nacho
                        if ((yProv + H) > r.getMaxY())
642 1040 fjp
                                altoAux = (int) r.getMaxY() - yProv;
643
                        else
644
                                altoAux = H;
645 9056 nacho
646 1040 fjp
                        xProv = (int) r.x;
647 9056 nacho
                        for (stepX=0; stepX < numW+1; stepX++)
648
                        {
649
                                    if ((xProv + A) > r.getMaxX())
650 1040 fjp
                                            anchoAux = (int) r.getMaxX() - xProv;
651
                                    else
652
                                            anchoAux = A;
653 9056 nacho
654 1040 fjp
                                Rectangle newRect = new Rectangle(xProv, yProv, anchoAux, altoAux);
655 9056 nacho
656 1040 fjp
                                // Parte que dibuja
657
                                srcPts[0] = xProv;
658
                                srcPts[1] = yProv;
659
                                srcPts[2] = xProv + anchoAux+1;
660
                                srcPts[3] = yProv;
661
                                srcPts[4] = xProv + anchoAux+1;
662
                                srcPts[5] = yProv + altoAux+1;
663
                                srcPts[6] = xProv;
664
                                srcPts[7] = yProv + altoAux+1;
665 9056 nacho
666 1040 fjp
                                try {
667
                                                mat.inverseTransform(srcPts, 0, dstPts, 0, 4);
668
                                        Rectangle2D.Double rectCuadricula = new Rectangle2D.Double(
669
                                                        dstPts[0], dstPts[1],
670 9056 nacho
                                                                dstPts[2] - dstPts[0], dstPts[5]-dstPts[3]);
671 1040 fjp
                                        Extent extent = new Extent(rectCuadricula);
672 9056 nacho
673 1040 fjp
                                        Dimension tam = new Dimension(anchoAux+1, altoAux+1);
674
                                        ViewPort vp = viewPort.cloneViewPort();
675 1152 fjp
                                        vp.setImageSize(tam);
676 1040 fjp
                                        vp.setExtent(rectCuadricula);
677 1152 fjp
                                        vp.setAffineTransform(mat);
678 1040 fjp
                                        // ViewPortData vp = new ViewPortData(getProjection(), extent, tam);
679
                                        // vp.setMat(mat);
680
                                        // rasterList.draw(g2, vp);
681 9056 nacho
682 2396 nacho
                                        /*System.out.println("FLyrRaster.print(): fila "+stepX+" de "
683 9056 nacho
                                                + numW + " , col "+stepY+" de " + numH +
684 1152 fjp
                                                "\n, Extent = "+vp.getExtent() + " imageSize: "
685 2396 nacho
                                                + tam);*/
686 2531 caballero
                                        draw(null, g, vp, cancel,scale);
687 9056 nacho
688 1040 fjp
                                        } catch (NoninvertibleTransformException e) {
689
                                                // TODO Auto-generated catch block
690
                                                e.printStackTrace();
691
                                        }
692
                                // Fin parte que dibuja
693 9056 nacho
                                        xProv = xProv + A;
694
                        }
695 1040 fjp
                        yProv = yProv + H;
696 9056 nacho
                }
697
698 569 luisw
        }
699 9056 nacho
700 2183 fernando
        /**
701
         * A?ade un fichero a la capa raster
702
         * @param fileName Nombre del fichero
703
         */
704
        public void addFiles(String fileName){
705
                ((RasterFileAdapter) source).addFile(fileName);
706
        }
707 9056 nacho
708 2183 fernando
        /**
709
         * Elimina un fichero a la capa raster
710
         * @param fileName Nombre del fichero
711
         */
712
        public void delFile(String fileName){
713
                ((RasterFileAdapter) source).delFile(fileName);
714
        }
715 9056 nacho
716 2623 nacho
        /* (non-Javadoc)
717
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setBand(int, int)
718
         */
719
        public void setBand(int flag, int nBand) {
720
                this.getSource().setBand(flag, nBand);
721
        }
722 9056 nacho
723 3257 nacho
        /**
724
         * Borra de la lista de listeners el que se pasa como par?metro.
725
         *
726
         * @param o LayerListener a borrar.
727
         *
728
         * @return True si ha sido correcto el borrado del Listener.
729
         */
730
        public boolean removeLayerListener(LayerListener o) {
731 3410 nacho
                if(this.isRemoveRasterFlag()){
732 4404 nacho
                        /*try{
733 3410 nacho
                                ((RasterFileAdapter) source).stop();
734
                        }catch(DriverIOException exc){
735 9056 nacho

736 4404 nacho
                        }*/
737 3410 nacho
                        this.setRemoveRasterFlag(true);
738 3257 nacho
                }
739
                return super.layerListeners.remove(o);
740
        }
741 9056 nacho
742 3410 nacho
        /**
743
         * @return Returns the removeRasterFlag.
744
         */
745
        public boolean isRemoveRasterFlag() {
746
                return removeRasterFlag;
747
        }
748 9056 nacho
749 3410 nacho
        /**
750
         * Asigna el valor del flag que dice si destruimos la memoria del raster
751
         * al eliminarlo del TOC o  no.
752
         * @param removeRasterFlag The removeRasterFlag to set.
753
         */
754
        public void setRemoveRasterFlag(boolean removeRasterFlag) {
755
                this.removeRasterFlag = removeRasterFlag;
756
        }
757 9056 nacho
758
        public ImageIcon getTocImageIcon() {
759 5761 jmvivo
                return new ImageIcon(MapControl.class.getResource("images/icolayerRaster.PNG"));
760
        }
761 9056 nacho
762 6007 nacho
        /*
763
         *  (non-Javadoc)
764
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getTileSize()
765
         */
766
        public int[] getTileSize() {
767 9056 nacho
                int[] size = {maxTileDrawWidth, maxTileDrawHeight};
768 6007 nacho
                return size;
769
        }
770
771
        /*
772
         *  (non-Javadoc)
773
         * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#isTiled()
774
         */
775
        public boolean isTiled() {
776
                return mustTileDraw;
777
        }
778 9056 nacho
779 6037 nacho
        /**
780
         * Obtiene el flag que dice si la imagen est? o no georreferenciada
781
         * @return true si est? georreferenciada y false si no lo est?.
782
         */
783
        public boolean isGeoreferenced() {
784
                return getSource().isGeoreferenced();
785
        }
786 9056 nacho
787
        /**
788
         * Obtiene el grid asociado
789
         * @return grid
790
         */
791
        public Grid getGrid(){
792
                return getSource().getGrid();
793
        }
794
795
        /**
796
         * Obtiene el tipo de dato de la capa raster
797
         * @return Entero que representa el tipo de dato de la capa raster.
798
         */
799
        public int getDataType() {
800
                return getSource().getDataType();
801
        }
802 9239 nacho
803
        /*
804
         *  (non-Javadoc)
805
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#cloneLayer()
806
         */
807
        public FLayer cloneLayer() throws Exception {
808
                FLyrRaster clonedLayer = new FLyrRaster();
809
810
                File f = new File(getSource().getFiles()[0].getName());
811
                RasterAdapter adapter = new RasterFileAdapter(f);
812
                com.hardcode.driverManager.Driver driver = LayerFactory.getDM().getDriver("gvSIG Image Driver");
813
                adapter.setDriver(driver);
814
815
                clonedLayer.setName(getName());
816
                clonedLayer.setVisible(isVisible());
817
                clonedLayer.setSource(adapter);
818
                clonedLayer.setProjection(getProjection());
819
820
                clonedLayer.setXMLEntity(getXMLEntity());
821
822
                clonedLayer.load();
823
824
                return clonedLayer;
825
        }
826 10195 nacho
827
        /**
828
         * Set data buffer to create a memory raster driver
829
         * @param buf data buffer
830
         */
831
        public void setBuffer(RasterBuf buf){
832
                source.setBuffer(buf);
833
        }
834
835
        /**
836
         * Set extent to create a memory raster driver
837
         * @param ext Extent
838
         */
839
        public void setExtent(Extent ext){
840
                source.setExtent(ext);
841
        }
842 214 fernando
}