Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / lib3DMap / src / org / gvsig / gvsig3d / cacheservices / VectorCacheService.java @ 22661

History | View | Annotate | Download (14.5 KB)

1
package org.gvsig.gvsig3d.cacheservices;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8

    
9
import org.apache.log4j.Logger;
10
import org.cresques.cts.IProjection;
11
import org.cresques.px.Extent;
12
import org.gvsig.cacheservice.CacheService;
13
import org.gvsig.gvsig3d.gui.FeatureFactory;
14
import org.gvsig.gvsig3d.labels3D.ILabel3D;
15
import org.gvsig.gvsig3d.labels3D.SimpleLabel3D;
16
import org.gvsig.gvsig3d.simbology3D.Line3DFeature;
17
import org.gvsig.gvsig3d.simbology3D.Point3DFeature;
18
import org.gvsig.gvsig3d.simbology3D.Polygon3DFeature;
19
import org.gvsig.osgvp.Group;
20
import org.gvsig.osgvp.Node;
21
import org.gvsig.osgvp.StateSet;
22
import org.gvsig.osgvp.Vec3;
23
import org.gvsig.osgvp.Vec4;
24
import org.gvsig.osgvp.exceptions.node.NodeException;
25
import org.gvsig.osgvp.features.PixelPoint;
26
import org.gvsig.osgvp.planets.Planet;
27
import org.gvsig.osgvp.planets.PlanetViewer;
28
import org.gvsig.osgvp.viewer.IViewerContainer;
29

    
30
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
31
import com.hardcode.gdbms.engine.values.NumericValue;
32
import com.hardcode.gdbms.engine.values.Value;
33
import com.iver.ai2.gvsig3d.map3d.layers.Layer3DProps;
34
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
35
import com.iver.cit.gvsig.fmap.core.FShape;
36
import com.iver.cit.gvsig.fmap.core.IFeature;
37
import com.iver.cit.gvsig.fmap.core.IGeometry;
38
import com.iver.cit.gvsig.fmap.core.ILabelable;
39
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
40
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
41
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
42
import com.iver.cit.gvsig.fmap.layers.FLayer;
43
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
44
import com.iver.cit.gvsig.fmap.layers.ISpatialDB;
45
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
46
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
47
import com.iver.cit.gvsig.fmap.rendering.IClassifiedVectorLegend;
48
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
49
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.AttrInTableLabelingStrategy;
50
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.ILabelingMethod;
51
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.ILabelingStrategy;
52
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelClass;
53

    
54
public class VectorCacheService extends CacheService {
55

    
56
        private IViewerContainer _canvas3D;
57

    
58
        private PlanetViewer _planetViewer;
59

    
60
        private Planet _planet;
61

    
62
        private Extent _extent;
63

    
64
        private Rectangle2D _lyrExtentRect;
65

    
66
        private FLayer _layer;
67

    
68
        private IProjection _viewProj;
69

    
70
        // vector caching for libJOSG
71
        private Group _layerNode;
72

    
73
        private Node _currentNode;
74

    
75
        private int _currentGeomType;
76

    
77
        private List<ILabel3D> m_labels;
78

    
79
        private float heigth = 5000;
80

    
81
        private boolean inPixels = true;
82

    
83
        private int fontSize = 14;
84

    
85
        private int n = 0;
86

    
87
        // Create a hash table
88
        Map pointsMap;
89

    
90
        private int option;
91

    
92
        private boolean primera = true;
93

    
94
        private static Logger logger = Logger.getLogger(VectorCacheService.class
95
                        .getName());
96

    
97
        // private ISymbol _currentSymbol;
98

    
99
        public VectorCacheService(IViewerContainer canvas3D, Planet planet,
100
                        String name, FLayer layer, IProjection viewProj) {
101
                super(planet.getPlanetName(), name);
102

    
103
                // getNames();
104

    
105
                _canvas3D = canvas3D;
106
                _planetViewer = (PlanetViewer) canvas3D.getOSGViewer();
107
                _planet = planet;
108
                _layer = layer;
109
                _viewProj = viewProj;
110
                _layerNode = new Group();
111
                _layerNode.setNodeName("layer3DVector");
112
                try {
113
                        _planetViewer.addSpecialNode(_layerNode);
114
                } catch (NodeException e1) {
115
                        logger.error("Command: "
116
                                        + "Error adding new child node to the special node.", e1);
117
                }
118

    
119
                int cacheType = CacheService.GLOBAL;
120
                if (planet.getCoordinateSystemType() == Planet.CoordinateSystemType.GEOCENTRIC)
121
                        cacheType += SPHERIC;
122
                else
123
                        cacheType += PLANE;
124
                setCacheType(cacheType);
125

    
126
                try {
127
                        _lyrExtentRect = _layer.getFullExtent();
128
                } catch (ExpansionFileReadException e) {
129
                        // TODO Auto-generated catch block
130
                        e.printStackTrace();
131
                } catch (ReadDriverException e) {
132
                        // TODO Auto-generated catch block
133
                        e.printStackTrace();
134
                }
135

    
136
                _extent = new Extent(_lyrExtentRect);
137

    
138
        }
139

    
140
        // uses a strategy to get all features from layer and their symbols
141
        // and add them to the planet
142

    
143
        public void AddFeaturesToPlanet() {
144

    
145
                try {
146
                        pointsMap = new HashMap();
147

    
148
                        newDraw();
149
                        _layerNode.addChild(FeatureFactory
150
                                        .insertPointS((HashMap) pointsMap));
151
                        getNames();
152
                        _canvas3D.repaint();
153
                } catch (Exception e) {
154
                        e.printStackTrace();
155
                }
156
        }
157

    
158
        public void RefreshFeaturesToPlanet() {
159
                this.DeleteFeaturesToPlanet();
160
                pointsMap = new HashMap();
161
                _layerNode = new Group();
162
                try {
163
                        _planetViewer.addSpecialNode(_layerNode);
164
                } catch (NodeException e1) {
165
                        logger.error("Command: "
166
                                        + "Error adding new child node to the special node.", e1);
167
                }
168
                _currentGeomType = -1;
169

    
170
                try {
171
                        newDraw();
172
                        _layerNode.addChild(FeatureFactory
173
                                        .insertPointS((HashMap) pointsMap));
174
                        getNames();
175
                } catch (Exception e) {
176
                        e.printStackTrace();
177
                }
178
        }
179

    
180
        public void DeleteFeaturesToPlanet() {
181
//                _layerNode.dispose();
182
//                _layerNode = null;
183
//                System.gc();
184
                try {
185
                        _planetViewer.removeSpecialNode(_layerNode);
186
                } catch (NodeException e) {
187
                        logger.error("Command: "
188
                                        + "Error removing new child node to the special node.", e);
189
                }
190
        }
191

    
192
        public void refreshFeaturesToPlanet(boolean visible) {
193
                // _canvas3D.removeSpecialNode(_layerNode);
194
                _layerNode.setEnabledNode(visible);
195
        }
196

    
197
        // FeatureVisitor methods
198

    
199
        private void startGeometry(int geomType, ISymbol symb) {
200

    
201
                // TODO: create new node if some properties change in symbol (like point
202
                // size)
203

    
204
                // if ((geomType != _currentGeomType) || ((geomType & FShape.POINT) ==
205
                // FShape.POINT)) {
206
                if (geomType != _currentGeomType) {
207
                        _currentGeomType = geomType;
208

    
209
                        if ((geomType & FShape.Z) == FShape.Z)
210
                                geomType = geomType - FShape.Z;
211

    
212
                        if (((geomType & FShape.POINT) == FShape.POINT)) {
213
                                Group g = new Group();
214
                                PixelPoint point = (PixelPoint) FeatureFactory.insertPointS(
215
                                                new Vec3(0.0, 0.0, 0.0), new Vec4(1.0f, 1.0f, 1.0f,
216
                                                                1.0f), 100);
217
                                _currentNode = point;
218
                                try {
219
                                        _layerNode.addChild(g);
220
                                } catch (NodeException e) {
221
                                        logger.error("Command: " + "Error adding new child node.",
222
                                                        e);
223
                                }
224
                                try {
225
                                        g.addChild(point);
226
                                } catch (NodeException e) {
227
                                        logger.error("Command: " + "Error adding new child node.",
228
                                                        e);
229
                                }
230
                        } else if (((geomType & FShape.LINE) == FShape.LINE)) {
231
                                ;
232
                        } else if (((geomType & FShape.POLYGON) == FShape.POLYGON)) {
233
                                ;
234
                        }
235
                        // switch (geomType) {
236
                        //
237
                        // case FShape.POINT:
238
                        // // Point point = (Point) VectorTest.insertPointS(new Vec3(0.0,
239
                        // // 0.0, 0.0), new Vec4(1.0f, 1.0f, 1.0f, 1.0f), 100);
240
                        // Group g = new Group();
241
                        // _currentNode = g;
242
                        // _layerNode.addChild(g);
243
                        // break;
244
                        // case FShape.LINE:
245
                        // // No set up
246
                        // break;
247
                        // case FShape.POLYGON:
248
                        // // No set up
249
                        // break;
250
                        //
251
                        // }
252

    
253
                }
254
        }
255

    
256
        private void getNames() {
257

    
258
                createLabels(_layer);
259
                if ((m_labels.size() > 0) && (m_labels != null))
260
                        try {
261
                                _layerNode.addChild(FeatureFactory.insertLabels(this.m_labels,
262
                                                this._planet));
263
                        } catch (NodeException e) {
264
                                logger.error("Command: " + "Error adding new child node.", e);
265
                        }
266

    
267
                // if (_layer instanceof FLyrVect) {
268
                // FLyrVect flyrVect = (FLyrVect) _layer;
269
                // // REVISAR COMO SE GENERAN LOS LABELS CON LOS ITERADORES
270
                // flyrVect.isLabeled();
271
                // if (m_labels != null)
272
                // if (((IVectorLegend) flyrVect.getLegend()).getLabelField() !=
273
                // null)
274
                //                                                
275
                // } catch (DrivierException e) {
276
                // // TODO Auto-generated catch block
277
                // e.printStackTrace();
278
                // }
279
        }
280

    
281
        public void createLabels(FLayer lyrVect) {
282

    
283
                // If this layer had enable the labels, we would need to get its.
284
                if (lyrVect instanceof ILabelable && ((FLyrVect) lyrVect).isLabeled()) {
285
                        // Cleaning the m_label list
286
                        m_labels = new ArrayList<ILabel3D>();
287
                        // Casting the layer to the interface
288
                        ILabelable lyrLable = (ILabelable) lyrVect;
289
                        // Getting the strategy
290
                        AttrInTableLabelingStrategy strategy = (AttrInTableLabelingStrategy) lyrLable
291
                                        .getLabelingStrategy();
292
                        // Getting the fields and Id to get after the values using the
293
                        // recorset.
294
                        try {
295
                                // Getting fields
296
                                // Text
297
                                int textFieldId = strategy.getTextFieldId();
298
                                // String textField = strategy.getTextField();
299

    
300
                                // Height
301
                                String heightField = strategy.getHeightField();
302
                                int heightFieldId = strategy.getHeightFieldId();
303

    
304
                                // Rotation
305
                                String rotationField = strategy.getRotationField();
306
                                int rotationFieldId = strategy.getRotationFieldId();
307

    
308
                                // Unit
309
                                int unit = strategy.getUnit();
310

    
311
                                // Getting the class and the symbol to get all his properties.
312
                                ILabelingMethod method = strategy.getLabelingMethod();
313
                                LabelClass lClass = method.getDefaultLabelClass();
314
                                SimpleTextSymbol sym = (SimpleTextSymbol) lClass
315
                                                .getTextSymbol();
316

    
317
                                // Getting the recordset
318
                                ReadableVectorial source = ((FLyrVect) lyrVect).getSource();
319
                                SelectableDataSource recordSet = source.getRecordset();
320

    
321
                                // Iterate over all the rows.
322
                                for (int i = 0; i < recordSet.getRowCount(); i++) {
323

    
324
                                        // Getting the geometry from the source
325
                                        IGeometry geom = source.getShape(i);
326

    
327
                                        // Getting the row with all the fields relactionship.
328
                                        Value[] vv = recordSet.getRow(i);
329

    
330
                                        Value texto = vv[textFieldId];
331

    
332
                                        NumericValue altura = (NumericValue) vv[heightFieldId];
333

    
334
                                        SimpleLabel3D simpleLabel3DAux = new SimpleLabel3D(geom
335
                                                        .createLabels(0, true)[0]);
336

    
337
                                        Vec3 position = simpleLabel3DAux.getPosition();
338
                                        Vec3 newPosition = new Vec3(position.x(), position.y(),
339
                                                        heigth + 50);
340
                                        simpleLabel3DAux.setPosition(newPosition);
341

    
342
                                        simpleLabel3DAux.setTextField(texto.toString());
343

    
344
                                        Value rotacion;
345
                                        if (rotationField != null)
346
                                                rotacion = vv[rotationFieldId];
347

    
348
                                        simpleLabel3DAux.setUnits(unit);
349

    
350
                                        m_labels.add(simpleLabel3DAux);
351
                                }
352

    
353
                        } catch (ReadDriverException e) {
354
                                e.printStackTrace();
355
                        }
356
                }
357

    
358
        }
359

    
360
        public void newDraw() {
361
                FLyrVect fLyrVect = (FLyrVect) _layer;
362
                try {
363

    
364
                        // String[] aux;
365
                        // ArrayList fieldList = new ArrayList();
366
                        // if (fLyrVect.isLabeled()) {
367
                        // aux = fLyrVect.getLabelingStrategy().getUsedFields();
368
                        // aux =
369
                        // fLyrVect.getLabelingStrategy().getPlacementConstraints().
370
                        // getLocationsFor(geom,
371
                        // labelShape, exclusionZone);
372
                        // for (int i = 0; i < aux.length; i++) {
373
                        // fieldList.add(aux[i]);
374
                        // }
375
                        // }
376
                        IVectorLegend legend = (IVectorLegend) fLyrVect.getLegend();
377
                        ArrayList<String> fieldList = new ArrayList<String>();
378

    
379
                        // fields from legendn
380
                        String[] aux = null;
381

    
382
                        if (legend instanceof IClassifiedVectorLegend) {
383
                                aux = ((IClassifiedVectorLegend) legend)
384
                                                .getClassifyingFieldNames();
385
                                for (int i = 0; i < aux.length; i++) {
386
                                        fieldList.add(aux[i]);
387
                                }
388

    
389
                        }
390

    
391
                        FLyrVect lyrVect = null;
392
                        if (_layer.getClass().equals(FLyrVect.class)) {
393
                                lyrVect = (FLyrVect) _layer;
394
                        }
395
                        if (lyrVect != null) {
396
                                if (lyrVect.isLabeled()) {
397
                                        aux = lyrVect.getLabelingStrategy().getUsedFields();
398
                                        for (int i = 0; i < aux.length; i++) {
399
                                                fieldList.add(aux[i]);
400
                                        }
401
                                }
402
                        }
403
                        ILabelingStrategy labelStrategy = fLyrVect.getLabelingStrategy();
404
                        if (labelStrategy != null) {
405
                                String[] usetFields = labelStrategy.getUsedFields();
406
                        }
407

    
408
                        // IFeatureIterator it = fLyrVect.getSource().getFeatureIterator(
409
                        // fieldList.toArray(new String[0]), _viewProj);
410
                        IFeatureIterator it = fLyrVect.getSource().getFeatureIterator(
411
                                        fieldList.toArray(new String[fieldList.size()]), _viewProj);
412

    
413
                        // Get the iterator over the visible features
414
                        // IFeatureIterator ite = fLyrVect.getSource().getFeatureIterator(
415
                        // viewPort.getAdjustedExtent(),
416
                        // fieldList.toArray(new String[fieldList.size()]),
417
                        // viewPort.getProjection(), true);
418
                        try {
419
                                while (it.hasNext()) {
420
                                        IFeature feat = it.next();
421
                                        ISymbol sym = legend.getSymbolByFeature(feat);
422
                                        IGeometry geom = feat.getGeometry();
423

    
424
                                        ReadableVectorial rv = fLyrVect.getSource();
425
                                        int selectionIndex = -1;
426
                                        if (rv instanceof ISpatialDB) {
427
                                                selectionIndex = ((ISpatialDB) rv)
428
                                                                .getRowIndexByFID(feat);
429
                                        } else {
430
                                                selectionIndex = Integer.parseInt(feat.getID());
431
                                        }
432
                                        if (selectionIndex != -1) {
433
                                                if (fLyrVect.getRecordset().getSelectionSupport()
434
                                                                .isSelected(selectionIndex)) {
435
                                                        sym = sym.getSymbolForSelection();
436
                                                }
437
                                        }
438

    
439
                                        int geomType = geom.getGeometryType();
440
                                        // Line Type
441
                                        Layer3DProps props3D = Layer3DProps.getLayer3DProps(_layer);
442
                                        if ((geomType & FShape.LINE) == FShape.LINE) {
443
                                                Line3DFeature l3D = new Line3DFeature(sym, geom);
444
                                                l3D.setPlanet(this._planet);
445
                                                l3D.setHeigth(props3D.getHeigth());
446
                                                this._layerNode = l3D.Draw(this._layerNode);
447
                                        }
448
                                        // Polygon type
449
                                        if ((geomType & FShape.POLYGON) == FShape.POLYGON) {
450
                                                Polygon3DFeature poly3D = new Polygon3DFeature(sym,
451
                                                                geom);
452
                                                poly3D.setPlanet(this._planet);
453
                                                poly3D.setHeigth(props3D.getHeigth());
454
                                                // StateSet s= new StateSet();
455
                                                // s.setPolygonOffset(1.0f,1.0f);
456
                                                // _layerNode.setStateSet(s);
457
                                                this._layerNode = poly3D.Draw(_layerNode);
458
                                        }
459
                                        // Point type
460
                                        if ((geomType & FShape.POINT) == FShape.POINT) {
461

    
462
                                                Point3DFeature point3D = new Point3DFeature(sym, geom);
463
                                                point3D.setPlanet(this._planet);
464
                                                point3D.setHeigth(props3D.getHeigth());
465
                                                this._layerNode = point3D.Draw(_layerNode);
466
                                        }
467

    
468
                                }
469
                        } catch (ExpansionFileReadException e) {
470
                                // TODO Auto-generated catch block
471
                                e.printStackTrace();
472
                        }
473

    
474
                } catch (ReadDriverException e) {
475
                        // TODO Auto-generated catch block
476
                        e.printStackTrace();
477
                }
478

    
479
        }
480

    
481
        // public void p(){
482
        // Matrix posMat = Matrix.translate(point2);
483
        // Quat quat = new Quat();
484
        // point2.normalize();
485
        // quat.makeRotate(new Vec3(0, 0, 1), point2);
486
        // Matrix rotMat = Matrix.rotate(quat);
487
        // Matrix scaleMat = Matrix.scale(1000000, 1000000, 1000000);
488
        // MatrixTransform matNode = new MatrixTransform();
489
        // matNode.setMatrix(rotMat.prod(scaleMat.prod(posMat)));
490
        // matNode.addChild(_flagNode);
491
        // ((PlanetViewer) _canvas3d.getOSGViewer()).addSpecialNode(matNode);
492
        // }
493

    
494
}