Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / georeferencing / ui / zoom / ViewMapRequestManager.java @ 18277

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

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

    
27
import org.gvsig.raster.dataset.InvalidSetViewException;
28
import org.gvsig.raster.datastruct.Extent;
29
import org.gvsig.raster.util.RasterUtilities;
30
import org.gvsig.rastertools.georeferencing.view.BaseZoomView;
31

    
32
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
33
import com.iver.cit.gvsig.fmap.MapControl;
34
import com.iver.cit.gvsig.fmap.ViewPort;
35
import com.iver.cit.gvsig.fmap.layers.FLayers;
36
import com.iver.utiles.swing.threads.Cancellable;
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 ViewMapRequestManager implements IExtensionRequest {
47
        private FLayers          lyrs          = null;
48
        private BaseZoomView     view          = null;
49
        private MapControl       mapControl    = null;
50
        
51
        /**
52
         * Asigna la capa a georreferenciar de donde se obtienen los datos.
53
         * @param lyr
54
         */
55
        public ViewMapRequestManager(BaseZoomView view, MapControl mapControl) {
56
                this.mapControl = mapControl;
57
                this.lyrs = mapControl.getMapContext().getLayers();
58
                this.view = view;
59
        }
60
                
61
        /**
62
         * Calcula la extensi?n que contendr? la vista a partir del extent
63
         * m?ximo de la capa/s que contienen . Tienen en cuenta las distintan proporciones
64
         * entre vista y petici?n
65
         * @param Rectangle2D
66
         */
67
        public Rectangle2D initRequest(Rectangle2D extent) throws InvalidRequestException {
68
                double x = extent.getX();
69
                double y = extent.getY();
70
                double w = extent.getWidth();
71
                double h = extent.getHeight();
72
                //Calculamos la extensi?n de la vista para el extent m?ximo que va a contener
73
                //teniendo en cuenta las proporciones de ambos.
74
                if(extent.getWidth() < extent.getHeight()) {
75
                        if(((double)view.getCanvasWidth() / (double)view.getCanvasHeight()) <= (extent.getWidth() / extent.getHeight())) {
76
                                h = (view.getCanvasHeight() * w) / view.getCanvasWidth();
77
                                y = extent.getCenterY() - (h / 2);
78
                        } else { //p1 < p2
79
                                w = (view.getCanvasWidth() * h) / view.getCanvasHeight();
80
                                x = extent.getCenterX() - (w / 2);
81
                        }
82
                } else {
83
                        if(((double)view.getCanvasWidth() / (double)view.getCanvasHeight()) >= (extent.getWidth() / extent.getHeight())) {
84
                                w = (view.getCanvasWidth() * h) / view.getCanvasHeight();
85
                                x = extent.getCenterX() - (w / 2);
86
                        } else { //p1 < p2
87
                                h = (view.getCanvasHeight() * w) / view.getCanvasWidth();
88
                                y = extent.getCenterY() - (h / 2);
89
                        }
90
                }
91
                Rectangle2D r = new Rectangle2D.Double(x, y, w, h);
92
                setDrawParams(null, r);
93
                request(r);
94
                return r;
95
        }
96
        
97
        /*
98
         * (non-Javadoc)
99
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#request(java.awt.geom.Rectangle2D)
100
         */
101
        public Rectangle2D request(Rectangle2D extent) throws InvalidRequestException {
102
                if(extent == null)
103
                        return lyrs.getFullExtent();
104
                                                        
105
                //Ajustamos el extent al del raster
106
                Extent ext = new Extent(extent);
107
                Extent extSelection = RasterUtilities.calculateAdjustedView(ext, new Extent(lyrs.getFullExtent()));
108
                
109
                //Obtenemos el viewport y calculamos la matriz de transformaci?n
110
                ViewPort vp = new ViewPort(null);
111
                vp.setImageSize(new Dimension(view.getCanvasWidth(), view.getCanvasHeight()));
112
                vp.setExtent(extent);
113
                
114
                //Calculamos el punto del canvas de la vista donde se empieza a dibujar el buffer de la imagen
115
                Point2D pt = new Point2D.Double(extSelection.getULX(), extSelection.getLRY());
116
                vp.getAffineTransform().transform(pt, pt);
117
                
118
                try {
119
                        //Dibujamos a trav?s del render de la capa en un graphics como el de la vista
120
                        BufferedImage initImg = new BufferedImage(view.getCanvasWidth(), view.getCanvasHeight(), BufferedImage.TYPE_INT_RGB);
121
                        lyrs.draw(initImg, (Graphics2D)initImg.getGraphics(), vp, new CancellableClass(), mapControl.getMapContext().getScaleView());
122
                        
123
                        //BufferedImage buf = new BufferedImage(view.getCanvasWidth(), view.getCanvasHeight(), BufferedImage.TYPE_INT_RGB);
124
                        //((Graphics2D)buf.getGraphics()).drawImage(initImg, (int) Math.round(pt.getX()), (int) Math.round(pt.getY()), null);
125
                        
126
                        setDrawParams(initImg, extent);
127
                        
128
                } catch (Exception e) {
129
                        throw new InvalidRequestException("Error en al acceso al fichero");
130
                }
131
                return extent;
132
        }
133
        
134
        /*
135
         * (non-Javadoc)
136
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#fullExtent(java.awt.Dimension)
137
         */
138
        public void fullExtent() throws InvalidRequestException  {
139
                this.initRequest(lyrs.getFullExtent());
140
        }
141
                        
142
        /**
143
         * Asigna los par?metros para el control de zoom del mapa
144
         * @param img BufferedImage
145
         * @param vp ViewPort
146
         */
147
        public void setDrawParams(BufferedImage img, Rectangle2D extBuf) {
148
                if(view != null && lyrs != null) {
149
                        if(img != null)
150
                                view.setDrawParams(img, extBuf, extBuf.getWidth()/img.getWidth(), new Point2D.Double(extBuf.getCenterX(), extBuf.getCenterY()));
151
                }
152
        }
153

    
154
        class CancellableClass implements Cancellable{
155
            private boolean cancel = false;
156
                   public void setCanceled(boolean canceled) {
157
                           this.cancel = canceled;
158
                   }
159

    
160
                public boolean isCanceled() {
161
                        return this.cancel;
162
                }
163
           }
164
}