Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / georeferencing / ViewRasterRequestManager.java @ 18230

History | View | Annotate | Download (7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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
package org.gvsig.rastertools.georeferencing;
20

    
21
import java.awt.Dimension;
22
import java.awt.geom.AffineTransform;
23
import java.awt.geom.Point2D;
24
import java.awt.geom.Rectangle2D;
25
import java.awt.image.BufferedImage;
26

    
27
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
28
import org.gvsig.raster.dataset.IBuffer;
29
import org.gvsig.raster.dataset.InvalidSetViewException;
30
import org.gvsig.raster.dataset.io.RasterDriverException;
31
import org.gvsig.raster.datastruct.Extent;
32
import org.gvsig.raster.datastruct.ViewPortData;
33
import org.gvsig.raster.util.RasterUtilities;
34
import org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest;
35
import org.gvsig.rastertools.georeferencing.view.ViewDialog;
36

    
37
/**
38
 * Gestor de peticiones de zoom sobre el panel con el raster a georreferenciar.
39
 * Implementa el interfaz IExtensionRequest para que este comunique la peticiones
40
 * de zoom y ViewRasterRequestManager pueda hacer las peticiones de datos a la capa.
41
 * 
42
 * 30/12/2007
43
 * @author Nacho Brodin (nachobrodin@gmail.com)
44
 */
45
public class ViewRasterRequestManager implements IExtensionRequest {
46
        private FLyrRasterSE     lyr = null;
47
        private ViewDialog        view = null;
48
        
49
        /**
50
         * Asigna la capa a georreferenciar de donde se obtienen los datos.
51
         * @param lyr
52
         */
53
        public ViewRasterRequestManager(ViewDialog view, FLyrRasterSE lyr) {
54
                this.lyr = lyr;
55
                this.view = view;
56
        }
57
                
58
        /**
59
         * Calcula la extensi?n que contendr? la vista a partir del extent
60
         * m?ximo de la capa/s que contienen . Tienen en cuenta las distintan proporciones
61
         * entre vista y petici?n
62
         * @param Rectangle2D
63
         */
64
        public Rectangle2D initRequest(Rectangle2D extent) {
65
                double x, y, w, h;
66
                //Calculamos la extensi?n de la vista para el extent m?ximo que va a contener
67
                //teniendo en cuenta las proporciones de ambos.
68
                if(extent.getHeight() > extent.getWidth()) {
69
                        y = extent.getY();
70
                        h = extent.getHeight();
71
                        w = (view.getCanvasWith() * h) / view.getCanvasHeight();
72
                        x = extent.getCenterX() - (w / 2);
73
                } else {
74
                        x = extent.getX();
75
                        w = extent.getWidth();
76
                        h = (view.getCanvasHeight() * w) / view.getCanvasWith();
77
                        y = extent.getCenterY() + (h / 2);
78
                }
79
                Rectangle2D r = new Rectangle2D.Double(x, y, w, h);
80
                request(r);
81
                return r;
82
        }
83
        
84
        public Rectangle2D request(Rectangle2D extent) {
85
                if(extent == null)
86
                        return lyr.getFullExtent();
87
                                                        
88
                //Ajustamos el extent al del raster
89
                Extent ext = new Extent(extent);
90
                Extent extSelection = RasterUtilities.calculateAdjustedView(ext, lyr.getFullRasterExtent());
91
                
92
                ViewPortData vp = new ViewPortData(null, ext, new Dimension(view.getCanvasWith(), view.getCanvasHeight()));
93
                vp.mat = new AffineTransform();
94
                vp.mat.setToScale(view.getCanvasWith()/ext.width() , -view.getCanvasHeight()/ext.height());
95
                
96
                try {
97
                        BufferedImage img = (BufferedImage)lyr.getRender().draw(view.getCanvasGraphic(), vp);
98
                        if(img != null)
99
                                setDrawParams(img, extSelection.toRectangle2D());
100
                        else
101
                                throw new InvalidSetViewException("Buffer de dibujado vacio");
102
                } catch (RasterDriverException e) {
103
                } catch (InvalidSetViewException e) {
104
                } catch (InterruptedException e) {
105
                }
106
                return extent;
107
        }
108
        
109
        /*
110
         * (non-Javadoc)
111
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#fullExtent(java.awt.Dimension)
112
         */
113
        public void fullExtent() {
114
                this.initRequest(lyr.getFullRasterExtent().toRectangle2D());
115
        }
116
        
117
        /**
118
         * Dibuja la capa raster pasada por par?metro sobre un buffer con las coordenadas
119
         * pixel especificadas.
120
         * @param size Tama?o del graphics sobre el que se dibuja
121
         * @param x Posici?n x donde se empieza a leer la capa raster para la petici?n
122
         * @param y Posici?n x donde se empieza a leer la capa raster para la petici?n
123
         * @param w Ancho de la petici?n sobre la capa raster
124
         * @param h Alto de la petici?n sobre la capa raster
125
         */
126
        private BufferedImage drawRasterLayer(Dimension size, int x, int y, int w, int h, int wCanvas, int hCanvas) {
127
                Rectangle2D r = new Rectangle2D.Double(x, y, w, h);
128
                
129
                int pxW = 0, pxH = 0;
130
                if(        (size.getWidth() > size.getHeight() && r.getWidth() > r.getHeight()) ||
131
                        (size.getWidth() < size.getHeight() && r.getWidth() > r.getHeight())) {
132
                        pxW = (int)size.getWidth();
133
                        pxH =  (int)((r.getHeight() * size.getWidth()) / r.getWidth());
134
                } else {
135
                        pxH = (int)size.getHeight();
136
                        pxW =  (int)((r.getWidth() * size.getHeight()) / r.getHeight());
137
                }
138
                BufferedImage img = new BufferedImage(pxW, pxH, BufferedImage.TYPE_INT_RGB);
139

    
140
                try {
141
                        if (lyr.getBufferFactory() != null && lyr.getBandCount() == 1) {
142
                                lyr.getBufferFactory().setDrawableBands(new int[] { 0, 0, 0 });
143
                        } else if (lyr.getBufferFactory() != null && lyr.getBufferFactory().getDataSource().getBandCount() == 2) {
144
                                lyr.getBufferFactory().setDrawableBands(new int[] { 0, 1, 1 });
145
                        } else {
146
                                lyr.getBufferFactory().setDrawableBands(new int[] { 0, 1, 2 });
147
                        }
148

    
149
                        lyr.getBufferFactory().setAreaOfInterest(x, y, w, h, pxW, pxH);
150
                        IBuffer buf = lyr.getBufferFactory().getRasterBuf();
151
                        for (int i = 0; i < pxH; i++) {
152
                                for (int j = 0; j < pxW; j++) {
153
                                        img.setRGB(j, i, ((buf.getElemByte(i, j, 0) & 0xff) << 16) + 
154
                                                                        ((buf.getElemByte(i, j, 1) & 0xff) << 8) +
155
                                                                        (buf.getElemByte(i, j, 2) & 0xff));
156
                                }
157
                        }
158
                } catch (RasterDriverException e) {
159
                        //TODO: Lanzar una excepci?n
160
                } catch (InvalidSetViewException e) {
161
                        //TODO: Lanzar una excepci?n
162
                } catch (InterruptedException e) {
163
                        //TODO: Lanzar una excepci?n
164
                } catch (NullPointerException e) {
165
                        //TODO: Lanzar una excepci?n
166
                }
167
                
168
                return img;
169
        }
170
        
171
        /**
172
         * Dibuja la capa raster pasada por par?metro sobre un buffer.
173
         * @param size Tama?o del graphics sobre el que se dibuja
174
         * @return BufferedImage cargado con los datos o null si no se ha podido dibujar
175
         */
176
        public BufferedImage drawRasterLayer(Dimension size) {
177
                return drawRasterLayer(size, 0, 0, (int)lyr.getPxWidth(), (int)lyr.getPxHeight(), view.getCanvasWith(), view.getCanvasHeight());
178
        }
179
        
180
        /**
181
         * Asigna los par?metros para el control de zoom del mapa
182
         * @param img BufferedImage
183
         * @param vp ViewPort
184
         */
185
        public void setDrawParams(BufferedImage img, Rectangle2D extBuf) {
186
                if(view != null && lyr != null) {
187
                        view.setPixelDrawParams(img, extBuf, extBuf.getWidth()/img.getWidth(), new Point2D.Double(extBuf.getCenterX(), extBuf.getCenterY()));        
188
                }
189
        }
190

    
191
}