Statistics
| Revision:

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

History | View | Annotate | Download (9.87 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.layers.FLyrPoints.GeoPoint;
54
import com.iver.cit.gvsig.fmap.operations.Cancellable;
55
import com.iver.cit.gvsig.gui.Panels.SelectFilePanel;
56

    
57

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

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

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

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

    
276
        /**
277
         * Transforma un pixel de la imagen cuyas coordenadas son (0..maxWidth, 0..maxHeight)
278
         * en coordenadas del mundo real a partir del extent asignado a la imagen. 
279
         * @param pixel Pixel de la imagen
280
         * @return        Coordenadas del mundo de ese pixel
281
         */
282
        public Point2D img2World(Point2D pixel){
283
                Point2D p = new Point2D.Double();
284
                double wcWidth = 0.0, wcHeight = 0.0, ptoWCX = 0.0, ptoWCY = 0.0;
285
                int pixelImgX = 0, pixelImgY = 0;
286
                try{
287
                        wcWidth = getFullExtent().getWidth();
288
                        wcHeight = getFullExtent().getHeight();
289
                        pixelImgX = (int)((((pixel.getX() * wcWidth) / widthPxImg)) + this.getFullExtent().getMinX());
290
                        pixelImgY = (int)((((pixel.getY() * wcHeight) / heightPxImg)) + this.getFullExtent().getMinY());
291
                }catch(DriverException ex){}
292
                p.setLocation(pixelImgX, pixelImgY);
293
                return p;
294
        }
295
        
296
        /**
297
         * Transforma un pixel de la imagen cuyas coordenadas son (0..maxWidth, 0..maxHeight)
298
         * en coordenadas del mundo real a partir del extent asignado a la imagen. 
299
         * @param x Coordenada X del pixel de la imagen
300
         * @param y Coordenada Y del pixel de la imagen
301
         * @return        Coordenadas del mundo de ese pixel
302
         */
303
        public Point2D img2World(double x, double y){
304
                Point2D p = new Point2D.Double();
305
                p.setLocation(x, y);
306
                return this.img2World(p);
307
        }
308
        
309
        /**
310
         * Asigna la capa de puntos asociada a la capa Georraster
311
         * @param lyr Capa de puntos
312
         */
313
        public void setFLyrPoints(FLyrPoints lyr){
314
                this.lyrPoint = lyr;
315
        }
316
        
317
        /**
318
         * Obtiene la capa de puntos asociada a la capa Georraster
319
         * @return        Capa de puntos
320
         */
321
        public FLyrPoints getFLyrPoints(){
322
                return this.lyrPoint;
323
        }
324
        
325
        /**
326
         * Asigna la dimensi?n de la imagen de disco en pixeles
327
         * @param w        Ancho
328
         * @param h Alto
329
         */
330
        public void setImageDimension(double w, double h){
331
                this.widthPxImg = w;
332
                this.heightPxImg = h;
333
        }
334
        
335
        /**
336
         * Obtiene el ancho de la imagen de disco en pixeles
337
         * @return
338
         */
339
        public double getImageWidth(){
340
                return this.widthPxImg;
341
        }
342
        
343
        /**
344
         * Obtiene el alto de la imagen de disco en pixeles
345
         * @return
346
         */
347
        public double getImageHeight(){
348
                return this.heightPxImg;
349
        }
350
}