Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / georeferencing / ViewRasterRequestManager.java @ 18216

History | View | Annotate | Download (6.6 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;
20

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

    
26
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
27
import org.gvsig.raster.dataset.IBuffer;
28
import org.gvsig.raster.dataset.InvalidSetViewException;
29
import org.gvsig.raster.dataset.io.RasterDriverException;
30
import org.gvsig.raster.datastruct.Extent;
31
import org.gvsig.raster.util.RasterUtilities;
32
import org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest;
33
import org.gvsig.rastertools.georeferencing.view.ViewDialog;
34

    
35
/**
36
 * Gestor de peticiones de zoom sobre el panel con el raster a georreferenciar.
37
 * Implementa el interfaz IExtensionRequest para que este comunique la peticiones
38
 * de zoom y ViewRasterRequestManager pueda hacer las peticiones de datos a la capa.
39
 * 
40
 * 30/12/2007
41
 * @author Nacho Brodin (nachobrodin@gmail.com)
42
 */
43
public class ViewRasterRequestManager implements IExtensionRequest {
44
        private FLyrRasterSE     lyr = null;
45
        private Dimension        size = null;
46
        private ViewDialog        view = null;
47
        
48
        /**
49
         * Asigna la capa a georreferenciar de donde se obtienen los datos.
50
         * @param lyr
51
         */
52
        public ViewRasterRequestManager(ViewDialog view, FLyrRasterSE lyr, Dimension size) {
53
                this.lyr = lyr;
54
                this.size = size;
55
                this.view = view;
56
        }
57
        
58
        /*
59
         * (non-Javadoc)
60
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#request(java.awt.geom.Rectangle2D)
61
         */
62
        public Rectangle2D request(Rectangle2D extent) {
63
                if(extent == null)
64
                        return new Rectangle2D.Double(0, 0, (int)lyr.getPxWidth(), (int)lyr.getPxHeight());
65
                
66
                //Ajustamos el extent al del raster
67
                Extent imgExt = new Extent(0, (int)lyr.getPxHeight(), (int)lyr.getPxWidth(), 0);
68
                Extent extToAdj = new Extent(extent.getMinX(), extent.getMaxY(), extent.getMaxX(), extent.getMinY());
69
                Extent e = RasterUtilities.calculateAdjustedView(extToAdj, imgExt);
70
                
71
                //Recalculamos el ancho y alto en caso de que el zoom sea < 1:1
72
                Dimension dim = size;
73
                if(extToAdj.width() > imgExt.width() || extToAdj.height() > imgExt.height()) { //Zoom menor 1:1
74
                        int w = (int)((size.getWidth() * imgExt.width()) / extent.getWidth());
75
                        int h = (int)((size.getHeight() * imgExt.height()) / extent.getHeight());
76
                        while(w <= 1) w ++; //Tama?o m?nimo de ancho y alto
77
                        while(h <= 1) h ++;
78
                        dim = new Dimension(w, h);
79
                }
80
                
81
                //Carga del buffer 
82
                BufferedImage img = drawRasterLayer(dim, (int)e.minX(), (int)(lyr.getPxHeight() - e.maxY()), (int)e.width(), (int)e.height());
83
                
84
                //Asignamos los par?metros al control del mapa 
85
                setPixelParams(img, e.toRectangle2D());
86
                
87
                return extent;
88
        }        
89
        
90
        /*
91
         * (non-Javadoc)
92
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IExtensionRequest#fullExtent(java.awt.Dimension)
93
         */
94
        public void fullExtent(Dimension dim) {
95
                BufferedImage img = drawRasterLayer(dim);
96
                Extent imgExt = new Extent(0, (int)lyr.getPxHeight(), (int)lyr.getPxWidth(), 0);
97
                setPixelParams(img, imgExt.toRectangle2D());
98
        }
99
        
100
        /**
101
         * Dibuja la capa raster pasada por par?metro sobre un buffer con las coordenadas
102
         * pixel especificadas.
103
         * @param size Tama?o del graphics sobre el que se dibuja
104
         * @param x Posici?n x donde se empieza a leer la capa raster para la petici?n
105
         * @param y Posici?n x donde se empieza a leer la capa raster para la petici?n
106
         * @param w Ancho de la petici?n sobre la capa raster
107
         * @param h Alto de la petici?n sobre la capa raster
108
         */
109
        private BufferedImage drawRasterLayer(Dimension size, int x, int y, int w, int h) {
110
                Rectangle2D r = new Rectangle2D.Double(x, y, w, h);
111
                
112
                int pxW = 0, pxH = 0;
113
                if(        (size.getWidth() > size.getHeight() && r.getWidth() > r.getHeight()) ||
114
                        (size.getWidth() < size.getHeight() && r.getWidth() > r.getHeight())) {
115
                        pxW = (int)size.getWidth();
116
                        pxH =  (int)((r.getHeight() * size.getWidth()) / r.getWidth());
117
                } else {
118
                        pxH = (int)size.getHeight();
119
                        pxW =  (int)((r.getWidth() * size.getHeight()) / r.getHeight());
120
                }        
121

    
122
                BufferedImage img = new BufferedImage(pxW, pxH, BufferedImage.TYPE_INT_RGB);
123

    
124
                try {
125
                        if (lyr.getBufferFactory() != null && lyr.getBandCount() == 1) {
126
                                lyr.getBufferFactory().setDrawableBands(new int[] { 0, 0, 0 });
127
                        } else if (lyr.getBufferFactory() != null && lyr.getBufferFactory().getDataSource().getBandCount() == 2) {
128
                                lyr.getBufferFactory().setDrawableBands(new int[] { 0, 1, 1 });
129
                        } else {
130
                                lyr.getBufferFactory().setDrawableBands(new int[] { 0, 1, 2 });
131
                        }
132

    
133
                        lyr.getBufferFactory().setAreaOfInterest(x, y, w, h, pxW, pxH);
134
                        IBuffer buf = lyr.getBufferFactory().getRasterBuf();
135
                        for (int i = 0; i < pxH; i++) {
136
                                for (int j = 0; j < pxW; j++) {
137
                                        img.setRGB(j, i, ((buf.getElemByte(i, j, 0) & 0xff) << 16) + 
138
                                                                        ((buf.getElemByte(i, j, 1) & 0xff) << 8) +
139
                                                                        (buf.getElemByte(i, j, 2) & 0xff));
140
                                }
141
                        }
142
                } catch (RasterDriverException e) {
143
                        //TODO: Lanzar una excepci?n
144
                } catch (InvalidSetViewException e) {
145
                        //TODO: Lanzar una excepci?n
146
                } catch (InterruptedException e) {
147
                        //TODO: Lanzar una excepci?n
148
                } catch (NullPointerException e) {
149
                        //TODO: Lanzar una excepci?n
150
                }
151
                
152
                return img;
153
        }
154
        
155
        /**
156
         * Dibuja la capa raster pasada por par?metro sobre un buffer.
157
         * @param size Tama?o del graphics sobre el que se dibuja
158
         * @return BufferedImage cargado con los datos o null si no se ha podido dibujar
159
         */
160
        public BufferedImage drawRasterLayer(Dimension size) {
161
                return drawRasterLayer(size, 0, 0, (int)lyr.getPxWidth(), (int)lyr.getPxHeight());
162
        }
163
        
164
        /**
165
         * Asigna los par?metros para el control de zoom del mapa
166
         * @param img BufferedImage
167
         * @param vp ViewPort
168
         */
169
        public void setPixelParams(BufferedImage img, Rectangle2D extBuf) {
170
                if(view != null && lyr != null) {
171
                        view.setPixelDrawParams(img, extBuf, extBuf.getWidth()/img.getWidth(), new Point2D.Double(extBuf.getCenterX(), extBuf.getCenterY()));        
172
                }
173
        }
174

    
175
}