Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / wizards / GeoRasterWizard.java @ 5818

History | View | Annotate | Download (17.1 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 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
package org.gvsig.georeferencing.wizards;
20

    
21
import java.awt.FlowLayout;
22
import java.awt.GridBagConstraints;
23
import java.awt.GridBagLayout;
24
import java.awt.event.ActionEvent;
25
import java.awt.geom.Rectangle2D;
26
import java.io.File;
27

    
28
import javax.swing.JButton;
29
import javax.swing.JCheckBox;
30
import javax.swing.JDialog;
31
import javax.swing.JFileChooser;
32
import javax.swing.JLabel;
33
import javax.swing.JPanel;
34
import javax.swing.JTextField;
35
import javax.swing.filechooser.FileFilter;
36

    
37
import org.cresques.cts.IProjection;
38
import org.cresques.io.GeoRasterFile;
39
import org.cresques.px.Extent;
40
import org.gvsig.georeferencing.gui.dialog.GeoreferencingDialog;
41
import org.gvsig.georeferencing.utils.GeoLayerFactory;
42
import org.gvsig.georeferencing.utils.GeoUtils;
43
import org.gvsig.georeferencing.utils.PointManager;
44

    
45
import com.hardcode.driverManager.DriverLoadException;
46
import com.iver.andami.PluginServices;
47
import com.iver.andami.messages.NotificationManager;
48
import com.iver.cit.gvsig.fmap.DriverException;
49
import com.iver.cit.gvsig.fmap.ViewPort;
50
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
51
import com.iver.cit.gvsig.fmap.layers.FLayer;
52
import com.iver.cit.gvsig.fmap.layers.FLyrGeoRaster;
53
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
54
import com.iver.cit.gvsig.gui.FOpenDialog;
55
import com.iver.cit.gvsig.gui.View;
56
import com.iver.cit.gvsig.gui.WizardPanel;
57
import com.iver.cit.gvsig.gui.Panels.ProjChooserPanel;
58
import com.iver.cit.gvsig.gui.wizards.WizardListener;
59
import com.iver.cit.gvsig.gui.wizards.WizardListenerSupport;
60
 
61
/**
62
 * Clase que implementa el Wizard para la georreferenciaci?n
63
 * @author Nacho Brodin (brodin_ign@gva.es)
64
 */
65
public class GeoRasterWizard extends WizardPanel{
66
        
67
        //**********************Vars**********************************
68
        private JPanel                                         pGeneral = null;
69
        private JPanel                                         pFileSelection = null;
70
        private JPanel                                         pControls = null;
71
        private JTextField                                 tFile = null;
72
        private JButton                                 bSelectFile = null;
73
        private JPanel                                         pProyection = null;
74
        private WizardListenerSupport         listenerSupport = new WizardListenerSupport();
75
        /**
76
         * Lista de formatos soportados
77
         */
78
        private String[]                                 fileFilters = {"tif", "tiff", "jpg", "jpeg", "png", "gif", "bmp"};
79
        /**
80
         * Nombre del fichero seleccionado
81
         */
82
        private String                                         fName = "";
83
        
84
        /**
85
         * Nombre de la capa
86
         */
87
        private String                                         lyrName = "";
88
        /**
89
         * Recuerda la ?ltima ruta seleccionada por el usuario
90
         */
91
        //private String                                         lastPath = "./";
92
        private static String                         lastPath = null;
93
        private FLyrGeoRaster                         lyrGeoRaster = null;
94
        private JPanel                                         pCheckUseGeoref = null;
95
        private JCheckBox                                 cbUseGeoref = null;
96
        private JLabel                                         lUseGeoref = null;
97
        private double                                         widthPxImg, heightPxImg;
98
        /**
99
         * Extent calculado a partir de la vista.
100
         */
101
        private Extent                                         ext = null;
102
        //**********************Vars**********************************
103

    
104
        //**********************Classes*******************************
105
        /**
106
         * Filtro para los ficheros de georreferenciaci?n.
107
         * @author Nacho Brodin (brodin_ign@gva.es)
108
         */
109
    public class DriverFileFilter extends FileFilter{
110

    
111
                public DriverFileFilter(){}
112

    
113
                /**
114
                 * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
115
                 */
116
                public boolean accept(File f) {
117
                        if (f.isDirectory()) return true;
118
                        for(int i=0; i<fileFilters.length;i++){
119
                                if(f.getName().endsWith(fileFilters[i]))
120
                                        return true;
121
                        }
122
                        return false;
123
                }
124

    
125
                /**
126
                 * @see javax.swing.filechooser.FileFilter#getDescription()
127
                 */
128
                public String getDescription() {
129
                        return PluginServices.getText(this, "georef_file") + " ( tif, jpg, gif, png)";
130
                }
131
        }
132
        //**********************End Classes***************************
133
        
134
        //**********************Methods*******************************
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
         * Calcula un extent posible para la imagen a partir del extent de la vista.
162
         * En este caso centra la imagen en la vista para que cuando se cargue la imagen
163
         * a georreferenciar nos quede centrada si elegimos la opci?n de no utilizar el
164
         * extent de la imagen.
165
         * @param w        Ancho de la imagen
166
         * @param h Alto de la imagen
167
         * @return        Extent para la imagen
168
         */
169
        protected Extent calcTempExtent(String file, ViewPort vp, IProjection proj){
170
                
171
                //Obtenemos el ancho y alto de la imagen y obtenemos un extent a partir
172
                //del viewport.
173
                Extent tempExtent = null;
174
                GeoRasterFile grf = GeoRasterFile.openFile(proj, file);
175
                widthPxImg = grf.getWidth();
176
                heightPxImg = grf.getHeight();
177
                if(vp == null || vp.getAdjustedExtent() == null){
178
                        vp = new ViewPort(proj);
179
                        Rectangle2D r2d = new Rectangle2D.Double(0, 0, widthPxImg, heightPxImg);
180
                        vp.setExtent(r2d);
181
                        tempExtent = new Extent(0, 0, widthPxImg, heightPxImg);
182
                }else
183
                        tempExtent = new Extent(vp.getAdjustedExtent());
184
                //grf.close();                
185
                
186
                double ulX = 0D, ulY = 0D, lrX = 0D, lrY = 0D;
187
                if(widthPxImg > heightPxImg){
188
                        double widthView = tempExtent.maxX() - tempExtent.minX();
189
                        ulX = tempExtent.minX() + (widthView / 4);
190
                        lrX = ulX + (widthView / 2);
191
                        double newAlto = ((heightPxImg * (widthView / 2)) / widthPxImg);
192
                        double centroY = tempExtent.minY()+((tempExtent.maxY() - tempExtent.minY())/2);
193
                        ulY = centroY - (newAlto / 2);
194
                        lrY = centroY + (newAlto / 2);
195
                }else{
196
                        double heightView = tempExtent.maxY() - tempExtent.minY();
197
                        ulY = tempExtent.minY() + (heightView / 4);
198
                        lrY = ulY + (heightView / 2);
199
                        double newAncho = ((widthPxImg * (heightView / 2)) / heightPxImg);
200
                        double centroX = tempExtent.minX()+((tempExtent.maxX() - tempExtent.minX())/2);
201
                        ulX = centroX - (newAncho / 2);
202
                        lrX = centroX + (newAncho / 2);
203
                }
204
                return new Extent(ulX, ulY, lrX, lrY);
205
        }
206
        
207
        /**
208
         * Evento de pulsado del bot?n de seleccionar fichero
209
         * @param e
210
         */
211
        private void acceptButtonActionPerformed(ActionEvent e) {
212
                //Selector de Fichero que se quiere georeferenciar
213
                                
214
                if(        e.getSource().equals(this.getBSelectFile())){
215
                        JFileChooser chooser = new JFileChooser(lastPath);
216
                        chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero"));
217
                        
218
                        DriverFileFilter f = new DriverFileFilter();
219
                        chooser.addChoosableFileFilter(f);
220
                        
221
                        int returnVal = chooser.showOpenDialog(this);
222
                        if(returnVal == JFileChooser.APPROVE_OPTION){                        
223
                                 this.fName = chooser.getSelectedFile().toString();
224
                                 FileFilter filter = chooser.getFileFilter();
225
                                 this.getTFile().setText(fName);
226
                                 lastPath = chooser.getCurrentDirectory().getAbsolutePath();
227
                                 
228
                                 if (PluginServices.getMainFrame() == null) {
229
                    ((JDialog) (getParent().getParent().getParent()
230
                            .getParent())).dispose();
231
                } else {
232
                                    //Hacemos un backup del fichero RMF antes que nada 
233
                        GeoUtils.fileBackup(fName.substring(0, fName.lastIndexOf(".")) + ".rmf");
234
                        
235
                        //Creamos la capa y la cargamos
236
                        IProjection proj = this.getPProyection().getCurProj();
237
                        try{
238
                                View  theView = null;
239
                                    try{
240
                                            theView = (View)PluginServices.getMDIManager().getActiveView();
241
                                    }catch(ClassCastException exc){
242
                                            return;
243
                                    }
244
                                                                
245
                                com.hardcode.driverManager.Driver driver = LayerFactory.getDM().getDriver("gvSIG Image Driver");
246
                                
247
                                File fich = new File(fName);
248
                                
249
                                                ViewPort viewPort = theView.getMapControl().getMapContext().getViewPort();
250
                                                
251
                                                //Calcula el extent para que la imagen que se carga quede centrada.
252
                                                ext = calcTempExtent( fName, viewPort, proj);
253
                                                
254
                                                //Asigna el nombre de la capa con el marcador *
255
                                this.setLyrName("*"+fName.substring(fName.lastIndexOf(File.separator)+1));
256
                                
257
                                //Crea la capa
258
                                lyrGeoRaster = GeoLayerFactory.createLayer(getLyrName(), 
259
                                                                                                                (RasterDriver) driver, 
260
                                                                                                                fich, 
261
                                                                                                                proj,
262
                                                                                                                widthPxImg,
263
                                                                                                                heightPxImg);
264
                                //Pone activa la capa                              
265
                                if(lyrGeoRaster != null)
266
                                    lyrGeoRaster.setActive(true);
267
                            
268
                                
269
                                if(        ext != null && fich != null && 
270
                                        this.lyrGeoRaster != null && !getTFile().getText().equals(""))
271
                                        listenerSupport.callStateChanged(true);
272
                                
273
                        }catch(DriverLoadException exc){
274
                                NotificationManager.addError("No se pudo acceder a los drivers", exc);
275
                        }catch(DriverException exc){
276
                                NotificationManager.addError("El driver ha producido un error", exc);
277
                        }
278
                   
279
                }
280
                         }
281
            }        
282
                
283
        }
284
        
285
        /**
286
         * M?todo que se ejecuta al pulsar el bot?n de aceptar. En este caso asignamos el
287
         * extent calculado desde la vista si el checkbox no est? marcado y lanzaremos el
288
         * dialogo de georeferenciaci?n. El resto del trabajo lo realiza la clase AddLayer.
289
         */
290
        public void execute() {
291
                if(!this.getCbUseGeoref().isSelected())
292
                        lyrGeoRaster.setAssignExtent(ext);  
293
                
294
                PointManager.initWindow(lyrGeoRaster, true, this);
295
        }
296
                           
297
        public void initWizard() {
298
        
299
        }
300

    
301
        /**
302
         * Adds the gvSIG's wizard listener 
303
         * 
304
         * @param listener
305
         */
306
        public void addWizardListener(WizardListener listener) { 
307
                listenerSupport.addWizardListener(listener);
308
        }
309

    
310
        /* (non-Javadoc)
311
         * @see com.iver.cit.gvsig.gui.WizardPanel#callError(java.lang.Exception)
312
         */
313
        public void callError(Exception descripcion) {
314
                // TODO Auto-generated method stub
315
                super.callError(descripcion);
316
        }
317

    
318
        /* (non-Javadoc)
319
         * @see com.iver.cit.gvsig.gui.WizardPanel#callStateChanged(boolean)
320
         */
321
        public void callStateChanged(boolean finishable) {
322
                // TODO Auto-generated method stub
323
                super.callStateChanged(finishable);
324
        }
325

    
326
        /* (non-Javadoc)
327
         * @see com.iver.cit.gvsig.gui.WizardPanel#removeWizardListener(com.iver.cit.gvsig.gui.wizards.WizardListener)
328
         */
329
        public void removeWizardListener(WizardListener listener) {
330
                // TODO Auto-generated method stub
331
                super.removeWizardListener(listener);
332
        }        
333
        //**********************End Methods***************************
334
        
335
        //**********************Getters & Setters*********************
336
        /* (non-Javadoc)
337
         * @see com.iver.cit.gvsig.gui.WizardPanel#setTabName(java.lang.String)
338
         */
339
        protected void setTabName(String name) {
340
                // TODO Auto-generated method stub
341
                super.setTabName(name);
342
        }
343
        
344
        /* (non-Javadoc)
345
         * @see com.iver.cit.gvsig.gui.WizardPanel#getLayer()
346
         */
347
        public FLayer getLayer() {
348
                if(lyrGeoRaster != null)
349
                        return lyrGeoRaster;
350
                return null;
351
        }
352

    
353
        /* (non-Javadoc)
354
         * @see com.iver.cit.gvsig.gui.WizardPanel#getTabName()
355
         */
356
        public String getTabName() {
357
                return PluginServices.getText(this, "georef");
358
        }
359
        
360
        /**
361
         * @return Returns the fName.
362
         */
363
        public String getLyrName() {
364
                return lyrName;
365
        }
366
        
367
        /**
368
         * @param name The fName to set.
369
         */
370
        public void setLyrName(String name) {
371
                lyrName = name;
372
        }
373
        
374
        /**
375
         * This method initializes jPanel        
376
         *         
377
         * @return javax.swing.JPanel        
378
         */    
379
        private JPanel getPCheckUseGeoref() {
380
                if (pCheckUseGeoref == null) {
381
                        FlowLayout flowLayout1 = new FlowLayout();
382
                        flowLayout1.setAlignment(java.awt.FlowLayout.LEFT);
383
                        lUseGeoref = new JLabel();
384
                        pCheckUseGeoref = new JPanel();
385
                        pCheckUseGeoref.setLayout(flowLayout1);
386
                        pCheckUseGeoref.setPreferredSize(new java.awt.Dimension(370,31));
387
                        lUseGeoref.setText("Usar georeferenciaci?n de la imagen");
388
                        pCheckUseGeoref.add(getCbUseGeoref(), null);
389
                        pCheckUseGeoref.add(lUseGeoref, null);
390
                }
391
                return pCheckUseGeoref;
392
        }
393
        
394
        /**
395
         * This method initializes jCheckBox        
396
         *         
397
         * @return javax.swing.JCheckBox        
398
         */    
399
        private JCheckBox getCbUseGeoref() {
400
                if (cbUseGeoref == null) {
401
                        cbUseGeoref = new JCheckBox();
402
                        cbUseGeoref.setSelected(false);
403
                }
404
                return cbUseGeoref;
405
        }
406
        
407
        /**
408
         * @return Returns the lyrRaster.
409
         */
410
        public FLyrGeoRaster getFLyrGeoRaster() {
411
                return lyrGeoRaster;
412
        }
413

    
414
        /**
415
         * @param lyrRaster The lyrRaster to set.
416
         */
417
        public void setFLyrGeoRaster(FLyrGeoRaster lyrGeoRaster) {
418
                this.lyrGeoRaster = lyrGeoRaster;
419
        }
420
        
421
        /**
422
         * This method initializes jPanel        
423
         *         
424
         * @return javax.swing.JPanel        
425
         */
426
        private JPanel getPGeneral() {
427
                if (pGeneral == null) {
428
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
429
                        gridBagConstraints1.insets = new java.awt.Insets(5,0,0,0);
430
                        gridBagConstraints1.gridy = 1;
431
                        gridBagConstraints1.gridx = 0;
432
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
433
                        gridBagConstraints.insets = new java.awt.Insets(0,0,2,0);
434
                        gridBagConstraints.gridy = 0;
435
                        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
436
                        gridBagConstraints.gridx = 0;
437
                        pGeneral = new JPanel();
438
                        pGeneral.setLayout(new GridBagLayout());
439
                        pGeneral.setPreferredSize(new java.awt.Dimension(750,320));
440
                        gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
441
                        pGeneral.add(getPFileSelection(), gridBagConstraints);
442
                        pGeneral.add(getPControls(), gridBagConstraints1);
443
                }
444
                return pGeneral;
445
        }
446

    
447
        /**
448
         * This method initializes jPanel        
449
         *         
450
         * @return javax.swing.JPanel        
451
         */
452
        private JPanel getPFileSelection() {
453
                if (pFileSelection == null) {
454
                        FlowLayout flowLayout = new FlowLayout();
455
                        flowLayout.setVgap(10);
456
                        pFileSelection = new JPanel();
457
                        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));
458
                        pFileSelection.setLayout(flowLayout);
459
                        pFileSelection.setPreferredSize(new java.awt.Dimension(475,70));
460
                        pFileSelection.add(getTFile(), null);
461
                        pFileSelection.add(getBSelectFile(), null);
462
                }
463
                return pFileSelection;
464
        }
