Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / gui / Panels / GeoRasterWizard.java @ 3003

History | View | Annotate | Download (18.5 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

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

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

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

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

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

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

    
188
        /**
189
         * This method initializes jPanel        
190
         *         
191
         * @return javax.swing.JPanel        
192
         */
193
        private JPanel getPFileSelection() {
194
                if (pFileSelection == null) {
195
                        FlowLayout flowLayout = new FlowLayout();
196
                        flowLayout.setVgap(10);
197
                        pFileSelection = new JPanel();
198
                        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));
199
                        pFileSelection.setLayout(flowLayout);
200
                        pFileSelection.setPreferredSize(new java.awt.Dimension(475,70));
201
                        pFileSelection.add(getTFile(), null);
202
                        pFileSelection.add(getBSelectFile(), null);
203
                }
204
                return pFileSelection;
205
        }
206

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

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

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

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

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

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

    
543
        /**
544
         * Adds the gvSIG's wizard listener 
545
         * 
546
         * @param listener
547
         */
548
        public void addWizardListener(WizardListener listener) { 
549
                listenerSupport.addWizardListener(listener);
550
        }
551

    
552
        /* (non-Javadoc)
553
         * @see com.iver.cit.gvsig.gui.WizardPanel#callError(java.lang.Exception)
554
         */
555
        public void callError(Exception descripcion) {
556
                // TODO Auto-generated method stub
557
                super.callError(descripcion);
558
        }
559

    
560
        /* (non-Javadoc)
561
         * @see com.iver.cit.gvsig.gui.WizardPanel#callStateChanged(boolean)
562
         */
563
        public void callStateChanged(boolean finishable) {
564
                // TODO Auto-generated method stub
565
                super.callStateChanged(finishable);
566
        }
567

    
568
        /* (non-Javadoc)
569
         * @see com.iver.cit.gvsig.gui.WizardPanel#getLayer()
570
         */
571
        public FLayer getLayer() {
572
                if(lyrGeoRaster != null)
573
                        return lyrGeoRaster;
574
                return null;
575
        }
576

    
577
        /* (non-Javadoc)
578
         * @see com.iver.cit.gvsig.gui.WizardPanel#getTabName()
579
         */
580
        public String getTabName() {
581
                return PluginServices.getText(this, "georef");
582
        }
583

    
584
        /* (non-Javadoc)
585
         * @see com.iver.cit.gvsig.gui.WizardPanel#removeWizardListener(com.iver.cit.gvsig.gui.wizards.WizardListener)
586
         */
587
        public void removeWizardListener(WizardListener listener) {
588
                // TODO Auto-generated method stub
589
                super.removeWizardListener(listener);
590
        }
591

    
592
        /* (non-Javadoc)
593
         * @see com.iver.cit.gvsig.gui.WizardPanel#setTabName(java.lang.String)
594
         */
595
        protected void setTabName(String name) {
596
                // TODO Auto-generated method stub
597
                super.setTabName(name);
598
        }
599
        
600
}
601