Statistics
| Revision:

root / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / wizards / GeoRasterWizard.java @ 5217

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 org.gvsig.georeferencing.wizards;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.FlowLayout;
45
import java.awt.GridBagConstraints;
46
import java.awt.GridBagLayout;
47
import java.awt.event.ActionEvent;
48
import java.awt.geom.Rectangle2D;
49
import java.io.File;
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
import org.gvsig.georeferencing.gui.dialogs.GeoreferencingDialog;
67
import org.gvsig.georeferencing.layers.FLyrGeoRaster;
68
import org.gvsig.georeferencing.utils.PointManager;
69

    
70
import com.hardcode.driverManager.DriverLoadException;
71
import com.iver.andami.PluginServices;
72
import com.iver.andami.messages.NotificationManager;
73
import com.iver.andami.ui.mdiFrame.MDIFrame;
74
import com.iver.cit.gvsig.fmap.DriverException;
75
import com.iver.cit.gvsig.fmap.ViewPort;
76
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
77
import com.iver.cit.gvsig.fmap.layers.FLayer;
78
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
79
import com.iver.cit.gvsig.gui.FOpenDialog;
80
import com.iver.cit.gvsig.gui.View;
81
import com.iver.cit.gvsig.gui.WizardPanel;
82
import com.iver.cit.gvsig.gui.Panels.ProjChooserPanel;
83
import com.iver.cit.gvsig.gui.wizards.WizardListener;
84
import com.iver.cit.gvsig.gui.wizards.WizardListenerSupport;
85
 
86
/**
87
 * Clase que implementa el Wizard para la georreferenciaci?n
88
 * @author Nacho Brodin (brodin_ign@gva.es)
89
 */
