Statistics
| Revision:

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

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
         * @throws XMLException
188
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
189
         */
190
        public XMLEntity getXMLEntity() throws XMLException {
191
                XMLEntity xml = super.getXMLEntity();
192

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

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

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

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

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

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