465

    
466
        private JPanel getPControls() {
467
                if (pControls == null) {
468
                        GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
469
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
470
                        gridBagConstraints2.gridx = 0;
471
                        gridBagConstraints2.gridy = 1;
472
                        pControls = new JPanel();
473
                        pControls.setLayout(new GridBagLayout());
474
                        pControls.setPreferredSize(new java.awt.Dimension(475,200));
475
                        pControls.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
476
                        gridBagConstraints11.gridx = 0;
477
                        gridBagConstraints11.gridy = 0;
478
                        pControls.add(getPCheckUseGeoref(), gridBagConstraints11);
479
                        pControls.add(getPProyection(), gridBagConstraints2);
480
                }
481
                return pControls;
482
        }
483
        
484
        /**
485
         * This method initializes jTextField        
486
         *         
487
         * @return javax.swing.JTextField        
488
         */
489
        private JTextField getTFile() {
490
                if (tFile == null) {
491
                        tFile = new JTextField();
492
                        tFile.setPreferredSize(new java.awt.Dimension(350,25));
493
                }
494
                return tFile;
495
        }
496

    
497
        /**
498
         * This method initializes jButton        
499
         *         
500
         * @return javax.swing.JButton        
501
         */
502
        private JButton getBSelectFile() {
503
                if (bSelectFile == null) {
504
                        bSelectFile = new JButton();
505
                        bSelectFile.setText(PluginServices.getText(this, "cargar"));
506
                }
507
                return bSelectFile;
508
        }
509
        
510
        /**
511
         * This method initializes jPanel        
512
         *         
513
         * @return javax.swing.JPanel        
514
         */    
515
        private ProjChooserPanel getPProyection() {
516
                if (pProyection == null) {
517
                        pProyection = new ProjChooserPanel(GeoreferencingDialog.getLastProjection());
518
                        ((ProjChooserPanel)pProyection).addActionListener(new java.awt.event.ActionListener() { 
519
                                public void actionPerformed(java.awt.event.ActionEvent e) {
520
                                if (((ProjChooserPanel)pProyection).isOkPressed()) {
521
                                        FOpenDialog.setLastProjection(((ProjChooserPanel)pProyection).getCurProj());
522
                                }
523
                                }
524
                        });
525
                }
526
                return ((ProjChooserPanel)pProyection);
527
        }
528
        //**********************End Getters & Setters*****************
529
}
530