Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / lib3DMap / src / com / iver / ai2 / gvsig3d / cacheservices / VectorCacheService.java @ 18214

History | View | Annotate | Download (9.5 KB)

1
package com.iver.ai2.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.cresques.cts.IProjection;
10
import org.cresques.px.Extent;
11
import org.gvsig.cacheservice.CacheService;
12

    
13
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
14
import com.hardcode.gdbms.engine.data.driver.DriverException;
15
import com.iver.ai2.gvsig3d.gui.FeatureFactory;
16
import com.iver.ai2.gvsig3d.map3d.layers.Layer3DProps;
17
import com.iver.ai2.gvsig3d.simbology3D.Line3DFeature;
18
import com.iver.ai2.gvsig3d.simbology3D.Point3DFeature;
19
import com.iver.ai2.gvsig3d.simbology3D.Polygon3DFeature;
20
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
21
import com.iver.cit.gvsig.fmap.core.FShape;
22
import com.iver.cit.gvsig.fmap.core.IFeature;
23
import com.iver.cit.gvsig.fmap.core.IGeometry;
24
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
25
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
26
import com.iver.cit.gvsig.fmap.layers.FLayer;
27
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
28
import com.iver.cit.gvsig.fmap.rendering.IClassifiedVectorLegend;
29
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
30

    
31
import es.upv.ai2.libjosg.Group;
32
import es.upv.ai2.libjosg.Node;
33
import es.upv.ai2.libjosg.Vec3;
34
import es.upv.ai2.libjosg.Vec4;
35
import es.upv.ai2.libjosg.features.PixelPoint;
36
import es.upv.ai2.libjosg.planets.Planet;
37
import es.upv.ai2.libjosg.planets.PlanetViewer;
38
import es.upv.ai2.libjosg.viewer.IViewerContainer;
39

    
40
public class VectorCacheService extends CacheService {
41

    
42
        private IViewerContainer _canvas3D;
43

    
44
        private PlanetViewer _planetViewer;
45

    
46
        private Planet _planet;
47

    
48
        private Extent _extent;
49

    
50
        private Rectangle2D _lyrExtentRect;
51

    
52
        private FLayer _layer;
53

    
54
        private IProjection _viewProj;
55

    
56
        // vector caching for libJOSG
57
        private Group _layerNode;
58

    
59
        private Node _currentNode;
60

    
61
        private int _currentGeomType;
62

    
63
        private List m_labels;
64

    
65
        private float heigth = 5000;
66

    
67
        private boolean inPixels = true;
68

    
69
        private int fontSize = 14;
70

    
71
        private int n = 0;
72

    
73
        // Create a hash table
74
        Map pointsMap;
75

    
76
        private int option;
77

    
78
        private boolean primera = true;
79

    
80
        // private ISymbol _currentSymbol;
81

    
82
        public VectorCacheService(IViewerContainer canvas3D, Planet planet,
83
                        String name, FLayer layer, IProjection viewProj) {
84
                super(planet.getPlanetName(), name);
85

    
86
                getNames();
87

    
88
                _canvas3D = canvas3D;
89
                _planetViewer = (PlanetViewer) canvas3D.getOSGViewer();
90
                _planet = planet;
91
                _layer = layer;
92
                _viewProj = viewProj;
93
                _layerNode = new Group();
94
                _planetViewer.addSpecialNode(_layerNode);
95

    
96
                int cacheType = CacheService.GLOBAL;
97
                if (planet.getCoordinateSystemType() == Planet.CoordinateSystemType.GEOCENTRIC)
98
                        cacheType += SPHERIC;
99
                else
100
                        cacheType += PLANE;
101
                setCacheType(cacheType);
102

    
103
                try {
104
                        _lyrExtentRect = _layer.getFullExtent();
105
                } catch (ExpansionFileReadException e) {
106
                        // TODO Auto-generated catch block
107
                        e.printStackTrace();
108
                } catch (ReadDriverException e) {
109
                        // TODO Auto-generated catch block
110
                        e.printStackTrace();
111
                }
112

    
113
                _extent = new Extent(_lyrExtentRect);
114

    
115
        }
116

    
117
        // uses a strategy to get all features from layer and their symbols
118
        // and add them to the planet
119

    
120
        public void AddFeaturesToPlanet() {
121

    
122
                try {
123
                        pointsMap = new HashMap();
124

    
125
                        newDraw();
126
                        _layerNode.addChild(FeatureFactory
127
                                        .insertPointS((HashMap) pointsMap));
128
                        getNames();
129
                        _canvas3D.repaint();
130
                } catch (Exception e) {
131
                        e.printStackTrace();
132
                }
133
        }
134

    
135
        public void RefreshFeaturesToPlanet() {
136
                this.DeleteFeaturesToPlanet();
137
                pointsMap = new HashMap();
138
                _layerNode = new Group();
139
                _planetViewer.addSpecialNode(_layerNode);
140
                _currentGeomType = -1;
141

    
142
                try {
143
                        newDraw();
144
                        _layerNode.addChild(FeatureFactory
145
                                        .insertPointS((HashMap) pointsMap));
146
                        getNames();
147
                } catch (Exception e) {
148
                        e.printStackTrace();
149
                }
150
        }
151

    
152
        public void DeleteFeaturesToPlanet() {
153
                _planetViewer.removeSpecialNode(_layerNode);
154
        }
155

    
156
        public void refreshFeaturesToPlanet(boolean visible) {
157
                // _canvas3D.removeSpecialNode(_layerNode);
158
                _layerNode.setEnabledNode(visible);
159
        }
160

    
161
        // FeatureVisitor methods
162

    
163
        
164

    
165
        private void startGeometry(int geomType, ISymbol symb) {
166

    
167
                // TODO: create new node if some properties change in symbol (like point
168
                // size)
169

    
170
                // if ((geomType != _currentGeomType) || ((geomType & FShape.POINT) ==
171
                // FShape.POINT)) {
172
                if (geomType != _currentGeomType) {
173
                        _currentGeomType = geomType;
174

    
175
                        if ((geomType & FShape.Z) == FShape.Z)
176
                                geomType = geomType - FShape.Z;
177

    
178
                        if (((geomType & FShape.POINT) == FShape.POINT)) {
179
                                Group g = new Group();
180
                                PixelPoint point = (PixelPoint) FeatureFactory.insertPointS(
181
                                                new Vec3(0.0, 0.0, 0.0), new Vec4(1.0f, 1.0f, 1.0f,
182
                                                                1.0f), 100);
183
                                _currentNode = point;
184
                                _layerNode.addChild(g);
185
                                g.addChild(point);
186
                        } else if (((geomType & FShape.LINE) == FShape.LINE)) {
187
                                ;
188
                        } else if (((geomType & FShape.POLYGON) == FShape.POLYGON)) {
189
                                ;
190
                        }
191
                        // switch (geomType) {
192
                        //
193
                        // case FShape.POINT:
194
                        // // Point point = (Point) VectorTest.insertPointS(new Vec3(0.0,
195
                        // // 0.0, 0.0), new Vec4(1.0f, 1.0f, 1.0f, 1.0f), 100);
196
                        // Group g = new Group();
197
                        // _currentNode = g;
198
                        // _layerNode.addChild(g);
199
                        // break;
200
                        // case FShape.LINE:
201
                        // // No set up
202
                        // break;
203
                        // case FShape.POLYGON:
204
                        // // No set up
205
                        // break;
206
                        //
207
                        // }
208

    
209
                }
210
        }
211

    
212
        private void getNames() {
213

    
214
                if (_layer instanceof FLyrVect) {
215
                        FLyrVect flyrVect = (FLyrVect) _layer;
216
                        // REVISAR COMO SE GENERAN LOS LABELS CON LOS ITERADORES
217

    
218
                        // createLabels(flyrVect);
219
                        // // if (m_labels != null)
220
                        // if (((VectorialLegend) flyrVect.getLegend()).getLabelField() !=
221
                        // null)
222
                        // if (m_labels.size() > 0)
223
                        // _layerNode.addChild(VectorTest.insertLabels(
224
                        // this.m_labels, fontSize, this.heigth,
225
                        // inPixels, this._planet.getType()));
226
                        //
227
                        // } catch (DriverException e) {
228
                        // // TODO Auto-generated catch block
229
                        // e.printStackTrace();
230
                        // }
231
                }
232

    
233
        }
234

    
235
        public void createLabels(FLyrVect lyrVect) throws DriverException {
236

    
237

    
238

    
239
        }
240

    
241
        public void newDraw() {
242
                FLyrVect fLyrVect = (FLyrVect) _layer;
243
                try {
244

    
245
                        // String[] aux;
246
                        // ArrayList fieldList = new ArrayList();
247
                        // if (fLyrVect.isLabeled()) {
248
                        // aux = fLyrVect.getLabelingStrategy().getUsedFields();
249
                        // aux =
250
                        // fLyrVect.getLabelingStrategy().getPlacementConstraints().getLocationsFor(geom,
251
                        // labelShape, exclusionZone);
252
                        // for (int i = 0; i < aux.length; i++) {
253
                        // fieldList.add(aux[i]);
254
                        // }
255
                        // }
256
                        IVectorLegend legend = (IVectorLegend) fLyrVect.getLegend();
257
                        ArrayList<String> fieldList = new ArrayList<String>();
258

    
259
                        // fields from legendn 
260
                        String[] aux = null;
261

    
262
                        if (legend instanceof IClassifiedVectorLegend) {
263
                                aux = ((IClassifiedVectorLegend) legend)
264
                                                .getClassifyingFieldNames();
265
                                for (int i = 0; i < aux.length; i++) {
266
                                        fieldList.add(aux[i]);
267
                                }
268

    
269
                        }
270
                        FLyrVect lyrVect = null;
271
                        if (_layer.getClass().equals(FLyrVect.class)) {
272
                                 lyrVect = (FLyrVect)_layer;
273
                        }
274
                        if (lyrVect != null) {
275
                                if (lyrVect.getLabelingStrategy() != null
276
                                                && lyrVect.isLabeled()) {
277
                                        aux = lyrVect.getLabelingStrategy().getUsedFields();
278
                                        for (int i = 0; i < aux.length; i++) {
279
                                                fieldList.add(aux[i]);
280
                                        }
281
                                }
282
                        }
283

    
284
//                        IFeatureIterator it = fLyrVect.getSource().getFeatureIterator(
285
//                                        fieldList.toArray(new String[0]), _viewProj);
286
                        IFeatureIterator it = fLyrVect.getSource().getFeatureIterator(
287
                                        fieldList.toArray(new String[fieldList.size()]), _viewProj);
288
                        
289
                        try {
290
                                while (it.hasNext()) {
291
                                        IFeature feat = it.next();
292
                                        ISymbol sym = legend.getSymbolByFeature(feat);
293
                                        IGeometry geom = feat.getGeometry();
294

    
295
                                        int geomType = geom.getGeometryType();
296
                                        // Line Type
297
                                        Layer3DProps props3D = Layer3DProps.getLayer3DProps(_layer);
298
                                        if ((geomType & FShape.LINE) == FShape.LINE) {
299
                                                Line3DFeature l3D = new Line3DFeature(sym, geom);
300
                                                l3D.setPlanet(this._planet);
301
                                                l3D.setHeigth(props3D.getHeigth());
302
                                                this._layerNode = l3D.Draw(this._layerNode);
303
                                        }
304
                                        // Polygon type
305
                                        if ((geomType & FShape.POLYGON) == FShape.POLYGON) {
306
                                                Polygon3DFeature poly3D = new Polygon3DFeature(sym,
307
                                                                geom);
308
                                                poly3D.setPlanet(this._planet);
309
                                                poly3D.setHeigth(props3D.getHeigth());
310
                                                this._layerNode = poly3D.Draw(_layerNode);
311
                                        }
312
                                        // Point type
313
                                        if ((geomType & FShape.POINT) == FShape.POINT) {
314

    
315
                                                Point3DFeature point3D = new Point3DFeature(sym, geom);
316
                                                point3D.setPlanet(this._planet);
317
                                                point3D.setHeigth(props3D.getHeigth());
318
                                                this._layerNode = point3D.Draw(_layerNode);
319
                                        }
320

    
321
                                }
322
                        } catch (ExpansionFileReadException e) {
323
                                // TODO Auto-generated catch block
324
                                e.printStackTrace();
325
                        }
326

    
327
                } catch (ReadDriverException e) {
328
                        // TODO Auto-generated catch block
329
                        e.printStackTrace();
330
                }
331

    
332
        }
333

    
334
        // public void p(){
335
        // Matrix posMat = Matrix.translate(point2);
336
        // Quat quat = new Quat();
337
        // point2.normalize();
338
        // quat.makeRotate(new Vec3(0, 0, 1), point2);
339
        // Matrix rotMat = Matrix.rotate(quat);
340
        // Matrix scaleMat = Matrix.scale(1000000, 1000000, 1000000);
341
        // MatrixTransform matNode = new MatrixTransform();
342
        // matNode.setMatrix(rotMat.prod(scaleMat.prod(posMat)));
343
        // matNode.addChild(_flagNode);
344
        // ((PlanetViewer) _canvas3d.getOSGViewer()).addSpecialNode(matNode);
345
        // }
346

    
347
}