Statistics
| Revision:

root / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / fmap / layers / FLyrGeoRaster.java @ 2980

History | View | Annotate | Download (8.97 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.layers;
42

    
43
import java.awt.Graphics2D;
44
import java.awt.geom.Point2D;
45
import java.awt.geom.Rectangle2D;
46
import java.awt.image.BufferedImage;
47

    
48
import org.cresques.px.Extent;
49

    
50
import com.iver.cit.gvsig.StackZoom;
51
import com.iver.cit.gvsig.fmap.DriverException;
52
import com.iver.cit.gvsig.fmap.ViewPort;
53
import com.iver.cit.gvsig.fmap.operations.Cancellable;
54
import com.iver.cit.gvsig.gui.Panels.SelectFilePanel;
55

    
56

    
57
/**
58
 * Clase de capa raster georeferenciada.
59
 *
60
 * @author Nacho Brodin (brodin_ign@gva.es)
61
 */
62
public class FLyrGeoRaster extends FLyrRaster{
63
        
64
        private Extent assignExtent = null;
65
        private StackZoom zoom = new StackZoom();
66
        
67
        /**
68
         * Funci?n encargada de realizar la transformaci?n para las coordenadas
69
         * del viewPort que se quiere dibujar. Para esta transformaci?n se tiene
70
         * en cuenta que la imagen tiene un extent propio y uno asignado por el 
71
         * usuario por lo que habr? que transformar las coordenadas que solicita
72
         * el usuario en coordenadas de la imagen.
73
         * @param vp        ViewPort solicitado por el usuario.
74
         * @return        ViewPort transformado a coordenadas de la imagen.
75
         */
76
        private ViewPort toImageCoord(ViewPort vp) throws DriverException{
77
                if(this.assignExtent == null)
78
                        return vp;
79
                
80
                //Para la transformaci?n trasladamos virtualmente los extents, el real de la imagen
81
                // y el asignado al centro del eje de coordenadas. Despu?s trasladamos el viewPort
82
                //(vpTrasExtent) y con las proporciones de los 2 extents hallamos en ancho, alto y 
83
                //coordenada de inicio del viewport. Todo esto equivale a trasladar y escalar el 
84
                //viewPort para hallar el nuevo.
85
                
86
                Rectangle2D userExtent = new Rectangle2D.Double(assignExtent.minX(), 
87
                                                                                                                assignExtent.minY(),
88
                                                                                                                assignExtent.width(),
89
                                                                                                                assignExtent.height());
90
                Rectangle2D imageExtent = super.getFullExtent();
91
                Rectangle2D vpExtent = vp.getExtent();
92
                double vpMinX = 0D, vpMinY = 0D;
93
                
94
                //Traslaci?n del viewport estando el extent posicionado en el 0,0
95
                Rectangle2D vpTrasExtent = new Rectangle2D.Double(        vpExtent.getMinX() - userExtent.getMinX(), 
96
                                                                                                                        vpExtent.getMinY() - userExtent.getMinY(), 
97
                                                                                                                        vpExtent.getWidth(),
98
                                                                                                                        vpExtent.getHeight());
99
                double x = 0D, y = 0D, width = 0D, height = 0D;
100
                
101
                //Escalado de la anchura y altura. No hace falta traslaci?n
102
                width = (vpExtent.getWidth() * imageExtent.getWidth())/assignExtent.width();
103
                height = (vpExtent.getHeight() * imageExtent.getHeight())/assignExtent.height();
104
                
105
                //Escalado del min del viewport para hallar el min del nuevo 
106
                x = (vpTrasExtent.getMinX() *  imageExtent.getWidth())/assignExtent.width();
107
                y = (vpTrasExtent.getMinY() *  imageExtent.getHeight())/assignExtent.height();
108
                
109
                //Traslaci?n
110
                x += imageExtent.getMinX();  
111
                y += imageExtent.getMinY();
112
                
113
                Rectangle2D newExtent = new Rectangle2D.Double(x, y, width, height);
114
                /*System.out.println("--userExtent="+userExtent);
115
                System.out.println("--imageExtent="+imageExtent);
116
                System.out.println("--vpExtent="+vpExtent);
117
                System.out.println("--vpTrasExtent="+vpTrasExtent);
118
                System.out.println("--imgSize="+vp.getImageSize());
119
                System.out.println("--newExtent="+newExtent);*/
120
                                
121
                ViewPort viewPort = new ViewPort(vp.getProjection());
122
                viewPort.setExtent(newExtent);
123
                viewPort.setImageSize(vp.getImageSize());
124
                viewPort.setScale();
125

    
126
                return viewPort;
127
        }
128
        
129
        /**
130
         * Dibujado de la capa de raster georeferenciado aplicando la 
131
         * transformaci?n del viewPort.
132
         */
133
        public void draw(BufferedImage image, Graphics2D g, ViewPort vp,
134
                        Cancellable cancel,double scale) throws DriverException {
135
                super.draw(image, g, this.toImageCoord(vp), cancel, scale);
136
        }
137
        
138
        /**
139
         * Impresi?n de la capa de raster georeferenciado aplicando la 
140
         * transformaci?n del viewPort.
141
         */
142
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
143
                double scale)throws DriverException {
144
                super.print(g, this.toImageCoord(viewPort), cancel, scale);
145
        }
146

    
147
        /**
148
         * Impresi?n de la capa de raster georeferenciado aplicando la 
149
         * transformaci?n del viewPort.
150
         */
151
        public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
152
                throws DriverException {                
153
                _print(g, this.toImageCoord(viewPort), cancel, scale);        
154
        }
155
        
156
        /**
157
         * Obtiene el extent de la capa
158
         * @return extent en Rectangle2D.
159
         */
160
        public Rectangle2D getFullExtent()throws DriverException {
161
                if(assignExtent == null)
162
                        return super.getFullExtent();
163
                return assignExtent.toRectangle2D();
164
        }
165
        
166
        /**
167
         * Obtiene el extent de la capa
168
         * @return extent de la capa
169
         */
170
        public Extent getAssignExtent(){
171
                try{
172
                        if(assignExtent == null)
173
                                return new Extent(        super.getFullExtent().getMinX(), 
174
                                                                        super.getFullExtent().getMinY(), 
175
                                                                        super.getFullExtent().getMaxX(), 
176
                                                                        super.getFullExtent().getMaxY());
177
                }catch(DriverException ex){}
178
                return assignExtent;
179
        }
180
        
181
        /**
182
         * Obtiene el valor del pixel del Image en la posici?n x,y
183
         * @param x Posici?n x
184
         * @param y Posici?n y
185
         * @return valor de pixel
186
         */
187
        /*public int[] getPixel(double wcx, double wcy){
188
                return ((RasterFileAdapter) source).getPixel(wcx, wcy);
189
        }*/
190
        
191
        /**
192
         * Obtiene la coordenada X m?xima
193
         */
194
        public double getMaxX(){
195
                if(assignExtent == null)
196
                        return super.getMaxX();
197
                return assignExtent.maxX();
198
        }
199
        
200
        /**
201
         * Obtiene la coordenada Y m?xima
202
         */
203
        public double getMaxY(){
204
                if(assignExtent == null)
205
                        return super.getMaxY();
206
                return assignExtent.maxY();
207
        }
208
        
209
        /**
210
         * Obtiene la coordenada X m?nima
211
         */
212
        public double getMinX(){
213
                if(assignExtent == null)
214
                        return super.getMinX();
215
                return assignExtent.minX();
216
        }
217
        
218
        /**
219
         * Obtiene la coordenada Y m?nima
220
         */
221
        public double getMinY(){
222
                if(assignExtent == null)
223
                        return super.getMinY();
224
                return assignExtent.minY();
225
        }
226
        
227
        /**
228
         * Obtiene el alto
229
         */
230
        public double getHeight(){
231
                if(assignExtent == null)
232
                        return super.getHeight();
233
                return assignExtent.height();
234
        }
235
        
236
        /**
237
         * Obtiene el ancho
238
         */
239
        public double getWidth(){
240
                if(assignExtent == null)
241
                        return super.getWidth();
242
                return assignExtent.width();
243
        }
244

    
245
        /**
246
         * @param assignExtent The assignExtent to set.
247
         */
248
        public void setAssignExtent(Extent assignExtent) {
249
                if(assignExtent.getMax().distance(assignExtent.getMin()) > 0){
250
                        this.assignExtent = assignExtent;
251
                        zoom.setZoom(assignExtent);
252
                }
253
        }
254
        
255
        /**
256
         * Asigna el ?ltimo extent seleccionado si hay alguno
257
         */
258
        public void setLastExtent(){
259
                Extent e = null;
260
                e = zoom.getLastZoom();
261
                if(e != null)
262
                        this.assignExtent = e;
263
        }
264
        
265
        /**
266
         * Obtiene la pila de extents aplicados
267
         * @return pila de extents
268
         */
269
        public StackZoom getStackZoom(){
270
                return zoom;
271
        }
272

    
273
        /**
274
         * Transforma un pixel de la imagen cuyas coordenadas son (0..maxWidth, 0..maxHeight)
275
         * en coordenadas del mundo real a partir del extent asignado a la imagen. 
276
         * @param pixel Pixel de la imagen
277
         * @return        Coordenadas del mundo de ese pixel
278
         */
279
        public Point2D img2World(Point2D pixel){
280
                Point2D p = new Point2D.Double();
281
                double wcWidth = 0.0, wcHeight = 0.0, ptoWCX = 0.0, ptoWCY = 0.0;
282
                int pixelImgX = 0, pixelImgY = 0;
283
                try{
284
                        wcWidth = getFullExtent().getWidth();
285
                        wcHeight = getFullExtent().getHeight();
286
                        pixelImgX = (int)((((pixel.getX() * wcWidth) / SelectFilePanel.widthPxImg)) + this.getFullExtent().getMinX());
287
                        pixelImgY = (int)((((pixel.getY() * wcHeight) / SelectFilePanel.heightPxImg)) + this.getFullExtent().getMinY());
288
                }catch(DriverException ex){}
289
                p.setLocation(pixelImgX, pixelImgY);
290
                return p;
291
        }
292
        
293
        /**
294
         * Transforma un pixel de la imagen cuyas coordenadas son (0..maxWidth, 0..maxHeight)
295
         * en coordenadas del mundo real a partir del extent asignado a la imagen. 
296
         * @param x Coordenada X del pixel de la imagen
297
         * @param y Coordenada Y del pixel de la imagen
298
         * @return        Coordenadas del mundo de ese pixel
299
         */
300
        public Point2D img2World(double x, double y){
301
                Point2D p = new Point2D.Double();
302
                p.setLocation(x, y);
303
                return this.img2World(p);
304
        }
305
}