Statistics
| Revision:

root / trunk / libraries / libJCRS / src / org / gvsig / crs / gui / panels / TransformationEpsgPanel.java @ 8911

History | View | Annotate | Download (15.7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional 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
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package org.gvsig.crs.gui.panels;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.FlowLayout;
47
import java.awt.GridLayout;
48
import java.sql.ResultSet;
49
import java.sql.SQLException;
50
import java.util.ArrayList;
51

    
52
import javax.swing.BorderFactory;
53
import javax.swing.JLabel;
54
import javax.swing.JPanel;
55
import javax.swing.JScrollPane;
56
import javax.swing.JTable;
57
import javax.swing.JTextArea;
58
import javax.swing.ListSelectionModel;
59
import javax.swing.event.ListSelectionEvent;
60
import javax.swing.event.ListSelectionListener;
61
import javax.swing.table.DefaultTableModel;
62
import javax.swing.table.TableColumn;
63

    
64
import org.cresques.cts.IProjection;
65
import org.gvsig.crs.CrsFactory;
66
import org.gvsig.crs.EpsgConnection;
67
import org.gvsig.crs.ICrs;
68
import org.gvsig.crs.Query;
69

    
70
import com.iver.andami.PluginServices;
71
import com.iver.andami.ui.mdiManager.WindowInfo;
72
import com.iver.cit.gvsig.gui.TableSorter;
73

    
74
/**
75
 * Clase para la creaci?n y el manejo del panel de transformaci?n
76
 * para el caso de la EPSG
77
 * 
78
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
79
 *
80
 */
81
public class TransformationEpsgPanel extends JPanel  {
82
        /**
83
         * 
84
         */
85
        private static final long serialVersionUID = 1L;
86

    
87
        private JPanel panel;
88
        private IProjection firstProj;
89
        
90
        String[] transformations = {"9603", "9606", "9607", "9613", "9615", "9633"};
91
        
92
        private int transformation_code = -1;
93
        private String[] values;
94
        private String params = "+towgs84=";
95
        
96
        private JTable transformationTable;        
97
        private JScrollPane jScrollPane = null;
98
        public boolean inverseTranformation = false;
99
        
100
        public EpsgConnection connect = null;;        
101
        public int crs_target = -1;
102
        
103
        public DefaultTableModel dtm = null;
104
        
105
        private int crs_source_code;
106
        private String cadWKT = "";
107
        private ListSelectionModel lsm2 = null;
108
        public int selectedRowTable = -1;
109
        boolean tra = false;
110
        
111
        private JTextArea info;        
112
        public TableSorter sorter = null;
113
        
114
        public TransformationEpsgPanel(int target) {
115
                connect = new EpsgConnection();
116
                connect.setConnectionEPSG();
117
                String sentence = "SELECT source_geogcrs_code, coord_ref_sys_kind " +
118
                        "FROM epsg_coordinatereferencesystem " +
119
                        "WHERE coord_ref_sys_code = "+ target ;
120
                ResultSet result = Query.select(sentence,connect.getConnection());                
121
                try {
122
                        result.next();
123
                        String kind = result.getString("coord_ref_sys_kind");
124
                        if (kind.equals("projected"))
125
                                target = result.getInt("source_geogcrs_code");
126
                } catch (SQLException e1) {
127
                        e1.printStackTrace();
128
                }        
129
                crs_target = target;                
130
        }
131
        
132
        public JPanel getJPanel(){
133
                if (panel == null) {
134
                        panel = new JPanel();
135
                        panel.setLayout(new GridLayout(1,2));
136
                        panel.setLayout(new FlowLayout(FlowLayout.LEADING,5,10));
137
                        panel.setPreferredSize(new Dimension(525, 100));
138
                        panel.setBorder(
139
                                    BorderFactory.createCompoundBorder(
140
                                                        BorderFactory.createCompoundBorder(
141
                                                                        BorderFactory.createTitledBorder("Transformacion EPSG"),
142
                                                                        BorderFactory.createEmptyBorder(2,2,2,2)),
143
                                                                        panel.getBorder()));
144
                        panel.add(getJScrollPane());
145
                }
146
                return panel;
147
        }
148
        
149
        private Component getInfo() {
150
                if (info == null){
151
                        info = new JTextArea();
152
                        info.setLineWrap(true);
153
                        info.setWrapStyleWord(true);
154
                        info.setPreferredSize(new Dimension(400, 240));
155
                        info.setEditable(false);
156
                        info.append(getWKT());
157
                }
158
                info.setText(getWKT());
159
                return info;
160
        }
161
        
162
        public JTable getJTable() {
163
                if (transformationTable == null) {
164
                        String[] columnNames= {PluginServices.getText(this,"code_transformation"),
165
                                        PluginServices.getText(this,"name_transformation"),
166
                                        PluginServices.getText(this,"type_transformation"),
167
                                        PluginServices.getText(this,"source_crs"),
168
                                        PluginServices.getText(this,"target_crs"),
169
                                        PluginServices.getText(this,"description_area")};
170
                        Object[][]data = {};
171
                        dtm = new DefaultTableModel(data, columnNames)
172
                        {
173
                                public boolean isCellEditable(int row, int column) {
174
                                        return false;
175
                                }
176
                                /*
177
                                 * metodo necesario para cuando utilizamos tablas ordenadas
178
                                 * ya que sino al ordenar por algun campo no se queda con el orden
179
                                 * actual al seleccionar una fila (non-Javadoc)
180
                                 * @see javax.swing.table.TableModel#getColumnClass(int)
181
                                 */
182
                                public Class getColumnClass(int column)
183
                                {
184
                                        return getValueAt(0, column).getClass();
185
                                }
186
                        };
187
                        sorter = new TableSorter(dtm);                        
188

    
189
                        transformationTable = new JTable(sorter);
190
                        sorter.setTableHeader(transformationTable.getTableHeader());        
191
                        transformationTable.setCellSelectionEnabled(false);
192
                        transformationTable.setRowSelectionAllowed(true);
193
                        transformationTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
194
                        transformationTable.setColumnSelectionAllowed(false);
195
                        transformationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
196
                        TableColumn column = null;
197
                        for (int i = 0; i < columnNames.length; i++) {
198
                            column = transformationTable.getColumnModel().getColumn(i);
199
                            if (i == 0 || i == 3 || i == 4) {
200
                                column.setPreferredWidth(40);                                     
201
                            }else if (i == 2) {
202
                                    column.setPreferredWidth(80);
203
                            }
204
                            else {                            
205
                                column.setPreferredWidth(160);
206
                            }
207
                        }                        
208
                }
209
                return transformationTable;
210
        }
211
        
212
        private JScrollPane getJScrollPane() {
213
                if (jScrollPane == null) {
214
                        jScrollPane = new JScrollPane();
215
                        jScrollPane.setPreferredSize(new Dimension(525,200));
216
                        jScrollPane.setBorder(
217
                                    BorderFactory.createCompoundBorder(
218
                                        BorderFactory.createCompoundBorder(
219
                                                        BorderFactory.createTitledBorder(PluginServices.getText(this,"transformations")),
220
                                                        BorderFactory.createEmptyBorder(5,5,5,5)),
221
                                                        jScrollPane.getBorder()));
222
                        jScrollPane.setViewportView(getJTable());
223
                }
224
                return jScrollPane;
225
        }
226
                
227
        /**
228
         * Busca las tranformaciones directas entre el CRS de la capa y el CRS de la
229
         * vista.
230
         * @param crsCode
231
         */
232
        private void callTransformation(int crsCode){                
233
        int numRow = dtm.getRowCount();
234
                
235
        while (numRow != 0) {
236
                        numRow = numRow - 1;
237
                        dtm.removeRow(numRow);
238
                }
239
        
240
        String sentence = "SELECT source_geogcrs_code " +
241
                                                "FROM epsg_coordinatereferencesystem " +
242
                                                "WHERE coord_ref_sys_code = "+ crsCode ;
243
                ResultSet result = Query.select(sentence,connect.getConnection());
244
                int source = 0;
245
                try {
246
                        result.next();
247
                        source = result.getInt("source_geogcrs_code");
248
                } catch (SQLException e1) {
249
                        e1.printStackTrace();
250
                }
251
                
252
        ResultSet result2 = null;
253
        ResultSet result3 = null;
254
        if (source != 0){
255
                        crsCode = source;                                    
256
        }
257
        
258
        ArrayList codecs = new ArrayList();
259
        codecs.add(String.valueOf(crsCode));                
260
                
261
                for (int j=0; j< codecs.size(); j++){
262
                        sentence = "SELECT coord_op_code, coord_op_name, coord_op_type, source_crs_code, target_crs_code, area_of_use_code, coord_op_method_code " +
263
                                        "FROM epsg_coordoperation " +                        
264
                                        "WHERE source_crs_code = " + codecs.get(j) + "AND target_crs_code = " + crs_target;
265

    
266
            result = Query.select(sentence,connect.getConnection());        
267
            
268
            try {
269
                    while(result.next()) {
270
                            Object[]data = new Object[6];                                                            
271
                            data[0] = String.valueOf(result.getInt("coord_op_code"));
272
                            data[1] = result.getString("coord_op_name");
273
                            data[2] = result.getString("coord_op_type");
274
                            data[3] = String.valueOf(result.getInt("source_crs_code"));
275
                            data[4] = String.valueOf(result.getInt("target_crs_code"));
276
                            /*codecs.add(data[4]);
277
                            codecs = deleteItems(codecs);*/
278
                            
279
                            int aouc = Integer.parseInt(result.getString("area_of_use_code"));
280
                                        
281
                                        sentence = "SELECT area_of_use FROM epsg_area " +
282
                                                                        "WHERE area_code = "+ aouc ;
283
                                        
284
                                        result2 = Query.select(sentence,connect.getConnection());
285
                            while (result2.next())
286
                                    data[5] = result2.getString("area_of_use");
287
                            
288
                            String coord_op_method = result.getString("coord_op_method_code");                                                            
289
                                    
290
                            sentence = "SELECT reverse_op FROM epsg_coordoperationmethod "+
291
                                                    "WHERE coord_op_method_code LIKE " + coord_op_method;
292
                            result3 = Query.select(sentence,connect.getConnection());
293
                            
294
                            while(result3.next()){
295
                                    if (Integer.parseInt(result3.getString("reverse_op")) == 1){
296
                                            for (int i=0; i< transformations.length; i++){
297
                                                    if (coord_op_method.equals(transformations[i])){
298
                                                            dtm.addRow(data);
299
                                                    }
300
                                            }                                            
301
                                    }
302
                            }
303
                    }
304
            }         
305
            catch (SQLException e1) {
306
                    e1.printStackTrace();
307
            }
308
                }
309
        }
310
        
311
        /**
312
         * M?todo auxiliar para eliminar valores repetidos dentro del ArrayList
313
         * de codecs.
314
         * @param codecs
315
         * @return
316
         */
317
        private ArrayList deleteItems(ArrayList codecs) {
318
                ArrayList cod = new ArrayList();
319
                boolean equal = false;
320
                for (int i = 0; i< codecs.size(); i++){
321
                        String c = (String) codecs.get(i);
322
                        for (int j = 0; j<cod.size(); j++){
323
                                if (cod.get(j).equals(c)){
324
                                        equal = true;
325
                                }
326
                        }
327
                        if (!equal){
328
                                cod.add(c);                                
329
                        }
330
                        equal = false;
331
                }
332
                return cod;
333
        }
334
        
335
        /**
336
         * Busca las tranformaciones inversas entre el CRS de la capa y el CRS de la
337
         * vista. Aquellas cuyo valor de reverse_op sean iguales a uno.
338
         * @param crsCode
339
         */
340
        private void callInverseTransformation(int crsCode){
341
                //inverseTranformation = true;
342
        
343
        String sentence = "SELECT source_geogcrs_code " +
344
                                                "FROM epsg_coordinatereferencesystem " +
345
                                                "WHERE coord_ref_sys_code = "+ crsCode ;
346
                ResultSet result = Query.select(sentence,connect.getConnection());
347
                int source = 0;
348
                try {
349
                        result.next();
350
                        source = result.getInt("source_geogcrs_code");
351
                } catch (SQLException e1) {
352
                        e1.printStackTrace();
353
                }
354
                
355
        ResultSet result2 = null;
356
        ResultSet result3 = null;
357
        if (source != 0){
358
                        crsCode = source;                                    
359
        }
360
        
361
        ArrayList codecs = new ArrayList();
362
        codecs.add(String.valueOf(crsCode));
363
       
364
                codecs = deleteItems(codecs);
365
                
366
                for (int j=0; j< codecs.size(); j++){
367
                        sentence = "SELECT coord_op_code, coord_op_name, coord_op_type, source_crs_code, target_crs_code, area_of_use_code, coord_op_method_code " +
368
                                        "FROM epsg_coordoperation " +                        
369
                                        "WHERE source_crs_code = " + codecs.get(j) + "AND target_crs_code = " + crs_target;
370

    
371
            result = Query.select(sentence,connect.getConnection());        
372
            
373
            try {
374
                    while(result.next()) {
375
                            Object[]data = new Object[6];                                                            
376
                            data[0] = String.valueOf(result.getInt("coord_op_code"));
377
                            data[1] = result.getString("coord_op_name");
378
                            data[2] = result.getString("coord_op_type");
379
                            data[4] = String.valueOf(result.getInt("source_crs_code"));
380
                            data[3] = String.valueOf(result.getInt("target_crs_code"));
381
                            /*codecs.add(data[4]);
382
                            codecs = deleteItems(codecs);*/
383
                            
384
                            int aouc = Integer.parseInt(result.getString("area_of_use_code"));
385
                                        
386
                                        sentence = "SELECT area_of_use FROM epsg_area " +
387
                                                                        "WHERE area_code = "+ aouc ;
388
                                        
389
                                        result2 = Query.select(sentence,connect.getConnection());
390
                            while (result2.next())
391
                                    data[5] = result2.getString("area_of_use");
392
                            
393
                            String coord_op_method = result.getString("coord_op_method_code");                                                            
394
                                    
395
                            sentence = "SELECT reverse_op FROM epsg_coordoperationmethod "+
396
                                                    "WHERE coord_op_method_code LIKE " + coord_op_method;
397
                            result3 = Query.select(sentence,connect.getConnection());
398
                            
399
                            while(result3.next()){
400
                                    if (Integer.parseInt(result3.getString("reverse_op")) == 1){
401
                                            for (int i=0; i< transformations.length; i++){
402
                                                    if (coord_op_method.equals(transformations[i])){
403
                                                            dtm.addRow(data);
404
                                                    }
405
                                            }
406
                                    }
407
                            }
408
                    }
409
            }         
410
            catch (SQLException e1) {
411
                    e1.printStackTrace();
412
            }
413
                }
414
        }
415
        
416
        /**
417
         * 
418
         * @param val
419
         */
420
        public void setValues(String[] val) {
421
                values = val;
422
        }
423
        
424
        /**
425
         * 
426
         * @return
427
         */
428
        public String[] getValues() {
429
                return values;
430
        }
431
        
432
        /**
433
         * 
434
         * @param t_cod
435
         */
436
        public void setTrasformation_code(int t_cod) {
437
                transformation_code = t_cod;
438
        }
439
        
440
        /**
441
         * 
442
         * @return
443
         */
444
        public int getTransformation_code() {
445
                return transformation_code;
446
        }
447
        
448
        /**
449
         * 
450
         * @return
451
         */
452
        public ICrs getProjection() {
453
                params += values[0];
454
                for(int i = 1; i < values.length; i++)
455
                        params +=","+values[i];
456
                try {
457
                        ICrs crs = new CrsFactory().getCRS(crs_source_code,
458
                                        cadWKT,params);
459
                        return crs;
460
                } catch (org.gvsig.crs.CrsException e) {
461
                        e.printStackTrace();
462
                }
463
                return null;
464
        }
465
        
466
        /**
467
         * 
468
         * @param proj
469
         */
470
        public void setProjection(IProjection proj) {
471
                firstProj = proj;
472
        }
473
        
474
        /**
475
         * 
476
         * @param cad
477
         */
478
        public void setWKT(String cad){
479
                cadWKT = cad;
480
                getInfo();
481
        }
482
        
483
        /**
484
         * 
485
         * @return
486
         */
487
        public String getWKT(){
488
                return cadWKT;
489
        }
490
        
491
        /**
492
         * Indica el CRS que se utilizar? en la capa y busca las transformaciones
493
         * directas e inversas que pueda tener
494
         * @param code
495
         */
496
        public void setSource(int code){
497
                connect = new EpsgConnection();
498
                connect.setConnectionEPSG();
499
                inverseTranformation = false;
500
                crs_source_code = code;
501
                callTransformation(crs_source_code);
502
                
503
                int new_target = crs_target;
504
                int base_target = code;
505
                String sentence = "SELECT source_geogcrs_code, coord_ref_sys_kind " +
506
                                        "FROM epsg_coordinatereferencesystem " +
507
                                        "WHERE coord_ref_sys_code = "+ code;
508
                ResultSet result = Query.select(sentence,connect.getConnection());                
509
                try {
510
                        result.next();
511
                        String kind = result.getString("coord_ref_sys_kind");
512
                        if (kind.equals("projected"))
513
                                base_target = result.getInt("source_geogcrs_code");
514
                } catch (SQLException e1) {
515
                        e1.printStackTrace();
516
                }        
517
                crs_target = base_target;
518
                
519
                crs_source_code = new_target;
520
                callInverseTransformation(crs_source_code);
521
                crs_target = new_target;
522
                crs_source_code = code;
523
            
524
                int numr = dtm.getRowCount();
525
                if (numr > 0 ){
526
                        this.getJTable().setRowSelectionInterval(0,0);
527
                }
528
        }
529
        
530
        /**
531
         * 
532
         * @return
533
         */
534
        public int getSource(){
535
                return crs_source_code;
536
        }
537
        
538
        public WindowInfo getWindowInfo() {
539
                WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG);
540
                   m_viewinfo.setTitle("Transformation EPSG");
541
                return m_viewinfo;
542
        }        
543

    
544
}