Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libJCRS / src / org / gvsig / crs / gui / panels / TransformationEpsgPanel.java @ 8708

History | View | Annotate | Download (14.7 KB)

1
package org.gvsig.crs.gui.panels;
2

    
3

    
4
import java.awt.BorderLayout;
5
import java.awt.Component;
6
import java.awt.Dimension;
7
import java.awt.FlowLayout;
8
import java.awt.GridLayout;
9
import java.sql.ResultSet;
10
import java.sql.SQLException;
11
import java.util.ArrayList;
12

    
13
import javax.swing.BorderFactory;
14
import javax.swing.JLabel;
15
import javax.swing.JPanel;
16
import javax.swing.JScrollPane;
17
import javax.swing.JTable;
18
import javax.swing.JTextArea;
19
import javax.swing.ListSelectionModel;
20
import javax.swing.event.ListSelectionEvent;
21
import javax.swing.event.ListSelectionListener;
22
import javax.swing.table.DefaultTableModel;
23
import javax.swing.table.TableColumn;
24

    
25
import org.cresques.cts.IProjection;
26
import org.gvsig.crs.CrsFactory;
27
import org.gvsig.crs.EpsgConnection;
28
import org.gvsig.crs.ICrs;
29
import org.gvsig.crs.Query;
30

    
31
import com.iver.andami.PluginServices;
32
import com.iver.andami.ui.mdiManager.WindowInfo;
33
import com.iver.cit.gvsig.gui.TableSorter;
34

    
35
public class TransformationEpsgPanel extends JPanel implements ListSelectionListener {
36

    
37
        
38
        /**
39
         * 
40
         */
41
        private static final long serialVersionUID = 1L;
42

    
43
        private JPanel panel;
44
        private IProjection firstProj;
45
        
46
        String[] transformations = {"9603", "9606", "9607", "9613", "9615", "9633"};
47
        
48
        private int transformation_code = -1;
49
        private String[] values;
50
        private String params = "+towgs84=";
51
        
52
        private JTable transformationTable;        
53
        private JScrollPane jScrollPane = null;
54
        public boolean inverseTranformation = false;
55
        
56
        public EpsgConnection connect = null;;        
57
        int crs_target = -1;
58
        
59
        public DefaultTableModel dtm = null;
60
        
61
        private int crs_source_code;
62
        private String cadWKT = "";
63
        private ListSelectionModel lsm2 = null;
64
        public int selectedRowTable = -1;
65
        boolean tra = false;
66
        
67
        private JTextArea info;        
68
        public TableSorter sorter = null;
69
        
70
        public TransformationEpsgPanel(int target) {
71
                connect = new EpsgConnection();
72
                connect.setConnectionEPSG();
73
                String sentence = "SELECT source_geogcrs_code, coord_ref_sys_kind " +
74
                        "FROM epsg_coordinatereferencesystem " +
75
                        "WHERE coord_ref_sys_code = "+ target ;
76
                ResultSet result = Query.select(sentence,connect.getConnection());                
77
                try {
78
                        result.next();
79
                        String kind = result.getString("coord_ref_sys_kind");
80
                        if (kind.equals("projected"))
81
                                target = result.getInt("source_geogcrs_code");
82
                } catch (SQLException e1) {
83
                        e1.printStackTrace();
84
                }        
85
                crs_target = target;                
86
        }
87
        
88
        public JPanel getJPanel(){
89
                if (panel == null) {
90
                        panel = new JPanel();
91
                        panel.setLayout(new GridLayout(1,2));
92
                        panel.setLayout(new FlowLayout(FlowLayout.LEADING,5,10));
93
                        panel.setPreferredSize(new Dimension(525, 100));
94
                        panel.setBorder(
95
                                    BorderFactory.createCompoundBorder(
96
                                                        BorderFactory.createCompoundBorder(
97
                                                                        BorderFactory.createTitledBorder("Transformacion EPSG"),
98
                                                                        BorderFactory.createEmptyBorder(2,2,2,2)),
99
                                                                        panel.getBorder()));
100
                        panel.add(getJScrollPane());
101
                }
102
                return panel;
103
        }
104
        
105
        private Component getInfo() {
106
                if (info == null){
107
                        info = new JTextArea();
108
                        info.setLineWrap(true);
109
                        info.setWrapStyleWord(true);
110
                        info.setPreferredSize(new Dimension(400, 240));
111
                        info.setEditable(false);
112
                        info.append(getWKT());
113
                }
114
                info.setText(getWKT());
115
                return info;
116
        }
117
        
118
        public JTable getJTable() {
119
                if (transformationTable == null) {
120
                        String[] columnNames= {PluginServices.getText(this,"code_transformation"),
121
                                        PluginServices.getText(this,"name_transformation"),
122
                                        PluginServices.getText(this,"type_transformation"),
123
                                        PluginServices.getText(this,"source_crs"),
124
                                        PluginServices.getText(this,"target_crs"),
125
                                        PluginServices.getText(this,"description_area")};
126
                        Object[][]data = {};
127
                        dtm = new DefaultTableModel(data, columnNames)
128
                        {
129
                                public boolean isCellEditable(int row, int column) {
130
                                        return false;
131
                                }
132
                                /*
133
                                 * metodo necesario para cuando utilizamos tablas ordenadas
134
                                 * ya que sino al ordenar por algun campo no se queda con el orden
135
                                 * actual al seleccionar una fila (non-Javadoc)
136
                                 * @see javax.swing.table.TableModel#getColumnClass(int)
137
                                 */
138
                                public Class getColumnClass(int column)
139
                                {
140
                                        return getValueAt(0, column).getClass();
141
                                }
142
                        };
143
                        sorter = new TableSorter(dtm);                        
144

    
145
                        transformationTable = new JTable(sorter);
146
                        sorter.setTableHeader(transformationTable.getTableHeader());        
147
                        transformationTable.setCellSelectionEnabled(false);
148
                        transformationTable.setRowSelectionAllowed(true);
149
                        transformationTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
150
                        transformationTable.setColumnSelectionAllowed(false);
151
                        transformationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
152
                        TableColumn column = null;
153
                        for (int i = 0; i < columnNames.length; i++) {
154
                            column = transformationTable.getColumnModel().getColumn(i);
155
                            if (i == 0 || i == 3 || i == 4) {
156
                                column.setPreferredWidth(40);                                     
157
                            }else if (i == 2) {
158
                                    column.setPreferredWidth(80);
159
                            }
160
                            else {                            
161
                                column.setPreferredWidth(160);
162
                            }
163
                        }
164
                        ListSelectionModel rowSM2 = transformationTable.getSelectionModel();
165
                        rowSM2.addListSelectionListener(this);
166
                }
167
                return transformationTable;
168
        }
169
        
170
        private JScrollPane getJScrollPane() {
171
                if (jScrollPane == null) {
172
                        jScrollPane = new JScrollPane();
173
                        jScrollPane.setPreferredSize(new Dimension(525,200));
174
                        jScrollPane.setBorder(
175
                                    BorderFactory.createCompoundBorder(
176
                                        BorderFactory.createCompoundBorder(
177
                                                        BorderFactory.createTitledBorder(PluginServices.getText(this,"transformations")),
178
                                                        BorderFactory.createEmptyBorder(5,5,5,5)),
179
                                                        jScrollPane.getBorder()));
180
                        jScrollPane.setViewportView(getJTable());
181
                }
182
                return jScrollPane;
183
        }
184
                
185
        private void callTransformation(int crsCode){                
186
        int numRow = dtm.getRowCount();
187
                
188
        while (numRow != 0) {
189
                        numRow = numRow - 1;
190
                        dtm.removeRow(numRow);
191
                }
192
        
193
        String sentence = "SELECT source_geogcrs_code " +
194
                                                "FROM epsg_coordinatereferencesystem " +
195
                                                "WHERE coord_ref_sys_code = "+ crsCode ;
196
                ResultSet result = Query.select(sentence,connect.getConnection());
197
                int source = 0;
198
                try {
199
                        result.next();
200
                        source = result.getInt("source_geogcrs_code");
201
                } catch (SQLException e1) {
202
                        e1.printStackTrace();
203
                }
204
                
205
        ResultSet result2 = null;
206
        ResultSet result3 = null;
207
        if (source != 0){
208
                        crsCode = source;                                    
209
        }
210
        
211
        ArrayList codecs = new ArrayList();
212
        codecs.add(String.valueOf(crsCode));                
213
                
214
                for (int j=0; j< codecs.size(); j++){
215
                        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 " +
216
                                        "FROM epsg_coordoperation " +                        
217
                                        "WHERE source_crs_code = " + codecs.get(j) + "AND target_crs_code = " + crs_target;
218

    
219
            result = Query.select(sentence,connect.getConnection());        
220
            
221
            try {
222
                    while(result.next()) {
223
                            Object[]data = new Object[6];                                                            
224
                            data[0] = String.valueOf(result.getInt("coord_op_code"));
225
                            data[1] = result.getString("coord_op_name");
226
                            data[2] = result.getString("coord_op_type");
227
                            data[3] = String.valueOf(result.getInt("source_crs_code"));
228
                            data[4] = String.valueOf(result.getInt("target_crs_code"));
229
                            /*codecs.add(data[4]);
230
                            codecs = deleteItems(codecs);*/
231
                            
232
                            int aouc = Integer.parseInt(result.getString("area_of_use_code"));
233
                                        
234
                                        sentence = "SELECT area_of_use FROM epsg_area " +
235
                                                                        "WHERE area_code = "+ aouc ;
236
                                        
237
                                        result2 = Query.select(sentence,connect.getConnection());
238
                            while (result2.next())
239
                                    data[5] = result2.getString("area_of_use");
240
                            
241
                            String coord_op_method = result.getString("coord_op_method_code");                                                            
242
                                    
243
                            sentence = "SELECT reverse_op FROM epsg_coordoperationmethod "+
244
                                                    "WHERE coord_op_method_code LIKE " + coord_op_method;
245
                            result3 = Query.select(sentence,connect.getConnection());
246
                            
247
                            while(result3.next()){
248
                                    if (Integer.parseInt(result3.getString("reverse_op")) == 1){
249
                                            for (int i=0; i< transformations.length; i++){
250
                                                    if (coord_op_method.equals(transformations[i])){
251
                                                            dtm.addRow(data);
252
                                                    }
253
                                            }                                            
254
                                    }
255
                            }
256
                    }
257
            }         
258
            catch (SQLException e1) {
259
                    e1.printStackTrace();
260
            }
261
                }
262
        }
263
        
264
        private ArrayList deleteItems(ArrayList codecs) {
265
                ArrayList cod = new ArrayList();
266
                boolean equal = false;
267
                for (int i = 0; i< codecs.size(); i++){
268
                        String c = (String) codecs.get(i);
269
                        for (int j = 0; j<cod.size(); j++){
270
                                if (cod.get(j).equals(c)){
271
                                        equal = true;
272
                                }
273
                        }
274
                        if (!equal){
275
                                cod.add(c);                                
276
                        }
277
                        equal = false;
278
                }
279
                return cod;
280
        }
281
        
282
        private void callInverseTransformation(int crsCode){
283
                //inverseTranformation = true;
284
        
285
        String sentence = "SELECT source_geogcrs_code " +
286
                                                "FROM epsg_coordinatereferencesystem " +
287
                                                "WHERE coord_ref_sys_code = "+ crsCode ;
288
                ResultSet result = Query.select(sentence,connect.getConnection());
289
                int source = 0;
290
                try {
291
                        result.next();
292
                        source = result.getInt("source_geogcrs_code");
293
                } catch (SQLException e1) {
294
                        e1.printStackTrace();
295
                }
296
                
297
        ResultSet result2 = null;
298
        ResultSet result3 = null;
299
        if (source != 0){
300
                        crsCode = source;                                    
301
        }
302
        
303
        ArrayList codecs = new ArrayList();
304
        codecs.add(String.valueOf(crsCode));
305
       
306
                codecs = deleteItems(codecs);
307
                
308
                for (int j=0; j< codecs.size(); j++){
309
                        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 " +
310
                                        "FROM epsg_coordoperation " +                        
311
                                        "WHERE source_crs_code = " + codecs.get(j) + "AND target_crs_code = " + crs_target;
312

    
313
            result = Query.select(sentence,connect.getConnection());        
314
            
315
            try {
316
                    while(result.next()) {
317
                            Object[]data = new Object[6];                                                            
318
                            data[0] = String.valueOf(result.getInt("coord_op_code"));
319
                            data[1] = result.getString("coord_op_name");
320
                            data[2] = result.getString("coord_op_type");
321
                            data[4] = String.valueOf(result.getInt("source_crs_code"));
322
                            data[3] = String.valueOf(result.getInt("target_crs_code"));
323
                            /*codecs.add(data[4]);
324
                            codecs = deleteItems(codecs);*/
325
                            
326
                            int aouc = Integer.parseInt(result.getString("area_of_use_code"));
327
                                        
328
                                        sentence = "SELECT area_of_use FROM epsg_area " +
329
                                                                        "WHERE area_code = "+ aouc ;
330
                                        
331
                                        result2 = Query.select(sentence,connect.getConnection());
332
                            while (result2.next())
333
                                    data[5] = result2.getString("area_of_use");
334
                            
335
                            String coord_op_method = result.getString("coord_op_method_code");                                                            
336
                                    
337
                            sentence = "SELECT reverse_op FROM epsg_coordoperationmethod "+
338
                                                    "WHERE coord_op_method_code LIKE " + coord_op_method;
339
                            result3 = Query.select(sentence,connect.getConnection());
340
                            
341
                            while(result3.next()){
342
                                    if (Integer.parseInt(result3.getString("reverse_op")) == 1){
343
                                            for (int i=0; i< transformations.length; i++){
344
                                                    if (coord_op_method.equals(transformations[i])){
345
                                                            dtm.addRow(data);
346
                                                    }
347
                                            }
348
                                    }
349
                            }
350
                    }
351
            }         
352
            catch (SQLException e1) {
353
                    e1.printStackTrace();
354
            }
355
                }
356
        }
357
        
358
        public void setValues(String[] val) {
359
                values = val;
360
        }
361
        
362
        public String[] getValues() {
363
                return values;
364
        }
365
        
366
        private void setTrasformation_code(int t_cod) {
367
                transformation_code = t_cod;
368
        }
369
        
370
        public int getTransformation_code() {
371
                return transformation_code;
372
        }
373
                
374
        public ICrs getProjection() {
375
                params += values[0];
376
                for(int i = 1; i < values.length; i++)
377
                        params +=","+values[i];
378
                try {
379
                        ICrs crs = new CrsFactory().getCRS(crs_source_code,
380
                                        cadWKT,params);
381
                        return crs;
382
                } catch (org.gvsig.crs.CrsException e) {
383
                        e.printStackTrace();
384
                }
385
                return null;
386
        }
387
        
388
        public void setProjection(IProjection proj) {
389
                firstProj = proj;
390
        }
391
        
392
        public void setWKT(String cad){
393
                cadWKT = cad;
394
                getInfo();
395
        }
396
        
397
        public String getWKT(){
398
                return cadWKT;
399
        }
400
        
401
        public void setSource(int code){
402
                connect = new EpsgConnection();
403
                connect.setConnectionEPSG();
404
                inverseTranformation = false;
405
                crs_source_code = code;
406
                callTransformation(crs_source_code);
407
                
408
                int new_target = crs_target;
409
                int base_target = code;
410
                String sentence = "SELECT source_geogcrs_code, coord_ref_sys_kind " +
411
                                        "FROM epsg_coordinatereferencesystem " +
412
                                        "WHERE coord_ref_sys_code = "+ code;
413
                ResultSet result = Query.select(sentence,connect.getConnection());                
414
                try {
415
                        result.next();
416
                        String kind = result.getString("coord_ref_sys_kind");
417
                        if (kind.equals("projected"))
418
                                base_target = result.getInt("source_geogcrs_code");
419
                } catch (SQLException e1) {
420
                        e1.printStackTrace();
421
                }        
422
                crs_target = base_target;
423
                
424
                crs_source_code = new_target;
425
                callInverseTransformation(crs_source_code);
426
                crs_target = new_target;
427
                crs_source_code = code;
428
            
429
                int numr = dtm.getRowCount();
430
                if (numr > 0 ){                        
431
                        this.getJTable().setRowSelectionInterval(0,0);
432
                }
433
        }
434
        
435
        public int getSource(){
436
                return crs_source_code;
437
        }
438
        
439
        public WindowInfo getWindowInfo() {
440
                WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG);
441
                   m_viewinfo.setTitle("Transformation EPSG");
442
                return m_viewinfo;
443
        }
444

    
445
        public void valueChanged(ListSelectionEvent e) {
446
                // TODO Auto-generated method stub
447
                if (e.getSource() == this.getJTable().getSelectionModel()){
448
                        lsm2 = (ListSelectionModel)e.getSource();
449
                if (lsm2.isSelectionEmpty()) {
450
                        selectedRowTable = -1;
451
                        tra = false;
452
                } 
453
                else {
454
                    selectedRowTable = lsm2.getMinSelectionIndex();
455
                    setTrasformation_code(Integer.parseInt((String)sorter.getValueAt(selectedRowTable,0)));
456
                    String sentence = "SELECT source_crs_code " +
457
                                                                "FROM epsg_coordoperation " +                        
458
                                                                "WHERE coord_op_code = " + getTransformation_code() ;
459
                    ResultSet result = Query.select(sentence,connect.getConnection());
460
                    try {
461
                                        result.next();
462
                                        int source = result.getInt("source_crs_code");
463
                                        if (source == getSource()) {
464
                                                inverseTranformation = false;
465
                                        }
466
                                        else inverseTranformation = true;
467
                                } catch (SQLException e1) {                                                        
468
                                        e1.printStackTrace();
469
                                }
470
                }
471
                }
472
        }
473

    
474
}