Statistics
| Revision:

root / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / gui / Panels / GeoRasterWizard.java @ 3014

History | View | Annotate | Download (18.1 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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.gui.Panels;
42

    
43
import java.awt.FlowLayout;
44
import java.awt.GridBagConstraints;
45
import java.awt.GridBagLayout;
46
import java.awt.event.ActionEvent;
47
import java.awt.geom.Rectangle2D;
48
import java.io.File;
49
import java.util.ArrayList;
50

    
51
import javax.swing.JButton;
52
import javax.swing.JCheckBox;
53
import javax.swing.JDesktopPane;
54
import javax.swing.JDialog;
55
import javax.swing.JFileChooser;
56
import javax.swing.JInternalFrame;
57
import javax.swing.JLabel;
58
import javax.swing.JLayeredPane;
59
import javax.swing.JPanel;
60
import javax.swing.JTextField;
61
import javax.swing.filechooser.FileFilter;
62

    
63
import org.cresques.cts.IProjection;
64
import org.cresques.io.GeoRasterFile;
65
import org.cresques.px.Extent;
66

    
67
import com.hardcode.driverManager.DriverLoadException;
68
import com.iver.andami.PluginServices;
69
import com.iver.andami.messages.NotificationManager;
70
import com.iver.andami.ui.mdiFrame.MDIFrame;
71
import com.iver.cit.gvsig.fmap.DriverException;
72
import com.iver.cit.gvsig.fmap.ViewPort;
73
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
74
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
75
import com.iver.cit.gvsig.fmap.layers.FLayer;
76
import com.iver.cit.gvsig.fmap.layers.FLyrGeoRaster;
77
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
78
import com.iver.cit.gvsig.fmap.layers.RasterAdapter;
79
import com.iver.cit.gvsig.fmap.layers.RasterFileAdapter;
80
import com.iver.cit.gvsig.gui.FOpenDialog;
81
import com.iver.cit.gvsig.gui.View;
82
import com.iver.cit.gvsig.gui.WizardPanel;
83
import com.iver.cit.gvsig.gui.Dialogs.GeoreferencingDialog;
84
import com.iver.cit.gvsig.gui.wizards.WizardListener;
85
import com.iver.cit.gvsig.gui.wizards.WizardListenerSupport;
86
 
87
/**
88
 * Clase que implementa el Wizard para la georreferenciaci?n
89
 * @author Nacho Brodin (brodin_ign@gva.es)
90
 */
91
public class GeoRasterWizard extends WizardPanel{
92
        private JPanel pGeneral = null;
93
        private JPanel pFileSelection = null;
94
        private JPanel pControls = null;
95
        private JTextField tFile = null;
96
        private JButton bSelectFile = null;
97
        private JPanel pProyection = null;
98
        private WizardListenerSupport listenerSupport = new WizardListenerSupport();
99
        /**
100
         * Lista de formatos soportados
101
         */
102
        private String[] fileFilters = {"ecw", "sid", "tif", "jpg", "png", "jp2"};
103
        /**
104
         * Nombre del fichero seleccionado
105
         */
106
        private String fName = "";
107
        
108
        /**
109
         * Nombre de la capa
110
         */
111
        private String lyrName = "";
112
        /**
113
         * Recuerda la ?ltima ruta seleccionada por el usuario
114
         */
115
        private String lastPath = "./";
116

    
117
        private FLyrGeoRaster lyrGeoRaster = null;
118
        
119
        private JPanel pCheckUseGeoref = null;
120
        private JCheckBox cbUseGeoref = null;
121
        private JLabel lUseGeoref = null;
122
        private double widthPxImg, heightPxImg;
123
        
124
        /**
125
         * Extent calculado a partir de la vista.
126
         */
127
        private Extent ext = null;
128
        
129
        /**
130
         * Dialogo para la asignaci?n de puntos en la imagen.
131
         */
132
        //public static GeoreferencingDialog geoDialog = null;
133
        
134
        
135
        /**
136
         * This is the default constructor
137
         */
138
        public GeoRasterWizard() {
139
                super();
140
                initialize();
141
        }
142

    
143
        /**
144
         * This method initializes this
145
         * 
146
         * @return void
147
         */
148
        private void initialize() {
149
        this.setPreferredSize(new java.awt.Dimension(750,320));
150
        this.setSize(new java.awt.Dimension(510,311));
151
        this.setLocation(new java.awt.Point(0,0));
152
        this.add(getPGeneral(), null);
153
        this.getBSelectFile().addActionListener(new java.awt.event.ActionListener() {
154
            public void actionPerformed(java.awt.event.ActionEvent evt) {
155
                    acceptButtonActionPerformed(evt);
156
            }
157
        });
158
         }
159

    
160
        /**
161
         * This method initializes jPanel        
162
         *         
163
         * @return javax.swing.JPanel        
164
         */
165
        private JPanel getPGeneral() {
166
                if (pGeneral == null) {
167
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
168
                        gridBagConstraints1.insets = new java.awt.Insets(5,0,0,0);
169
                        gridBagConstraints1.gridy = 1;
170
                        gridBagConstraints1.gridx = 0;
171
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
172
                        gridBagConstraints.insets = new java.awt.Insets(0,0,2,0);
173
                        gridBagConstraints.gridy = 0;
174
                        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
175
                        gridBagConstraints.gridx = 0;
176
                        pGeneral = new JPanel();
177
                        pGeneral.setLayout(new GridBagLayout());
178
                        pGeneral.setPreferredSize(new java.awt.Dimension(750,320));
179
                        gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
180
                        pGeneral.add(getPFileSelection(), gridBagConstraints);
181
                        pGeneral.add(getPControls(), gridBagConstraints1);
182
                }
183
                return pGeneral;
184
        }
185

    
186
        /**
187
         * This method initializes jPanel        
188
         *         
189
         * @return javax.swing.JPanel        
190
         */
191
        private JPanel getPFileSelection() {
192
                if (pFileSelection == null) {
193
                        FlowLayout flowLayout = new FlowLayout();
194
                        flowLayout.setVgap(10);
195
                        pFileSelection = new JPanel();
196
                        pFileSelection.setBorder(javax.swing.BorderFactory.createTitledBorder(null, PluginServices.getText(this,"cargar"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
197
                        pFileSelection.setLayout(flowLayout);
198
                        pFileSelection.setPreferredSize(new java.awt.Dimension(475,70));
199
                        pFileSelection.add(getTFile(), null);
200
                        pFileSelection.add(getBSelectFile(), null);
201
                }
202
                return pFileSelection;
203
        }
204

    
205
        private JPanel getPControls() {
206
                if (pControls == null) {
207
                        GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
208
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
209
                        gridBagConstraints2.gridx = 0;
210
                        gridBagConstraints2.gridy = 1;
211
                        pControls = new JPanel();
212
                        pControls.setLayout(new GridBagLayout());
213
                        pControls.setPreferredSize(new java.awt.Dimension(475,200));
214
                        pControls.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
215
                        gridBagConstraints11.gridx = 0;
216
                        gridBagConstraints11.gridy = 0;
217
                        pControls.add(getPCheckUseGeoref(), gridBagConstraints11);
218
                        pControls.add(getPProyection(), gridBagConstraints2);
219
                }
220
                return pControls;
221
        }
222
        
223
        /**
224
         * This method initializes jTextField        
225
         *         
226
         * @return javax.swing.JTextField        
227
         */
228
        private JTextField getTFile() {
229
                if (tFile == null) {
230
                        tFile = new JTextField();
231
                        tFile.setPreferredSize(new java.awt.Dimension(350,25));
232
                }
233
                return tFile;
234
        }
235

    
236
        /**
237
         * This method initializes jButton        
238
         *         
239
         * @return javax.swing.JButton        
240
         */
241
        private JButton getBSelectFile() {
242
                if (bSelectFile == null) {
243
                        bSelectFile = new JButton();
244
                        bSelectFile.setText(PluginServices.getText(this, "cargar"));
245
                }
246
                return bSelectFile;
247
        }
248
        
249
        /**
250
         * This method initializes jPanel        
251
         *         
252
         * @return javax.swing.JPanel        
253
         */    
254
        private ProjChooserPanel getPProyection() {
255
                if (pProyection == null) {
256
                        pProyection = new ProjChooserPanel(GeoreferencingDialog.getLastProjection());
257
                        ((ProjChooserPanel)pProyection).addActionListener(new java.awt.event.ActionListener() { 
258
                                public void actionPerformed(java.awt.event.ActionEvent e) {
259
                                if (((ProjChooserPanel)pProyection).isOkPressed()) {
260
                                        FOpenDialog.setLastProjection(((ProjChooserPanel)pProyection).getCurProj());
261
                                }
262
                                }
263
                        });
264
                }
265
                return ((ProjChooserPanel)pProyection);
266
        }
267

    
268
        /**
269
         * Crea una capa Raster a partir del nombre driver, fichero y proyecci?n y 
270
         * coordenadas de georeferenciaci?n. Esta funci?n es para georeferenciar 
271
         * capas raster. Para imagenes que no tienen georeferenciaci?n hay que asignarle
272
         * una temporal, normalmente a partir de la vista activa.
273
         *
274
         * @param layerName Nombre de la capa.
275
         * @param d RasterDriver.
276
         * @param f Fichero.
277
         * @param proj Proyecci?n.
278
         * @param extent Extent de la vista activa
279
         *
280
         * @return Nueva capa de tipo raster.
281
         *
282
         * @throws DriverIOException
283
         */
284
        private FLyrGeoRaster createLayer(String layerName, RasterDriver d,
285
                File f, IProjection proj) throws DriverException {
286
                RasterAdapter adapter = new RasterFileAdapter(f);
287
                adapter.setDriver(d);
288
                FLyrGeoRaster capa = new FLyrGeoRaster();
289
                if (capa != null) {
290
                        capa.setName(layerName);
291
                        capa.setSource(adapter);
292
                        capa.setProjection(proj);
293
                        capa.setImageDimension(widthPxImg, heightPxImg);
294
                        try {
295
                                capa.load();
296
                        } catch (DriverIOException e) {
297
                                throw new DriverException(e);
298
                        }
299
                
300
                capa.setVisible(true);
301
                }else 
302
                        return null;
303
                return capa;
304
        }
305
        
306
        /**
307
         * Calcula un extent posible para la imagen a partir del extent de la vista.
308
         * En este caso centra la imagen en la vista.
309
         * @param w        Ancho de la imagen
310
         * @param h Alto de la imagen
311
         * @return        Extent para la imagen
312
         */
313
        protected Extent calcTempExtent(String file, ViewPort vp, IProjection proj){
314
                
315
                //Obtenemos el ancho y alto de la imagen y obtenemos un extent a partir
316
                //del viewport.
317
                Extent tempExtent = null;
318
                GeoRasterFile grf = GeoRasterFile.openFile(proj, file);
319
                widthPxImg = grf.getWidth();
320
                heightPxImg = grf.getHeight();
321
                if(vp == null || vp.getAdjustedExtent() == null){
322
                        vp = new ViewPort(proj);
323
                        Rectangle2D r2d = new Rectangle2D.Double(0, 0, widthPxImg, heightPxImg);
324
                        vp.setExtent(r2d);
325
                        tempExtent = new Extent(0, 0, widthPxImg, heightPxImg);
326
                }else
327
                        tempExtent = new Extent(vp.getAdjustedExtent());
328
                grf.close();
329
                
330
                
331
                double ulX = 0D, ulY = 0D, lrX = 0D, lrY = 0D;
332
                if(widthPxImg > heightPxImg){
333
                        double widthView = tempExtent.maxX() - tempExtent.minX();
334
                        ulX = tempExtent.minX() + (widthView / 4);
335
                        lrX = ulX + (widthView / 2);
336
                        double newAlto = ((heightPxImg * (widthView / 2)) / widthPxImg);
337
                        double centroY = tempExtent.minY()+((tempExtent.maxY() - tempExtent.minY())/2);
338
                        ulY = centroY - (newAlto / 2);
339
                        lrY = centroY + (newAlto / 2);
340
                }else{
341
                        double heightView = tempExtent.maxY() - tempExtent.minY();
342
                        ulY = tempExtent.minY() + (heightView / 4);
343
                        lrY = ulY + (heightView / 2);
344
                        double newAncho = ((widthPxImg * (heightView / 2)) / heightPxImg);
345
                        double centroX = tempExtent.minX()+((tempExtent.maxX() - tempExtent.minX())/2);
346
                        ulX = centroX - (newAncho / 2);
347
                        lrX = centroX + (newAncho / 2);
348
                }
349
                return new Extent(ulX, ulY, lrX, lrY);
350
        }
351
        
352
        /**
353
         * Evento de pulsado del bot?n de seleccionar fichero
354
         * @param e
355
         */
356
        private void acceptButtonActionPerformed(ActionEvent e) {
357
                //Selector de Fichero que se quiere georeferenciar
358
                                
359
                if(        e.getSource().equals(this.getBSelectFile())){
360
                        JFileChooser chooser = new JFileChooser(lastPath);
361
                        chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero"));
362
                        FileFilter f = null;
363
                        for(int i=0; i<this.fileFilters.length;i++){
364
                                f = new SelectFileFilter(chooser, this.fileFilters[i]);
365
                                chooser.addChoosableFileFilter(f);
366
                        }
367
                        int returnVal = chooser.showOpenDialog(this);
368
                        if(returnVal == JFileChooser.APPROVE_OPTION){
369
                                 this.fName = chooser.getSelectedFile().toString();
370
                                 FileFilter filter = chooser.getFileFilter();
371
                                 this.getTFile().setText(fName);
372
                                 lastPath = chooser.getCurrentDirectory().getAbsolutePath();
373
                                 
374
                                 if (PluginServices.getMainFrame() == null) {
375
                    ((JDialog) (getParent().getParent().getParent()
376
                            .getParent())).dispose();
377
                } else {
378
                        //Creamos la capa y la cargamos
379
                        IProjection proj = this.getPProyection().getCurProj();
380
                        try{
381
                                View theView = (View) PluginServices.getMDIManager().getActiveView();
382
                                
383
                                com.hardcode.driverManager.Driver driver = LayerFactory.getDM().getDriver("gvSIG Image Driver");
384
                                                        
385
                                File fich = new File(fName);
386
                                
387
                                                ViewPort viewPort = theView.getMapControl().getMapContext().getViewPort();
388
                                                
389
                                                ext = calcTempExtent(        fName, 
390
                                                                                                viewPort, 
391
                                                                                                proj);
392
                                                
393
                                this.setLyrName("*"+fName.substring(fName.lastIndexOf("/")+1));
394
                                
395
                                lyrGeoRaster = this.createLayer(this.getLyrName(), 
396
                                                                                                (RasterDriver) driver, 
397
                                                                                                                fich, 
398
                                                                                                                proj);
399
                                                                 
400

    
401
                                if(lyrGeoRaster != null){        
402
                                    lyrGeoRaster.getStackZoom().setInitExtent(ext);
403
                                    lyrGeoRaster.setActive(true);
404
                            }
405
                                
406
                                if(        ext != null && 
407
                                                fich != null && 
408
                                                this.lyrGeoRaster != null && 
409
                                                !getTFile().getText().equals(""))
410
                                                listenerSupport.callStateChanged(true);
411
                                
412
                        }catch(DriverLoadException exc){
413
                                NotificationManager.addError("No se pudo acceder a los drivers", exc);
414
                        }catch(DriverException exc){
415
                                NotificationManager.addError("El driver ha producido un error", exc);
416
                        }
417
                   
418
                }
419
                         }
420
            }        
421
                
422
        }
423
        
424
        /**
425
         * M?todo que se ejecuta al pulsar el bot?n de aceptar. En este caso asignamos el
426
         * extent calculado desde la vista si el checkbox no est? marcado y lanzaremos el
427
         * dialogo de georeferenciaci?n. El resto del trabajo lo realiza la clase AddLayer.
428
         */
429
        public void execute() {
430
                if(!this.getCbUseGeoref().isSelected())
431
                        lyrGeoRaster.setAssignExtent(ext);    
432
                GeoreferencingDialog geoDialog = null;
433
                
434
                JInternalFrame panel = new JInternalFrame();
435
                panel.addInternalFrameListener(new com.iver.cit.gvsig.gui.toolListeners.GeoRasterFrameListener());
436
        panel.setClosable(true);
437
        geoDialog = new GeoreferencingDialog(panel);
438
        lyrGeoRaster.setGeoDialog(geoDialog);
439
        panel.setSize(geoDialog.getWidth(), geoDialog.getHeight() + 10);
440
        panel.setTitle(PluginServices.getText(this,"georreferenciar"));
441
        panel.getContentPane().add(geoDialog);
442
       
443
        MDIFrame mainFrame = (MDIFrame) PluginServices.getMainFrame();
444
        JLayeredPane lyrPane = mainFrame.getLayeredPane();
445
        lyrPane.add(panel, JDesktopPane.PALETTE_LAYER);       
446
        panel.show();
447
        }
448
        
449
        /**
450
         * @return Returns the lyrRaster.
451
         */
452
        public FLyrGeoRaster getFLyrGeoRaster() {
453
                return lyrGeoRaster;
454
        }
455

    
456
        /**
457
         * @param lyrRaster The lyrRaster to set.
458
         */
459
        public void setFLyrGeoRaster(FLyrGeoRaster lyrGeoRaster) {
460
                this.lyrGeoRaster = lyrGeoRaster;
461
        }
462
   
463
        /**
464
         * 
465
         * @author Nacho Brodin (brodin_ign@gva.es)
466
         *
467
         */
468
        class SelectFileFilter extends javax.swing.filechooser.FileFilter {
469
                
470
                private JFileChooser chooser = null;
471
                private String file = null;
472
                
473
                public SelectFileFilter(JFileChooser ch, String file){
474
                        this.chooser = ch;
475
                        this.file = file;
476
                }
477
                
478
            public boolean accept(File f) {
479

    
480
                    return f.isDirectory() || f.getName().toLowerCase().endsWith("."+file);
481
            }
482
            
483
            public String getDescription() {
484
                    return file;
485
            }
486
            
487
        }
488
        
489
        /**
490
         * @return Returns the fName.
491
         */
492
        public String getLyrName() {
493
                return lyrName;
494
        }
495
        /**
496
         * @param name The fName to set.
497
         */
498
        public void setLyrName(String name) {
499
                lyrName = name;
500
        }
501
        /**
502
         * This method initializes jPanel        
503
         *         
504
         * @return javax.swing.JPanel        
505
         */    
506
        private JPanel getPCheckUseGeoref() {
507
                if (pCheckUseGeoref == null) {
508
                        FlowLayout flowLayout1 = new FlowLayout();
509
                        flowLayout1.setAlignment(java.awt.FlowLayout.LEFT);
510
                        lUseGeoref = new JLabel();
511
                        pCheckUseGeoref = new JPanel();
512
                        pCheckUseGeoref.setLayout(flowLayout1);
513
                        pCheckUseGeoref.setPreferredSize(new java.awt.Dimension(370,31));
514
                        lUseGeoref.setText("Usar georeferenciaci?n de la imagen");
515
                        pCheckUseGeoref.add(getCbUseGeoref(), null);
516
                        pCheckUseGeoref.add(lUseGeoref, null);
517
                }
518
                return pCheckUseGeoref;
519
        }
520
        /**
521
         * This method initializes jCheckBox        
522
         *         
523
         * @return javax.swing.JCheckBox        
524
         */    
525
        private JCheckBox getCbUseGeoref() {
526
                if (cbUseGeoref == null) {
527
                        cbUseGeoref = new JCheckBox();
528
                        cbUseGeoref.setSelected(true);
529
                }
530
                return cbUseGeoref;
531
        }
532
        
533
        public void initWizard() {
534
        
535
        }
536

    
537
        /**
538
         * Adds the gvSIG's wizard listener 
539
         * 
540
         * @param listener
541
         */
542
        public void addWizardListener(WizardListener listener) { 
543
                listenerSupport.addWizardListener(listener);
544
        }
545

    
546
        /* (non-Javadoc)
547
         * @see com.iver.cit.gvsig.gui.WizardPanel#callError(java.lang.Exception)
548
         */
549
        public void callError(Exception descripcion) {
550
                // TODO Auto-generated method stub
551
                super.callError(descripcion);
552
        }
553

    
554
        /* (non-Javadoc)
555
         * @see com.iver.cit.gvsig.gui.WizardPanel#callStateChanged(boolean)
556
         */
557
        public void callStateChanged(boolean finishable) {
558
                // TODO Auto-generated method stub
559
                super.callStateChanged(finishable);
560
        }
561

    
562
        /* (non-Javadoc)
563
         * @see com.iver.cit.gvsig.gui.WizardPanel#getLayer()
564
         */
565
        public FLayer getLayer() {
566
                if(lyrGeoRaster != null)
567
                        return lyrGeoRaster;
568
                return null;
569
        }
570

    
571
        /* (non-Javadoc)
572
         * @see com.iver.cit.gvsig.gui.WizardPanel#getTabName()
573
         */
574
        public String getTabName() {
575
                return PluginServices.getText(this, "georef");
576
        }
577

    
578
        /* (non-Javadoc)
579
         * @see com.iver.cit.gvsig.gui.WizardPanel#removeWizardListener(com.iver.cit.gvsig.gui.wizards.WizardListener)
580
         */
581
        public void removeWizardListener(WizardListener listener) {
582
                // TODO Auto-generated method stub
583
                super.removeWizardListener(listener);
584
        }
585

    
586
        /* (non-Javadoc)
587
         * @see com.iver.cit.gvsig.gui.WizardPanel#setTabName(java.lang.String)
588
         */
589
        protected void setTabName(String name) {
590
                // TODO Auto-generated method stub
591
                super.setTabName(name);
592
        }
593
        
594
}
595