Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / gvsig / geoprocessing / gui / GeoProcessingPanel.java @ 3052

History | View | Annotate | Download (91.3 KB)

1
/*
2
 * Created on 01-jul-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.gvsig.geoprocessing.gui;
45

    
46
import javax.swing.JCheckBox;
47
import javax.swing.JComboBox;
48
import javax.swing.JDialog;
49
import javax.swing.JList;
50
import javax.swing.JPanel;
51
import javax.swing.JRadioButton;
52
import javax.swing.JTextField;
53
import javax.swing.JScrollPane;
54

    
55
import com.hardcode.driverManager.DriverLoadException;
56
import com.hardcode.gdbms.engine.data.DataSource;
57
import com.hardcode.gdbms.engine.data.DataSourceFactory;
58
import com.hardcode.gdbms.engine.data.NoSuchTableException;
59
import com.hardcode.gdbms.engine.data.edition.DataWare;
60
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
61
import com.hardcode.gdbms.engine.values.BooleanValue;
62
import com.hardcode.gdbms.engine.values.Value;
63
import com.hardcode.gdbms.engine.values.ValueFactory;
64
import com.iver.andami.PluginServices;
65
import com.iver.andami.ui.mdiManager.View;
66
import com.iver.andami.ui.mdiManager.ViewInfo;
67
import com.iver.cit.gvsig.fmap.DriverException;
68
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
69
import com.iver.cit.gvsig.fmap.core.FShape;
70
import com.iver.cit.gvsig.fmap.core.IGeometry;
71
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
72
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
73
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
74
import com.iver.cit.gvsig.fmap.layers.FBitSet;
75
import com.iver.cit.gvsig.fmap.layers.FLayer;
76
import com.iver.cit.gvsig.fmap.layers.FLayers;
77
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
78
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
79
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
80
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
81
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
82
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
83
import com.iver.cit.gvsig.fmap.operations.strategies.SelectedShapeVisitor;
84
import com.iver.cit.gvsig.fmap.operations.strategies.SelectedZoomVisitor;
85
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
86
import com.iver.gvsig.geoprocessing.gui.operationpanels.GeoProcessingBufferPanel;
87
import com.iver.gvsig.geoprocessing.gui.operationpanels.GeoProcessingClipPanel;
88
import com.iver.gvsig.geoprocessing.gui.operationpanels.GeoProcessingDissolvePanel;
89
import com.iver.gvsig.geoprocessing.gui.operationpanels.GeoProcessingIntersectPanel;
90
import com.iver.gvsig.geoprocessing.gui.operationpanels.GeoProcessingMergePanel;
91
import com.iver.gvsig.geoprocessing.gui.operationpanels.GeoProcessingSpatialjoinPanel;
92
import com.iver.gvsig.geoprocessing.gui.operationpanels.GeoProcessingUnionPanel;
93
import com.iver.gvsig.geoprocessing.operations.GeoProcessingPersistenceOperations;
94
import com.iver.gvsig.geoprocessing.operations.GeoProcessingTableOperations;
95
import com.iver.gvsig.geoprocessing.operations.GeoProcessingTopologyOperations;
96
import com.iver.gvsig.geoprocessing.operations.strategies.SelectedShapeToJTSVisitor;
97
import com.vividsolutions.jts.geom.Coordinate;
98
import com.vividsolutions.jts.geom.Geometry;
99
import com.vividsolutions.jts.geom.GeometryCollection;
100
import com.vividsolutions.jts.geom.GeometryFactory;
101
import com.vividsolutions.jts.geom.LineString;
102
import com.vividsolutions.jts.geom.MultiPoint;
103
import com.vividsolutions.jts.geom.Point;
104
import com.vividsolutions.jts.geom.Polygon;
105
import com.vividsolutions.jts.geom.PrecisionModel;
106
import com.vividsolutions.jts.operation.distance.DistanceOp;
107

    
108
import java.awt.Component;
109
import java.awt.GridLayout;
110
import java.awt.GridBagLayout;
111
import java.awt.GridBagConstraints;
112
import java.awt.FlowLayout;
113
import java.awt.CardLayout;
114
import javax.swing.BoxLayout;
115
import java.awt.BorderLayout;
116
import java.io.File;
117
import java.sql.Types;
118
import java.util.BitSet;
119
import java.util.Vector;
120

    
121
import javax.swing.JButton;
122

    
123
public class GeoProcessingPanel extends JPanel implements View {
124

    
125
    /**
126
     * 
127
     */
128
    private static final long serialVersionUID = 1L;
129
    private GeoProcessingOperationSelectorPanel geoProcessingOperationSelectorPanel = null;
130
    private GeoProcessingBufferPanel geoProcessingBufferPanel = null;
131
    private GeoProcessingClipPanel geoProcessingClipPanel = null;
132
    private GeoProcessingDissolvePanel geoProcessingDissolvePanel = null;
133
    private GeoProcessingMergePanel geoProcessingMergePanel = null;
134
    private GeoProcessingIntersectPanel geoProcessingIntersectPanel = null;
135
    private GeoProcessingUnionPanel geoProcessingUnionPanel = null;
136
    private GeoProcessingSpatialjoinPanel geoProcessingSpatialjoinPanel = null;
137
    private JPanel buttonsPanel = null;
138

    
139
        private JButton jButton = null;
140
        private JButton jButton1 = null;
141
        private JButton jButton2 = null;
142
    private ViewInfo viewInfo = null;
143
    private FLayers layers = null;
144
    /**
145
     * This constructor initializes the set of layers
146
     */
147
    public GeoProcessingPanel(FLayers layers) {
148
        super();
149
        this.layers = layers;
150
        initialize();
151
    }
152

    
153
    /**
154
     * This method initializes this
155
     * 
156
     * @return void
157
     */
158
    private void initialize() {
159
        this.setLayout(new BorderLayout());
160
        this.setSize(416, 271);
161
        this.add(getButtonsPanel(), java.awt.BorderLayout.SOUTH);
162
        this.add(getGeoProcessingOperationSelectorPanel(), java.awt.BorderLayout.NORTH);
163
        this.add(getGeoProcessingBufferPanel(), java.awt.BorderLayout.NORTH);
164
        this.add(getGeoProcessingClipPanel(), java.awt.BorderLayout.NORTH);
165
        this.add(getGeoProcessingDissolvePanel(), java.awt.BorderLayout.NORTH);
166
        this.add(getGeoProcessingMergePanel(), java.awt.BorderLayout.NORTH);
167
        this.add(getGeoProcessingIntersectPanel(), java.awt.BorderLayout.NORTH);
168
        this.add(getGeoProcessingUnionPanel(), java.awt.BorderLayout.NORTH);
169
        this.add(getGeoProcessingSpatialjoinPanel(), java.awt.BorderLayout.NORTH);
170
        geoProcessingOperationSelectorPanel.setVisible(true);
171
        geoProcessingBufferPanel.setVisible(false);
172
        geoProcessingClipPanel.setVisible(false);
173
        geoProcessingDissolvePanel.setVisible(false);
174
        geoProcessingMergePanel.setVisible(false);
175
        geoProcessingIntersectPanel.setVisible(false);
176
        geoProcessingUnionPanel.setVisible(false);
177
        geoProcessingSpatialjoinPanel.setVisible(false);
178
        jButton1.setEnabled(false);
179
    }
180

    
181
    public ViewInfo getViewInfo() {
182
        // TODO Auto-generated method stub
183
        if (viewInfo == null) {
184
            viewInfo=new ViewInfo(ViewInfo.MODALDIALOG);
185
            viewInfo.setTitle(PluginServices.getText(this,"Herramientas_de_analisis"));
186
        }
187
        return viewInfo;
188
    }
189

    
190
    /**
191
     * This method initializes geoProcessingOperationSelectorPanel        
192
     *         
193
     * @return javax.swing.JPanel        
194
     */    
195
    private JPanel getGeoProcessingOperationSelectorPanel() {
196
            if (geoProcessingOperationSelectorPanel == null) {
197
                    geoProcessingOperationSelectorPanel = new GeoProcessingOperationSelectorPanel();
198
                    geoProcessingOperationSelectorPanel.setName("geoProcessingOperationSelectorPanel");
199
            }
200
            return geoProcessingOperationSelectorPanel;
201
    }
202

    
203
    /**
204
     * This method initializes geoProcessingBufferPanel        
205
     *         
206
     * @return javax.swing.JPanel        
207
     */    
208
    private JPanel getGeoProcessingBufferPanel() {
209
            if (geoProcessingBufferPanel == null) {
210
                    geoProcessingBufferPanel = new GeoProcessingBufferPanel(layers);
211
                    geoProcessingBufferPanel.setName("geoProcessingBufferPanel");
212
            }
213
            return geoProcessingBufferPanel;
214
    }
215

    
216
    /**
217
     * This method initializes geoProcessingClipPanel        
218
     *         
219
     * @return javax.swing.JPanel        
220
     */    
221
    private JPanel getGeoProcessingClipPanel() {
222
            if (geoProcessingClipPanel == null) {
223
                    geoProcessingClipPanel = new GeoProcessingClipPanel(layers);
224
                    geoProcessingClipPanel.setName("geoProcessingClipPanel");
225
                    // Si no le meto esta l?nea, no se visualiza el men?. Ver que puede
226
            // estar pasando ...
227
            geoProcessingClipPanel.setPreferredSize(new java.awt.Dimension(300,300));
228
            }
229
            return geoProcessingClipPanel;
230
    }
231

    
232
    /**
233
     * This method initializes geoProcessingDissolvePanel 
234
     *  
235
     * @return javax.swing.JPanel   
236
     */    
237
    private JPanel getGeoProcessingDissolvePanel() {
238
        if (geoProcessingDissolvePanel == null) {
239
            geoProcessingDissolvePanel = new GeoProcessingDissolvePanel(layers);
240
            geoProcessingDissolvePanel.setName("geoProcessingDissolvePanel");
241
        }
242
        return geoProcessingDissolvePanel;
243
    }
244

    
245
    /**
246
     * This method initializes geoProcessingMergePanel 
247
     *  
248
     * @return javax.swing.JPanel   
249
     */    
250
    private JPanel getGeoProcessingMergePanel() {
251
        if (geoProcessingMergePanel == null) {
252
            geoProcessingMergePanel = new GeoProcessingMergePanel(layers);
253
            geoProcessingMergePanel.setName("geoProcessingMergePanel");
254
        }
255
        return geoProcessingMergePanel;
256
    }
257

    
258
    /**
259
     * This method initializes geoProcessingIntersectPanel 
260
     *  
261
     * @return javax.swing.JPanel   
262
     */    
263
    private JPanel getGeoProcessingIntersectPanel() {
264
        if (geoProcessingIntersectPanel == null) {
265
            geoProcessingIntersectPanel = new GeoProcessingIntersectPanel(layers);
266
            geoProcessingIntersectPanel.setName("geoProcessingIntersectPanel");
267
        }
268
        return geoProcessingIntersectPanel;
269
    }
270

    
271
    /**
272
     * This method initializes geoProcessingUnionPanel 
273
     *  
274
     * @return javax.swing.JPanel   
275
     */    
276
    private JPanel getGeoProcessingUnionPanel() {
277
        if (geoProcessingUnionPanel == null) {
278
            geoProcessingUnionPanel = new GeoProcessingUnionPanel(layers);
279
            geoProcessingUnionPanel.setName("geoProcessingUnionPanel");
280
        }
281
        return geoProcessingUnionPanel;
282
    }
283

    
284
    /**
285
     * This method initializes geoProcessingSpatialjoinPanel 
286
     *  
287
     * @return javax.swing.JPanel   
288
     */    
289
    private JPanel getGeoProcessingSpatialjoinPanel() {
290
        if (geoProcessingSpatialjoinPanel == null) {
291
            geoProcessingSpatialjoinPanel = new GeoProcessingSpatialjoinPanel(layers);
292
            geoProcessingSpatialjoinPanel.setName("geoProcessingSpatialjoinPanel");
293
            // Si no le meto esta l?nea, no se visualiza el men?. Ver que puede
294
            // estar pasando ...
295
            geoProcessingSpatialjoinPanel.setPreferredSize(new java.awt.Dimension(300,300));
296
        }
297
        return geoProcessingSpatialjoinPanel;
298
    }
