Statistics
| Revision:

root / branches / F2 / extensions / extJCRS / src / org / gvsig / crs / gui / panels / TransformationEpsgPanel.java @ 10786

History | View | Annotate | Download (15.6 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.sql.ResultSet;
47
import java.sql.SQLException;
48
import java.util.ArrayList;
49

    
50
import javax.swing.BorderFactory;
51
import javax.swing.JPanel;
52
import javax.swing.JScrollPane;
53
import javax.swing.JTable;
54
import javax.swing.JTextArea;
55
import javax.swing.ListSelectionModel;
56
import javax.swing.table.DefaultTableModel;
57
import javax.swing.table.TableColumn;
58

    
59
import org.cresques.cts.IProjection;
60
import org.gvsig.crs.CrsFactory;
61
import org.gvsig.crs.EpsgConnection;
62
import org.gvsig.crs.ICrs;
63
import org.gvsig.crs.Query;
64

    
65
import com.iver.andami.PluginServices;
66
import com.iver.andami.ui.mdiManager.WindowInfo;
67
import com.iver.cit.gvsig.gui.TableSorter;
68

    
69
/**
70
 * Clase para la creaci?n y el manejo del panel de transformaci?n
71
 * para el caso de la EPSG
72
 * 
73
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
74
 * @author Luisa Marina Fern?ndez (luisam.fernandez@uclm.es)
75
 *
76
 */
77
public class TransformationEpsgPanel extends JPanel  {
78
        /**
79
         * 
80
         */
81
        private static final long serialVersionUID = 1L;
82
        private IProjection firstProj;
83
        
84
        String[] transformations = {"9603", "9606", "9607", "9613", "9615", "9633"};
85
        
86
        private int transformation_code = -1;
87
        private String[] values;
88
        private String params = "+towgs84=";
89
        
90
        private JTable transformationTable;        
91
        private JScrollPane jScrollPane = null;
92
        public boolean inverseTranformation = false;
93
        
94
        public EpsgConnection connect = null;        
95
        public int crs_target = -1;
96
        
97
        public DefaultTableModel dtm = null;
98
        
99
        private int crs_source_code;
100
        private String cadWKT = "";
101
        private ListSelectionModel lsm2 = null;
102
        public int selectedRowTable = -1;
103
        boolean tra = false;
104
        int real_target;
105
        
106
        private JTextArea info;        
107
        public TableSorter sorter = null;
108
        
109
        public TransformationEpsgPanel(int target) {
110
                connect = new EpsgConnection();
111
                connect.setConnectionEPSG();
112
                real_target = target;
113
                String sentence = "SELECT source_geogcrs_code, coord_ref_sys_kind " +
114
                        "FROM epsg_coordinatereferencesystem " +
115
                        "WHERE coord_ref_sys_code = "+ target ;
116
                ResultSet result = Query.select(sentence,connect.getConnection());                
117
                try {
118
                        result.next();
119
                        String kind = result.getString("coord_ref_sys_kind");
120
                        if (kind.equals("projected"))
121
                                target = result.getInt("source_geogcrs_code");
122
                } catch (SQLException e1) {
123
                        e1.printStackTrace();
124
                }        
125
                crs_target = target;
126
                initialize();
127
        }
128
        
129
        private void initialize(){
130
                        setLayout(new BorderLayout(1,50));
131
                        setBorder(
132
                                    BorderFactory.createCompoundBorder(
133
                                                        BorderFactory.createCompoundBorder(
134
                                                                        BorderFactory.createTitledBorder("Transformacion EPSG"),
135
                                                                        BorderFactory.createEmptyBorder(12,2,80,2)),
136
                                                                        getBorder()));
137
                        add(getJScrollPane(),BorderLayout.CENTER);
138
        }
139
        
140
        private Component getInfo() {
141
                if (info == null){
142
                        info = new JTextArea();
143
                        info.setLineWrap(true);
144
                        info.setWrapStyleWord(true);
145
                        info.setPreferredSize(new Dimension(400, 240));
146
                        info.setEditable(false);
147
                        info.append(getWKT());
148
                }
149
                info.setText(getWKT());
150
                return info;
151
        }
152
        
153
        public JTable getJTable() {
154
                if (transformationTable == null) {
155
                        String[] columnNames= {PluginServices.getText(this,"code_transformation"),
156
                                        PluginServices.getText(this,"name_transformation"),
157
                                        PluginServices.getText(this,"type_transformation"),
158
                                        PluginServices.getText(this,"source_crs"),
159
                                        PluginServices.getText(this,"target_crs"),
160
                                        PluginServices.getText(this,"description_area")};
161
                        Object[][]data = {};
162
                        dtm = new DefaultTableModel(data, columnNames)
163
                        {
164
                                private static final long serialVersionUID = 1L;
165
                                public boolean isCellEditable(int row, int column) {
166
                                        return false;
167
                                }
168
                                /*
169
                                 * metodo necesario para cuando utilizamos tablas ordenadas
170
                                 * ya que sino al ordenar por algun campo no se queda con el orden
171
                                 * actual al seleccionar una fila (non-Javadoc)
172
                                 * @see javax.swing.table.TableModel#getColumnClass(int)
173
                                 */
174
                                public Class getColumnClass(int column)
175
                                {
176
                                        return getValueAt(0, column).getClass();
177
                                }
178
                        };
179
                        sorter = new TableSorter(dtm);                        
180

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

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

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

    
540
}