Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeoreferencing / src / org / gvsig / georeferencing / ui / zoom / ViewRasterRequestManager.java @ 27098

History | View | Annotate | Download (8.29 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.geom.primitive.Envelope;
30
import org.gvsig.fmap.geom.util.UtilFunctions;
31
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
32
import org.gvsig.georeferencing.ui.zoom.layers.GCPsGraphicLayer;
33
import org.gvsig.georeferencing.view.BaseZoomView;
34
import org.gvsig.raster.dataset.InvalidSetViewException;
35
import org.gvsig.raster.dataset.io.RasterDriverException;
36
import org.gvsig.raster.datastruct.Extent;
37
import org.gvsig.raster.datastruct.ViewPortData;
38
import org.gvsig.raster.util.RasterToolsUtil;
39
import org.gvsig.raster.util.RasterUtilities;
40

    
41
/**
42
 * Gestor de peticiones de zoom sobre el panel con el raster a georreferenciar.
43
 * Implementa el interfaz IExtensionRequest para que este comunique la peticiones
44
 * de zoom y ViewRasterRequestManager pueda hacer las peticiones de datos a la capa.
45
 *
46
 * 30/12/2007
47
 * @author Nacho Brodin (nachobrodin@gmail.com)
48
 */
49
public class ViewRasterRequestManager implements IExtensionRequest {
50
        private FLyrRasterSE       lyr = null;
51
        private BaseZoomView       view = null;
52
        private GCPsGraphicLayer   graphicLayer = null;
53
        private Color              backGroundColor   = null;
54

    
55
        /**
56
         * Asigna la capa a georreferenciar de donde se obtienen los datos.
57
         * @param lyr
58
         */
59
        public ViewRasterRequestManager(BaseZoomView view, FLyrRasterSE lyr) {
60
                this.lyr = lyr;
61
                this.view = view;
62
        }
63

    
64
        /**
65
         * Asigna la capa de puntos de control
66
         * @param gl
67
         */
68
        public void setGCPsGraphicLayer(GCPsGraphicLayer gl) {
69
                this.graphicLayer = gl;
70
        }
71

    
72
        /**
73
         * Calcula la extensi?n que contendr? la vista a partir del extent
74
         * m?ximo de la capa/s que contienen . Tienen en cuenta las distintan proporciones
75
         * entre vista y petici?n
76
         * @param Rectangle2D
77
         */
78
        public Envelope initRequest(Envelope env) throws InvalidRequestException {
79
                double x = env.getMinimum(0);
80
                double y = env.getMinimum(1);
81
                double w = env.getLength(0);
82
                double h = env.getLength(1);
83
                //Calculamos la extensi?n de la vista para el extent m?ximo que va a contener
84
                //teniendo en cuenta las proporciones de ambos.
85
                if(env.getLength(0) < env.getLength(1)) {
86
                        if(((double)view.getCanvasWidth() / (double)view.getCanvasHeight()) <= (env.getLength(0) / env.getLength(1))) {
87
                                h = (view.getCanvasHeight() * w) / view.getCanvasWidth();
88
                                y = env.getCenter(1) - (h / 2);
89
                        } else { //p1 < p2
90
                                w = (view.getCanvasWidth() * h) / view.getCanvasHeight();
91
                                x = env.getCenter(0) - (w / 2);
92
                        }
93
                } else {
94
                        if(((double)view.getCanvasWidth() / (double)view.getCanvasHeight()) >= (env.getLength(0) / env.getLength(1))) {
95
                                w = (view.getCanvasWidth() * h) / view.getCanvasHeight();
96
                                x = env.getCenter(0) - (w / 2);
97
                        } else { //p1 < p2
98
                                h = (view.getCanvasHeight() * w) / view.getCanvasWidth();
99
                                y = env.getCenter(1) - (h / 2);
100
                        }
101
                }
102
                Envelope r = UtilFunctions.createEnvelope(x, y, x + w, y + h);
103
                setDrawParams(null, r);
104
                request(r);
105
                return r;
106
        }
107

    
108
        /*
109
         * (non-Javadoc)
110
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#request(java.awt.geom.Rectangle2D)
111
         */
112
        public Envelope request(Envelope extent) throws InvalidRequestException {
113
                if(extent == null) {
114
                        return lyr.getFullEnvelope();
115
                }
116

    
117
                if(lyr.getFullRasterExtent() == null) {
118
                        RasterToolsUtil.messageBoxError("error_set_view",null);
119
                        return null;
120
                }
121

    
122
                //Ajustamos el extent al del raster
123
                Extent ext = null;
124
                Extent extSelection = null;
125
                if(view.getCanvas().getMinxMaxyUL()) {
126
                        Rectangle2D rect=new Rectangle2D.Double(extent.getMinimum(0),extent.getMinimum(1),extent.getLength(0),extent.getLength(1));
127
                        ext = new Extent(rect);
128
                        extSelection = calculateAdjustedView(ext, lyr.getFullRasterExtent());
129
                } else {
130
                        ext = new Extent(extent.getMinimum(0), extent.getMinimum(1), extent.getMaximum(0), extent.getMaximum(1));
131
                    extSelection = RasterUtilities.calculateAdjustedView(ext, lyr.getFullRasterExtent());
132
                }
133

    
134
                //Obtenemos el viewport y calculamos la matriz de transformaci?n
135
                ViewPortData vp = new ViewPortData(        null, ext, new Dimension(view.getCanvasWidth(), view.getCanvasHeight()));
136
                vp.calculateAffineTransform();
137

    
138
                //Calculamos el punto del canvas de la vista donde se empieza a dibujar el buffer de la imagen
139
                Point2D pt = new Point2D.Double(extSelection.getULX(), extSelection.getULY());
140
                AffineTransform at = vp.mat;
141
                if(!view.getCanvas().getMinxMaxyUL()) {
142
                        at = new AffineTransform(1, 0, 0, -1, 0, 0);
143
                        at.concatenate(vp.mat);
144
                }
145
                at.transform(pt, pt);
146

    
147
                try {
148
                        //Dibujamos a trav?s del render de la capa en un graphics como el de la vista
149
                        BufferedImage initImg = new BufferedImage(view.getCanvasWidth(), view.getCanvasHeight(), BufferedImage.TYPE_INT_RGB);
150
                        Graphics2D img = ((Graphics2D)initImg.getGraphics());
151
                        if(backGroundColor != null && backGroundColor != Color.BLACK) {
152
                                img.setColor(backGroundColor);
153
                                img.fillRect(0, 0, view.getCanvasWidth(), view.getCanvasHeight());
154
                        }
155
                        BufferedImage drawedImg = (BufferedImage)lyr.getRender().draw(img, vp);
156
                        img.drawImage(drawedImg, (int) Math.round(pt.getX()), (int) Math.round(pt.getY()), null);
157

    
158
                        setDrawParams(initImg, extent);
159

    
160
                        if(graphicLayer != null) {
161
                                graphicLayer.recalcPixelDrawCoordinates();
162
                        }
163

    
164
                } catch (RasterDriverException e) {
165
                        throw new InvalidRequestException("Error en al acceso al fichero");
166
                } catch (InvalidSetViewException e) {
167
                        throw new InvalidRequestException("Error asignando el ?rea de la petici?n");
168
                } catch (InterruptedException e) {
169
                }
170
                return extent;
171
        }
172

    
173
        /**
174
         * Ajusta la extensi?n del primer par?metro a los l?mites de la segunda
175
         * @param extToAdj
176
         * @param imgExt
177
         * @return
178
         */
179
        private Extent calculateAdjustedView(Extent extToAdj, Extent imgExt) {
180
        double vx = extToAdj.minX();
181
        double vy = extToAdj.minY();
182
        double vx2 = extToAdj.maxX();
183
        double vy2 = extToAdj.maxY();
184

    
185
        if (extToAdj.minX() < imgExt.minX()) {
186
                        vx = imgExt.minX();
187
                }
188
        if (extToAdj.minY() < imgExt.minY()) {
189
                        vy = imgExt.minY();
190
                }
191
        if (extToAdj.maxX() > imgExt.maxX()) {
192
                        vx2 = imgExt.maxX();
193
                }
194
        if (extToAdj.maxY() > imgExt.maxY()) {
195
                        vy2 = imgExt.maxY();
196
                }
197

    
198
        return new Extent(vx, vy2, vx2, vy);
199
    }
200

    
201
        /*
202
         * (non-Javadoc)
203
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#fullExtent(java.awt.Dimension)
204
         */
205
        public void fullExtent() throws InvalidRequestException  {
206
                Rectangle2D r=lyr.getFullRasterExtent().toRectangle2D();
207
                Envelope env = UtilFunctions.createEnvelope(r.getX(), r.getY(), r
208
                                .getMaxX(), r.getMaxY());
209
                this.initRequest(env);
210
        }
211

    
212
        /**
213
         * Asigna los par?metros para el control de zoom del mapa
214
         * @param img BufferedImage
215
         * @param vp ViewPort
216
         */
217
        public void setDrawParams(BufferedImage img, Envelope extBuf) {
218
                if(view != null && lyr != null) {
219
                        if(img != null) {
220
                                view.setDrawParams(img, extBuf, extBuf.getLength(0)/img.getWidth(), new Point2D.Double(extBuf.getCenter(0), extBuf.getCenter(1)));
221
                        } else {
222
                                view.setDrawParams(img, extBuf, extBuf.getLength(0)/view.getCanvasWidth(), new Point2D.Double(extBuf.getCenter(0), extBuf.getCenter(1)));
223
                        }
224
                }
225
        }
226

    
227
        /**
228
         * Obtiene el color de fondo
229
         * @return
230
         */
231
        public Color getBackGroundColor() {
232
                return backGroundColor;
233
        }
234

    
235
        /**
236
         * Asigna el color de fondo
237
         * @param backGroundColor
238
         */
239
        public void setBackGroundColor(Color backGroundColor) {
240
                this.backGroundColor = backGroundColor;
241
        }
242

    
243
}