Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrRaster.java @ 1717

History | View | Annotate | Download (10.1 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.util.ArrayList;
53

    
54
import org.cresques.cts.IProjection;
55
import org.cresques.px.Extent;
56

    
57
import com.hardcode.driverManager.Driver;
58
import com.hardcode.driverManager.DriverLoadException;
59
import com.iver.cit.gvsig.fmap.DriverException;
60
import com.iver.cit.gvsig.fmap.ViewPort;
61
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
62
import com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver;
63
import com.iver.cit.gvsig.fmap.operations.Cancellable;
64
import com.iver.utiles.XMLEntity;
65

    
66

    
67
/**
68
 * Clase b?sica de capa raster.
69
 *
70
 * @author Vicente Caballero Navarro
71
 */
72
public class FLyrRaster extends FLyrDefault implements RasterOperations {
73
        private RasterAdapter source;
74
        boolean isPrinting = false;
75
        boolean mustTileDraw = false;
76
        boolean mustTilePrint = true;
77
        int maxTileDrawWidth = -1;
78
        int maxTileDrawHeight = -1;
79
        int maxTilePrintWidth = 1500;
80
        int maxTilePrintHeight = 1500;
81

    
82
        /**
83
         * Devuelve el RasterAdapter de la capa.
84
         *
85
         * @return RasterAdapter.
86
         */
87
        public RasterAdapter getSource() {
88
                return source;
89
        }
90

    
91
        /**
92
         * Inserta el RasterAdapter.
93
         *
94
         * @param ra RasterAdapter.
95
         */
96
        public void setSource(RasterAdapter ra) {
97
                source = ra;
98
        }
99

    
100
        /*
101
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#load()
102
         */
103
        public void load() throws DriverIOException {
104
                ((RasterFileAdapter) source).start();
105
                ((RasterFileAdapter) source).setTransparency(getTransparency());
106
        }
107

    
108
        /**
109
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
110
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
111
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
112
         */
113
        public void draw(BufferedImage image, Graphics2D g, ViewPort vp,
114
                Cancellable cancel) throws DriverException {
115
                try {
116
                        ((RasterFileAdapter) source).draw(image, g, vp, cancel);
117
                } catch (DriverIOException e) {
118
                        throw new DriverException(e);
119
                }
120

    
121
                if (getVirtualLayers() != null) {
122
                        getVirtualLayers().draw(image, g, vp, cancel);
123
                }
124
        }
125

    
126
        /**
127
         * Inserta la proyecci?n.
128
         *
129
         * @param proj Proyecci?n.
130
         */
131
        public void setProjection(IProjection proj) {
132
                super.setProjection(proj);
133

    
134
                if (source != null && source.getDriver() != null && source.getDriver() instanceof GeorreferencedRasterDriver) {
135
                        GeorreferencedRasterDriver geoDrv = (GeorreferencedRasterDriver) source.getDriver();
136

    
137
                        if (geoDrv.getProjection() == null) {
138
                                geoDrv.setProjection(proj);
139
                        }
140
                }
141
        }
142
        
143
        public void setTransparency(int trans) {
144
                super.setTransparency(trans);
145
                ((RasterFileAdapter) source).setTransparency(trans);
146
                // TODO LWS Guarradita para actualizar el MapControl con ApplyButton
147
                getFMap().invalidate();
148
        }
149

    
150
/*        public int getTransparency() {
151
                return ((RasterFileAdapter) source).getTransparency();
152
        }
153
*/
154
        /*
155
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
156
         */
157
        public Rectangle2D getFullExtent() throws DriverException {
158
                return ((RasterFileAdapter) source).getFullExtent();
159
        }
160

    
161
        /* (non-Javadoc)
162
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(java.awt.Point)
163
         */
164
        public String queryByPoint(Point p) throws DriverException {
165
                String data = "<"+getName()+">\n";
166

    
167
                ArrayList attr = source.getAttributes();
168
                data += "  <raster\n";
169
                data += "    File=\""+((RasterFileAdapter) source).getFile()+"\"\n";
170
                for (int i=0; i<attr.size(); i++) {
171
                        Object [] a = (Object []) attr.get(i);
172

    
173
                        data += "    "+a[0].toString()+"=";
174
                        if (a[1].toString() instanceof String)
175
                                data += "\""+a[1].toString()+"\"\n";
176
                        else
177
                                data += a[1].toString()+"\n";
178
                }
179
                data += "  />\n";
180

    
181
                data += "</"+getName()+">\n";
182
                System.out.println(data);
183
                return data;
184
        }
185

    
186
        /**
187
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
188
         */
189
        public XMLEntity getXMLEntity() {
190
                XMLEntity xml = super.getXMLEntity();
191

    
192
                if (source instanceof RasterFileAdapter) {
193
                        xml.putProperty("file", ((RasterFileAdapter) source).getFile());
194
                }
195

    
196
                xml.putProperty("driverName", getSource().getDriver().getName());
197

    
198
                return xml;
199
        }
200
        
201
        public void setXMLEntity(XMLEntity xml)
202
        throws XMLException {
203
                super.setXMLEntity(xml);
204
                try {
205
                        Driver d = LayerFactory.getDM().getDriver(
206
                                xml.getStringProperty("driverName"));
207
                        File f = new File(xml.getStringProperty("file"));
208
                        RasterAdapter adapter = new RasterFileAdapter(f);
209
                        adapter.setDriver(d);
210
                        setSource(adapter);
211
                        // Para notificar al adapter-driver cual es la proyecci?n.
212
                        setProjection(super.getProjection());
213
                } catch (DriverLoadException e) {
214
                        throw new XMLException(e);
215
                }
216
        }
217

    
218
        /* (non-Javadoc)
219
         * @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)
220
         */
221
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
222
                throws DriverException {
223
                isPrinting = true;
224
                if (!mustTilePrint) {
225
                        draw(null, g, viewPort, cancel);
226
                } else {
227
                // Para no pedir imagenes demasiado grandes, vamos
228
                // a hacer lo mismo que hace EcwFile: chunkear.
229
                // Llamamos a drawView con cuadraditos m?s peque?os
230
                // del BufferedImage ni caso, cuando se imprime viene con null
231
                        Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
232
                        tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
233
                        for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) {
234
                            // Parte que dibuja
235
                            try {
236
                                ViewPort vp = tiles.getTileViewPort(viewPort, tileNr);
237
                                draw(null, g, vp, cancel);
238
                                } catch (NoninvertibleTransformException e) {
239
                                        // TODO Auto-generated catch block
240
                                        e.printStackTrace();
241
                                }
242
                }
243
                }
244
            isPrinting = false;
245
        }
246

    
247
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
248
                throws DriverException {                
249
                // Para no pedir imagenes demasiado grandes, vamos
250
                // a hacer lo mismo que hace EcwFile: chunkear.
251
                // Llamamos a drawView con cuadraditos m?s peque?os
252
                // del BufferedImage ni caso, cuando se imprime viene con null
253
                int numW, numH;
254
                int stepX, stepY;
255
                int xProv, yProv, wProv, hProv;
256
                double xProvD, yProvD, wProvD, hProvD;
257
                int A = 1500; 
258
                int H = 1500;
259
                int altoAux, anchoAux;
260
                
261
                AffineTransform mat = (AffineTransform) viewPort.getAffineTransform().clone();
262
                
263
                // Vamos a hacerlo en trozos de AxH
264
                Rectangle r = g.getClipRect();
265
                numW = (int) (r.width) / A;
266
                numH = (int) (r.height) / H;                
267
                
268
                
269
                double[] srcPts = new double[8];
270
                double[] dstPts= new double[8];
271

    
272
                yProv = (int) r.y;
273
                for (stepY=0; stepY < numH+1; stepY++)
274
                {
275
                        if ((yProv + H) > r.getMaxY()) 
276
                                altoAux = (int) r.getMaxY() - yProv;
277
                        else
278
                                altoAux = H;
279
                                                
280
                        xProv = (int) r.x;
281
                        for (stepX=0; stepX < numW+1; stepX++)                                
282
                        {                        
283
                                    if ((xProv + A) > r.getMaxX()) 
284
                                            anchoAux = (int) r.getMaxX() - xProv;
285
                                    else
286
                                            anchoAux = A;
287
                                
288
                                Rectangle newRect = new Rectangle(xProv, yProv, anchoAux, altoAux);
289
                                
290
                                // Parte que dibuja
291
                                srcPts[0] = xProv;
292
                                srcPts[1] = yProv;
293
                                srcPts[2] = xProv + anchoAux+1;
294
                                srcPts[3] = yProv;
295
                                srcPts[4] = xProv + anchoAux+1;
296
                                srcPts[5] = yProv + altoAux+1;
297
                                srcPts[6] = xProv;
298
                                srcPts[7] = yProv + altoAux+1;
299
                                
300
                                try {
301
                                                mat.inverseTransform(srcPts, 0, dstPts, 0, 4);
302
                                        Rectangle2D.Double rectCuadricula = new Rectangle2D.Double(
303
                                                        dstPts[0], dstPts[1],
304
                                                                dstPts[2] - dstPts[0], dstPts[5]-dstPts[3]); 
305
                                        Extent extent = new Extent(rectCuadricula);
306
                                        
307
                                        Dimension tam = new Dimension(anchoAux+1, altoAux+1);
308
                                        ViewPort vp = viewPort.cloneViewPort();
309
                                        vp.setImageSize(tam);
310
                                        vp.setExtent(rectCuadricula);
311
                                        vp.setAffineTransform(mat);
312
                                        // ViewPortData vp = new ViewPortData(getProjection(), extent, tam);
313
                                        // vp.setMat(mat);
314
                                        // rasterList.draw(g2, vp);
315
                                                                                
316
                                        System.out.println("FLyrRaster.print(): fila "+stepX+" de "
317
                                                + numW + " , col "+stepY+" de " + numH + 
318
                                                "\n, Extent = "+vp.getExtent() + " imageSize: "
319
                                                + tam);
320
                                        draw(null, g, vp, cancel);
321
                                                
322
                                        } catch (NoninvertibleTransformException e) {
323
                                                // TODO Auto-generated catch block
324
                                                e.printStackTrace();
325
                                        }
326
                                // Fin parte que dibuja
327
                                        xProv = xProv + A;        
328
                        }                        
329
                        yProv = yProv + H;
330
                }  
331
                
332
        }
333
}