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 / ViewRasterRequestManager.java @ 1712

History | View | Annotate | Download (7.47 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.fmap.dal.coverage.RasterLocator;
32
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
33
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
34
import org.gvsig.fmap.dal.exception.ReadException;
35
import org.gvsig.fmap.geom.GeometryLocator;
36
import org.gvsig.fmap.geom.GeometryManager;
37
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
38
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
39
import org.gvsig.fmap.geom.primitive.Envelope;
40
import org.gvsig.fmap.mapcontext.ViewPort;
41
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
42
import org.gvsig.raster.fmap.layers.DefaultFLyrRaster;
43
import org.gvsig.raster.fmap.layers.FLyrRaster;
44
import org.gvsig.raster.georeferencing.swing.exception.InvalidRequestException;
45
import org.gvsig.raster.georeferencing.swing.impl.GeoreferencingSwingImplLibrary;
46
import org.gvsig.raster.georeferencing.swing.impl.layer.GCPsGraphicLayer;
47
import org.gvsig.raster.georeferencing.swing.view.GeoreferencingView;
48
import org.gvsig.raster.georeferencing.swing.view.IViewRequestManager;
49
import org.gvsig.raster.georeferencing.swing.view.IGraphicLayer;
50

    
51
/** 
52
 * @author Nacho Brodin (nachobrodin@gmail.com)
53
 */
54
public class ViewRasterRequestManager implements IViewRequestManager {
55
        private GeometryManager          geomManager       = GeometryLocator.getGeometryManager();
56
        private GeoreferencingView       view              = null;
57
        private RasterDataStore          store             = null;
58
        private IGraphicLayer            graphicLayer      = null;
59
        private Color                    backGroundColor   = null;
60
        private FLyrRaster               lyr               = null;
61
        
62
        /**
63
         * Asigna la capa a georreferenciar de donde se obtienen los datos.
64
         * @param lyr
65
         */
66
        public ViewRasterRequestManager(GeoreferencingView view, RasterDataStore store) {
67
                this.store = store;
68
                this.view = view;
69
                lyr = new DefaultFLyrRaster();
70
                try {
71
                        ((DefaultFLyrRaster)lyr).setDataStore(store);
72
                } catch (LoadLayerException e) {
73
                        GeoreferencingSwingImplLibrary.messageBoxError("error_setview_preview", null, e);
74
                }
75
        }
76
        
77
        /**
78
         * Asigna la capa de puntos de control
79
         * @param gl
80
         */
81
        public void setGCPsGraphicLayer(GCPsGraphicLayer gl) {
82
                this.graphicLayer = gl;
83
        }
84
        
85
        /**
86
         * Calcula la extensi?n que contendr? la vista a partir del extent
87
         * m?ximo de la capa/s que contienen . Tienen en cuenta las distintan proporciones
88
         * entre vista y petici?n
89
         * @param Rectangle2D
90
         */
91
        public Rectangle2D initRequest(Rectangle2D extent) throws InvalidRequestException {
92
                double x = extent.getX();
93
                double y = extent.getY();
94
                double w = extent.getWidth();
95
                double h = extent.getHeight();
96
                //Calculamos la extensi?n de la vista para el extent m?ximo que va a contener
97
                //teniendo en cuenta las proporciones de ambos.
98
                if(extent.getWidth() < extent.getHeight()) {
99
                        if(((double)view.getCanvasWidth() / (double)view.getCanvasHeight()) <= (extent.getWidth() / extent.getHeight())) {
100
                                h = (view.getCanvasHeight() * w) / view.getCanvasWidth();
101
                                y = extent.getCenterY() + (h / 2);
102
                        } else { //p1 < p2
103
                                w = (view.getCanvasWidth() * h) / view.getCanvasHeight();
104
                                x = extent.getCenterX() - (w / 2);
105
                        }
106
                } else {
107
                        if(((double)view.getCanvasWidth() / (double)view.getCanvasHeight()) >= (extent.getWidth() / extent.getHeight())) {
108
                                w = (view.getCanvasWidth() * h) / view.getCanvasHeight();
109
                                x = extent.getCenterX() - (w / 2);
110
                        } else { //p1 < p2
111
                                h = (view.getCanvasHeight() * w) / view.getCanvasWidth();
112
                                y = extent.getCenterY() + (h / 2);
113
                        }
114
                }
115
                Rectangle2D r = new Rectangle2D.Double(x, y, w, h);
116
                setDrawParams(null, r);
117
                request(r);
118
                return r;
119
        }
120
        
121
        /**
122
         * Asigna los par?metros para el control de zoom del mapa
123
         * @param img BufferedImage
124
         * @param vp ViewPort
125
         */
126
        public void setDrawParams(BufferedImage img, Rectangle2D extBuf) {
127
                if(view != null) {
128
                        if(img != null)
129
                                view.setDrawParams(img, extBuf, extBuf.getWidth()/img.getWidth(), new Point2D.Double(extBuf.getCenterX(), extBuf.getCenterY()));
130
                        else
131
                                view.setDrawParams(img, extBuf, extBuf.getWidth()/view.getCanvasWidth(), new Point2D.Double(extBuf.getCenterX(), extBuf.getCenterY()));
132
                }
133
        }
134
        
135
        /**
136
         * Obtiene el color de fondo
137
         * @return
138
         */
139
        public Color getBackGroundColor() {
140
                return backGroundColor;
141
        }
142

    
143
        /**
144
         * Asigna el color de fondo
145
         * @param backGroundColor
146
         */
147
        public void setBackGroundColor(Color backGroundColor) {
148
                this.backGroundColor = backGroundColor;
149
        }
150

    
151
        public void fullExtent() throws InvalidRequestException {
152
                this.initRequest(store.getExtent().toRectangle2D());
153
        }
154

    
155
        public Rectangle2D request(Rectangle2D extent) throws InvalidRequestException {
156
                if(extent == null)
157
                        return store.getExtent().toRectangle2D();
158
                
159
                if(store.getExtent() == null) {
160
                        GeoreferencingSwingImplLibrary.messageBoxError("error_set_view", this, null);
161
                        return null;
162
                }
163
                                                        
164
                //Ajustamos el extent al del raster
165
                Extent ext = RasterLocator.getManager().getDataStructFactory().createExtent(extent.getMinX(), extent.getMaxY(), extent.getMaxX(), extent.getMinY());
166
                /*if(view.getCanvas().getMinxMaxyUL()) {
167
                        ext = RasterLocator.getManager().getDataStructFactory().createExtent(extent);
168
                } else {
169
                        ext = RasterLocator.getManager().getDataStructFactory().createExtent(extent.getMinX(), extent.getMinY(), extent.getMaxX(), extent.getMaxY());
170
                }*/
171
                
172
                try {
173
                        ViewPort vp = new ViewPort();
174
                        vp.setImageSize(new Dimension(view.getCanvasWidth(), view.getCanvasHeight()));
175
                        Envelope env = geomManager.createEnvelope(ext.getMin().getX(), ext.getMin().getY(), ext.getMax().getX(), ext.getMax().getY(), SUBTYPES.GEOM2D);
176
                        vp.setEnvelope(env);
177
                        
178
                        //Dibujamos a trav?s del render de la capa en un graphics como el de la vista
179
                        BufferedImage initImg = new BufferedImage(view.getCanvasWidth(), view.getCanvasHeight(), BufferedImage.TYPE_INT_ARGB);
180
                        Graphics2D g2d = ((Graphics2D)initImg.getGraphics());
181
                        if(backGroundColor != null && backGroundColor != Color.BLACK) {
182
                                g2d.setColor(backGroundColor);
183
                                g2d.fillRect(0, 0, view.getCanvasWidth(), view.getCanvasHeight());
184
                        }
185
                        lyr.draw(initImg, g2d, vp, null, 1);
186
                
187
                        setDrawParams(initImg, extent);
188
                        
189
                        if(graphicLayer != null)
190
                                graphicLayer.recalcPixelDrawCoordinates();
191
                        
192
                } catch (ReadException e) {
193
                        throw new InvalidRequestException("Error en al acceso al fichero", e);
194
                } catch (CreateEnvelopeException e) {
195
                        throw new InvalidRequestException("Error asignando el ?rea de la petici?n", e);
196
                }
197
                return extent;
198
        }
199
}