Statistics
| Revision:

root / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / wizards / SelectFilePanel.java @ 6138

History | View | Annotate | Download (15.4 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.GeoPointPersistence;
42
import org.gvsig.georeferencing.utils.StackZoom;
43

    
44
import com.hardcode.driverManager.DriverLoadException;
45
import com.iver.andami.PluginServices;
46
import com.iver.andami.messages.NotificationManager;
47
import com.iver.cit.gvsig.fmap.DriverException;
48
import com.iver.cit.gvsig.fmap.ViewPort;
49
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
50
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
51
import com.iver.cit.gvsig.fmap.layers.FLyrGeoRaster;
52
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
53
import com.iver.cit.gvsig.fmap.layers.RasterAdapter;
54
import com.iver.cit.gvsig.fmap.layers.RasterFileAdapter;
55
import com.iver.cit.gvsig.gui.FOpenDialog;
56
import com.iver.cit.gvsig.gui.View;
57
import com.iver.cit.gvsig.gui.panels.ProjChooserPanel;
58

    
59
/**
60
 * Esta clase gestiona la carga de ficheros a georreferenciar mostrando un
61
 * wizard para su selecci?n. 
62
 * 
63
 * NO SE USA!!!!! 
64
 * 
65
 * @author Nacho Brodin (brodin_ign@gva.es)
66
 *
67
 */
68
public class SelectFilePanel extends JPanel {
69

    
70
        private JPanel pGeneral = null;
71
        private JPanel pFileSelection = null;
72
        private JPanel pControls = null;
73
        private JTextField tFile = null;
74
        private JButton bSelectFile = null;
75
        //private ProjChooserPanel jPanelProj = null;
76
        private JPanel pProyection = null;
77
        /**
78
         * Lista de formatos soportados
79
         */
80
        private String[] fileFilters = {"ecw", "sid", "tif", "jpg", "png"};
81
        /**
82
         * Nombre del fichero seleccionado
83
         */
84
        private String fName = "";
85
        
86
        /**
87
         * Nombre de la capa
88
         */
89
        private String lyrName = "";
90
        /**
91
         * Recuerda la ?ltima ruta seleccionada por el usuario
92
         */
93
        private String lastPath = "./";
94
        private GeoreferencingDialog parent = null;
95
        private FLyrGeoRaster lyrGeoRaster = null;
96
        
97
        private JPanel pCheckUseGeoref = null;
98
        private JCheckBox cbUseGeoref = null;
99
        private JLabel lUseGeoref = null;
100
        public double widthPxImg, heightPxImg;
101
        
102
        /**
103
         * This is the default constructor
104
         */
105
        public SelectFilePanel(GeoreferencingDialog georef) {
106
                super();
107
                initialize();
108
                this.parent = georef;
109
        }
110

    
111
        /**
112
         * This method initializes this
113
         * 
114
         * @return void
115
         */
116
        private void initialize() {
117
        this.setPreferredSize(new java.awt.Dimension(400,240));
118
        this.setSize(new java.awt.Dimension(400,240));
119
        this.setLocation(new java.awt.Point(0,0));
120
        this.add(getPGeneral(), null);
121
        this.getBSelectFile().addActionListener(new java.awt.event.ActionListener() {
122
            public void actionPerformed(java.awt.event.ActionEvent evt) {
123
                    acceptButtonActionPerformed(evt);
124
            }
125
        });
126
         }
127

    
128
        /**
129
         * This method initializes jPanel        
130
         *         
131
         * @return javax.swing.JPanel        
132
         */
133
        private JPanel getPGeneral() {
134
                if (pGeneral == null) {
135
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
136
                        gridBagConstraints1.insets = new java.awt.Insets(5,0,0,0);
137
                        gridBagConstraints1.gridy = 1;
138
                        gridBagConstraints1.gridx = 0;
139
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
140
                        gridBagConstraints.insets = new java.awt.Insets(0,0,2,0);
141
                        gridBagConstraints.gridy = 0;
142
                        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
143
                        gridBagConstraints.gridx = 0;
144
                        pGeneral = new JPanel();
145
                        pGeneral.setLayout(new GridBagLayout());
146
                        pGeneral.setPreferredSize(new java.awt.Dimension(400,240));
147
                        gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
148
                        pGeneral.add(getPFileSelection(), gridBagConstraints);
149
                        pGeneral.add(getPControls(), gridBagConstraints1);
150
                }
151
                return pGeneral;
152
        }
153

    
154
        /**
155
         * This method initializes jPanel        
156
         *         
157
         * @return javax.swing.JPanel        
158
         */
159
        private JPanel getPFileSelection() {
160
                if (pFileSelection == null) {
161
                        FlowLayout flowLayout = new FlowLayout();
162
                        flowLayout.setVgap(10);
163
                        pFileSelection = new JPanel();
164
                        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));
165
                        pFileSelection.setLayout(flowLayout);
166
                        pFileSelection.setPreferredSize(new java.awt.Dimension(380,70));
167
                        pFileSelection.add(getTFile(), null);
168
                        pFileSelection.add(getBSelectFile(), null);
169
                }
170
                return pFileSelection;
171
        }
