Statistics
| Revision:

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

History | View | Annotate | Download (11.6 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.Component;
44
import java.awt.Graphics2D;
45
import java.awt.geom.Point2D;
46
import java.awt.geom.Rectangle2D;
47
import java.awt.image.BufferedImage;
48

    
49
import javax.swing.JOptionPane;
50

    
51
import org.cresques.px.Extent;
52

    
53
import com.iver.andami.PluginServices;
54
import com.iver.cit.gvsig.StackZoom;
55
import com.iver.cit.gvsig.fmap.DriverException;
56
import com.iver.cit.gvsig.fmap.ViewPort;
57
import com.iver.cit.gvsig.fmap.operations.Cancellable;
58
import com.iver.cit.gvsig.gui.Dialogs.GeoreferencingDialog;
59

    
60

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

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

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

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

    
277
        /**
278
         * Transforma un pixel de la imagen cuyas coordenadas son (0..maxWidth, 0..maxHeight)
279
         * en coordenadas del mundo real a partir del extent asignado a la imagen. 
280
         * @param pixel Pixel de la imagen
281
         * @return        Coordenadas del mundo de ese pixel
282
         */
283
        public Point2D img2World(Point2D pixel){
284
                Point2D p = new Point2D.Double();
285
                double wcWidth = 0.0, wcHeight = 0.0, ptoWCX = 0.0, ptoWCY = 0.0;
286
                int pixelImgX = 0, pixelImgY = 0;
287
                try{
288
                        wcWidth = getFullExtent().getWidth();
289
                        wcHeight = getFullExtent().getHeight();
290
                        pixelImgX = (int)((((pixel.getX() * wcWidth) / widthPxImg)) + this.getFullExtent().getMinX());
291
                        pixelImgY = (int)((((pixel.getY() * wcHeight) / heightPxImg)) + this.getFullExtent().getMinY());
292
                }catch(DriverException ex){}
293
                p.setLocation(pixelImgX, pixelImgY);
294
                return p;
295
        }
296
        
297
        /**
298
         * Transforma un pixel de la imagen cuyas coordenadas son (0..maxWidth, 0..maxHeight)
299
         * en coordenadas del mundo real a partir del extent asignado a la imagen. 
300
         * @param x Coordenada X del pixel de la imagen
301
         * @param y Coordenada Y del pixel de la imagen
302
         * @return        Coordenadas del mundo de ese pixel
303
         */
304
        public Point2D img2World(double x, double y){
305
                Point2D p = new Point2D.Double();
306
                p.setLocation(x, y);
307
                return this.img2World(p);
308
        }
309
        
310
        /**
311
         * Transforma una coordenada del mundo real a un pixel  (0..maxWidth, 0..maxHeight)
312
         * en coordenadas de la imagen a partir del extent asignado a esta. 
313
         * @param wcPoint coordenada del mundo
314
         * @return        Pixel de la imagen
315
         */
316
        public Point2D world2Img(Point2D wcPoint){
317
                //Si el punto cae fuera de la imagen salimos devolviendo null
318
                while(        wcPoint.getX() < this.getMinX() ||
319
                        wcPoint.getX() > this.getMaxX() ||
320
                        wcPoint.getY() < this.getMinY() ||
321
                        wcPoint.getY() > this.getMaxY())
322
                        return null;
323
                
324
                //Hallamos pixelImg q son las coordenadas en pixeles de la imagen que corresponden
325
                //con el punto wcPoint
326
                double wcWidth = 0.0, wcHeight = 0.0, ptoWCX = 0.0, ptoWCY = 0.0;
327
                int pixelImgX = 0, pixelImgY = 0;
328
                try{
329
                        wcWidth = this.getFullExtent().getWidth();
330
                        wcHeight = this.getFullExtent().getHeight();
331
                        ptoWCX = wcPoint.getX() - this.getFullExtent().getMinX();
332
                        ptoWCY = wcPoint.getY() - this.getFullExtent().getMinY();
333
                        pixelImgX = (int)((ptoWCX * this.getImageWidth()) / wcWidth);
334
                        pixelImgY = (int)((ptoWCY * this.getImageHeight()) / wcHeight);
335
                }catch(DriverException ex){}
336
                
337
                Point2D result = new Point2D.Double();
338
                result.setLocation(pixelImgX, pixelImgY);
339
                return result;
340
        }
341
        
342
        /**
343
         * Transforma una coordenada del mundo real a un pixel  (0..maxWidth, 0..maxHeight)
344
         * en coordenadas de la imagen a partir del extent asignado a esta. 
345
         * @param x wcPoint coordenada X del mundo
346
         * @param y wcPoint coordenada Y del mundo
347
         * @return        Pixel de la imagen
348
         */
349
        public Point2D world2Img(double x, double y){
350
                Point2D p = new Point2D.Double();
351
                p.setLocation(x, y);
352
                return this.world2Img(p);
353
        }
354
        
355
        /**
356
         * Asigna la capa de puntos asociada a la capa Georraster
357
         * @param lyr Capa de puntos
358
         */
359
        public void setFLyrPoints(FLyrPoints lyr){
360
                this.lyrPoint = lyr;
361
        }
362
        
363
        /**
364
         * Obtiene la capa de puntos asociada a la capa Georraster
365
         * @return        Capa de puntos
366
         */
367
        public FLyrPoints getFLyrPoints(){
368
                return this.lyrPoint;
369
        }
370
        
371
        /**
372
         * Asigna la dimensi?n de la imagen de disco en pixeles
373
         * @param w        Ancho
374
         * @param h Alto
375
         */
376
        public void setImageDimension(double w, double h){
377
                this.widthPxImg = w;
378
                this.heightPxImg = h;
379
        }
380
        
381
        /**
382
         * Obtiene el ancho de la imagen de disco en pixeles
383
         * @return
384
         */
385
        public double getImageWidth(){
386
                return this.widthPxImg;
387
        }
388
        
389
        /**
390
         * Obtiene el alto de la imagen de disco en pixeles
391
         * @return
392
         */
393
        public double getImageHeight(){
394
                return this.heightPxImg;
395
        }
396
        /**
397
         * @return Returns the geoDialog.
398
         */
399
        public GeoreferencingDialog getGeoDialog() {
400
                return geoDialog;
401
        }
402
        /**
403
         * @param geoDialog The geoDialog to set.
404
         */
405
        public void setGeoDialog(GeoreferencingDialog geoDialog) {
406
                this.geoDialog = geoDialog;
407
        }
408
}