Statistics
| Revision:

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

History | View | Annotate | Download (9.97 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()+"</File>\n";
170
                for (int i=0; i<attr.size(); i++) {
171
                        Object [] a = (Object []) attr.get(i);
172

    
173
                        data += "  <"+a[0].toString()+">\""+
174
                                a[1].toString()+"\"</"+a[0].toString()+">\n";
175
                }
176
                data += "</raster>";
177

    
178
                return data+"</"+getName()+">\n";
179
        }
180

    
181
        /**
182
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
183
         */
184
        public XMLEntity getXMLEntity() {
185
                XMLEntity xml = super.getXMLEntity();
186

    
187
                if (source instanceof RasterFileAdapter) {
188
                        xml.putProperty("file", ((RasterFileAdapter) source).getFile());
189
                }
190

    
191
                xml.putProperty("driverName", getSource().getDriver().getName());
192

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

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

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

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