90
public class GeoRasterWizard extends WizardPanel{
91
        
92
        //**********************Vars**********************************
93
        private JPanel                                         pGeneral = null;
94
        private JPanel                                         pFileSelection = null;
95
        private JPanel                                         pControls = null;
96
        private JTextField                                 tFile = null;
97
        private JButton                                 bSelectFile = null;
98
        private JPanel                                         pProyection = null;
99
        private WizardListenerSupport         listenerSupport = new WizardListenerSupport();
100
        /**
101
         * Lista de formatos soportados
102
         */
103
        private String[]                                 fileFilters = {"tif", "tiff", "jpg", "jpeg", "png", "gif", "bmp"};
104
        /**
105
         * Nombre del fichero seleccionado
106
         */
107
        private String                                         fName = "";
108
        
109
        /**
110
         * Nombre de la capa
111
         */
112
        private String                                         lyrName = "";
113
        /**
114
         * Recuerda la ?ltima ruta seleccionada por el usuario
115
         */
116
        //private String                                         lastPath = "./";
117
        private static String                         lastPath = null;
118
        private FLyrGeoRaster                         lyrGeoRaster = null;
119
        private JPanel                                         pCheckUseGeoref = null;
120
        private JCheckBox                                 cbUseGeoref = null;
121
        private JLabel                                         lUseGeoref = null;
122
        private double                                         widthPxImg, heightPxImg;
123
        /**
124
         * Extent calculado a partir de la vista.
125
         */
126
        private Extent                                         ext = null;
127
        //**********************Vars**********************************
128

    
129
        //**********************Classes*******************************
130
        /**
131
         * @author Nacho Brodin (brodin_ign@gva.es)
132
         */
133
        class SelectFileFilter extends javax.swing.filechooser.FileFilter {
134
                
135
                private JFileChooser chooser = null;
136
                private String file = null;
137
                
138
                public SelectFileFilter(JFileChooser ch, String file){
139
                        this.chooser = ch;
140
                        this.file = file;
141
                }
142
                
143
            public boolean accept(File f) {
144

    
145
                    return f.isDirectory() || f.getName().toLowerCase().endsWith("."+file);
146
            }
147
            
148
            public String getDescription() {
149
                    return file;
150
            }
151
            
152
        }
153
        //**********************End Classes***************************
154
        
155
        //**********************Methods*******************************
156
        /**
157
         * This is the default constructor
158
         */
159
        public GeoRasterWizard() {
160
                super();
161
                initialize();
162
        }
163

    
164
        /**
165
         * This method initializes this
166
         * 
167
         * @return void
168
         */
169
        private void initialize() {
170
        this.setPreferredSize(new java.awt.Dimension(750,320));
171
        this.setSize(new java.awt.Dimension(510,311));
172
        this.setLocation(new java.awt.Point(0,0));
173
        this.add(getPGeneral(), null);
174
        this.getBSelectFile().addActionListener(new java.awt.event.ActionListener() {
175
            public void actionPerformed(java.awt.event.ActionEvent evt) {
176
                    acceptButtonActionPerformed(evt);
177
            }
178
        });
179
         }
180
        
181
        /**
182
         * Calcula un extent posible para la imagen a partir del extent de la vista.
183
         * En este caso centra la imagen en la vista para que cuando se cargue la imagen
184
         * a georreferenciar nos quede centrada si elegimos la opci?n de no utilizar el
185
         * extent de la imagen.
186
         * @param w        Ancho de la imagen
187
         * @param h Alto de la imagen
188
         * @return        Extent para la imagen
189
         */
190
        protected Extent calcTempExtent(String file, ViewPort vp, IProjection proj){
191
                
192
                //Obtenemos el ancho y alto de la imagen y obtenemos un extent a partir
193
                //del viewport.
194
                Extent tempExtent = null;
195
                GeoRasterFile grf = GeoRasterFile.openFile(proj, file);
196
                widthPxImg = grf.getWidth();
197
                heightPxImg = grf.getHeight();
198
                if(vp == null || vp.getAdjustedExtent() == null){
199
                        vp = new ViewPort(proj);
200
                        Rectangle2D r2d = new Rectangle2D.Double(0, 0, widthPxImg, heightPxImg);
201
                        vp.setExtent(r2d);
202
                        tempExtent = new Extent(0, 0, widthPxImg, heightPxImg);
203
                }else
204
                        tempExtent = new Extent(vp.getAdjustedExtent());
205
                //grf.close();                
206
                
207
                double ulX = 0D, ulY = 0D, lrX = 0D, lrY = 0D;
208
                if(widthPxImg > heightPxImg){
209
                        double widthView = tempExtent.maxX() - tempExtent.minX();
210
                        ulX = tempExtent.minX() + (widthView / 4);
211
                        lrX = ulX + (widthView / 2);
212
                        double newAlto = ((heightPxImg * (widthView / 2)) / widthPxImg);
213
                        double centroY = tempExtent.minY()+((tempExtent.maxY() - tempExtent.minY())/2);
214
                        ulY = centroY - (newAlto / 2);
215
                        lrY = centroY + (newAlto / 2);
216
                }else{
217
                        double heightView = tempExtent.maxY() - tempExtent.minY();
218
                        ulY = tempExtent.minY() + (heightView / 4);
219
                        lrY = ulY + (heightView / 2);
220
                        double newAncho = ((widthPxImg * (heightView / 2)) / heightPxImg);
221
                        double centroX = tempExtent.minX()+((tempExtent.maxX() - tempExtent.minX())/2);
222
                        ulX = centroX - (newAncho / 2);
223
                        lrX = centroX + (newAncho / 2);
224
                }
225
                return new Extent(ulX, ulY, lrX, lrY);
226
        }
227
        
228
        /**
229
         * Evento de pulsado del bot?n de seleccionar fichero
230
         * @param e
231
         */
232
        private void acceptButtonActionPerformed(ActionEvent e) {
233
                //Selector de Fichero que se quiere georeferenciar
234
                                
235
                if(        e.getSource().equals(this.getBSelectFile())){
236
                        JFileChooser chooser = new JFileChooser(lastPath);
237
                        chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero"));
238
                        FileFilter f = null;
239
                        for(int i=0; i<this.fileFilters.length;i++){
240
                                f = new SelectFileFilter(chooser, this.fileFilters[i]);
241
                                chooser.addChoosableFileFilter(f);
242
                        }
243
                        int returnVal = chooser.showOpenDialog(this);
244
                        if(returnVal == JFileChooser.APPROVE_OPTION){
245
                                 this.fName = chooser.getSelectedFile().toString();
246
                                 FileFilter filter = chooser.getFileFilter();
247
                                 this.getTFile().setText(fName);
248
                                 lastPath = chooser.getCurrentDirectory().getAbsolutePath();
249
                                 
250
                                 if (PluginServices.getMainFrame() == null) {
251
                    ((JDialog) (getParent().getParent().getParent()
252
                            .getParent())).dispose();
253
                } else {
254
                        //Creamos la capa y la cargamos
255
                        IProjection proj = this.getPProyection().getCurProj();
256
                        try{
257
                                View  theView = null;
258
                                    try{
259
                                            theView = (View)PluginServices.getMDIManager().getActiveView();
260
                                    }catch(ClassCastException exc){
261
                                            return;
262
                                    }
263
                                                                
264
                                com.hardcode.driverManager.Driver driver = LayerFactory.getDM().getDriver("gvSIG Image Driver");
265
                                                        
266
                                File fich = new File(fName);
267
                                
268
                                                ViewPort viewPort = theView.getMapControl().getMapContext().getViewPort();
269
                                                
270
                                                //Calcula el extent para que la imagen que se carga quede centrada.
271
                                                ext = calcTempExtent( fName, viewPort, proj);
272
                                                
273
                                                //Asigna el nombre de la capa con el marcador *
274
                                this.setLyrName("*"+fName.substring(fName.lastIndexOf(File.separator)+1));
275
                                
276
                                //Crea la capa
277
                                lyrGeoRaster = FLyrGeoRaster.createLayer(getLyrName(), 
278
                                                                                                                (RasterDriver) driver, 
279
                                                                                                                fich, 
280
                                                                                                                proj,
281
                                                                                                                widthPxImg,
282
                                                                                                                heightPxImg);
283
                                //Pone activa la capa                              
284
                                if(lyrGeoRaster != null)
285
                                    lyrGeoRaster.setActive(true);
286
                            
287
                                
288
                                if(        ext != null && fich != null && 
289
                                        this.lyrGeoRaster != null && !getTFile().getText().equals(""))
290
                                        listenerSupport.callStateChanged(true);
291
                                
292
                        }catch(DriverLoadException exc){
293
                                NotificationManager.addError("No se pudo acceder a los drivers", exc);
294
                        }catch(DriverException exc){
295
                                NotificationManager.addError("El driver ha producido un error", exc);
296
                        }
297
                   
298
                }
299
                         }
300
            }        
301
                
302
        }
303
        
304
        /**
305
         * M?todo que se ejecuta al pulsar el bot?n de aceptar. En este caso asignamos el
306
         * extent calculado desde la vista si el checkbox no est? marcado y lanzaremos el
307
         * dialogo de georeferenciaci?n. El resto del trabajo lo realiza la clase AddLayer.
308
         */
309
        public void execute() {
310
                if(!this.getCbUseGeoref().isSelected())
311
                        lyrGeoRaster.setAssignExtent(ext);  
312
                
313
                PointManager.initWindow(lyrGeoRaster, true, this);
314
                
315
                /*lyrGeoRaster.getStackZoom().setInitExtent(lyrGeoRaster.getAssignExtent());
316
                GeoreferencingDialog geoDialog = null;
317
                
318
                JInternalFrame panel = PointManager.getInternalFrame(this);
319
        geoDialog = new GeoreferencingDialog(panel, lyrGeoRaster);             
320
            lyrGeoRaster.getFLyrPoints().saveState();
321
        lyrGeoRaster.setGeoDialog(geoDialog);
322
        panel.setSize(geoDialog.getWidth() + 12, geoDialog.getHeight() + 10);
323
        panel.getContentPane().add(geoDialog);
324
       
325
        MDIFrame mainFrame = (MDIFrame) PluginServices.getMainFrame();
326
        
327
        JDesktopPane desk = new JDesktopPane();
328
        desk.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
329
              
330
        JLayeredPane lyrPane = mainFrame.getLayeredPane();
331
        lyrPane.add(panel, JDesktopPane.PALETTE_LAYER);    
332
        lyrPane.add(desk, JDesktopPane.OUTLINE_DRAG_MODE);
333
        panel.show();*/
334
        }
335
                           
336
        public void initWizard() {
337
        
338
        }
339

    
340
        /**
341
         * Adds the gvSIG's wizard listener 
342
         * 
343
         * @param listener
344
         */
345
        public void addWizardListener(WizardListener listener) { 
346
                listenerSupport.addWizardListener(listener);
347
        }
348

    
349
        /* (non-Javadoc)
350
         * @see com.iver.cit.gvsig.gui.WizardPanel#callError(java.lang.Exception)
351
         */
352
        public void callError(Exception descripcion) {
353
                // TODO Auto-generated method stub
354
                super.callError(descripcion);
355
        }
356

    
357
        /* (non-Javadoc)
358
         * @see com.iver.cit.gvsig.gui.WizardPanel#callStateChanged(boolean)
359
         */
360
        public void callStateChanged(boolean finishable) {
361
                // TODO Auto-generated method stub
362
                super.callStateChanged(finishable);
363
        }
364

    
365
        /* (non-Javadoc)
366
         * @see com.iver.cit.gvsig.gui.WizardPanel#removeWizardListener(com.iver.cit.gvsig.gui.wizards.WizardListener)
367
         */
368
        public void removeWizardListener(WizardListener listener) {
369
                // TODO Auto-generated method stub
370
                super.removeWizardListener(listener);
371
        }        
372
        //**********************End Methods***************************
373
        
374
        //**********************Getters & Setters*********************
375
        /* (non-Javadoc)
376
         * @see com.iver.cit.gvsig.gui.WizardPanel#setTabName(java.lang.String)
377
         */
378
        protected void setTabName(String name) {
379
                // TODO Auto-generated method stub
380
                super.setTabName(name);
381
        }
382
        
383
        /* (non-Javadoc)
384
         * @see com.iver.cit.gvsig.gui.WizardPanel#getLayer()
385
         */
386
        public FLayer getLayer() {
387
                if(lyrGeoRaster != null)
388
                        return lyrGeoRaster;
389
                return null;
390
        }
391

    
392
        /* (non-Javadoc)
393
         * @see com.iver.cit.gvsig.gui.WizardPanel#getTabName()
394
         */
395
        public String getTabName() {
396
                return PluginServices.getText(this, "georef");
397
        }
398
        
399
        /**
400
         * @return Returns the fName.
401
         */
402
        public String getLyrName() {
403
                return lyrName;
404
        }
405
        
406
        /**
407
         * @param name The fName to set.
408
         */
409
        public void setLyrName(String name) {
410
                lyrName = name;
411
        }
412
        
413
        /**
414
         * This method initializes jPanel        
415
         *         
416
         * @return javax.swing.JPanel        
417
         */    
418
        private JPanel getPCheckUseGeoref() {
419
                if (pCheckUseGeoref == null) {
420
                        FlowLayout flowLayout1 = new FlowLayout();
421
                        flowLayout1.setAlignment(java.awt.FlowLayout.LEFT);
422
                        lUseGeoref = new JLabel();
423
                        pCheckUseGeoref = new JPanel();
424
                        pCheckUseGeoref.setLayout(flowLayout1);
425
                        pCheckUseGeoref.setPreferredSize(new java.awt.Dimension(370,31));
426
                        lUseGeoref.setText("Usar georeferenciaci?n de la imagen");
427
                        pCheckUseGeoref.add(getCbUseGeoref(), null);
428
                        pCheckUseGeoref.add(lUseGeoref, null);
429
                }
430
                return pCheckUseGeoref;
431
        }
432
        
433
        /**
434
         * This method initializes jCheckBox        
435
         *         
436
         * @return javax.swing.JCheckBox        
437
         */    
438
        private JCheckBox getCbUseGeoref() {
439
                if (cbUseGeoref == null) {
440
                        cbUseGeoref = new JCheckBox();
441
                        cbUseGeoref.setSelected(false);
442
                }
443
                return cbUseGeoref;
444
        }
445
        
446
        /**
447
         * @return Returns the lyrRaster.
448
         */
449
        public FLyrGeoRaster getFLyrGeoRaster() {
450
                return lyrGeoRaster;
451
        }
452

    
453
        /**
454
         * @param lyrRaster The lyrRaster to set.
455
         */
456
        public void setFLyrGeoRaster(FLyrGeoRaster lyrGeoRaster) {
457
                this.lyrGeoRaster = lyrGeoRaster;
458
        }
459
        
460
        /**
461
         * This method initializes jPanel        
462
         *         
463
         * @return javax.swing.JPanel        
464
         */
465
        private JPanel getPGeneral() {
466
                if (pGeneral == null) {
467
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
468
                        gridBagConstraints1.insets = new java.awt.Insets(5,0,0,0);
469
                        gridBagConstraints1.gridy = 1;
470
                        gridBagConstraints1.gridx = 0;
471
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
472
                        gridBagConstraints.insets = new java.awt.Insets(0,0,2,0);
473
                        gridBagConstraints.gridy = 0;
474
                        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
475
                        gridBagConstraints.gridx = 0;
476
                        pGeneral = new JPanel();
477
                        pGeneral.setLayout(new GridBagLayout());
478
                        pGeneral.setPreferredSize(new java.awt.Dimension(750,320));
479
                        gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
480
                        pGeneral.add(getPFileSelection(), gridBagConstraints);
481
                        pGeneral.add(getPControls(), gridBagConstraints1);
482
                }
483
                return pGeneral;
484
        }
485

    
486
        /**
487
         * This method initializes jPanel        
488
         *         
489
         * @return javax.swing.JPanel        
490
         */
491
        private JPanel getPFileSelection() {
492
                if (pFileSelection == null) {
493
                        FlowLayout flowLayout = new FlowLayout();
494
                        flowLayout.setVgap(10);
495
                        pFileSelection = new JPanel();
496
                        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));
497
                        pFileSelection.setLayout(flowLayout);
498
                        pFileSelection.setPreferredSize(new java.awt.Dimension(475,70));
499
                        pFileSelection.add(getTFile(), null);
500
                        pFileSelection.add(getBSelectFile(), null);
501
                }