299

    
300
    /**
301
     * This method initializes buttonsPanel        
302
     *         
303
     * @return javax.swing.JPanel        
304
     */    
305
    private JPanel getButtonsPanel() {
306
            if (buttonsPanel == null) {
307
                    buttonsPanel = new JPanel();
308
                    buttonsPanel.setName("buttonsPanel");
309
                    buttonsPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED));
310
                    buttonsPanel.add(getJButton(), null);
311
                    buttonsPanel.add(getJButton1(), null);
312
                    buttonsPanel.add(getJButton2(), null);
313
            }
314
            return buttonsPanel;
315
    }
316

    
317
        /**
318
         * This method initializes jButton        
319
         *         
320
         * @return javax.swing.JButton        
321
         */    
322
        private JButton getJButton() {
323
                if (jButton == null) {
324
                        jButton = new JButton();
325
                        jButton.setText(PluginServices.getText(this,"Cerrar"));
326
                        jButton.addActionListener(new java.awt.event.ActionListener() { 
327
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
328
                                        System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
329
                    closeMethod();
330
                                }
331
                        });
332
                }
333
                return jButton;
334
        }
335
        /**
336
         * This method initializes jButton1        
337
         *         
338
         * @return javax.swing.JButton        
339
         */    
340
        private JButton getJButton1() {
341
                if (jButton1 == null) {
342
                        jButton1 = new JButton();
343
                        jButton1.setText(PluginServices.getText(this,"Anterior"));
344
                        jButton1.addActionListener(new java.awt.event.ActionListener() { 
345
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
346
                                        System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
347
                    getGeoProcessingOperationSelectorPanel().setVisible(true);
348
                    getGeoProcessingBufferPanel().setVisible(false);
349
                    getGeoProcessingClipPanel().setVisible(false);
350
                    getGeoProcessingDissolvePanel().setVisible(false);
351
                    getGeoProcessingMergePanel().setVisible(false);
352
                    getGeoProcessingIntersectPanel().setVisible(false);
353
                    getGeoProcessingUnionPanel().setVisible(false);
354
                    getGeoProcessingSpatialjoinPanel().setVisible(false);
355
                    jButton1.setEnabled(false);
356
                    jButton2.setText(PluginServices.getText(this,"Siguiente"));
357
                                }
358
                        });
359
                }
360
                return jButton1;
361
        }
362
        /**
363
         * This method initializes jButton2        
364
         *         
365
         * @return javax.swing.JButton        
366
         */    
367
        private JButton getJButton2() {
368
                if (jButton2 == null) {
369
                        jButton2 = new JButton();
370
                        jButton2.setText(PluginServices.getText(this,"Siguiente"));
371
                        jButton2.addActionListener(new java.awt.event.ActionListener() { 
372
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
373
                                        System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
374
                                        if (jButton2.getText().equals(PluginServices.getText(this,"Siguiente"))) {
375
                        if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(1)).isSelected()) {
376
                            getGeoProcessingOperationSelectorPanel().setVisible(false);
377
                            getGeoProcessingBufferPanel().setVisible(true);
378
                            getGeoProcessingClipPanel().setVisible(false);
379
                            getGeoProcessingDissolvePanel().setVisible(false);
380
                            getGeoProcessingMergePanel().setVisible(false);
381
                            getGeoProcessingIntersectPanel().setVisible(false);
382
                            getGeoProcessingUnionPanel().setVisible(false);
383
                            getGeoProcessingSpatialjoinPanel().setVisible(false);
384
                        } else if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(2)).isSelected()) {
385
                            getGeoProcessingOperationSelectorPanel().setVisible(false);
386
                            getGeoProcessingBufferPanel().setVisible(false);
387
                            getGeoProcessingClipPanel().setVisible(true);
388
                            getGeoProcessingDissolvePanel().setVisible(false);
389
                            getGeoProcessingMergePanel().setVisible(false);
390
                            getGeoProcessingIntersectPanel().setVisible(false);
391
                            getGeoProcessingUnionPanel().setVisible(false);
392
                            getGeoProcessingSpatialjoinPanel().setVisible(false);
393
                        } else if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(3)).isSelected()) {
394
                            getGeoProcessingOperationSelectorPanel().setVisible(false);
395
                            getGeoProcessingBufferPanel().setVisible(false);
396
                            getGeoProcessingClipPanel().setVisible(false);
397
                            getGeoProcessingDissolvePanel().setVisible(true);
398
                            getGeoProcessingMergePanel().setVisible(false);
399
                            getGeoProcessingIntersectPanel().setVisible(false);
400
                            getGeoProcessingUnionPanel().setVisible(false);
401
                            getGeoProcessingSpatialjoinPanel().setVisible(false);
402
                        } else if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(4)).isSelected()) {
403
                            getGeoProcessingOperationSelectorPanel().setVisible(false);
404
                            getGeoProcessingBufferPanel().setVisible(false);
405
                            getGeoProcessingClipPanel().setVisible(false);
406
                            getGeoProcessingDissolvePanel().setVisible(false);
407
                            getGeoProcessingMergePanel().setVisible(true);
408
                            getGeoProcessingIntersectPanel().setVisible(false);
409
                            getGeoProcessingUnionPanel().setVisible(false);
410
                            getGeoProcessingSpatialjoinPanel().setVisible(false);
411
                        } else if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(5)).isSelected()) {
412
                            getGeoProcessingOperationSelectorPanel().setVisible(false);
413
                            getGeoProcessingBufferPanel().setVisible(false);
414
                            getGeoProcessingClipPanel().setVisible(false);
415
                            getGeoProcessingDissolvePanel().setVisible(false);
416
                            getGeoProcessingMergePanel().setVisible(false);
417
                            getGeoProcessingIntersectPanel().setVisible(true);
418
                            getGeoProcessingUnionPanel().setVisible(false);
419
                            getGeoProcessingSpatialjoinPanel().setVisible(false);
420
                        } else if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(6)).isSelected()) {
421
                            getGeoProcessingOperationSelectorPanel().setVisible(false);
422
                            getGeoProcessingBufferPanel().setVisible(false);
423
                            getGeoProcessingClipPanel().setVisible(false);
424
                            getGeoProcessingDissolvePanel().setVisible(false);
425
                            getGeoProcessingMergePanel().setVisible(false);
426
                            getGeoProcessingIntersectPanel().setVisible(false);
427
                            getGeoProcessingUnionPanel().setVisible(true);
428
                            getGeoProcessingSpatialjoinPanel().setVisible(false);
429
                        } else if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(7)).isSelected()) {
430
                            getGeoProcessingOperationSelectorPanel().setVisible(false);
431
                            getGeoProcessingBufferPanel().setVisible(false);
432
                            getGeoProcessingClipPanel().setVisible(false);
433
                            getGeoProcessingDissolvePanel().setVisible(false);
434
                            getGeoProcessingMergePanel().setVisible(false);
435
                            getGeoProcessingIntersectPanel().setVisible(false);
436
                            getGeoProcessingUnionPanel().setVisible(false);
437
                            getGeoProcessingSpatialjoinPanel().setVisible(true);
438
                        } else {
439
                            // Si no hay ninguna operaci?n de geoprocesamiento seleccionada no se cambia el di?logo de opciones.
440
                            getGeoProcessingOperationSelectorPanel().setVisible(true);
441
                            getGeoProcessingBufferPanel().setVisible(false);
442
                            getGeoProcessingClipPanel().setVisible(false);
443
                            getGeoProcessingDissolvePanel().setVisible(false);
444
                            getGeoProcessingMergePanel().setVisible(false);
445
                            getGeoProcessingIntersectPanel().setVisible(false);
446
                            getGeoProcessingUnionPanel().setVisible(false);
447
                            getGeoProcessingSpatialjoinPanel().setVisible(false);
448
                        }
449
                        jButton1.setEnabled(true);
450
                        jButton2.setText(PluginServices.getText(this,"Terminar"));
451
                    } else if (jButton2.getText().equals(PluginServices.getText(this,"Terminar"))) {
452
                        if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(1)).isSelected()) {
453
                            bufferMethod();
454
                        } else if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(2)).isSelected()) {
455
                            clipMethod();
456
                        } else if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(3)).isSelected()) {
457
                            dissolveMethod();
458
                        } else if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(4)).isSelected()) {
459
                            mergeMethod();
460
                        } else if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(5)).isSelected()) {
461
                            intersectMethod();
462
                        } else if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(6)).isSelected()) {
463
                            unionMethod();
464
                        } else if (((JRadioButton)geoProcessingOperationSelectorPanel.getComponent(7)).isSelected()) {
465
                            spatialjoinMethod();
466
                        } else {
467
                            //
468
                        }
469
                        closeMethod();
470
                    }
471
                                }
472
                        });
473
                }
474
                return jButton2;
475
        }
476
    private Geometry[] getJtsGeometries(FLayer bufferingLayer, boolean selected) {
477
        FBitSet fBitSet = ((Selectable)bufferingLayer).getSelection();
478
        // Extraigo todos los elementos de la capa o solo los seleccionados
479
        //if (!((JCheckBox)component).isSelected()) {
480
        if (!selected) {
481
            VectorialAdapter va = (VectorialAdapter)((SingleLayer)bufferingLayer).getSource();
482
            fBitSet = new FBitSet();
483
            try {
484
                for (int i=0;i<va.getShapeCount();i++) {
485
                   fBitSet.set(i);
486
                }
487
            } catch (DriverIOException e) {
488
                // TODO Auto-generated catch block
489
                e.printStackTrace();
490
            }
491
            ((Selectable)bufferingLayer).setSelection(fBitSet);
492
        }
493
        SelectedShapeToJTSVisitor visitor = new SelectedShapeToJTSVisitor();
494
        try {
495
            ((VectorialData)bufferingLayer).process(visitor);
496
        } catch (DriverException e1) {
497
            // TODO Auto-generated catch block
498
            e1.printStackTrace();
499
        } catch (VisitException e1) {
500
            // TODO Auto-generated catch block
501
            e1.printStackTrace();
502
        }
503
        // Si no hab?an elementos seleccionados, cambio la selecci?n temporal
504
        // completa actual por la original selecci?n nula
505
        if (!selected) ((Selectable)bufferingLayer).clearSelection();
506
        // Almaceno en un vector los elementos para el buffer
507
        return visitor.getJtsGeometries();
508
    }
509
    private IGeometry[] getFmapGeometries(Geometry[] jtsGeometries) {
510
        FShape[] shapes = new FShape[jtsGeometries.length];
511
        IGeometry[] geoms = new IGeometry[jtsGeometries.length];
512
        for (int i=0;i<jtsGeometries.length;i++) {
513
            if (jtsGeometries[i] instanceof MultiPoint) {
514
                double[] xcoords = new double[jtsGeometries[i].getNumGeometries()];
515
                double[] ycoords = new double[jtsGeometries[i].getNumGeometries()];
516
                for (int j=0;j<jtsGeometries[i].getNumGeometries();j++) {
517
                    xcoords[j] = ((Point)jtsGeometries[i].getGeometryN(j)).getX();
518
                    ycoords[j] = ((Point)jtsGeometries[i].getGeometryN(j)).getY();
519
                }
520
                geoms[i] = ShapeFactory.createMultipoint2D(xcoords, ycoords);
521
            } else {
522
                shapes[i] = FConverter.jts_to_java2d(jtsGeometries[i]);
523
                geoms[i] = ShapeFactory.createGeometry(shapes[i]);
524
            }
525
        }
526
        return geoms;
527
    }
