Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / ui / zoom / ViewMapRequestManager.java @ 28978

History | View | Annotate | Download (7.45 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.Point2D;
25
import java.awt.geom.Rectangle2D;
26
import java.awt.image.BufferedImage;
27

    
28
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
29
import org.gvsig.georeferencing.ui.zoom.layers.GCPsGraphicLayer;
30
import org.gvsig.georeferencing.view.BaseZoomView;
31

    
32
import com.iver.andami.PluginServices;
33
import com.iver.cit.gvsig.fmap.DefaultMapContextDrawer;
34
import com.iver.cit.gvsig.fmap.MapControl;
35
import com.iver.cit.gvsig.fmap.ViewPort;
36
import com.iver.cit.gvsig.fmap.layers.FLayers;
37
import com.iver.utiles.swing.threads.Cancellable;
38

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

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

    
65
        /**
66
         * A?ade una capa raster a la lista de capas
67
         * @param lyr
68
         * @throws InvalidRequestException
69
         */
70
        public void addTestRasterLayer(FLyrRasterSE lyr) throws InvalidRequestException {
71
                if(lyrs == null || lyr == null)
72
                        return;
73
                testLayer = lyr;
74
                lyrs.addLayer(lyr);
75
                view.getCanvas().setForceRequest(true);
76
                fullExtent();
77
        }
78

    
79
        /**
80
         * Elimina la capa de test de la vista de mapa
81
         * @throws InvalidRequestException
82
         */
83
        public void removeTestRasterLayer() throws InvalidRequestException {
84
                if(testLayer != null){
85
                        lyrs.removeLayer(testLayer);
86
                        PluginServices.getMainFrame().enableControls();
87
                }
88
                view.getCanvas().setForceRequest(true);
89
                fullExtent();
90
        }
91

    
92
        /**
93
         * Asigna la capa de puntos de control
94
         * @param gl
95
         */
96
        public void setGCPsGraphicLayer(GCPsGraphicLayer gl) {
97
                this.graphicLayer = gl;
98
        }
99

    
100
        /**
101
         * Calcula la extensi?n que contendr? la vista a partir del extent
102
         * m?ximo de la capa/s que contienen . Tienen en cuenta las distintan proporciones
103
         * entre vista y petici?n
104
         * @param Rectangle2D
105
         */
106
        public Rectangle2D initRequest(Rectangle2D extent) throws InvalidRequestException {
107
                double x = extent.getX();
108
                double y = extent.getY();
109
                double w = extent.getWidth();
110
                double h = extent.getHeight();
111
                //Calculamos la extensi?n de la vista para el extent m?ximo que va a contener
112
                //teniendo en cuenta las proporciones de ambos.
113
                if(extent.getWidth() < extent.getHeight()) {
114
                        if(((double)view.getCanvasWidth() / (double)view.getCanvasHeight()) <= (extent.getWidth() / extent.getHeight())) {
115
                                h = (view.getCanvasHeight() * w) / view.getCanvasWidth();
116
                                y = extent.getCenterY() - (h / 2);
117
                        } else { //p1 < p2
118
                                w = (view.getCanvasWidth() * h) / view.getCanvasHeight();
119
                                x = extent.getCenterX() - (w / 2);
120
                        }
121
                } else {
122
                        if(((double)view.getCanvasWidth() / (double)view.getCanvasHeight()) >= (extent.getWidth() / extent.getHeight())) {
123
                                w = (view.getCanvasWidth() * h) / view.getCanvasHeight();
124
                                x = extent.getCenterX() - (w / 2);
125
                        } else { //p1 < p2
126
                                h = (view.getCanvasHeight() * w) / view.getCanvasWidth();
127
                                y = extent.getCenterY() - (h / 2);
128
                        }
129
                }
130
                Rectangle2D r = new Rectangle2D.Double(x, y, w, h);
131
                setDrawParams(null, r);
132
                request(r);
133
                return r;
134
        }
135

    
136
        /*
137
         * (non-Javadoc)
138
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#request(java.awt.geom.Rectangle2D)
139
         */
140
        public Rectangle2D request(Rectangle2D extent) throws InvalidRequestException {
141
                if(extent == null)
142
                        return lyrs.getFullExtent();
143

    
144
                //Obtenemos el viewport y calculamos la matriz de transformaci?n
145
                ViewPort vp = new ViewPort(null);
146
                vp.setImageSize(new Dimension(view.getCanvasWidth(), view.getCanvasHeight()));
147
                vp.setExtent(extent);
148
                vp.setProjection(lyrs.getProjection());
149
                try {
150
                        //Dibujamos a trav?s del render de la capa en un graphics como el de la vista
151
                        BufferedImage initImg = new BufferedImage(view.getCanvasWidth(), view.getCanvasHeight(), BufferedImage.TYPE_INT_RGB);
152
                        Graphics2D img = ((Graphics2D)initImg.getGraphics());
153
                        if(backGroundColor != null && backGroundColor != Color.BLACK) {
154
                                img.setColor(backGroundColor);
155
                                img.fillRect(0, 0, view.getCanvasWidth(), view.getCanvasHeight());
156
                        }
157

    
158
                        DefaultMapContextDrawer mapContextDrawer = new DefaultMapContextDrawer();
159
                        mapContextDrawer.setMapContext(lyrs.getMapContext());
160
                        mapContextDrawer.setViewPort(vp);
161
                        mapContextDrawer.draw(lyrs, initImg, img, new CancellableClass(), mapControl.getMapContext().getScaleView());
162
                        //lyrs.draw(initImg, img, vp, new CancellableClass(), mapControl.getMapContext().getScaleView());
163

    
164
                        setDrawParams(initImg, extent);
165

    
166
                        if(graphicLayer != null)
167
                                graphicLayer.recalcMapDrawCoordinates();
168

    
169
                } catch (Exception e) {
170
                        throw new InvalidRequestException("Error en al acceso al fichero");
171
                }
172
                return extent;
173
        }
174

    
175
        /*
176
         * (non-Javadoc)
177
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#fullExtent(java.awt.Dimension)
178
         */
179
        public void fullExtent() throws InvalidRequestException  {
180
                this.initRequest(lyrs.getFullExtent());
181
        }
182

    
183
        /**
184
         * Asigna los par?metros para el control de zoom del mapa
185
         * @param img BufferedImage
186
         * @param vp ViewPort
187
         */
188
        public void setDrawParams(BufferedImage img, Rectangle2D extBuf) {
189
                if(view != null && lyrs != null) {
190
                        if(img != null)
191
                                view.setDrawParams(img, extBuf, extBuf.getWidth()/img.getWidth(), new Point2D.Double(extBuf.getCenterX(), extBuf.getCenterY()));
192
                        else
193
                                view.setDrawParams(img, extBuf, extBuf.getWidth()/view.getCanvasWidth(), new Point2D.Double(extBuf.getCenterX(), extBuf.getCenterY()));
194
                }
195
        }
196

    
197
        /**
198
         * Obtiene el color de fondo
199
         * @return
200
         */
201
        public Color getBackGroundColor() {
202
                return backGroundColor;
203
        }
204

    
205
        /**
206
         * Asigna el color de fondo
207
         * @param backGroundColor
208
         */
209
        public void setBackGroundColor(Color backGroundColor) {
210
                this.backGroundColor = backGroundColor;
211
        }
212

    
213
        class CancellableClass implements Cancellable{
214
            private boolean cancel = false;
215
                   public void setCanceled(boolean canceled) {
216
                           this.cancel = canceled;
217
                   }
218

    
219
                public boolean isCanceled() {
220
                        return this.cancel;
221
                }
222
           }
223
}