Statistics
| Revision:

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

History | View | Annotate | Download (18.4 KB)

1 2993 nacho
/* 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 2998 nacho
public class GeoRasterWizard extends WizardPanel{
94 2993 nacho
        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 2998 nacho
                panel.addInternalFrameListener(new com.iver.cit.gvsig.gui.toolListeners.GeoRasterFrameListener());
440 2993 nacho
        panel.setClosable(true);
441
        geoDialog = new GeoreferencingDialog(panel);
442
        panel.setSize(400, 340);
443
        panel.setTitle(PluginServices.getText(this,"georreferenciar"));
444
        panel.getContentPane().add(geoDialog);
445
446
        MDIFrame mainFrame = (MDIFrame) PluginServices.getMainFrame();
447
        JLayeredPane lyrPane = mainFrame.getLayeredPane();
448
        lyrPane.add(panel, JDesktopPane.PALETTE_LAYER);
449
        panel.show();
450
        }
451
452
        /**
453
         * @return Returns the lyrRaster.
454
         */
455
        public FLyrGeoRaster getFLyrGeoRaster() {
456
                return lyrGeoRaster;
457
        }
458
459
        /**
460
         * @param lyrRaster The lyrRaster to set.
461
         */
462
        public void setFLyrGeoRaster(FLyrGeoRaster lyrGeoRaster) {
463
                this.lyrGeoRaster = lyrGeoRaster;
464
        }
465
466
        /**
467
         *
468
         * @author Nacho Brodin (brodin_ign@gva.es)
469
         *
470
         */
471
        class SelectFileFilter extends javax.swing.filechooser.FileFilter {
472
473
                private JFileChooser chooser = null;
474
                private String file = null;
475
476
                public SelectFileFilter(JFileChooser ch, String file){
477
                        this.chooser = ch;
478
                        this.file = file;
479
                }
480
481
            public boolean accept(File f) {
482
483
                    return f.isDirectory() || f.getName().toLowerCase().endsWith("."+file);
484
            }
485
486
            public String getDescription() {
487
                    return file;
488
            }
489
490
        }
491
492
        /**
493
         * @return Returns the fName.
494
         */
495
        public String getLyrName() {
496
                return lyrName;
497
        }
498
        /**
499
         * @param name The fName to set.
500
         */
501
        public void setLyrName(String name) {
502
                lyrName = name;
503
        }
504
        /**
505
         * This method initializes jPanel
506
         *
507
         * @return javax.swing.JPanel
508
         */
509
        private JPanel getPCheckUseGeoref() {
510
                if (pCheckUseGeoref == null) {
511
                        FlowLayout flowLayout1 = new FlowLayout();
512
                        flowLayout1.setAlignment(java.awt.FlowLayout.LEFT);
513
                        lUseGeoref = new JLabel();
514
                        pCheckUseGeoref = new JPanel();
515
                        pCheckUseGeoref.setLayout(flowLayout1);
516
                        pCheckUseGeoref.setPreferredSize(new java.awt.Dimension(370,31));
517
                        lUseGeoref.setText("Usar georeferenciaci?n de la imagen");
518
                        pCheckUseGeoref.add(getCbUseGeoref(), null);
519
                        pCheckUseGeoref.add(lUseGeoref, null);
520
                }
521
                return pCheckUseGeoref;
522
        }
523
        /**
524
         * This method initializes jCheckBox
525
         *
526
         * @return javax.swing.JCheckBox
527
         */
528
        private JCheckBox getCbUseGeoref() {
529
                if (cbUseGeoref == null) {
530
                        cbUseGeoref = new JCheckBox();
531
                        cbUseGeoref.setSelected(true);
532
                }
533
                return cbUseGeoref;
534
        }
535
536
        public void initWizard() {
537
538
        }
539
540
        /**
541
         * Adds the gvSIG's wizard listener
542
         *
543
         * @param listener
544
         */
545
        public void addWizardListener(WizardListener listener) {
546
                listenerSupport.addWizardListener(listener);
547
        }
548
549
        /* (non-Javadoc)
550
         * @see com.iver.cit.gvsig.gui.WizardPanel#callError(java.lang.Exception)
551
         */
552
        public void callError(Exception descripcion) {
553
                // TODO Auto-generated method stub
554
                super.callError(descripcion);
555
        }
556
557
        /* (non-Javadoc)
558
         * @see com.iver.cit.gvsig.gui.WizardPanel#callStateChanged(boolean)
559
         */
560
        public void callStateChanged(boolean finishable) {
561
                // TODO Auto-generated method stub
562
                super.callStateChanged(finishable);
563
        }
564
565
        /* (non-Javadoc)
566
         * @see com.iver.cit.gvsig.gui.WizardPanel#getLayer()
567
         */
568
        public FLayer getLayer() {
569
                if(lyrGeoRaster != null)
570
                        return lyrGeoRaster;
571
                return null;
572
        }
573
574
        /* (non-Javadoc)
575
         * @see com.iver.cit.gvsig.gui.WizardPanel#getTabName()
576
         */
577
        public String getTabName() {
578
                return PluginServices.getText(this, "georef");
579
        }
580
581
        /* (non-Javadoc)
582
         * @see com.iver.cit.gvsig.gui.WizardPanel#removeWizardListener(com.iver.cit.gvsig.gui.wizards.WizardListener)
583
         */
584
        public void removeWizardListener(WizardListener listener) {
585
                // TODO Auto-generated method stub
586
                super.removeWizardListener(listener);
587
        }
588
589
        /* (non-Javadoc)
590
         * @see com.iver.cit.gvsig.gui.WizardPanel#setTabName(java.lang.String)
591
         */
592
        protected void setTabName(String name) {
593
                // TODO Auto-generated method stub
594
                super.setTabName(name);
595
        }
596
597
}