502
                return pFileSelection;
503
        }
504

    
505
        private JPanel getPControls() {
506
                if (pControls == null) {
507
                        GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
508
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
509
                        gridBagConstraints2.gridx = 0;
510
                        gridBagConstraints2.gridy = 1;
511
                        pControls = new JPanel();
512
                        pControls.setLayout(new GridBagLayout());
513
                        pControls.setPreferredSize(new java.awt.Dimension(475,200));
514
                        pControls.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
515
                        gridBagConstraints11.gridx = 0;
516
                        gridBagConstraints11.gridy = 0;
517
                        pControls.add(getPCheckUseGeoref(), gridBagConstraints11);
518
                        pControls.add(getPProyection(), gridBagConstraints2);
519
                }
520
                return pControls;
521
        }
522
        
523
        /**
524
         * This method initializes jTextField        
525
         *         
526
         * @return javax.swing.JTextField        
527
         */
528
        private JTextField getTFile() {
529
                if (tFile == null) {
530
                        tFile = new JTextField();
531
                        tFile.setPreferredSize(new java.awt.Dimension(350,25));
532
                }
533
                return tFile;
534
        }
535

    
536
        /**
537
         * This method initializes jButton        
538
         *         
539
         * @return javax.swing.JButton        
540
         */
541
        private JButton getBSelectFile() {
542
                if (bSelectFile == null) {
543
                        bSelectFile = new JButton();
544
                        bSelectFile.setText(PluginServices.getText(this, "cargar"));
545
                }
546
                return bSelectFile;
547
        }
548
        
549
        /**
550
         * This method initializes jPanel        
551
         *         
552
         * @return javax.swing.JPanel        
553
         */    
554
        private ProjChooserPanel getPProyection() {
555
                if (pProyection == null) {
556
                        pProyection = new ProjChooserPanel(GeoreferencingDialog.getLastProjection());
557
                        ((ProjChooserPanel)pProyection).addActionListener(new java.awt.event.ActionListener() { 
558
                                public void actionPerformed(java.awt.event.ActionEvent e) {
559
                                if (((ProjChooserPanel)pProyection).isOkPressed()) {
560
                                        FOpenDialog.setLastProjection(((ProjChooserPanel)pProyection).getCurProj());
561
                                }
562
                                }
563
                        });
564
                }
565
                return ((ProjChooserPanel)pProyection);
566
        }
567
        //**********************End Getters & Setters*****************
568
}
569