528
    private void bufferMethod() {
529
        String bufferingLayerName = (String)((JComboBox)geoProcessingBufferPanel.getComponent(2)).getSelectedItem();
530
        // TODO: Provisional. Hasta que Fernando le meta PKs al resto de ficheros.
531
        //System.out.println("bufferingLayerName = " + bufferingLayerName);
532
        boolean isDbfFile = false;
533
        if (bufferingLayerName.endsWith(".shp") || bufferingLayerName.endsWith(".SHP")) isDbfFile = true;
534
        FLayer bufferingLayer = layers.getLayer(bufferingLayerName);
535
        boolean selected = false;
536
        if (((JCheckBox)geoProcessingBufferPanel.getComponent(3)).isSelected()) selected = true;
537
        Geometry[] geometries = getJtsGeometries(bufferingLayer, selected);
538
        IGeometry[] shapes = null;
539
        boolean dissolveBuffer = false;
540
        // Realizo el buffer llamando al metodo makeBuffer de GeoProcessingTopologyOperations
541
        if (((JRadioButton)geoProcessingBufferPanel.getComponent(4)).isSelected()) {
542
            //dissolveBuffer = true;
543
            double bufferDistance = Double.parseDouble(((JTextField)geoProcessingBufferPanel.getComponent(7)).getText());
544
            if (((JCheckBox)geoProcessingBufferPanel.getComponent(13)).isSelected()) {
545
                dissolveBuffer = true;
546
                Geometry jtsBuffer = GeoProcessingTopologyOperations.makeDissolveBuffer(geometries, bufferDistance, getQuadrantSegments());
547
                // Realizo las operaciones relacionadas con tablas que el buffer requiere llamando a los m?todos convenientes de GeoProcessingTableOperations
548
                // (A lo mejor este paso no es necesario) Convierto las tablas modificadas y las nuevas entidades JTS al modelo de gvSIG
549
                System.out.println("jtsBuffer = " + jtsBuffer);
550
                FPolygon2D shapeBuffer = (FPolygon2D)FConverter.jts_to_java2d(jtsBuffer);
551
                System.out.println("shapeBuffer = " + shapeBuffer);
552
                shapes = new IGeometry[]{ShapeFactory.createGeometry(shapeBuffer)};
553
                // Guardo la tabla mediante gdbms. Guardo tambi?n las entidades JTS mediante gvSIG o usando solo gdbms (probablemente bastar? con usar gdbms)
554
                // Con dissolve. Habr? que hacer una tabla para un ?nico pol?gono o
555
                // multipol?gono. En la tabla solo meteremos los campos para los
556
                // que todos los elementos tengan atributos comunes. Si no hay
557
                // ning?n campo con estas caracter?sticas crearemos la tabla en
558
                // blanco.
559
            } else {
560
                double[] bufferDistances = new double[geometries.length];
561
                for (int i=0;i<bufferDistances.length;i++) {
562
                    bufferDistances[i] = bufferDistance;
563
                }
564
                Geometry[] jtsBuffers = GeoProcessingTopologyOperations.makeBuffers(geometries, bufferDistances, getQuadrantSegments());
565
                // Realizo las operaciones relacionadas con tablas que el buffer requiere llamando a los m?todos convenientes de GeoProcessingTableOperations
566
                // (A lo mejor este paso no es necesario) Convierto las tablas modificadas y las nuevas entidades JTS al modelo de gvSIG
567
                shapes = getFmapGeometries(jtsBuffers);
568
                // Guardo la tabla mediante gdbms. Guardo tambi?n las entidades JTS mediante gvSIG o usando solo gdbms (probablemente bastar? con usar gdbms)
569
                // En este caso se guarda una tabla semejante a la del tema de entrada
570
                // solo que ahora los atributos se corresponden con los buffers
571
                // producidos por las entidades a las que correspond?an estos
572
                // atributos.
573
            }
574
        } else if (((JRadioButton)geoProcessingBufferPanel.getComponent(5)).isSelected()) {
575
            //dissolveBuffer = false;
576
            double[] bufferDistances = getBufferDistancesFromTable(bufferingLayer);
577
            if (((JCheckBox)geoProcessingBufferPanel.getComponent(13)).isSelected()) {
578
                dissolveBuffer = true;
579
                // Aqu? hay problemas ... Con distintas distancias y haciendo dissolve ...
580
                Geometry jtsBuffer = GeoProcessingTopologyOperations.makeDissolveBuffer(geometries, bufferDistances, getQuadrantSegments());
581
                // Realizo las operaciones relacionadas con tablas que el buffer requiere llamando a los m?todos convenientes de GeoProcessingTableOperations
582
                // (A lo mejor este paso no es necesario) Convierto las tablas modificadas y las nuevas entidades JTS al modelo de gvSIG
583
                System.out.println("jtsBuffer = " + jtsBuffer);
584
                FPolygon2D shapeBuffer = (FPolygon2D)FConverter.jts_to_java2d(jtsBuffer);
585
                System.out.println("shapeBuffer = " + shapeBuffer);
586
                shapes = new IGeometry[]{ShapeFactory.createGeometry(shapeBuffer)};
587
                // Guardo la tabla mediante gdbms. Guardo tambi?n las entidades JTS mediante gvSIG o usando solo gdbms (probablemente bastar? con usar gdbms)
588
            } else {
589
                Geometry[] jtsBuffers = GeoProcessingTopologyOperations.makeBuffers(geometries, bufferDistances, getQuadrantSegments());
590
                // Realizo las operaciones relacionadas con tablas que el buffer requiere llamando a los m?todos convenientes de GeoProcessingTableOperations
591
                // (A lo mejor este paso no es necesario) Convierto las tablas modificadas y las nuevas entidades JTS al modelo de gvSIG
592
                shapes = getFmapGeometries(jtsBuffers);
593
                // Guardo la tabla mediante gdbms. Guardo tambi?n las entidades JTS mediante gvSIG o usando solo gdbms (probablemente bastar? con usar gdbms)
594
            }
595
        }
596
        AlphanumericData lyr = (AlphanumericData)(layers.getLayer(geoProcessingBufferPanel.getSelectedLayerName()));
597
        GeoProcessingPersistenceOperations.saveResultsToShapeFile(shapes, geoProcessingBufferPanel.getOutputFile());
598
        try {
599
            // TODO: Provisional. Hasta que Fernando le meta PKs al resto de ficheros.
600
            GeoProcessingTableOperations.saveBufferResultsToDbaseFile(geoProcessingBufferPanel.getOutputFile(), lyr, dissolveBuffer, isDbfFile);
601
        } catch (IncompatibleTypesException e) {
602
            // TODO Auto-generated catch block
603
            e.printStackTrace();
604
        } catch (DriverException e) {
605
            // TODO Auto-generated catch block
606
            e.printStackTrace();
607
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
608
            // TODO Auto-generated catch block
609
            e.printStackTrace();
610
        } catch (DriverLoadException e) {
611
            // TODO Auto-generated catch block
612
            e.printStackTrace();
613
        } catch (NoSuchTableException e) {
614
            // TODO Auto-generated catch block
615
            e.printStackTrace();
616
        }
617
        if (!((JCheckBox)geoProcessingBufferPanel.getComponent(3)).isSelected()) ((Selectable)bufferingLayer).clearSelection();
618
    }
619
    private void mergeMethod() {
620
        Object[] layerNamesToMerge = geoProcessingMergePanel.getLayerSelectedNames();
621
        String fieldsFromLayerName = geoProcessingMergePanel.getFieldsFromLayerName();
622
        // TODO: Provisional. Hasta que Fernando le meta PKs al resto de ficheros.
623
        boolean isDbfFile = false;
624
        if (fieldsFromLayerName.endsWith(".shp") || fieldsFromLayerName.endsWith(".SHP")) isDbfFile = true;
625
        AlphanumericData lyr = (AlphanumericData)layers.getLayer(fieldsFromLayerName);
626
        FLayer[] layersToMerge = new FLayer[layerNamesToMerge.length];
627
        boolean[] selecteds = new boolean[layersToMerge.length];
628
        Vector allGeometriesToMerge = new Vector();
629
        for (int i=0;i<layerNamesToMerge.length;i++) {
630
            layersToMerge[i] = layers.getLayer((String)layerNamesToMerge[i]);
631
            if (!((Selectable)layersToMerge[i]).getSelection().isEmpty()) selecteds[i] = true;
632
            Geometry[] geometriesToMerge = getJtsGeometries(layersToMerge[i], selecteds[i]);
633
            for (int j=0;j<geometriesToMerge.length;j++) {
634
                allGeometriesToMerge.add(geometriesToMerge[j]);
635
            }
636
        }
637
        Geometry[] mergedGeometries = new Geometry[allGeometriesToMerge.size()];
638
        for (int i=0;i<allGeometriesToMerge.size();i++) {
639
            mergedGeometries[i] = (Geometry)allGeometriesToMerge.get(i);
640
        }
641
        IGeometry[] shapes = getFmapGeometries(mergedGeometries);
642
        AlphanumericData[] lyrs = new AlphanumericData[layersToMerge.length];
643
        for (int i=0;i<layersToMerge.length;i++) {
644
            lyrs[i] = (AlphanumericData)layersToMerge[i];
645
        }
646
        // Pasarle tb la capa con los campos.
647
        GeoProcessingPersistenceOperations.saveResultsToShapeFile(shapes, geoProcessingMergePanel.getOutputFile());
648
        try {
649
            GeoProcessingTableOperations.saveMergeResultsToDbaseFile(geoProcessingMergePanel.getOutputFile(), lyrs, lyr, isDbfFile);
650
            for (int i=0;i<lyrs.length;i++) {
651
                lyrs[i].getRecordset().getSelection().clear();
652
            }
653
        } catch (IncompatibleTypesException e) {
654
            // TODO Auto-generated catch block
655
            e.printStackTrace();
656
        } catch (DriverException e) {
657
            // TODO Auto-generated catch block
658
            e.printStackTrace();
659
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
660
            // TODO Auto-generated catch block
661
            e.printStackTrace();
662
        } catch (DriverLoadException e) {
663
            // TODO Auto-generated catch block
664
            e.printStackTrace();
665
        } catch (NoSuchTableException e) {
666
            // TODO Auto-generated catch block
667
            e.printStackTrace();
668
        }
669
    }
