Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / fmap / layers / FLyrGeoRaster.java @ 3057

History | View | Annotate | Download (14.3 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.View;
55
import com.iver.cit.gvsig.gui.Dialogs.GeoreferencingDialog;
56
import com.iver.cit.gvsig.gui.Panels.ZoomControlPanel;
57
import com.iver.cit.gvsig.gui.Panels.TablePointsPanel.PointTable;
58

    
59

    
60
/**
61
 * Clase de capa raster georeferenciada.
62
 *
63
 * @author Nacho Brodin (brodin_ign@gva.es)
64
 */
65
public class FLyrGeoRaster extends FLyrRaster{
66
        
67
        private Extent assignExtent = null;
68
        private StackZoom zoom = new StackZoom();
69
        private FLyrPoints lyrPoint = null; 
70
        private double widthPxImg, heightPxImg;
71
        
72
        /**
73
         * Dialogo de georreferenciaci?n asociado a la capa.
74
         */
75
        private GeoreferencingDialog geoDialog = null;
76
        /**
77
         * Funci?n encargada de realizar la transformaci?n para las coordenadas
78
         * del viewPort que se quiere dibujar. Para esta transformaci?n se tiene
79
         * en cuenta que la imagen tiene un extent propio y uno asignado por el 
80
         * usuario por lo que habr? que transformar las coordenadas que solicita
81
         * el usuario en coordenadas de la imagen.
82
         * @param vp        ViewPort solicitado por el usuario.
83
         * @return        ViewPort transformado a coordenadas de la imagen.
84
         */
85
        private ViewPort toImageCoord(ViewPort vp) throws DriverException{
86
                if(this.assignExtent == null)
87
                        return vp;
88
                
89
                //Para la transformaci?n trasladamos virtualmente los extents, el real de la imagen
90
                // y el asignado al centro del eje de coordenadas. Despu?s trasladamos el viewPort
91
                //(vpTrasExtent) y con las proporciones de los 2 extents hallamos en ancho, alto y 
92
                //coordenada de inicio del viewport. Todo esto equivale a trasladar y escalar el 
93
                //viewPort para hallar el nuevo.
94
                
95
                Rectangle2D userExtent = new Rectangle2D.Double(assignExtent.minX(), 
96
                                                                                                                assignExtent.minY(),
97
                                                                                                                assignExtent.width(),
98
                                                                                                                assignExtent.height());
99
                Rectangle2D imageExtent = super.getFullExtent();
100
                Rectangle2D vpExtent = vp.getExtent();
101
                double vpMinX = 0D, vpMinY = 0D;
102
                
103
                //Traslaci?n del viewport estando el extent posicionado en el 0,0
104
                Rectangle2D vpTrasExtent = new Rectangle2D.Double(        vpExtent.getMinX() - userExtent.getMinX(), 
105
                                                                                                                        vpExtent.getMinY() - userExtent.getMinY(), 
106
                                                                                                                        vpExtent.getWidth(),
107
                                                                                                                        vpExtent.getHeight());
108
                double x = 0D, y = 0D, width = 0D, height = 0D;
109
                
110
                //Escalado de la anchura y altura. No hace falta traslaci?n
111
                width = (vpExtent.getWidth() * imageExtent.getWidth())/assignExtent.width();
112
                height = (vpExtent.getHeight() * imageExtent.getHeight())/assignExtent.height();
113
                
114
                //Escalado del min del viewport para hallar el min del nuevo 
115
                x = (vpTrasExtent.getMinX() *  imageExtent.getWidth())/assignExtent.width();
116
                y = (vpTrasExtent.getMinY() *  imageExtent.getHeight())/assignExtent.height();
117
                
118
                //Traslaci?n
119
                x += imageExtent.getMinX();  
120
                y += imageExtent.getMinY();
121
                
122
                Rectangle2D newExtent = new Rectangle2D.Double(x, y, width, height);
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
         * Transforma una coordenada del mundo real a un pixel  (0..maxWidth, 0..maxHeight)
311
         * en coordenadas de la imagen a partir del extent asignado a esta. 
312
         * @param wcPoint coordenada del mundo
313
         * @return        Pixel de la imagen
314
         */
315
        public Point2D world2Img(Point2D wcPoint){
316
                //Si el punto cae fuera de la imagen salimos devolviendo null
317
                while(        wcPoint.getX() < this.getMinX() ||
318
                        wcPoint.getX() > this.getMaxX() ||
319
                        wcPoint.getY() < this.getMinY() ||
320
                        wcPoint.getY() > this.getMaxY())
321
                        return null;
322
                
323
                //Hallamos pixelImg q son las coordenadas en pixeles de la imagen que corresponden
324
                //con el punto wcPoint
325
                double wcWidth = 0.0, wcHeight = 0.0, ptoWCX = 0.0, ptoWCY = 0.0;
326
                int pixelImgX = 0, pixelImgY = 0;
327
                try{
328
                        wcWidth = this.getFullExtent().getWidth();
329
                        wcHeight = this.getFullExtent().getHeight();
330
                        ptoWCX = wcPoint.getX() - this.getFullExtent().getMinX();
331
                        ptoWCY = wcPoint.getY() - this.getFullExtent().getMinY();
332
                        pixelImgX = (int)((ptoWCX * this.getImageWidth()) / wcWidth);
333
                        pixelImgY = (int)((ptoWCY * this.getImageHeight()) / wcHeight);
334
                }catch(DriverException ex){}
335
                
336
                Point2D result = new Point2D.Double();
337
                result.setLocation(pixelImgX, pixelImgY);
338
                return result;
339
        }
340
        
341
        /**
342
         * Transforma una coordenada del mundo real a un pixel  (0..maxWidth, 0..maxHeight)
343
         * en coordenadas de la imagen a partir del extent asignado a esta. 
344
         * @param x wcPoint coordenada X del mundo
345
         * @param y wcPoint coordenada Y del mundo
346
         * @return        Pixel de la imagen
347
         */
348
        public Point2D world2Img(double x, double y){
349
                Point2D p = new Point2D.Double();
350
                p.setLocation(x, y);
351
                return this.world2Img(p);
352
        }
353
        
354
        /**
355
         * Asigna la capa de puntos asociada a la capa Georraster
356
         * @param lyr Capa de puntos
357
         */
358
        public void setFLyrPoints(FLyrPoints lyr){
359
                this.lyrPoint = lyr;
360
        }
361
        
362
        /**
363
         * Obtiene la capa de puntos asociada a la capa Georraster
364
         * @return        Capa de puntos
365
         */
366
        public FLyrPoints getFLyrPoints(){
367
                return this.lyrPoint;
368
        }
369
        
370
        /**
371
         * Asigna la dimensi?n de la imagen de disco en pixeles
372
         * @param w        Ancho
373
         * @param h Alto
374
         */
375
        public void setImageDimension(double w, double h){
376
                this.widthPxImg = w;
377
                this.heightPxImg = h;
378
        }
379
        
380
        /**
381
         * Obtiene el ancho de la imagen de disco en pixeles
382
         * @return
383
         */
384
        public double getImageWidth(){
385
                return this.widthPxImg;
386
        }
387
        
388
        /**
389
         * Obtiene el alto de la imagen de disco en pixeles
390
         * @return
391
         */
392
        public double getImageHeight(){
393
                return this.heightPxImg;
394
        }
395
        /**
396
         * @return Returns the geoDialog.
397
         */
398
        public GeoreferencingDialog getGeoDialog() {
399
                return geoDialog;
400
        }
401
        /**
402
         * @param geoDialog The geoDialog to set.
403
         */
404
        public void setGeoDialog(GeoreferencingDialog geoDialog) {
405
                this.geoDialog = geoDialog;
406
        }
407
        
408
        /**
409
         * A partir de nuevas coordenadas actualiza la vista, minimagen, capa de puntos el
410
         * dialogo y la tabla.
411
         *
412
         */
413
        public void updateData(int nPunto, Point2D pixel, Point2D map, GeoreferencingDialog dialog, View vista, FLyrPoints plyr){
414
                if(dialog == null || vista == null || plyr == null)
415
                        return;
416
                if(pixel == null && map == null)
417
                        return;
418
                
419
                //Actualizamos los textBox
420
                if(pixel != null){
421
                        dialog.setPixelText(pixel.getX(), pixel.getY());
422
                        dialog.getConectorPanel().getDataPointsTabPanel().getSelectPointsPanel().lastTx = String.valueOf(pixel.getX());
423
                        dialog.getConectorPanel().getDataPointsTabPanel().getSelectPointsPanel().lastTy = String.valueOf(pixel.getY());
424
                }
425
                if(map != null){
426
                        dialog.setMapCoordText(map.getX(), map.getY());
427
                        dialog.getConectorPanel().getDataPointsTabPanel().getSelectPointsPanel().lastLong = String.valueOf(map.getX());
428
                        dialog.getConectorPanel().getDataPointsTabPanel().getSelectPointsPanel().lastLat = String.valueOf(map.getX());
429
                }
430
                
431
                //Actualizamos la tabla
432
                PointTable pointsTable = dialog.getConectorPanel().getDataPointsTabPanel().getTablePointsPanel().getJTable();
433
                if(pointsTable.getTable().getRowCount() < nPunto){
434
                        pointsTable.getTableModel().addNew();
435
                        pointsTable.getTableModel().setValueAt(String.valueOf(nPunto - 1),nPunto - 1,0);
436
                }
437
                if(pixel != null){        
438
                        pointsTable.getTableModel().setValueAt(String.valueOf(pixel.getX()),nPunto - 1,1);
439
                        pointsTable.getTableModel().setValueAt(String.valueOf(pixel.getY()),nPunto - 1,2);
440
                }
441
                if(map != null){
442
                        pointsTable.getTableModel().setValueAt(String.valueOf(map.getX()),nPunto - 1,3);
443
                        pointsTable.getTableModel().setValueAt(String.valueOf(map.getY()),nPunto - 1,4);
444
                }
445
                
446
                //Actualizamos la miniimagen
447
                ViewPort viewPort = vista.getMapControl().getMapContext().getViewPort();
448
                if(pixel != null){
449
                        dialog.getConectorPanel().getAdjustGeorefPanel().getZoomLeft().setCenterPoint(this.img2World(pixel));
450
                        dialog.getConectorPanel().getAdjustGeorefPanel().setNumberPoint(nPunto);
451
                        dialog.getConectorPanel().getAdjustGeorefPanel().setViewPort(viewPort);
452
                        dialog.getConectorPanel().getAdjustGeorefPanel().getZoomLeft().draw();
453
                }
454
                
455
                if(map != null){
456
                        dialog.getConectorPanel().getAdjustGeorefPanel().getZoomRight().setCenterPoint(map);
457
                        dialog.getConectorPanel().getAdjustGeorefPanel().setNumberPoint(nPunto);
458
                        dialog.getConectorPanel().getAdjustGeorefPanel().setViewPort(viewPort);
459
                        dialog.getConectorPanel().getAdjustGeorefPanel().getZoomRight().draw();
460
                }
461
                
462
                //Actualizamos la capa de puntos
463
                plyr.updatePoint(pixel, map, nPunto - 1);
464
                                
465
                //Actualizamos la vista
466
                vista.getMapControl().getMapContext().invalidate();        
467
        }
468
        
469
}