Statistics
| Revision:

root / trunk / extensions / extGraph / src / org / gvsig / graph / TinExtension.java @ 23212

History | View | Annotate | Download (12.8 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 Software Colaborativo (www.scolab.es)   development
26
*/
27
 
28
package org.gvsig.graph;
29

    
30
import java.awt.Component;
31
import java.io.File;
32
import java.io.IOException;
33
import java.sql.Types;
34
import java.util.ArrayList;
35
import java.util.Arrays;
36
import java.util.Collection;
37

    
38
import javax.swing.JOptionPane;
39

    
40
import org.gvsig.exceptions.BaseException;
41
import org.gvsig.fmap.algorithm.contouring.ContourCalculator;
42
import org.gvsig.fmap.algorithm.triangulation.OrbisGisTriangulator;
43
import org.gvsig.fmap.algorithm.triangulation.PirolTriangulator;
44
import org.gvsig.fmap.algorithm.triangulation.TIN;
45
import org.gvsig.fmap.algorithm.triangulation.Triangle;
46
import org.gvsig.fmap.algorithm.triangulation.Vertex;
47
import org.gvsig.fmap.algorithm.triangulation.WatsonTriangulator;
48
import org.gvsig.graph.core.GraphException;
49
import org.gvsig.graph.core.NetworkUtils;
50
import org.gvsig.gui.beans.swing.JFileChooser;
51

    
52
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
53
import com.hardcode.gdbms.engine.values.NumericValue;
54
import com.hardcode.gdbms.engine.values.Value;
55
import com.hardcode.gdbms.engine.values.ValueFactory;
56
import com.iver.andami.PluginServices;
57
import com.iver.andami.plugins.Extension;
58
import com.iver.andami.ui.mdiManager.IWindow;
59
import com.iver.cit.gvsig.fmap.MapContext;
60
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
61
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D;
62
import com.iver.cit.gvsig.fmap.core.FPoint2D;
63
import com.iver.cit.gvsig.fmap.core.FShape;
64
import com.iver.cit.gvsig.fmap.core.IFeature;
65
import com.iver.cit.gvsig.fmap.core.IGeometry;
66
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
67
import com.iver.cit.gvsig.fmap.drivers.ConcreteMemoryDriver;
68
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
69
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
70
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
71
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
72
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
73
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
74
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
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.FLyrVect;
78
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
79
import com.iver.cit.gvsig.fmap.layers.LayersIterator;
80
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
81
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
82
import com.iver.cit.gvsig.project.documents.view.gui.View;
83
import com.iver.utiles.GenericFileFilter;
84
import com.vividsolutions.jts.geom.CoordinateList;
85
import com.vividsolutions.jts.geom.Geometry;
86
import com.vividsolutions.jts.geom.GeometryFactory;
87
import com.vividsolutions.jts.geom.LineString;
88
import com.vividsolutions.jts.geom.LinearRing;
89
import com.vividsolutions.jts.geom.Polygon;
90
import com.vividsolutions.jts.operation.polygonize.Polygonizer;
91

    
92
public class TinExtension extends Extension {
93
        
94
        private static String     lastPath        = null;
95
        private PluginServices ps=PluginServices.getPluginServices(this);
96

    
97
        private ArrayList<FLyrVect> pointLayers = null;
98
        
99
        public void execute(String actionCommand) {
100
                for (FLyrVect lyr : pointLayers) {
101
                        try {
102
                                doTIN(lyr);
103
                        } catch (BaseException e) {
104
                                // TODO Auto-generated catch block
105
                                e.printStackTrace();
106
                        }
107
                }
108

    
109
        }
110
        
111
        private void doTIN(FLyrVect lyrVect) throws BaseException {
112
                ReadableVectorial rv = lyrVect.getSource();
113
                
114
                JFileChooser fileChooser = new JFileChooser("OPEN_LAYER_FILE_CHOOSER_ID", lastPath);
115
                fileChooser.setFileFilter(new GenericFileFilter(".shp", "Shape files", true));
116
                fileChooser.setMultiSelectionEnabled(false);
117
                fileChooser.setDialogTitle(ps.getText("choose_file_to_save_triangles"));
118
                int result = fileChooser.showSaveDialog((Component) PluginServices.getMDIManager().getActiveWindow());
119
                if (result == JFileChooser.CANCEL_OPTION)
120
                        return;
121
                File triShpFile = fileChooser.getSelectedFile();
122
                try {
123
                        if (!triShpFile.getCanonicalPath().toLowerCase().endsWith(".shp"))
124
                                triShpFile = new File(triShpFile.getAbsolutePath() + ".shp");
125
                } catch (IOException e) {
126
                        e.printStackTrace();
127
                        throw new GraphException(e);
128
                }
129

    
130
//                fileChooser.setDialogTitle(PluginServices.getPluginServices(TinExtension.class)
131
//                                .getText("choose_file_to_save_contours"));
132
//                result = fileChooser.showSaveDialog((Component) PluginServices.getMDIManager());
133
//                if (result == JFileChooser.CANCEL_OPTION)
134
//                        return;
135
//                File contourShpFile = fileChooser.getSelectedFile();
136
                SelectableDataSource rs = lyrVect.getRecordset();
137
                String[] selecValues = rs.getFieldNames();
138
                String strField = (String) JOptionPane.showInputDialog(null, "choose_field", "choose_field", JOptionPane.QUESTION_MESSAGE, null, selecValues, null);
139
                
140
                int idField = rs.getFieldIndexByName(strField);
141
                String aux = JOptionPane.showInputDialog("input_contour_desired_heights_separated_by_commas (;)");
142
                if (aux == null)
143
                        return;
144
                double[] heights = NetworkUtils.string2doubleArray(aux, ";");
145
                Arrays.sort(heights);
146

    
147
                ShpWriter triShpWriter = createShapeTriangles(triShpFile);
148
//                ShpWriter contourShpWriter = createShapeContours(triShpFile);
149
                
150
                long t1 = System.currentTimeMillis();
151
//                WatsonTriangulator triangulator = new WatsonTriangulator(); // [1]
152
//                ChewTriangulator triangulator = new ChewTriangulator();
153
//                PirolTriangulator triangulator = new PirolTriangulator();
154
                OrbisGisTriangulator triangulator = new OrbisGisTriangulator();
155
                rv.start();
156
                for (int i=0; i < rv.getShapeCount(); i++) {
157
                        IFeature feat = rv.getFeature(i);
158
                        IGeometry geom = feat.getGeometry();
159
                        Object shp = geom.getInternalShape();
160
                        if (shp instanceof FPoint2D)
161
                        {
162
                                FPoint2D p = (FPoint2D) geom.getInternalShape();
163
                                NumericValue val = (NumericValue) feat.getAttribute(idField);
164
                                triangulator.addVertex(new Vertex(p.getX(), p.getY(), val.doubleValue()));
165
                        }
166
                        if (shp instanceof FMultiPoint2D)
167
                        {
168
                                FMultiPoint2D multi = (FMultiPoint2D) shp;
169
                                for (int j=0; j < multi.getNumPoints(); j++) {
170
                                        FPoint2D p = multi.getPoint(j);
171
                                        NumericValue val = (NumericValue) feat.getAttribute(idField);
172
                                        triangulator.addVertex(new Vertex(p.getX(), p.getY(), val.doubleValue()));
173
                                }
174
                        }
175
                        
176
                }
177
                rv.stop();
178
                
179
                TIN tin = triangulator.calculateTriangulation();
180
                
181
                
182
                GeometryFactory geomFact = new GeometryFactory();
183
                
184
                View view = (View) PluginServices.getMDIManager().getActiveWindow();
185
                MapContext mapContext = view.getMapControl().getMapContext();
186
//                GraphicLayer graphicLayer = view.getMapControl().getMapContext().getGraphicsLayer();
187
//                FSymbol symbol = new FSymbol(FShape.LINE);
188
//                int idSym = graphicLayer.addSymbol(symbol);
189
                
190
                ConcreteMemoryDriver memDriver = new ConcreteMemoryDriver();
191
                ArrayList<String> arrayFields = new ArrayList<String>();
192
                arrayFields.add("ID");
193
                arrayFields.add("VALUE");
194

    
195
                memDriver.getTableModel().setColumnIdentifiers(arrayFields.toArray());
196

    
197
//                memDriver.getf
198
                memDriver.setShapeType(FShape.LINE);
199
                
200
                int i = 0;
201
                ContourCalculator contourCalculator = new ContourCalculator(tin);
202
                for (int indexContour = 0; indexContour < heights.length; indexContour++) {                        
203
                        double height = heights[indexContour];
204
                        Collection<LineString> contour = contourCalculator.getContour(height);
205

    
206
                        IGeometry geom = null;
207
//                        Geometry seqGeom = contourCalculator.getContourPolygon(contour);
208
//                        geom = FConverter.jts_to_igeometry(seqGeom);
209
                        Value[] row = new Value[2];
210
//                        row[0] = ValueFactory.createValue(i++);
211
//                        row[1] = ValueFactory.createValue(height);
212
//                        memDriver.addGeometry(geom, row);
213
                        
214
                        Polygonizer pol = new Polygonizer();
215
                        pol.add(contour);
216
                        Collection<Polygon> polygons = pol.getPolygons();
217
                        
218
                        for (Polygon p : polygons) {                                
219
                                geom = FConverter.jts_to_igeometry(p);
220
                                row = new Value[2];
221
                                row[0] = ValueFactory.createValue(i++);
222
                                row[1] = ValueFactory.createValue(height);
223
                                memDriver.addGeometry(geom, row);
224
                                
225
        //                        FGraphic graf = new FGraphic(geom, idSym);
226
        //                        graphicLayer.addGraphic(graf);
227
                        }
228
                } // indexContour
229
                FLayer lyrContour = LayerFactory.createLayer("Contours", memDriver, mapContext.getProjection());
230
                mapContext.getLayers().addLayer(lyrContour);
231
                
232
                Value[] att = new Value[1];
233
                i=0;
234
                triShpWriter.preProcess();
235
                for (Triangle tri: tin.getTriangles()) {
236
                        CoordinateList auxC = new CoordinateList();
237
                        auxC.add(tri.getV1(), true);
238
                        auxC.add(tri.getV2(), true);
239
                        auxC.add(tri.getV3(), true);
240
                        auxC.add(tri.getV1(), true);
241
                        LinearRing ring = geomFact.createLinearRing(auxC.toCoordinateArray());
242
                        IGeometry geom = FConverter.jts_to_igeometry(ring);
243
//                        FGraphic graf = new FGraphic(geom, idSym);
244
//                        graphicLayer.addGraphic(graf);
245
                        att[0] = ValueFactory.createValue(i);
246
                        DefaultFeature feat = new DefaultFeature(geom, att, i + "");
247

    
248
                        triShpWriter.process(new DefaultRowEdited(feat, IRowEdited.STATUS_ADDED, i++));
249
                        
250
                        
251
                }
252
                triShpWriter.postProcess();
253
                
254
                
255
                VectorialFileDriver shpDriver = new IndexedShpDriver();
256
                FLyrVect lyrTri = (FLyrVect) LayerFactory.createLayer("Triangles", shpDriver, triShpFile, mapContext.getProjection());
257
                mapContext.getLayers().addLayer(lyrTri);
258
                
259

    
260
        }
261
        
262
        private ShpWriter createShapeTriangles(File file) throws BaseException {
263
        
264
                
265
                FieldDescription[] fieldsDescrip = new FieldDescription[1];
266
                
267
                FieldDescription f1 = new FieldDescription();
268
                f1.setFieldName("Id");
269
                f1.setFieldType(Types.INTEGER);
270
                f1.setFieldLength(8);
271
                f1.setFieldDecimalCount(0);
272
                fieldsDescrip[0] = f1;
273

    
274
                SHPLayerDefinition lyrDefPolygon = new SHPLayerDefinition();
275
                lyrDefPolygon.setFieldsDesc(fieldsDescrip);
276

    
277
                lyrDefPolygon.setFile(file);
278
                lyrDefPolygon.setName(file.getName());
279
                lyrDefPolygon.setShapeType(FShape.LINE | FShape.Z);
280
                ShpWriter writer = new ShpWriter();
281
                writer.setFile(file);
282
                writer.initialize(lyrDefPolygon);
283
                writer.preProcess();
284
                writer.postProcess();
285
                return writer;
286
        }
287

    
288
        private ShpWriter createShapeContours(File file) throws BaseException {
289
        
290
                
291
                FieldDescription[] fieldsDescrip = new FieldDescription[2];
292
                
293
                FieldDescription f1 = new FieldDescription();
294
                f1.setFieldName("Id");
295
                f1.setFieldType(Types.INTEGER);
296
                f1.setFieldLength(8);
297
                f1.setFieldDecimalCount(0);
298
                fieldsDescrip[0] = f1;
299

    
300
                FieldDescription f5 = new FieldDescription();
301
                f5.setFieldName("Height");
302
                f5.setFieldType(Types.DOUBLE);
303
                f5.setFieldDecimalCount(3);
304
                f5.setFieldLength(16);
305
                fieldsDescrip[1] = f5;
306

    
307

    
308
        
309
                SHPLayerDefinition lyrDef = new SHPLayerDefinition();
310
                lyrDef.setFieldsDesc(fieldsDescrip);
311

    
312
                lyrDef.setFile(file);
313
                lyrDef.setName(file.getName());
314
                lyrDef.setShapeType(FShape.LINE);
315
                ShpWriter writer = new ShpWriter();
316
                writer.setFile(file);
317
                writer.initialize(lyrDef);
318
                writer.preProcess();
319
                writer.postProcess();
320
                return writer;
321
        }
322

    
323

    
324
        public void initialize() {
325
                // TODO Auto-generated method stub
326

    
327
        }
328

    
329
        public boolean isEnabled() {
330
                IWindow wnd = PluginServices.getMDIManager().getActiveWindow();
331
                View view = (View) wnd;
332
                FLayers lyrs = view.getMapControl().getMapContext().getLayers();
333
                LayersIterator it = new LayersIterator(lyrs) {
334
                        public boolean evaluate(FLayer layer) {
335
                                return layer.isActive();
336
                        }
337
                };
338
                pointLayers = new ArrayList<FLyrVect>();
339
                while (it.hasNext()) {
340
                        FLayer lyr = it.nextLayer();
341
                        if (lyr instanceof FLyrVect) {
342
                                FLyrVect lyrVect = (FLyrVect) lyr;
343
                                try {
344
                                        if ((lyrVect.getShapeType() == FShape.POINT) || (lyrVect.getShapeType() == FShape.MULTIPOINT)) {
345
                                                pointLayers.add(lyrVect);
346
                                        }
347
                                } catch (ReadDriverException e) {
348
                                        // TODO Auto-generated catch block
349
                                        e.printStackTrace();
350
                                }
351
                        }
352
                }
353
                if (pointLayers.size() >= 1)
354
                        return true;
355
                return false;
356
        }
357

    
358
        public boolean isVisible() {
359
                IWindow wnd = PluginServices.getMDIManager().getActiveWindow();
360
                if (wnd instanceof View)
361
                        return true;
362
                return false;
363
        }
364

    
365
}
366