670
    private void dissolveMethod() {
671
        String layerToDissolveName = (String)((JComboBox)geoProcessingDissolvePanel.getComponent(2)).getSelectedItem();
672
        FLayer layerToDissolve = layers.getLayer(layerToDissolveName);
673
        boolean selected = false;
674
        if (((JCheckBox)geoProcessingDissolvePanel.getComponent(3)).isSelected()) selected = true;
675
        Geometry[] geometriesToDissolve = getJtsGeometries(layerToDissolve, selected);
676
        String dissolveAttributeName = (String)((JComboBox)geoProcessingDissolvePanel.getComponent(5)).getSelectedItem();
677
        AlphanumericData lyr = (AlphanumericData)layerToDissolve;
678
        // No lo puedo llevar a GeoProcessingTopologyOperations porque no es solo
679
        // un problema JTS. Tampoco lo puedo llevar a GeoProcessingTableOperations
680
        // porque tampoco es un problema solo de GDBMS. Es mixto y por el momento
681
        // se queda aqu?.
682
        Vector resultGeometries = new Vector(); 
683
        DataSource ds;
684
        SelectableDataSource sds;
685
        try {
686
            ds = lyr.getRecordset();
687
            sds = lyr.getRecordset();
688
            int fieldId = ds.getFieldIndexByName(dissolveAttributeName);
689
            FBitSet fBitSet = sds.getSelection();
690
            if (fBitSet.cardinality()==0) fBitSet.set(0, (int)lyr.getRecordset().getRowCount());
691
            Value[] fieldValues = new Value[sds.getSelection().cardinality()];
692
            int k=0;
693
            for (int i=0;i<ds.getRowCount();i++) {
694
                if (fBitSet.get(i)) {
695
                    fieldValues[k] = ds.getFieldValue(i, fieldId);
696
                    k++;
697
                }
698
            }
699
            for (int i=0;i<fieldValues.length;i++) {
700
                Value valuei = fieldValues[i];
701
                Geometry geometryi = geometriesToDissolve[i];
702
                for (int j=0;j<fieldValues.length;j++) {
703
                    if (j!=i) {
704
                        Value valuej = fieldValues[j];
705
                        if (((BooleanValue)(valuej.equals(valuei))).getValue()) {
706
                            Geometry geometryj = geometriesToDissolve[j];
707
                                geometryi = geometryj.union(geometryi);
708
                        }
709
                    }
710
                }
711
                resultGeometries.add(geometryi);
712
            }
713
            for (int i=0;i<resultGeometries.size();i++) {
714
                for (int j=0;j<resultGeometries.size();j++) {
715
                    if (j>i) {
716
                        if (((Geometry)resultGeometries.get(i)).equals((Geometry)resultGeometries.get(j))) {
717
                            resultGeometries.remove(j);
718
                            j--;
719
                        }
720
                    }
721
                }
722
            }
723
        } catch (DriverException e) {
724
            // TODO Auto-generated catch block
725
            e.printStackTrace();
726
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
727
            // TODO Auto-generated catch block
728
            e.printStackTrace();
729
        } catch (IncompatibleTypesException e) {
730
            // TODO Auto-generated catch block
731
            e.printStackTrace();
732
        }
733
        Geometry[] dissolvedGeometries = new Geometry[resultGeometries.size()];
734
        for (int i=0;i<resultGeometries.size();i++) {
735
            dissolvedGeometries[i] = (Geometry)resultGeometries.get(i);
736
        }
737
        //
738
        //Geometry[] dissolvedGeometries = GeoProcessingTopologyOperations.makeDissolve(geometriesToDissolve, lyr, dissolveAttributeName);
739
        IGeometry[] shapes = getFmapGeometries(dissolvedGeometries);
740
        // Guardo la tabla mediante gdbms. Guardo tambi?n las entidades JTS mediante gvSIG o usando solo gdbms (probablemente bastar? con usar gdbms)
741
        GeoProcessingPersistenceOperations.saveResultsToShapeFile(shapes, geoProcessingDissolvePanel.getOutputFile());
742
        try {
743
            GeoProcessingTableOperations.saveDissolveResultsToDbaseFile(geoProcessingDissolvePanel.getOutputFile(), lyr, dissolveAttributeName);
744
            lyr.getRecordset().getSelection().clear();
745
        } catch (IncompatibleTypesException e) {
746
            // TODO Auto-generated catch block
747
            e.printStackTrace();
748
        } catch (DriverException e) {
749
            // TODO Auto-generated catch block
750
            e.printStackTrace();
751
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
752
            // TODO Auto-generated catch block
753
            e.printStackTrace();
754
        } catch (DriverLoadException e) {
755
            // TODO Auto-generated catch block
756
            e.printStackTrace();
757
        } catch (NoSuchTableException e) {
758
            // TODO Auto-generated catch block
759
            e.printStackTrace();
760
        }
761
    }
762
    private void spatialjoinMethod() {
763
        String layerToJoinName = (String)((JComboBox)geoProcessingSpatialjoinPanel.getComponent(0)).getSelectedItem();
764
        FLayer layerToJoin = layers.getLayer(layerToJoinName);
765
        String joiningLayerName = (String)((JComboBox)geoProcessingSpatialjoinPanel.getComponent(3)).getSelectedItem();
766
        FLayer joiningLayer = layers.getLayer(joiningLayerName);
767
        boolean selected = false;
768
        try {
769
            if (((JCheckBox)geoProcessingSpatialjoinPanel.getComponent(1)).isSelected()) selected = true;
770
            Geometry[] geometriesToJoin = getJtsGeometries(layerToJoin, selected);
771
            AlphanumericData lyrToJoin = (AlphanumericData)layerToJoin;
772
            SelectableDataSource lyrToJoinSDS = lyrToJoin.getRecordset();
773
            FBitSet bitsetToJoin = lyrToJoinSDS.getSelection();
774
            if (bitsetToJoin.cardinality()==0) bitsetToJoin.set(0, (int)lyrToJoin.getRecordset().getRowCount());
775
            if (((JCheckBox)geoProcessingSpatialjoinPanel.getComponent(5)).isSelected()) selected = true;
776
            Geometry[] joiningGeometries = getJtsGeometries(joiningLayer, selected);
777
            AlphanumericData joiningLyr = (AlphanumericData)joiningLayer;
778
            SelectableDataSource joiningLyrSDS = joiningLyr.getRecordset();
779
            FBitSet joiningBitset = joiningLyrSDS.getSelection();
780
            if (joiningBitset.cardinality()==0) joiningBitset.set(0, (int)joiningLyr.getRecordset().getRowCount());
781
            DataSource lyrToJoinDS;
782
            DataSource joiningLyrDS;
783
            lyrToJoinDS = lyrToJoin.getRecordset();
784
            joiningLyrDS = joiningLyr.getRecordset();
785
            // Los campos se a?aden siempre. Los valores solo cuando toca ...
786
            String[] lyrToJoinFieldNames = new String[lyrToJoinDS.getFieldCount()];
787
            int[] lyrToJoinFieldTypes = new int[lyrToJoinDS.getFieldCount()];
788
            for (int i = 0; i < lyrToJoinDS.getFieldCount(); i++) {
789
                lyrToJoinFieldNames[i] = lyrToJoinDS.getFieldName(i);
790
                lyrToJoinFieldTypes[i] = lyrToJoinDS.getFieldType(i);
791
            }
792
            String[] joiningLyrFieldNames = new String[joiningLyrDS.getFieldCount()];
793
            int[] joiningLyrFieldTypes = new int[joiningLyrDS.getFieldCount()];
794
            for (int i = 0; i < joiningLyrDS.getFieldCount(); i++) {
795
                joiningLyrFieldNames[i] = joiningLyrDS.getFieldName(i);
796
                joiningLyrFieldTypes[i] = joiningLyrDS.getFieldType(i);
797
            }
798
            String[] joinedLyrFieldNames = new String[lyrToJoinDS.getFieldCount()+joiningLyrDS.getFieldCount()-2]; // Le quito los dos PKs
799
            int[] joinedLyrFieldTypes = new int[lyrToJoinDS.getFieldCount()+joiningLyrDS.getFieldCount()-2];
800
            int l=0;
801
            for (int i=0;i<lyrToJoinFieldNames.length-1;i++) {
802
                joinedLyrFieldNames[i] = lyrToJoinFieldNames[i];
803
                joinedLyrFieldTypes[i] = lyrToJoinFieldTypes[i];
804
                l++;
805
            }
806
            for (int i=0;i<joiningLyrFieldNames.length-1;i++) {
807
                joinedLyrFieldNames[i+l] = joiningLyrFieldNames[i];
808
                joinedLyrFieldTypes[i+l] = joiningLyrFieldTypes[i];
809
            }
810
            DataSourceFactory dsf = lyrToJoinSDS.getDataSourceFactory();
811
            File file = geoProcessingSpatialjoinPanel.getOutputFile();
812
            dsf.createFileDataSource("gdbms dbf driver", "new_table", file.getAbsolutePath().replaceAll(".shp", ".dbf"), joinedLyrFieldNames, joinedLyrFieldTypes);
813
            DataSource ds = dsf.createRandomDataSource("new_table");
814
            ds.start();
815
            DataWare dw = ds.getDataWare(DataSourceFactory.MANUAL_OPENING);
816
            dw.beginTrans();
817
            ds.stop();
818
            lyrToJoinDS.stop();
819
            joiningLyrDS.stop();
820
            lyrToJoinSDS.stop();
821
            joiningLyrSDS.stop();
822
            
823
            // Recorrer las geometr?as buscando las que intersectan. No porque
824
            // por ejemplo los puntos no intersectan ...
825
            // Ver de que combinaci?n se trata y calcular nearest, part of o inside
826
            // seg?n corresponda.
827
            // Si intersecta copiarle los atributos. O sea rellenar Values como
828
            // toque ...
829
            Value[][] values = new Value[geometriesToJoin.length][joinedLyrFieldNames.length+1]; // +1 porque faltaba el PK
830
            // Movida para el nearest entre puntos
831
            //Point[] joiningPoints = new Point[joiningGeometries.length];
832
            Geometry[] joiningGeometrySet = new Geometry[joiningGeometries.length];
833
            /*for (int i=0;i<joiningGeometries.length;i++) {
834
                joiningPoints[i] = (Point)joiningGeometries[i];
835
            }*/
836
            for (int i=0;i<joiningGeometries.length;i++) {
837
                joiningGeometrySet[i] = (Geometry)joiningGeometries[i];
838
            }
839
            //MultiPoint joiningMultiPoint = new MultiPoint(joiningPoints, new GeometryFactory());
840
            GeometryCollection joiningGeometryCollection = new GeometryCollection(joiningGeometrySet, new GeometryFactory());
841
            for (int i=0;i<geometriesToJoin.length;i++) {
842
                Geometry geometryToJoin = geometriesToJoin[i];
843
                // Movida para el nearest entre puntos
844
                //Coordinate[] coords = DistanceOp.closestPoints(geometryToJoin, joiningMultiPoint);
845
                Coordinate[] coords = DistanceOp.closestPoints(geometryToJoin, joiningGeometryCollection);
846
                for (int j=0;j<joiningGeometries.length;j++) {
847
                    Geometry joiningGeometry = joiningGeometries[j];
848
                    if (geometryToJoin instanceof Point && joiningGeometry instanceof Point) {
849
                        // Rellenar el PK. Lo tengo que hacer antes del break
850
                        values[i][joinedLyrFieldNames.length] = ValueFactory.createValue(i);
851
                        for (int k=0;k<lyrToJoinFieldNames.length-1;k++) {
852
                            values[i][k] = lyrToJoinDS.getFieldValue(i, k);
853
                        }
854
                        if (coords[1].equals2D(joiningGeometry.getCoordinate())) {
855
                            for (int k=0;k<joiningLyrFieldNames.length-1;k++) {
856
                                values[i][k+l] = joiningLyrDS.getFieldValue(j, k);
857
                            }
858
                            break; // En el momento encontremos un pol?gono que contiene a otro le metemos los valores y nos vamos
859
                        } else {
860
                            for (int k=0;k<joiningLyrFieldNames.length-1;k++) {
861
                                values[i][k+l] = ValueFactory.createNullValue();
862
                            }
863
                        }
864
                    } else if (geometryToJoin instanceof Point && joiningGeometry instanceof LineString) {
865
                        // part of not supported yet
866
                    } else if (geometryToJoin instanceof Point && joiningGeometry instanceof Polygon) {
867
                        // Rellenar el PK. Lo tengo que hacer antes del break
868
                        values[i][joinedLyrFieldNames.length] = ValueFactory.createValue(i);
869
                        for (int k=0;k<lyrToJoinFieldNames.length-1;k++) {
870
                            values[i][k] = lyrToJoinDS.getFieldValue(i, k);
871
                        }
872
                        if (joiningGeometry.contains(geometryToJoin)) {
873
                            for (int k=0;k<joiningLyrFieldNames.length-1;k++) {
874
                                System.out.println(i);
875
                                values[i][k+l] = joiningLyrDS.getFieldValue(j, k);
876
                            }
877
                            break; // En el momento encontremos un pol?gono que contiene a otro le metemos los valores y nos vamos
878
                        } else {
879
                            for (int k=0;k<joiningLyrFieldNames.length-1;k++) {
880
                                values[i][k+l] = ValueFactory.createNullValue();
881
                            }
882
                        }
883
                    } else if (geometryToJoin instanceof LineString && joiningGeometry instanceof Point) {
884
                        // Rellenar el PK. Lo tengo que hacer antes del break
885
                        values[i][joinedLyrFieldNames.length] = ValueFactory.createValue(i);
886
                        for (int k=0;k<lyrToJoinFieldNames.length-1;k++) {
887
                            values[i][k] = lyrToJoinDS.getFieldValue(i, k);
888
                        }
889
                        if (coords[1].equals2D(joiningGeometry.getCoordinate())) {
890
                            for (int k=0;k<joiningLyrFieldNames.length-1;k++) {
891
                                values[i][k+l] = joiningLyrDS.getFieldValue(j, k);
892
                            }
893
                            break; // En el momento encontremos un pol?gono que contiene a otro le metemos los valores y nos vamos
894
                        } else {
895
                            for (int k=0;k<joiningLyrFieldNames.length-1;k++) {
896
                                values[i][k+l] = ValueFactory.createNullValue();
897
                            }
898
                        }
899
                    } else if (geometryToJoin instanceof LineString && joiningGeometry instanceof LineString) {
900
                        // Rellenar el PK. Lo tengo que hacer antes del break
901
                        values[i][joinedLyrFieldNames.length] = ValueFactory.createValue(i);
902
                        for (int k=0;k<lyrToJoinFieldNames.length-1;k++) {
903
                            values[i][k] = lyrToJoinDS.getFieldValue(i, k);
904
                        }
905
                        //if (coords[1].equals2D(joiningGeometry.getCoordinate())) {
906
                        if (joiningGeometry.contains(geometryToJoin) || geometryToJoin.contains(joiningGeometry)) {
907
                            for (int k=0;k<joiningLyrFieldNames.length-1;k++) {
908
                                values[i][k+l] = joiningLyrDS.getFieldValue(j, k);
909
                            }
910
                            break; // En el momento encontremos un pol?gono que contiene a otro le metemos los valores y nos vamos
911
                        } else {
912
                            for (int k=0;k<joiningLyrFieldNames.length-1;k++) {
913
                                values[i][k+l] = ValueFactory.createNullValue();
914
                            }
915
                        }
916
                    } else if (geometryToJoin instanceof LineString && joiningGeometry instanceof Polygon) {
917
                        // Rellenar el PK. Lo tengo que hacer antes del break
918
                        values[i][joinedLyrFieldNames.length] = ValueFactory.createValue(i);
919
                        for (int k=0;k<lyrToJoinFieldNames.length-1;k++) {
920
                            values[i][k] = lyrToJoinDS.getFieldValue(i, k);
921
                        }
922
                        if (joiningGeometry.contains(geometryToJoin)) {
923
                            for (int k=0;k<joiningLyrFieldNames.length-1;k++) {
924
                                values[i][k+l] = joiningLyrDS.getFieldValue(j, k);
925
                            }
926
                            break; // En el momento encontremos un pol?gono que contiene a otro le metemos los valores y nos vamos
927
                        } else {
928
                            for (int k=0;k<joiningLyrFieldNames.length-1;k++) {
929
                                values[i][k+l] = ValueFactory.createNullValue();
930
                            }
931
                        }
932
                    } else if (geometryToJoin instanceof Polygon && joiningGeometry instanceof Point) {
933
                        // nearest not supported yet
934
                    } else if (geometryToJoin instanceof Polygon && joiningGeometry instanceof LineString) {
935
                        // nearest not supported yet
936
                    } else if (geometryToJoin instanceof Polygon && joiningGeometry instanceof Polygon) {
937
                        // Rellenar el PK. Lo tengo que hacer antes del break
938
                        values[i][joinedLyrFieldNames.length] = ValueFactory.createValue(i);
939
                        for (int k=0;k<lyrToJoinFieldNames.length-1;k++) {
940
                            values[i][k] = lyrToJoinDS.getFieldValue(i, k);
941
                        }
942
                        if (joiningGeometry.contains(geometryToJoin)) {
943
                            for (int k=0;k<joiningLyrFieldNames.length-1;k++) {
944
                                values[i][k+l] = joiningLyrDS.getFieldValue(j, k);
945
                            }
946
                            break; // En el momento encontremos un pol?gono que contiene a otro le metemos los valores y nos vamos
947
                        } else {
948
                            for (int k=0;k<joiningLyrFieldNames.length-1;k++) {
949
                                values[i][k+l] = ValueFactory.createNullValue();
950
                            }
951
                        }
952
                    } else {
953
                        // MultiPoint, LineSegment, LinearRing, MultiLineString, MultiPolygon faltan
954
                        // por implementar ...
955
                    }
956
                }
957
            }
958
            for (int i=0;i<lyrToJoinSDS.getSelection().cardinality();i++) {
959
                dw.insertFilledRow(values[i]);
960
            }
961
            /*for (int i=0;i<lyrToJoinDS.getRowCount();i++) {
962
                dw.insertFilledRow(values[i]);
963
            }*/
964
            dw.commitTrans();
965
            IGeometry[] shapes = getFmapGeometries(geometriesToJoin);
966
            // Guardo la tabla mediante gdbms. Guardo tambi?n las entidades JTS mediante gvSIG o usando solo gdbms (probablemente bastar? con usar gdbms)
967
            // Guardar las viejas geometr?as.
968
            GeoProcessingPersistenceOperations.saveResultsToShapeFile(shapes, geoProcessingSpatialjoinPanel.getOutputFile());
969
            //lyrToJoinSDS.getSelection().clear();
970
            //joiningLyrSDS.getSelection().clear();
971
            lyrToJoinSDS.setSelection(new FBitSet());
972
            joiningLyrSDS.setSelection(new FBitSet());
973
        } catch (DriverException e) {
974
            // TODO Auto-generated catch block
975
            e.printStackTrace();
976
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
977
            // TODO Auto-generated catch block
978
            e.printStackTrace();
979
        } catch (DriverLoadException e) {
980
            // TODO Auto-generated catch block
981
            e.printStackTrace();
982
        } catch (NoSuchTableException e) {
983
            // TODO Auto-generated catch block
984
            e.printStackTrace();
985
        }
986
    }
