Statistics
| Revision:

root / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / ui / zoom / ViewRasterRequestManager.java @ 18530

History | View | Annotate | Download (7.83 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.georeferencing.ui.zoom;
20

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

    
29
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
30
import org.gvsig.georeferencing.ui.zoom.layers.GCPsGraphicLayer;
31
import org.gvsig.georeferencing.view.BaseZoomView;
32
import org.gvsig.raster.dataset.InvalidSetViewException;
33
import org.gvsig.raster.dataset.io.RasterDriverException;
34
import org.gvsig.raster.datastruct.Extent;
35
import org.gvsig.raster.datastruct.ViewPortData;
36
import org.gvsig.raster.util.RasterUtilities;
37

    
38
/**
39
 * Gestor de peticiones de zoom sobre el panel con el raster a georreferenciar.
40
 * Implementa el interfaz IExtensionRequest para que este comunique la peticiones
41
 * de zoom y ViewRasterRequestManager pueda hacer las peticiones de datos a la capa.
42
 * 
43
 * 30/12/2007
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 */
46
public class ViewRasterRequestManager implements IExtensionRequest {
47
        private FLyrRasterSE       lyr = null;
48
        private BaseZoomView       view = null;
49
        private GCPsGraphicLayer   graphicLayer = null;
50
        private Color              backGroundColor   = null;
51
        
52
        /**
53
         * Asigna la capa a georreferenciar de donde se obtienen los datos.
54
         * @param lyr
55
         */
56
        public ViewRasterRequestManager(BaseZoomView view, FLyrRasterSE lyr) {
57
                this.lyr = lyr;
58
                this.view = view;
59
        }
60
        
61
        /**
62
         * Asigna la capa de puntos de control
63
         * @param gl
64
         */
65
        public void setGCPsGraphicLayer(GCPsGraphicLayer gl) {
66
                this.graphicLayer = gl;
67
        }
68
                
69
        /**
70
         * Calcula la extensi?n que contendr? la vista a partir del extent
71
         * m?ximo de la capa/s que contienen . Tienen en cuenta las distintan proporciones
72
         * entre vista y petici?n
73
         * @param Rectangle2D
74
         */
75
        public Rectangle2D initRequest(Rectangle2D extent) throws InvalidRequestException {
76
                double x = extent.getX();
77
                double y = extent.getY();
78
                double w = extent.getWidth();
79
                double h = extent.getHeight();
80
                //Calculamos la extensi?n de la vista para el extent m?ximo que va a contener
81
                //teniendo en cuenta las proporciones de ambos.
82
                if(extent.getWidth() < extent.getHeight()) {
83
                        if(((double)view.getCanvasWidth() / (double)view.getCanvasHeight()) <= (extent.getWidth() / extent.getHeight())) {
84
                                h = (view.getCanvasHeight() * w) / view.getCanvasWidth();
85
                                y = extent.getCenterY() - (h / 2);
86
                        } else { //p1 < p2
87
                                w = (view.getCanvasWidth() * h) / view.getCanvasHeight();
88
                                x = extent.getCenterX() - (w / 2);
89
                        }
90
                } else {
91
                        if(((double)view.getCanvasWidth() / (double)view.getCanvasHeight()) >= (extent.getWidth() / extent.getHeight())) {
92
                                w = (view.getCanvasWidth() * h) / view.getCanvasHeight();
93
                                x = extent.getCenterX() - (w / 2);
94
                        } else { //p1 < p2
95
                                h = (view.getCanvasHeight() * w) / view.getCanvasWidth();
96
                                y = extent.getCenterY() - (h / 2);
97
                        }
98
                }
99
                Rectangle2D r = new Rectangle2D.Double(x, y, w, h);
100
                setDrawParams(null, r);
101
                request(r);
102
                return r;
103
        }
104
        
105
        /*
106
         * (non-Javadoc)
107
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#request(java.awt.geom.Rectangle2D)
108
         */
109
        public Rectangle2D request(Rectangle2D extent) throws InvalidRequestException {
110
                if(extent == null)
111
                        return lyr.getFullExtent();
112
                                                        
113
                //Ajustamos el extent al del raster
114
                Extent ext = null;
115
                Extent extSelection = null;
116
                if(view.getCanvas().getMinxMaxyUL()) {
117
                        ext = new Extent(extent);
118
                        extSelection = calculateAdjustedView(ext, lyr.getFullRasterExtent());
119
                } else {
120
                        ext = new Extent(extent.getMinX(), extent.getMinY(), extent.getMaxX(), extent.getMaxY());
121
                    extSelection = RasterUtilities.calculateAdjustedView(ext, lyr.getFullRasterExtent());
122
                }
123
                
124
                //Obtenemos el viewport y calculamos la matriz de transformaci?n
125
                ViewPortData vp = new ViewPortData(        null, ext, new Dimension(view.getCanvasWidth(), view.getCanvasHeight()));
126
                vp.calculateAffineTransform();
127
                
128
                //Calculamos el punto del canvas de la vista donde se empieza a dibujar el buffer de la imagen
129
                Point2D pt = new Point2D.Double(extSelection.getULX(), extSelection.getULY());
130
                AffineTransform at = vp.mat;
131
                if(!view.getCanvas().getMinxMaxyUL()) {
132
                        at = new AffineTransform(1, 0, 0, -1, 0, 0);
133
                        at.concatenate(vp.mat);
134
                }
135
                at.transform(pt, pt);
136
                
137
                try {
138
                        //Dibujamos a trav?s del render de la capa en un graphics como el de la vista
139
                        BufferedImage initImg = new BufferedImage(view.getCanvasWidth(), view.getCanvasHeight(), BufferedImage.TYPE_INT_RGB);
140
                        Graphics2D img = ((Graphics2D)initImg.getGraphics());
141
                        if(backGroundColor != null && backGroundColor != Color.BLACK) {
142
                                img.setColor(backGroundColor);
143
                                img.fillRect(0, 0, view.getCanvasWidth(), view.getCanvasHeight());
144
                        }
145
                        BufferedImage drawedImg = (BufferedImage)lyr.getRender().draw(img, vp);
146
                        img.drawImage(drawedImg, (int) Math.round(pt.getX()), (int) Math.round(pt.getY()), null);
147
                
148
                        setDrawParams(initImg, extent);
149
                        
150
                        if(graphicLayer != null)
151
                                graphicLayer.recalcPixelDrawCoordinates();
152
                        
153
                } catch (RasterDriverException e) {
154
                        throw new InvalidRequestException("Error en al acceso al fichero");
155
                } catch (InvalidSetViewException e) {
156
                        throw new InvalidRequestException("Error asignando el ?rea de la petici?n");
157
                } catch (InterruptedException e) {
158
                }
159
                return extent;
160
        }
161
        
162
        /**
163
         * Ajusta la extensi?n del primer par?metro a los l?mites de la segunda
164
         * @param extToAdj
165
         * @param imgExt
166
         * @return
167
         */
168
        private Extent calculateAdjustedView(Extent extToAdj, Extent imgExt) { 
169
        double vx = extToAdj.minX();
170
        double vy = extToAdj.minY();
171
        double vx2 = extToAdj.maxX();
172
        double vy2 = extToAdj.maxY();
173

    
174
        if (extToAdj.minX() < imgExt.minX())
175
            vx = imgExt.minX();
176
        if (extToAdj.minY() < imgExt.minY()) 
177
            vy = imgExt.minY();
178
        if (extToAdj.maxX() > imgExt.maxX()) 
179
            vx2 = imgExt.maxX();
180
        if (extToAdj.maxY() > imgExt.maxY())
181
            vy2 = imgExt.maxY();
182
        
183
        return new Extent(vx, vy2, vx2, vy);
184
    }
185
        
186
        /*
187
         * (non-Javadoc)
188
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#fullExtent(java.awt.Dimension)
189
         */
190
        public void fullExtent() throws InvalidRequestException  {
191
                this.initRequest(lyr.getFullRasterExtent().toRectangle2D());
192
        }
193
                        
194
        /**
195
         * Asigna los par?metros para el control de zoom del mapa
196
         * @param img BufferedImage
197
         * @param vp ViewPort
198
         */
199
        public void setDrawParams(BufferedImage img, Rectangle2D extBuf) {
200
                if(view != null && lyr != null) {
201
                        if(img != null)
202
                                view.setDrawParams(img, extBuf, extBuf.getWidth()/img.getWidth(), new Point2D.Double(extBuf.getCenterX(), extBuf.getCenterY()));
203
                        else
204
                                view.setDrawParams(img, extBuf, extBuf.getWidth()/view.getCanvasWidth(), new Point2D.Double(extBuf.getCenterX(), extBuf.getCenterY()));
205
                }
206
        }
207
        
208
        /**
209
         * Obtiene el color de fondo
210
         * @return
211
         */
212
        public Color getBackGroundColor() {
213
                return backGroundColor;
214
        }
215

    
216
        /**
217
         * Asigna el color de fondo
218
         * @param backGroundColor
219
         */
220
        public void setBackGroundColor(Color backGroundColor) {
221
                this.backGroundColor = backGroundColor;
222
        }
223

    
224
}