Statistics
| Revision:

gvsig-raster / org.gvsig.raster.georeferencing / trunk / org.gvsig.raster.georeferencing / org.gvsig.raster.georeferencing.app / org.gvsig.raster.georeferencing.app.georeferencingclient / src / main / java / org / gvsig / raster / georeferencing / app / georeferencingclient / listener / ViewMapRequestManager.java @ 1725

History | View | Annotate | Download (7.93 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.georeferencing.app.georeferencingclient.listener;
23

    
24
import java.awt.Color;
25
import java.awt.Dimension;
26
import java.awt.Graphics2D;
27
import java.awt.geom.Point2D;
28
import java.awt.geom.Rectangle2D;
29
import java.awt.image.BufferedImage;
30

    
31
import org.gvsig.andami.PluginServices;
32
import org.gvsig.fmap.geom.GeometryLocator;
33
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
34
import org.gvsig.fmap.geom.primitive.Envelope;
35
import org.gvsig.fmap.mapcontext.ViewPort;
36
import org.gvsig.fmap.mapcontext.impl.DefaultMapContextDrawer;
37
import org.gvsig.fmap.mapcontext.layers.FLayers;
38
import org.gvsig.fmap.mapcontrol.MapControl;
39
import org.gvsig.raster.fmap.layers.FLyrRaster;
40
import org.gvsig.raster.georeferencing.swing.exception.InvalidRequestException;
41
import org.gvsig.raster.georeferencing.swing.view.GeoreferencingView;
42
import org.gvsig.raster.georeferencing.swing.view.IGraphicLayer;
43
import org.gvsig.raster.georeferencing.swing.view.IViewRequestManager;
44
import org.gvsig.tools.task.Cancellable;
45

    
46
/** 
47
 * @author Nacho Brodin (nachobrodin@gmail.com)
48
 */
49
public class ViewMapRequestManager implements IViewRequestManager {
50
        private FLayers            lyrs              = null;
51
        private GeoreferencingView view              = null;
52
        private MapControl         mapControl        = null;
53
        private IGraphicLayer      graphicLayer      = null;
54
        private Color              backGroundColor   = null;
55
        private FLyrRaster         testLayer         = null;
56
        
57
        public ViewMapRequestManager(GeoreferencingView view, MapControl mapControl) {
58
                this.mapControl = mapControl;
59
                if(mapControl != null)
60
                        this.lyrs = mapControl.getMapContext().getLayers();
61
                this.view = view;
62
        }
63
        /**
64
         * A?ade una capa raster a la lista de capas
65
         * @param lyr
66
         * @throws InvalidRequestException
67
         */
68
        public void addTestRasterLayer(FLyrRaster lyr) throws InvalidRequestException {
69
                if(lyrs == null || lyr == null)
70
                        return;
71
                testLayer = lyr;
72
                lyrs.addLayer(lyr);
73
                view.getCanvas().setForceRequest(true);
74
                fullExtent();
75
        }
76

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

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

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

    
134
        /*
135
         * (non-Javadoc)
136
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#request(java.awt.geom.Rectangle2D)
137
         */
138
        public Rectangle2D request(Rectangle2D req) throws InvalidRequestException {
139
                if(view.getCanvasWidth() == 0 || view.getCanvasHeight() == 0)
140
                        return req;
141
                
142
                if(req == null) {
143
                        Envelope e = lyrs.getFullEnvelope();
144
                        Rectangle2D r = new Rectangle2D.Double(e.getMinimum(0), e.getMinimum(1), e.getLength(0), e.getLength(1));
145
                        return r;
146
                }
147

    
148
                try {
149
                        //Obtenemos el viewport y calculamos la matriz de transformaci?n
150
                        ViewPort vp = new ViewPort(null);
151
                        vp.setImageSize(new Dimension(view.getCanvasWidth(), view.getCanvasHeight()));
152
                        Envelope e = GeometryLocator.getGeometryManager().createEnvelope(req.getMinX(), req.getMinY(), req.getMaxX(), req.getMaxY(), SUBTYPES.GEOM2D);
153
                        vp.setEnvelope(e);
154
                        vp.setProjection(mapControl.getMapContext().getProjection());
155
                        
156
                        //Dibujamos a trav?s del render de la capa en un graphics como el de la vista
157
                        BufferedImage initImg = new BufferedImage(view.getCanvasWidth(), view.getCanvasHeight(), BufferedImage.TYPE_INT_RGB);
158
                        Graphics2D img = ((Graphics2D)initImg.getGraphics());
159
                        if(backGroundColor != null && backGroundColor != Color.BLACK) {
160
                                img.setColor(backGroundColor);
161
                                img.fillRect(0, 0, view.getCanvasWidth(), view.getCanvasHeight());
162
                        }
163

    
164
                        DefaultMapContextDrawer mapContextDrawer = new DefaultMapContextDrawer();
165
                        mapContextDrawer.setMapContext(lyrs.getMapContext());
166
                        mapContextDrawer.setViewPort(vp);
167
                        Cancellable c = new Cancellable() {
168
                                public void setCanceled(boolean canceled) {
169
                                }
170
                                
171
                                public boolean isCanceled() {
172
                                        return false;
173
                                }
174
                        };
175
                        mapContextDrawer.draw(lyrs, initImg, img, c, mapControl.getMapContext().getScaleView());
176
                        //lyrs.draw(initImg, img, vp, new CancellableClass(), mapControl.getMapContext().getScaleView());
177

    
178
                        setDrawParams(initImg, req);
179

    
180
                        //if(graphicLayer != null)
181
                                //graphicLayer.recalcMapDrawCoordinates();
182

    
183
                } catch (Exception ex) {
184
                        throw new InvalidRequestException("Error en al acceso al fichero", ex);
185
                }
186
                return req;
187
        }
188

    
189
        /*
190
         * (non-Javadoc)
191
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#fullExtent(java.awt.Dimension)
192
         */
193
        public void fullExtent() throws InvalidRequestException  {
194
                Envelope e = lyrs.getFullEnvelope();
195
                Rectangle2D r = new Rectangle2D.Double(e.getMinimum(0), e.getMinimum(1), e.getLength(0), e.getLength(1));
196
                this.initRequest(r);
197
        }
198

    
199
        /**
200
         * Asigna los par?metros para el control de zoom del mapa
201
         * @param img BufferedImage
202
         * @param vp ViewPort
203
         */
204
        public void setDrawParams(BufferedImage img, Rectangle2D extBuf) {
205
                if(view != null && lyrs != null) {
206
                        if(img != null)
207
                                view.setDrawParams(img, extBuf, extBuf.getWidth()/img.getWidth(), new Point2D.Double(extBuf.getCenterX(), extBuf.getCenterY()));
208
                        else
209
                                view.setDrawParams(img, extBuf, extBuf.getWidth()/view.getCanvasWidth(), new Point2D.Double(extBuf.getCenterX(), extBuf.getCenterY()));
210
                }
211
        }
212

    
213
        /**
214
         * Obtiene el color de fondo
215
         * @return
216
         */
217
        public Color getBackGroundColor() {
218
                return backGroundColor;
219
        }
220

    
221
        /**
222
         * Asigna el color de fondo
223
         * @param backGroundColor
224
         */
225
        public void setBackGroundColor(Color backGroundColor) {
226
                this.backGroundColor = backGroundColor;
227
        }
228
        
229
}