987
    // clip: intersect + dissolve
988
    private void clipMethod() {
989
        String layerToClipName = (String)((JComboBox)geoProcessingClipPanel.getComponent(0)).getSelectedItem();
990
        FLayer layerToClip = layers.getLayer(layerToClipName);
991
        boolean selected = false;
992
        if (((JCheckBox)geoProcessingClipPanel.getComponent(1)).isSelected()) selected = true;
993
        Geometry[] geometriesToClip = getJtsGeometries(layerToClip, selected);
994
        String clippingLayerName = (String)((JComboBox)geoProcessingClipPanel.getComponent(3)).getSelectedItem();
995
        FLayer clippingLayer = layers.getLayer(clippingLayerName);
996
        if (((JCheckBox)geoProcessingClipPanel.getComponent(5)).isSelected()) selected = true;
997
        Geometry[] clippingGeometries = getJtsGeometries(clippingLayer, selected);
998
        AlphanumericData lyr = (AlphanumericData)layerToClip;
999
        SelectableDataSource sds;
1000
        try {
1001
            DataSource ds;
1002
            ds = lyr.getRecordset();
1003
            sds = lyr.getRecordset();
1004
            // Tener en cuenta que podemos estar trabajando solo con una selecci?n
1005
            FBitSet fBitSet = sds.getSelection();
1006
            // Clono porque preguntarle al FBitSet si est? lleno es una putada ...
1007
            FBitSet otherFBitSet = (FBitSet)fBitSet.clone();
1008
            if (otherFBitSet.cardinality()==0) otherFBitSet.set(0, (int)sds.getRowCount());
1009
            Value[][] selectedValues = new Value[otherFBitSet.cardinality()][ds.getFieldCount()];
1010
            int l=0;
1011
            for (int i=0;i<ds.getRowCount();i++) {
1012
                if (otherFBitSet.get(i)) {
1013
                    selectedValues[l] = ds.getRow(i);
1014
                    l++;
1015
                }
1016
            }
1017
            String[] fieldNames = new String[sds.getFieldCount()-1];
1018
            int[] fieldTypes = new int[sds.getFieldCount()-1];
1019
            for (int i = 0; i < sds.getFieldCount()-1; i++) {
1020
                fieldNames[i] = sds.getFieldName(i);
1021
                fieldTypes[i] = sds.getFieldType(i);
1022
            }
1023
            DataSourceFactory dsf = sds.getDataSourceFactory();
1024
            File file = geoProcessingClipPanel.getOutputFile();
1025
            dsf.createFileDataSource("gdbms dbf driver", "new_table", file.getAbsolutePath().replaceAll(".shp", ".dbf"), fieldNames, fieldTypes);
1026
            DataSource dataSource = dsf.createRandomDataSource("new_table");
1027
            dataSource.start();
1028
            DataWare dw = dataSource.getDataWare(DataSourceFactory.MANUAL_OPENING);
1029
            dw.beginTrans();
1030
            Vector values = new Vector();
1031
            Vector geometriesClipped = new Vector();
1032
            for (int i=0;i<geometriesToClip.length;i++) {
1033
                Geometry geometryToClip = geometriesToClip[i];
1034
                for (int j=0;j<clippingGeometries.length;j++) {
1035
                    Geometry clippingGeometry = clippingGeometries[j];
1036
                    if (clippingGeometry.intersects(geometryToClip)) {
1037
                        geometriesClipped.add(geometryToClip.intersection(clippingGeometry));
1038
                        Value[] row = new Value[fieldNames.length+1]; // +1 porque faltaba el PK
1039
                        for (int m=0;m<fieldNames.length;m++) {
1040
                            row[m] = selectedValues[i][m];
1041
                        }
1042
                        row[fieldNames.length] = ValueFactory.createValue(i);
1043
                        values.add(row);
1044
                    }
1045
                }
1046
            }
1047
            Vector resultGeometries = new Vector();
1048
            for (int i=0;i<values.size();i++) {
1049
                Value[] rowi = ((Value[])values.get(i));
1050
                Geometry geometryi = ((Geometry)geometriesClipped.get(i));
1051
                for (int j=0;j<values.size();j++) {
1052
                    if (j!=i) {
1053
                        Value[] rowj = ((Value[])values.get(j));
1054
                        boolean areTheSameElement = true;
1055
                        for (int k=0;k<rowj.length;k++) {
1056
                        //for (int k=0;k<rowj.length-1;k++) {
1057
                            Value valuei = rowi[k];
1058
                            Value valuej = rowj[k];
1059
                            if (!((BooleanValue)(valuej.equals(valuei))).getValue()) {
1060
                                areTheSameElement = false;
1061
                                break;
1062
                            }
1063
                        }
1064
                        if (areTheSameElement) {
1065
                            Geometry geometryj = ((Geometry)geometriesClipped.get(j));
1066
                            geometryi = geometryj.union(geometryi);
1067
                        }
1068
                    }
1069
                }
1070
                resultGeometries.add(geometryi);
1071
            }
1072
            for (int i=0;i<resultGeometries.size();i++) {
1073
                for (int j=0;j<resultGeometries.size();j++) {
1074
                    if (j>i) {
1075
                        if (((Geometry)resultGeometries.get(i)).equals((Geometry)resultGeometries.get(j))) {
1076
                            resultGeometries.remove(j);
1077
                            j--;
1078
                        }
1079
                    }
1080
                }
1081
            }
1082
            for (int i=0;i<values.size();i++) {
1083
                Value[] rowi = ((Value[])values.get(i));
1084
                for (int j=0;j<values.size();j++) {
1085
                    if (j>i) {
1086
                        Value[] rowj = ((Value[])values.get(j));
1087
                        boolean areTheSameElement = true;
1088
                        for (int k=0;k<rowj.length;k++) {
1089
                        //for (int k=0;k<rowj.length-1;k++) {
1090
                            Value valuei = rowi[k];
1091
                            Value valuej = rowj[k];
1092
                            if (!((BooleanValue)(valuej.equals(valuei))).getValue()) {
1093
                                areTheSameElement = false;
1094
                            }
1095
                        }
1096
                        if (areTheSameElement) {
1097
                            values.remove(j);
1098
                            j--;
1099
                        }
1100
                    }
1101
                }
1102
            }
1103
            Geometry[] dissolvedGeometries = new Geometry[resultGeometries.size()];
1104
            for (int i=0;i<resultGeometries.size();i++) {
1105
                dissolvedGeometries[i] = (Geometry)resultGeometries.get(i);
1106
            }
1107
            IGeometry[] shapes = getFmapGeometries(dissolvedGeometries);
1108
            Geometry[] clippedGeometries = new Geometry[geometriesClipped.size()];
1109
            for (int i=0;i<geometriesClipped.size();i++) {
1110
                clippedGeometries[i] = (Geometry)geometriesClipped.get(i);
1111
            }
1112
            GeoProcessingPersistenceOperations.saveResultsToShapeFile(shapes, geoProcessingClipPanel.getOutputFile());
1113
            dataSource.stop();
1114
            sds.stop();
1115
            ds.stop();
1116
            for (int i=0;i<values.size();i++) {
1117
                dw.insertFilledRow(((Value[])values.get(i)));
1118
            }
1119
            dw.commitTrans();
1120
            //if (((AlphanumericData)layerToClip).getRecordset().getSelection().nextClearBit(0)==-1 || ((AlphanumericData)layerToClip).getRecordset().getSelection().isEmpty()) ((AlphanumericData)layerToClip).getRecordset().setSelection(new FBitSet());
1121
            //if (((AlphanumericData)layerToClip).getRecordset().getSelection().nextClearBit(0)==-1) ((AlphanumericData)layerToClip).getRecordset().setSelection(new FBitSet());
1122
            //if (((AlphanumericData)clippingLayer).getRecordset().getSelection().nextClearBit(0)==-1) ((AlphanumericData)clippingLayer).getRecordset().setSelection(new FBitSet());
1123
        } catch (DriverException e) {
1124
            // TODO Auto-generated catch block
1125
            e.printStackTrace();
1126
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1127
            // TODO Auto-generated catch block
1128
            e.printStackTrace();
1129
        } catch (DriverLoadException e) {
1130
            // TODO Auto-generated catch block
1131
            e.printStackTrace();
1132
        } catch (NoSuchTableException e) {
1133
            // TODO Auto-generated catch block
1134
            e.printStackTrace();
1135
        } catch (IncompatibleTypesException e) {
1136
            // TODO Auto-generated catch block
1137
            e.printStackTrace();
1138
        }
1139
    }