172

    
173
        private JPanel getPControls() {
174
                if (pControls == null) {
175
                        GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
176
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
177
                        gridBagConstraints2.gridx = 0;
178
                        gridBagConstraints2.gridy = 1;
179
                        pControls = new JPanel();
180
                        pControls.setLayout(new GridBagLayout());
181
                        pControls.setPreferredSize(new java.awt.Dimension(380,135));
182
                        pControls.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
183
                        gridBagConstraints11.gridx = 0;
184
                        gridBagConstraints11.gridy = 0;
185
                        pControls.add(getPCheckUseGeoref(), gridBagConstraints11);
186
                        pControls.add(getPProyection(), gridBagConstraints2);
187
                }
188
                return pControls;
189
        }
190
        
191
        /**
192
         * This method initializes jTextField        
193
         *         
194
         * @return javax.swing.JTextField        
195
         */
196
        private JTextField getTFile() {
197
                if (tFile == null) {
198
                        tFile = new JTextField();
199
                        tFile.setPreferredSize(new java.awt.Dimension(280,25));
200
                }
201
                return tFile;
202
        }
203

    
204
        /**
205
         * This method initializes jButton        
206
         *         
207
         * @return javax.swing.JButton        
208
         */
209
        private JButton getBSelectFile() {
210
                if (bSelectFile == null) {
211
                        bSelectFile = new JButton();
212
                        bSelectFile.setText(PluginServices.getText(this, "cargar"));
213
                }
214
                return bSelectFile;
215
        }
216
        
217
        /**
218
         * This method initializes jPanel        
219
         *         
220
         * @return javax.swing.JPanel        
221
         */    
222
        private ProjChooserPanel getPProyection() {
223
                if (pProyection == null) {
224
                        pProyection = new ProjChooserPanel(GeoreferencingDialog.getLastProjection());
225
                        ((ProjChooserPanel)pProyection).addActionListener(new java.awt.event.ActionListener() { 
226
                                public void actionPerformed(java.awt.event.ActionEvent e) {
227
                                if (((ProjChooserPanel)pProyection).isOkPressed()) {
228
                                        FOpenDialog.setLastProjection(((ProjChooserPanel)pProyection).getCurProj());
229
                                }
230
                                }
231
                        });
232
                }
233
                return ((ProjChooserPanel)pProyection);
234
        }
235

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

    
404
        /**
405
         * @param lyrRaster The lyrRaster to set.
406
         */
407
        public void setFLyrGeoRaster(FLyrGeoRaster lyrGeoRaster) {
408
                this.lyrGeoRaster = lyrGeoRaster;
409
        }
410
   
411
        /**
412
         * 
413
         * @author Nacho Brodin (brodin_ign@gva.es)
414
         *
415
         */
416
        class SelectFileFilter extends javax.swing.filechooser.FileFilter {
417
                
418
                private JFileChooser chooser = null;
419
                private String file = null;
420
                
421
                public SelectFileFilter(JFileChooser ch, String file){
422
                        this.chooser = ch;
423
                        this.file = file;
424
                }
425
                
426
            public boolean accept(File f) {
427

    
428
                    return f.isDirectory() || f.getName().toLowerCase().endsWith("."+file);
429
            }
430
            
431
            public String getDescription() {
432
                    return file;
433
            }
434
            
435
        }
436
        
437
        /**
438
         * @return Returns the fName.
439
         */
440
        public String getLyrName() {
441
                return lyrName;
442
        }
443
        /**
444
         * @param name The fName to set.
445
         */
446
        public void setLyrName(String name) {
447
                lyrName = name;
448
        }
449
        /**
450
         * This method initializes jPanel        
451
         *         
452
         * @return javax.swing.JPanel        
453
         */    
454
        private JPanel getPCheckUseGeoref() {
455
                if (pCheckUseGeoref == null) {
456
                        FlowLayout flowLayout1 = new FlowLayout();
457
                        flowLayout1.setAlignment(java.awt.FlowLayout.LEFT);
458
                        lUseGeoref = new JLabel();
459
                        pCheckUseGeoref = new JPanel();
460
                        pCheckUseGeoref.setLayout(flowLayout1);
461
                        pCheckUseGeoref.setPreferredSize(new java.awt.Dimension(370,31));
462
                        lUseGeoref.setText("Usar georeferenciaci?n de la imagen");
463
                        pCheckUseGeoref.add(getCbUseGeoref(), null);
464
                        pCheckUseGeoref.add(lUseGeoref, null);
465
                }
466
                return pCheckUseGeoref;
467
        }
468
        /**
469
         * This method initializes jCheckBox        
470
         *         
471
         * @return javax.swing.JCheckBox        
472
         */    
473
        private JCheckBox getCbUseGeoref() {
474
                if (cbUseGeoref == null) {
475
                        cbUseGeoref = new JCheckBox();
476
                        cbUseGeoref.setSelected(true);
477
                }
478
                return cbUseGeoref;
479
        }
480
   }