1140
    // Spatial join con Intersect
1141
    // Funciona bien salvo para el caso de l?neas y puntos, donde habr?a que aumentar
1142
    // la precisi?n del PrecisionModel a costa probablemente de perder velocidad ...
1143
    private void intersectMethod() {
1144
        String layerToIntersectName = (String)((JComboBox)geoProcessingIntersectPanel.getComponent(0)).getSelectedItem();
1145
        FLayer layerToIntersect = layers.getLayer(layerToIntersectName);
1146
        String intersectingLayerName = (String)((JComboBox)geoProcessingIntersectPanel.getComponent(3)).getSelectedItem();
1147
        FLayer intersectingLayer = layers.getLayer(intersectingLayerName);
1148
        boolean selected = false;
1149
        try {
1150
            if (((JCheckBox)geoProcessingIntersectPanel.getComponent(1)).isSelected()) selected = true;
1151
            AlphanumericData lyrToIntersect = (AlphanumericData)layerToIntersect;
1152
            if (((JCheckBox)geoProcessingIntersectPanel.getComponent(5)).isSelected()) selected = true;
1153
            AlphanumericData intersectingLyr = (AlphanumericData)intersectingLayer;
1154
            Geometry[] geometriesToIntersect = getJtsGeometries(layerToIntersect, selected);
1155
            Geometry[] intersectingGeometries = getJtsGeometries(intersectingLayer, selected);
1156
            SelectableDataSource lyrToIntersectSDS = lyrToIntersect.getRecordset();
1157
            SelectableDataSource intersectingLyrSDS = intersectingLyr.getRecordset();
1158
            //DataSource lyrToIntersectDS;
1159
            //DataSource intersectingLyrDS;
1160
            //lyrToIntersectDS = lyrToIntersect.getRecordset();
1161
            //intersectingLyrDS = intersectingLyr.getRecordset();
1162
            // Tener en cuenta que podemos estar trabajando solo con una selecci?n
1163
            FBitSet fBitSet1 = lyrToIntersectSDS.getSelection();
1164
            FBitSet fBitSet2 = intersectingLyrSDS.getSelection();
1165
            // Clono porque preguntarle al FBitSet si est? lleno es una putada ...
1166
            FBitSet otherFBitSet1 = (FBitSet)fBitSet1.clone();
1167
            if (otherFBitSet1.cardinality()==0) otherFBitSet1.set(0, (int)lyrToIntersectSDS.getRowCount());
1168
            Value[][] selectedValues1 = new Value[otherFBitSet1.cardinality()][lyrToIntersectSDS.getFieldCount()];
1169
            int n=0;
1170
            for (int i=0;i<lyrToIntersectSDS.getRowCount();i++) {
1171
                if (otherFBitSet1.get(i)) {
1172
                    selectedValues1[n] = lyrToIntersectSDS.getRow(i);
1173
                    n++;
1174
                }
1175
            }
1176
            // Clono porque preguntarle al FBitSet si est? lleno es una putada ...
1177
            FBitSet otherFBitSet2 = (FBitSet)fBitSet2.clone();
1178
            if (otherFBitSet2.cardinality()==0) otherFBitSet2.set(0, (int)intersectingLyrSDS.getRowCount());
1179
            Value[][] selectedValues2 = new Value[otherFBitSet2.cardinality()][intersectingLyrSDS.getFieldCount()];
1180
            int m=0;
1181
            for (int i=0;i<intersectingLyrSDS.getRowCount();i++) {
1182
                if (otherFBitSet2.get(i)) {
1183
                    selectedValues2[m] = intersectingLyrSDS.getRow(i);
1184
                    m++;
1185
                }
1186
            }
1187
            // Los campos se a?aden siempre. Los valores solo cuando toca ...
1188
            String[] lyrToIntersectFieldNames = new String[lyrToIntersectSDS.getFieldCount()];
1189
            int[] lyrToIntersectFieldTypes = new int[lyrToIntersectSDS.getFieldCount()];
1190
            for (int i = 0; i < lyrToIntersectSDS.getFieldCount(); i++) {
1191
                lyrToIntersectFieldNames[i] = lyrToIntersectSDS.getFieldName(i);
1192
                lyrToIntersectFieldTypes[i] = lyrToIntersectSDS.getFieldType(i);
1193
            }
1194
            String[] intersectingLyrFieldNames = new String[intersectingLyrSDS.getFieldCount()];
1195
            int[] intersectingLyrFieldTypes = new int[intersectingLyrSDS.getFieldCount()];
1196
            for (int i = 0; i < intersectingLyrSDS.getFieldCount(); i++) {
1197
                intersectingLyrFieldNames[i] = intersectingLyrSDS.getFieldName(i);
1198
                intersectingLyrFieldTypes[i] = intersectingLyrSDS.getFieldType(i);
1199
            }
1200
            String[] intersectedLyrFieldNames = new String[lyrToIntersectSDS.getFieldCount()+intersectingLyrSDS.getFieldCount()-2]; // Le quito los dos PKs
1201
            int[] intersectedLyrFieldTypes = new int[lyrToIntersectSDS.getFieldCount()+intersectingLyrSDS.getFieldCount()-2];
1202
            int l=0;
1203
            for (int i=0;i<lyrToIntersectFieldNames.length-1;i++) {
1204
                intersectedLyrFieldNames[i] = lyrToIntersectFieldNames[i];
1205
                intersectedLyrFieldTypes[i] = lyrToIntersectFieldTypes[i];
1206
                l++;
1207
            }
1208
            for (int i=0;i<intersectingLyrFieldNames.length-1;i++) {
1209
                intersectedLyrFieldNames[i+l] = intersectingLyrFieldNames[i];
1210
                intersectedLyrFieldTypes[i+l] = intersectingLyrFieldTypes[i];
1211
            }
1212
            DataSourceFactory dsf = lyrToIntersectSDS.getDataSourceFactory();
1213
            File file = geoProcessingIntersectPanel.getOutputFile();
1214
            dsf.createFileDataSource("gdbms dbf driver", "new_table", file.getAbsolutePath().replaceAll(".shp", ".dbf"), intersectedLyrFieldNames, intersectedLyrFieldTypes);
1215
            DataSource ds = dsf.createRandomDataSource("new_table");
1216
            ds.start();
1217
            DataWare dw = ds.getDataWare(DataSourceFactory.MANUAL_OPENING);
1218
            dw.beginTrans();
1219
            
1220
            Vector values = new Vector();
1221
            Vector geometriesIntersected = new Vector();
1222
            for (int i=0;i<geometriesToIntersect.length;i++) {
1223
                Geometry geometryToIntersect = geometriesToIntersect[i];
1224
                String geometryToIntersectType = geometryToIntersect.getGeometryType();
1225
                System.out.println("geometryToIntersectType = " + geometryToIntersectType);
1226
                for (int j=0;j<intersectingGeometries.length;j++) {
1227
                    Geometry intersectingGeometry = intersectingGeometries[j];
1228
                    if (intersectingGeometry.intersects(geometryToIntersect)) {
1229
                        geometriesIntersected.add(geometryToIntersect.intersection(intersectingGeometry));
1230
                        Value[] rows = new Value[intersectedLyrFieldNames.length+1]; // +1 porque faltaba el PK
1231
                        for (int k=0;k<lyrToIntersectFieldNames.length-1;k++) {
1232
                            //rows[k] = lyrToIntersectDS.getFieldValue(i, k);
1233
                            rows[k] = selectedValues1[i][k];
1234
                        }
1235
                        for (int k=0;k<intersectingLyrFieldNames.length-1;k++) {
1236
                            //rows[k+l] = intersectingLyrDS.getFieldValue(j, k);
1237
                            rows[k+l] = selectedValues2[j][k];
1238
                        }
1239
                        rows[intersectedLyrFieldNames.length] = ValueFactory.createValue(i);
1240
                        values.add(rows);
1241
                    }
1242
                }
1243
            }
1244
            
1245
            ds.stop();
1246
            lyrToIntersectSDS.stop();
1247
            intersectingLyrSDS.stop();
1248
            
1249
            for (int i=0;i<geometriesIntersected.size();i++) {
1250
                dw.insertFilledRow(((Value[])values.get(i)));
1251
            }
1252
            dw.commitTrans();
1253
            Geometry[] geometriesIntersectedArray = new Geometry[geometriesIntersected.size()];
1254
            for (int i=0;i<geometriesIntersected.size();i++) {
1255
                geometriesIntersectedArray[i] = (Geometry)geometriesIntersected.get(i);
1256
            }
1257
            IGeometry[] shapes = getFmapGeometries(geometriesIntersectedArray);
1258
            // Guardo la tabla mediante gdbms. Guardo tambi?n las entidades JTS mediante gvSIG o usando solo gdbms (probablemente bastar? con usar gdbms)
1259
            // Guardar las viejas geometr?as.
1260
            GeoProcessingPersistenceOperations.saveResultsToShapeFile(shapes, geoProcessingIntersectPanel.getOutputFile());
1261
        } catch (DriverException e) {
1262
            // TODO Auto-generated catch block
1263
            e.printStackTrace();
1264
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1265
            // TODO Auto-generated catch block
1266
            e.printStackTrace();
1267
        } catch (DriverLoadException e) {
1268
            // TODO Auto-generated catch block
1269
            e.printStackTrace();
1270
        } catch (NoSuchTableException e) {
1271
            // TODO Auto-generated catch block
1272
            e.printStackTrace();
1273
        }
1274
    }
1275
    private void unionMethod() {
1276
        String layerToUnionName = (String)((JComboBox)geoProcessingUnionPanel.getComponent(0)).getSelectedItem();
1277
        FLayer layerToUnion = layers.getLayer(layerToUnionName);
1278
        boolean selected = false;
1279
        //if (((JCheckBox)geoProcessingUnionPanel.getComponent(1)).isSelected()) selected = true;
1280
        Geometry[] geometriesToUnion = getJtsGeometries(layerToUnion, selected);
1281
        String uningLayerName = (String)((JComboBox)geoProcessingUnionPanel.getComponent(3)).getSelectedItem();
1282
        FLayer uningLayer = layers.getLayer(uningLayerName);
1283
        //if (((JCheckBox)geoProcessingUnionPanel.getComponent(5)).isSelected()) selected = true;
1284
        Geometry[] uningGeometries = getJtsGeometries(uningLayer, selected);
1285
        
1286
        //if (((JCheckBox)geoProcessingIntersectPanel.getComponent(1)).isSelected()) selected = true;
1287
        AlphanumericData lyrToUnion = (AlphanumericData)layerToUnion;
1288
        SelectableDataSource lyrToUnionSDS;
1289
        try {
1290
            lyrToUnionSDS = lyrToUnion.getRecordset();
1291
            //if (((JCheckBox)geoProcessingIntersectPanel.getComponent(5)).isSelected()) selected = true;
1292
            AlphanumericData uningLyr = (AlphanumericData)uningLayer;
1293
            DataSource lyrToUnionDS;
1294
            DataSource uningLyrDS;
1295
            lyrToUnionDS = lyrToUnion.getRecordset();
1296
            uningLyrDS = uningLyr.getRecordset();
1297
            // Los campos se a?aden siempre. Los valores solo cuando toca ...
1298
            String[] lyrToUnionFieldNames = new String[lyrToUnionDS.getFieldCount()];
1299
            int[] lyrToUnionFieldTypes = new int[lyrToUnionDS.getFieldCount()];
1300
            for (int i = 0; i < lyrToUnionDS.getFieldCount(); i++) {
1301
                lyrToUnionFieldNames[i] = lyrToUnionDS.getFieldName(i);
1302
                lyrToUnionFieldTypes[i] = lyrToUnionDS.getFieldType(i);
1303
            }
1304
            String[] uningLyrFieldNames = new String[uningLyrDS.getFieldCount()];
1305
            int[] uningLyrFieldTypes = new int[uningLyrDS.getFieldCount()];
1306
            for (int i = 0; i < uningLyrDS.getFieldCount(); i++) {
1307
                uningLyrFieldNames[i] = uningLyrDS.getFieldName(i);
1308
                uningLyrFieldTypes[i] = uningLyrDS.getFieldType(i);
1309
            }
1310
            String[] unedLyrFieldNames = new String[lyrToUnionDS.getFieldCount()+uningLyrDS.getFieldCount()-2]; // Le quito los dos PKs
1311
            int[] unedLyrFieldTypes = new int[lyrToUnionDS.getFieldCount()+uningLyrDS.getFieldCount()-2];
1312
            int l=0;
1313
            for (int i=0;i<lyrToUnionFieldNames.length-1;i++) {
1314
                unedLyrFieldNames[i] = lyrToUnionFieldNames[i];
1315
                unedLyrFieldTypes[i] = lyrToUnionFieldTypes[i];
1316
                l++;
1317
            }
1318
            for (int i=0;i<uningLyrFieldNames.length-1;i++) {
1319
                unedLyrFieldNames[i+l] = uningLyrFieldNames[i];
1320
                unedLyrFieldTypes[i+l] = uningLyrFieldTypes[i];
1321
            }
1322
            DataSourceFactory dsf = lyrToUnionSDS.getDataSourceFactory();
1323
            File file = geoProcessingUnionPanel.getOutputFile();
1324
            dsf.createFileDataSource("gdbms dbf driver", "new_table", file.getAbsolutePath().replaceAll(".shp", ".dbf"), unedLyrFieldNames, unedLyrFieldTypes);
1325
            DataSource ds = dsf.createRandomDataSource("new_table");
1326
            ds.start();
1327
            DataWare dw = ds.getDataWare(DataSourceFactory.MANUAL_OPENING);
1328
            dw.beginTrans();
1329
            ds.stop();
1330
            lyrToUnionSDS.stop();
1331
            
1332
            Vector values = new Vector();
1333
            
1334
            //Geometry[] unedGeometries = GeoProcessingTopologyOperations.makeUnion(geometriesToUnion, uningGeometries);
1335
            Geometry[] result = new Geometry[geometriesToUnion.length];
1336
            Vector geometriesUned = new Vector();
1337
            //Selectable selectable = (Selectable)layerToUnion;
1338
            //FBitSet fBitSet = new FBitSet();
1339
            // Metememos en geometriesUned las intersecciones.
1340
            for (int i=0;i<geometriesToUnion.length;i++) {
1341
                Geometry geometryToUnion = geometriesToUnion[i];
1342
                for (int j=0;j<uningGeometries.length;j++) {
1343
                    Geometry uningGeometry = uningGeometries[j];
1344
                    if (uningGeometry.intersects(geometryToUnion)) {
1345
                        //fBitSet.set(i);
1346
                        geometriesUned.add(geometryToUnion.intersection(uningGeometry));
1347
                        Value[] intersectRow = new Value[unedLyrFieldNames.length+1];
1348
                        for (int k=0;k<lyrToUnionFieldNames.length-1;k++) {
1349
                            intersectRow[k] = lyrToUnionDS.getFieldValue(i, k);
1350
                        }
1351
                        for (int k=0;k<uningLyrFieldNames.length-1;k++) {
1352
                            intersectRow[k+l] = uningLyrDS.getFieldValue(j, k);
1353
                        }
1354
                        intersectRow[unedLyrFieldNames.length] = ValueFactory.createValue(i);
1355
                        values.add(intersectRow);
1356
                        //geometriesUned.add(geometryToUnion.difference(uningGeometry));
1357
                    }
1358
                }
1359
            }
1360
            // A?ado a geometriesUned2 las geometr?as de geometriesToUnion que no
1361
            // intersectan con uningGeometries.
1362
            /*for (int i=0;i<geometriesToUnion.length;i++) {
1363
                Geometry geometryToUnion = geometriesToUnion[i];
1364
                boolean intersectsWithAnyGeometry = false;
1365
                for (int j=0;j<uningGeometries.length;j++) {
1366
                    Geometry uningGeometry = uningGeometries[j];
1367
                    if (geometryToUnion.intersects(uningGeometry)) intersectsWithAnyGeometry = true;
1368
                }
1369
                if (intersectsWithAnyGeometry) {
1370
                    //Geometry difference = geometryToUnion.difference(geometryUned);
1371
                    //geometriesUned2.add(difference);
1372
                } else {
1373
                    geometriesUned2.add(geometryToUnion);
1374
                    Value[] unionRow = new Value[unedLyrFieldNames.length+1];
1375
                    for (int k=0;k<lyrToUnionFieldNames.length-1;k++) {
1376
                        unionRow[k] = lyrToUnionDS.getFieldValue(i, k);
1377
                    }
1378
                    for (int k=0;k<uningLyrFieldNames.length-1;k++) {
1379
                        unionRow[k+l] = ValueFactory.createNullValue();
1380
                    }
1381
                    unionRow[unedLyrFieldNames.length] = ValueFactory.createValue(i);
1382
                    values.add(unionRow);
1383
                }
1384
            }
1385
            // A?ado a geometriesUned2 las geometr?as de uningGeometries que no
1386
            // intersectan con geometriesToUnion.
1387
            for (int i=0;i<uningGeometries.length;i++) {
1388
                Geometry uningGeometry = uningGeometries[i];
1389
                boolean intersectsWithAnyGeometry = false;
1390
                for (int j=0;j<geometriesToUnion.length;j++) {
1391
                    Geometry geometryToUnion = geometriesToUnion[j];
1392
                    if (uningGeometry.intersects(geometryToUnion)) intersectsWithAnyGeometry = true;
1393
                }
1394
                if (intersectsWithAnyGeometry) {
1395
                    //Geometry difference = jtsUningGeometry.difference(jtsGeometryUned);
1396
                    //jtsGeometriesUned2.add(difference);
1397
                } else {
1398
                    geometriesUned2.add(uningGeometry);
1399
                    Value[] uningRow = new Value[unedLyrFieldNames.length+1];
1400
                    for (int k=0;k<lyrToUnionFieldNames.length-1;k++) {
1401
                        uningRow[k] = ValueFactory.createNullValue();
1402
                    }
1403
                    for (int k=0;k<uningLyrFieldNames.length-1;k++) {
1404
                        uningRow[k+l] = uningLyrDS.getFieldValue(i, k);
1405
                    }
1406
                    uningRow[unedLyrFieldNames.length] = ValueFactory.createValue(i);
1407
                    values.add(uningRow);
1408
                }
1409
            }*/
1410
            Vector geometriesToUnionAux = new Vector();
1411
            for (int i=0;i<geometriesToUnion.length;i++) {
1412
                geometriesToUnionAux.add(geometriesToUnion[i]);
1413
            }
1414
            Vector uningGeometriesAux = new Vector();
1415
            for (int i=0;i<uningGeometries.length;i++) {
1416
                uningGeometriesAux.add(uningGeometries[i]);
1417
            }
1418
            // Al restar una geometr?a consigo misma se producen GeometryCollections que
1419
            // en realidad son un s?ntoma de los errores de redondeo. Ver que hacemos
1420
            // con esto ...
1421
            for (int i=0;i<geometriesToUnionAux.size();i++) {
1422
                for (int j=0;j<geometriesUned.size();j++) {
1423
                    if (((Geometry)geometriesToUnionAux.get(i)).equals((Geometry)geometriesUned.get(j))) {
1424
                    } else {
1425
                        geometriesToUnionAux.set(i,((Geometry)geometriesToUnionAux.get(i)).difference((Geometry)geometriesUned.get(j)));
1426
                    }
1427
                }
1428
            }
1429
            for (int i=0;i<uningGeometriesAux.size();i++) {
1430
                for (int j=0;j<geometriesUned.size();j++) {
1431
                    if (((Geometry)uningGeometriesAux.get(i)).equals((Geometry)geometriesUned.get(j))) {
1432
                    } else {
1433
                        uningGeometriesAux.set(i,((Geometry)uningGeometriesAux.get(i)).difference((Geometry)geometriesUned.get(j)));
1434
                    }
1435
                }
1436
            }
1437
            // Rellenamos geometriesUned2 con estas intersecciones.
1438
            Vector geometriesUned2 = new Vector();
1439
            for (int i=0;i<geometriesUned.size();i++) {
1440
                geometriesUned2.add(geometriesUned.get(i));
1441
            }
1442
            // A?ado a geometriesUned2 las geometr?as de geometriesToUnion menos las
1443
            // intersecciones.
1444
            for (int i=0;i<geometriesToUnionAux.size();i++) {
1445
                geometriesUned2.add(geometriesToUnionAux.get(i));
1446
                Value[] unionRow = new Value[unedLyrFieldNames.length+1];
1447
                for (int k=0;k<lyrToUnionFieldNames.length-1;k++) {
1448
                    unionRow[k] = lyrToUnionDS.getFieldValue(i, k);
1449
                }
1450
                for (int k=0;k<uningLyrFieldNames.length-1;k++) {
1451
                    unionRow[k+l] = ValueFactory.createNullValue();
1452
                }
1453
                unionRow[unedLyrFieldNames.length] = ValueFactory.createValue(i);
1454
                values.add(unionRow);
1455
            }
1456
            for (int i=0;i<uningGeometriesAux.size();i++) {
1457
                geometriesUned2.add(uningGeometriesAux.get(i));
1458
                Value[] uningRow = new Value[unedLyrFieldNames.length+1];
1459
                for (int k=0;k<lyrToUnionFieldNames.length-1;k++) {
1460
                    uningRow[k] = ValueFactory.createNullValue();
1461
                }
1462
                for (int k=0;k<uningLyrFieldNames.length-1;k++) {
1463
                    uningRow[k+l] = uningLyrDS.getFieldValue(i, k);
1464
                }
1465
                uningRow[unedLyrFieldNames.length] = ValueFactory.createValue(i);
1466
                values.add(uningRow);
1467
            }
1468
            // Quito las repetidas que se forman cuando son la misma. C?digo sacado del
1469
            // clip.
1470
            for (int i=0;i<geometriesUned2.size();i++) {
1471
                for (int j=0;j<geometriesUned2.size();j++) {
1472
                    if (j>i) {
1473
                        if (((Geometry)geometriesUned2.get(i)).equals((Geometry)geometriesUned2.get(j))) {
1474
                            geometriesUned2.remove(j);
1475
                            values.remove(j);
1476
                            j--;
1477
                        }
1478
                    }
1479
                }
1480
            }
1481
            // Le quito los cachos que ya tengo a las geometrias que faltan de
1482
            // geometriesToUnion.
1483
            //BitSet bitSet1 = new BitSet();
1484
            //bitSet1.clear();
1485
            /*for (int i=0;i<geometriesUned.size();i++) {
1486
                Geometry geometryUned = (Geometry)geometriesUned.get(i);
1487
                for (int j=0;j<geometriesToUnion.length;j++) {
1488
                    if (geometriesToUnion[j].intersects(geometryUned) && !geometriesToUnion[j].equals(geometryUned)) {
1489
                        geometriesToUnion[j] = geometriesToUnion[j].difference(geometryUned);
1490
                        bitSet1.set(j);
1491
                    }
1492
                }
1493
            }*/
1494
            /*for (int i=0;i<geometriesToUnion.length;i++) {
1495
                //Geometry geometryToUnion = geometriesToUnion[i];
1496
                for (int j=0;j<uningGeometries.length;j++) {
1497
                    // el equals da problemas porque el intersect crea una geometr?a ligeramente distinta
1498
                    if (geometriesToUnion[i].intersects(uningGeometries[j]) && !(geometriesToUnion[i].equals((geometriesToUnion[i].intersection(uningGeometries[j]))))) {
1499
                        // Buscar el cacho grande para utilizarlo en la resta y evitar el pol?gono con un
1500
                        // agujero en el borde
1501
                        /*Geometry difPol = null;
1502
                        for (int k=0;k<uningGeometries.length;k++) {
1503
                            if (uningGeometries[k].intersects(((Geometry)geometriesUned.get(j)))) {
1504
                                difPol = uningGeometries[k];
1505
                                break;
1506
                            }
1507
                        }
1508
                        geometriesToUnion[i] = geometriesToUnion[i].difference(difPol);*/
1509
                        /*geometriesToUnion[i] = geometriesToUnion[i].difference(uningGeometries[j]);
1510
                        bitSet1.set(i);
1511
                    } else {
1512
                        //bitSet1.set(i, false);
1513
                    }
1514
                }
1515
            }*/
1516
            /*for (int i=0;i<geometriesUned.size();i++) {
1517
                Geometry geometryUned = (Geometry)geometriesUned.get(i);
1518
                for (int j=0;j<geometriesToUnion.length;j++) {
1519
                    if (geometriesToUnion[j].intersects(geometryUned) && !geometriesToUnion[j].equals(geometryUned)) {
1520
                        Geometry difPol = null;
1521
                        for (int k=0;k<uningGeometries.length;k++) {
1522
                            if (uningGeometries[k].intersects(geometryUned)) {
1523
                                difPol = uningGeometries[k];
1524
                                break;
1525
                            }
1526
                        }
1527
                        geometriesToUnion[j] = geometriesToUnion[j].difference(difPol);
1528
                        geometriesToUnion[j] = geometriesToUnion[j].difference(geometryUned);
1529
                        bitSet1.set(j);
1530
                    } else {
1531
                        //bitSet1.set(j, false);
1532
                    }
1533
                }
1534
            }*/
1535
            // Le quito los cachos que ya tengo a las geometrias que faltan de
1536
            // uningGeometries.
1537
            //BitSet bitSet2 = new BitSet();
1538
            //bitSet2.clear();
1539
            /*for (int i=0;i<geometriesUned.size();i++) {
1540
                Geometry geometryUned = (Geometry)geometriesUned.get(i);
1541
                for (int j=0;j<uningGeometries.length;j++) {
1542
                    if (uningGeometries[j].intersects(geometryUned) && !uningGeometries[j].equals(geometryUned)) {
1543
                        uningGeometries[j] = uningGeometries[j].difference(geometryUned);
1544
                        bitSet2.set(j);
1545
                    }
1546
                }
1547
            }*/
1548
            /*for (int i=0;i<uningGeometries.length;i++) {
1549
                //Geometry uningGeometry = uningGeometries[i];
1550
                for (int j=0;j<geometriesToUnionAux.length;j++) {
1551
                    if ((uningGeometries[i].intersects(geometriesToUnionAux[j]) && !(uningGeometries[i]).equals((uningGeometries[i].intersection(geometriesToUnionAux[j]))))) {
1552
                        // Buscar el cacho grande para utilizarlo en la resta y evitar el pol?gono con un
1553
                        // agujero en el borde
1554
                        /*Geometry difPol = null;
1555
                        for (int k=0;k<geometriesToUnionAux.length;k++) {
1556
                            if (geometriesToUnionAux[k].intersects(((Geometry)geometriesUned.get(j)))) {
1557
                                difPol = geometriesToUnionAux[k];
1558
                                break;
1559
                            }
1560
                        }
1561
                        uningGeometries[i] = uningGeometries[i].difference(difPol);*/
1562
                        /*uningGeometries[i] = uningGeometries[i].difference(geometriesToUnionAux[j]);
1563
                        bitSet2.set(i);
1564
                    } else {
1565
                        //bitSet2.set(i, false);
1566
                    }
1567
                }
1568
            }*/
1569
            /*for (int i=0;i<geometriesUned.size();i++) {
1570
                Geometry geometryUned = (Geometry)geometriesUned.get(i);
1571
                for (int j=0;j<uningGeometries.length;j++) {
1572
                    if (uningGeometries[j].intersects(geometryUned) && !uningGeometries[j].equals(geometryUned)) {
1573
                        Geometry difPol = null;
1574
                        for (int k=0;k<geometriesToUnionAux.length;k++) {
1575
                            if (geometriesToUnionAux[k].intersects(geometryUned)) {
1576
                                difPol = geometriesToUnionAux[k];
1577
                                break;
1578
                            }
1579
                        }
1580
                        uningGeometries[j] = uningGeometries[j].difference(difPol);
1581
                        uningGeometries[j] = uningGeometries[j].difference(geometryUned);
1582
                        bitSet2.set(j);
1583
                    } else {
1584
                        bitSet2.set(j, false);
1585
                    }
1586
                }
1587
            }*/
1588
            // Ahora que les he quitado los cachos que ya ten?a, le meto los features que
1589
            // faltaban de geometriesToUnion
1590
            /*for (int i=0;i<geometriesToUnion.length;i++) {
1591
                if (bitSet1.get(i)) {
1592
                    geometriesUned2.add(geometriesToUnion[i]);
1593
                    Value[] unionRow = new Value[unedLyrFieldNames.length+1];
1594
                    for (int k=0;k<lyrToUnionFieldNames.length-1;k++) {
1595
                        unionRow[k] = lyrToUnionDS.getFieldValue(i, k);
1596
                    }
1597
                    for (int k=0;k<uningLyrFieldNames.length-1;k++) {
1598
                        unionRow[k+l] = ValueFactory.createNullValue();
1599
                    }
1600
                    unionRow[unedLyrFieldNames.length] = ValueFactory.createValue(i);
1601
                    values.add(unionRow);
1602
                }
1603
            }
1604
            // Ahora que les he quitado los cachos que ya ten?a, le meto los features que
1605
            // faltaban de uningGeometries
1606
            for (int i=0;i<uningGeometries.length;i++) {
1607
                if (bitSet2.get(i)) {
1608
                    geometriesUned2.add(uningGeometries[i]);
1609
                    Value[] uningRow = new Value[unedLyrFieldNames.length+1];
1610
                    for (int k=0;k<lyrToUnionFieldNames.length-1;k++) {
1611
                        uningRow[k] = ValueFactory.createNullValue();
1612
                    }
1613
                    for (int k=0;k<uningLyrFieldNames.length-1;k++) {
1614
                        uningRow[k+l] = uningLyrDS.getFieldValue(i, k);
1615
                    }
1616
                    uningRow[unedLyrFieldNames.length] = ValueFactory.createValue(i);
1617
                    values.add(uningRow);
1618
                }
1619
            }*/
1620
            //selectable.setSelection(fBitSet);
1621
            result = new Geometry[geometriesUned2.size()];
1622
            for (int i=0;i<geometriesUned2.size();i++) {
1623
                result[i] = (Geometry)geometriesUned2.get(i);
1624
            }
1625
            
1626
            for (int i=0;i<result.length;i++) {
1627
                dw.insertFilledRow(((Value[])values.get(i)));
1628
            }
1629
            dw.commitTrans();
1630
            
1631
            IGeometry[] shapes = getFmapGeometries(result);
1632
            // Guardo la tabla mediante gdbms. Guardo tambi?n las entidades JTS mediante gvSIG o usando solo gdbms (probablemente bastar? con usar gdbms)
1633
            GeoProcessingPersistenceOperations.saveResultsToShapeFile(shapes, geoProcessingUnionPanel.getOutputFile());
1634
        } catch (DriverException e) {
1635
            // TODO Auto-generated catch block
1636
            e.printStackTrace();
1637
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1638
            // TODO Auto-generated catch block
1639
            e.printStackTrace();
1640
        } catch (DriverLoadException e) {
1641
            // TODO Auto-generated catch block
1642
            e.printStackTrace();
1643
        } catch (NoSuchTableException e) {
1644
            // TODO Auto-generated catch block
1645
            e.printStackTrace();
1646
        }
1647
    }
1648
    public double[] getBufferDistancesFromTable(FLayer bufferingLayer) {
1649
        AlphanumericData lyr = (AlphanumericData)bufferingLayer;
1650
        DataSource ds;
1651
        double[] bufferDistances = null;
1652
        try {
1653
            ds = lyr.getRecordset();
1654
            int fieldId = ds.getFieldIndexByName(getSelectedField());
1655
            Value[] fieldValues = new Value[(int)ds.getRowCount()];
1656
            SelectableDataSource selectableDataSource = (SelectableDataSource)ds;
1657
            FBitSet fBitSet = selectableDataSource.getSelection();
1658
            if (fBitSet.cardinality()==0) {
1659
                fBitSet.set(0, (int)lyr.getRecordset().getRowCount());
1660
            }
1661
            Value[] valuesSelected = new Value[fBitSet.cardinality()];
1662
            int j = 0;
1663
            for (int i=0;i<ds.getRowCount();i++) {
1664
                boolean isSelected = fBitSet.get(i);
1665
                fieldValues[i] = ds.getFieldValue(i, fieldId);
1666
                if (isSelected) {
1667
                    valuesSelected[j] = fieldValues[i];
1668
                    j++;
1669
                }
1670
            }
1671
            bufferDistances = new double[fBitSet.cardinality()];
1672
            for (int i=0;i<fBitSet.cardinality();i++) {
1673
                bufferDistances[i] = new Double(valuesSelected[i].toString()).doubleValue();
1674
            }
1675
        } catch (DriverException e) {
1676
            // TODO Auto-generated catch block
1677
            e.printStackTrace();
1678
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1679
            // TODO Auto-generated catch block
1680
            e.printStackTrace();
1681
        }
1682
        return bufferDistances;
1683
    }
1684
    private String getSelectedField() {
1685
        return ((String)((JComboBox)geoProcessingBufferPanel.getComponent(6)).getSelectedItem());
1686
    }
1687
    private int getQuadrantSegments() {
1688
        return (Integer.valueOf((String)((JComboBox)geoProcessingBufferPanel.getComponent(11)).getSelectedItem())).intValue();
1689
    }
1690
    private void closeMethod() {
1691
        if (PluginServices.getMainFrame() == null)
1692
            ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
1693
        else
1694
            PluginServices.getMDIManager().closeView(GeoProcessingPanel.this);
1695
    }
1696
   }  //  @jve:decl-index=0:visual-constraint="10,10"