Statistics
| Revision:

gvsig-3d / 2.1 / trunk / org.gvsig.view3d / org.gvsig.view3d.vector / org.gvsig.view3d.vector.lib / org.gvsig.view3d.vector.lib.impl / src / main / java / org / gvsig / view3d / vector / lib / impl / vector / DefaultVectorRenderableLayer.java @ 774

History | View | Annotate | Download (13.3 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2017 gvSIG Association
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.view3d.vector.lib.impl.vector;
25

    
26
import java.awt.Color;
27
import java.util.ArrayList;
28
import java.util.List;
29

    
30
import org.cresques.cts.ICoordTrans;
31

    
32
import org.gvsig.fmap.dal.feature.Feature;
33
import org.gvsig.fmap.dal.feature.FeatureStore;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.Geometry.TYPES;
36
import org.gvsig.fmap.geom.aggregate.MultiLine;
37
import org.gvsig.fmap.geom.aggregate.MultiPoint;
38
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
39
import org.gvsig.fmap.geom.primitive.Curve;
40
import org.gvsig.fmap.geom.primitive.Line;
41
import org.gvsig.fmap.geom.primitive.Point;
42
import org.gvsig.fmap.geom.primitive.Polygon;
43
import org.gvsig.fmap.geom.primitive.Ring;
44
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
45
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
46
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
47
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
48
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.ISimpleMarkerSymbol;
49
import org.gvsig.view3d.vector.lib.api.VectorElevationMode;
50
import org.gvsig.view3d.vector.lib.api.VectorLoaderParameters;
51
import org.gvsig.view3d.vector.lib.impl.AbstractRenderableLayer;
52

    
53
import gov.nasa.worldwind.geom.Position;
54
import gov.nasa.worldwind.render.BasicShapeAttributes;
55
import gov.nasa.worldwind.render.Material;
56
import gov.nasa.worldwind.render.Path;
57
import gov.nasa.worldwind.render.PointPlacemark;
58
import gov.nasa.worldwind.render.PointPlacemarkAttributes;
59
import gov.nasa.worldwind.render.Renderable;
60

    
61
/**
62
 * 
63
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
64
 *
65
 */
66
public class DefaultVectorRenderableLayer extends AbstractRenderableLayer {
67

    
68
    private BasicShapeAttributes polygonShapeAttributes;
69
    private BasicShapeAttributes lineShapeAttributes;
70
    private PointPlacemarkAttributes markAttributes;
71

    
72
    public DefaultVectorRenderableLayer(VectorLoaderParameters parameters, FeatureStore featureStore,
73
        IVectorLegend legend) {
74
        super(parameters, featureStore, legend);
75
    }
76

    
77
    @Override
78
    protected List<Renderable> getRenderables(Feature feature) {
79
        Geometry geom = feature.getDefaultGeometry();
80
        List<Renderable> renderables = new ArrayList<>();
81
        if (geom.getGeometryType().isTypeOf(TYPES.MULTIPOLYGON)) {
82
            MultiPolygon multiPolygon = (MultiPolygon) geom;
83
            for (int i = 0; i < multiPolygon.getPrimitivesNumber(); i++) {
84
                Polygon polygon = (Polygon) multiPolygon.getSurfaceAt(i);
85
                if (polygon.getNumVertices() >= 4) {
86
                    renderables.add(getRenderable(polygon));
87
                }
88
            }
89
        } else if (geom.getGeometryType().isTypeOf(TYPES.POLYGON)) {
90
            Polygon polygon = (Polygon) geom;
91
            if (polygon.getNumVertices() >= 4) {
92
                renderables.add(getRenderable(polygon));
93
            }
94
        } else if (geom.getGeometryType().isTypeOf(TYPES.MULTIPOINT)) {
95
            MultiPoint multiPoint = (MultiPoint) geom;
96
            for (int i = 0; i < multiPoint.getPrimitivesNumber(); i++) {
97
                renderables.add(getRenderable(multiPoint.getPointAt(i)));
98
            }
99
        } else if (geom.getGeometryType().isTypeOf(TYPES.POINT)) {
100
            renderables.add(getRenderable((Point) geom));
101
        } else if (geom.getGeometryType().isTypeOf(TYPES.MULTILINE)) {
102
            MultiLine multiLine = (MultiLine) geom;
103
            for (int i = 0; i < multiLine.getPrimitivesNumber(); i++) {
104
                if (multiLine.getCurveAt(i).getNumVertices() >= 2) {
105
                    renderables.add(getRenderable(multiLine.getCurveAt(i)));
106
                }
107
            }
108
        } else if (geom.getGeometryType().isTypeOf(TYPES.LINE) && ((Line) geom).getNumVertices() >= 2) {
109
            renderables.add(getRenderable((Line) geom));
110
        }
111
        return renderables;
112
    }
113

    
114
    private gov.nasa.worldwind.render.Polygon getRenderable(Polygon polygon) {
115
        gov.nasa.worldwind.render.Polygon renderable = new gov.nasa.worldwind.render.Polygon();
116
        boolean hasZ = polygon.getDimension() > 2;
117
        double h = 0.0;
118

    
119
        if (VectorElevationMode.CLAMP_TO_GROUND.equals(parameters.getVectorElevationMode())) {
120
            hasZ = false;
121
        } else {
122
            if (VectorElevationMode.RELATIVE_TO_GROUND.equals(parameters.getVectorElevationMode())) {
123
                hasZ = false;
124
            }
125
            if (parameters.getConstantHeight() != null) {
126
                h = parameters.getConstantHeight();
127
            }
128
        }
129

    
130
        ICoordTrans coordTrans = getCoordTrans(this.parameters.getLayer().getProjection());
131
        List<Position> verticesList = getVertices(polygon, h, hasZ, coordTrans);
132
        renderable.setOuterBoundary(verticesList);
133

    
134
        int numInteriorRings = polygon.getNumInteriorRings();
135
        for (int i = 0; i < numInteriorRings; i++) {
136
            renderable.addInnerBoundary(getInteriorRing(polygon.getInteriorRing(i), hasZ, h));
137
        }
138
        renderable.setAltitudeMode(getAltitudeMode(this.parameters.getVectorElevationMode()));
139
        renderable.setAttributes(getPolygonBasicShapeAttributes(legend));
140
        return renderable;
141
    }
142

    
143
    private List<Position> getInteriorRing(Ring interiorRing, boolean hasZ, double h) {
144
        int internalNumVertices = interiorRing.getNumVertices();
145
        List<Position> internalVerticesList = new ArrayList<>(internalNumVertices);
146
        for (int j = 0; j < internalNumVertices; j++) {
147
            Point vertex = interiorRing.getVertex(j);
148
            if (hasZ) {
149
                double z = vertex.getCoordinateAt(2);
150
                internalVerticesList.add(Position.fromDegrees(vertex.getY(), vertex.getX(), z + h));
151
            } else {
152
                internalVerticesList.add(Position.fromDegrees(vertex.getY(), vertex.getX(), h));
153
            }
154
        }
155
        return internalVerticesList;
156
    }
157

    
158
    private PointPlacemark getRenderable(Point point) {
159
        boolean hasZ = point.getDimension() > 2;
160
        double h = 0.0;
161

    
162
        ICoordTrans coordTrans = getCoordTrans(this.parameters.getLayer().getProjection());
163
        if (coordTrans != null) {
164
            point.reProject(coordTrans);
165
        }
166

    
167
        double y = point.getY();
168
        double x = point.getX();
169

    
170
        if (VectorElevationMode.CLAMP_TO_GROUND.equals(parameters.getVectorElevationMode())) {
171
            hasZ = false;
172
        } else {
173
            if (VectorElevationMode.RELATIVE_TO_GROUND.equals(parameters.getVectorElevationMode())) {
174
                hasZ = false;
175
            }
176
            if (parameters.getConstantHeight() != null) {
177
                h = parameters.getConstantHeight();
178
            }
179
        }
180

    
181
        PointPlacemark marker;
182
        if (hasZ) {
183
            double z = point.getCoordinateAt(2);
184
            marker = new PointPlacemark(Position.fromDegrees(y, x, z + h));
185
        } else {
186
            Position position = Position.fromDegrees(y, x, h);
187
            marker = new PointPlacemark(position);
188
        }
189
        marker.setAltitudeMode(getAltitudeMode(parameters.getVectorElevationMode()));
190
        marker.setAttributes(getBasicMarkerAttributes(super.legend));
191
        return marker;
192
    }
193

    
194
    private PointPlacemarkAttributes getBasicMarkerAttributes(IVectorLegend legend) {
195

    
196
        if (this.markAttributes != null) {
197
            return this.markAttributes;
198
        }
199

    
200
        double markerSize = 0;
201
        Material fillMaterial = null;
202

    
203
        if (legend == null) {
204
            fillMaterial = new Material(parameters.getDefaultColor());
205
            markerSize = 0.3;
206
        } else {
207
            ISymbol symbol = legend.getDefaultSymbol();
208
            if (symbol instanceof ISimpleMarkerSymbol) {
209
                ISimpleMarkerSymbol markerSymbol = (ISimpleMarkerSymbol) symbol;
210
                Color fillColor = markerSymbol.getColor();
211
                markerSize = markerSymbol.getSize();
212
                fillMaterial = new Material(fillColor);
213
            } else {
214
                Color defaultColor = parameters.getDefaultColor();
215
                if (defaultColor == null) {
216
                    defaultColor = Color.BLACK;
217
                }
218
                fillMaterial = new Material(defaultColor);
219
                markerSize = 4;
220
            }
221
        }
222
        markAttributes = new PointPlacemarkAttributes();
223
        Color color = fillMaterial.getDiffuse();
224
        markAttributes.setImageColor(color);
225
        markAttributes.setLineMaterial(fillMaterial);
226
        markAttributes.setLineWidth(1d);
227
        markAttributes.setUsePointAsDefaultImage(true);
228
        markAttributes.setScale(markerSize);
229
        return markAttributes;
230
    }
231

    
232
    private BasicShapeAttributes getPolygonBasicShapeAttributes(IVectorLegend legend) {
233

    
234
        if (polygonShapeAttributes != null) {
235
            return this.polygonShapeAttributes;
236
        }
237

    
238
        Material fillMaterial = Material.LIGHT_GRAY;
239
        Material outlineMaterial = Material.BLACK;
240
        double fillOpacity = 0.7;
241
        double outlineWidth = 2;
242

    
243
        ISymbol symbol = legend.getDefaultSymbol();
244
        if (symbol instanceof IFillSymbol) {
245
            IFillSymbol fillSymbol = (IFillSymbol) symbol;
246
            Color fillColor = fillSymbol.getFillColor();
247
            int fillAlpha = fillSymbol.getFillAlpha();
248
            ILineSymbol outline = fillSymbol.getOutline();
249
            Color lineColor = outline.getColor();
250
            fillMaterial = new Material(fillColor);
251
            outlineMaterial = new Material(lineColor);
252
            fillOpacity = fillAlpha / 255.0;
253
            outlineWidth = outline.getLineWidth();
254
        } else {
255
            outlineMaterial = new Material(this.parameters.getDefaultColor());
256
            fillMaterial = new Material(this.parameters.getDefaultColor());
257
            fillOpacity = 0.7;
258
            outlineWidth = 2;
259
        }
260
        polygonShapeAttributes = new BasicShapeAttributes();
261
        polygonShapeAttributes.setInteriorMaterial(fillMaterial);
262
        polygonShapeAttributes.setInteriorOpacity(fillOpacity);
263
        polygonShapeAttributes.setOutlineMaterial(outlineMaterial);
264
        polygonShapeAttributes.setOutlineWidth(outlineWidth);
265
        return polygonShapeAttributes;
266
    }
267

    
268
    private Renderable getRenderable(Curve curve) {
269
        boolean hasZ = curve.getDimension() > 2;
270
        double h = 0.0;
271

    
272
        if (VectorElevationMode.CLAMP_TO_GROUND.equals(parameters.getVectorElevationMode())) {
273
            hasZ = false;
274
        } else {
275
            if (VectorElevationMode.RELATIVE_TO_GROUND.equals(parameters.getVectorElevationMode())) {
276
                hasZ = false;
277
            }
278
            if (parameters.getConstantHeight() != null) {
279
                h = parameters.getConstantHeight();
280
            }
281
        }
282

    
283
        ICoordTrans coordTrans = getCoordTrans(this.parameters.getLayer().getProjection());
284
        List<Position> verticesList = getVertices(curve, h, hasZ, coordTrans);
285
        Path path = new Path(verticesList);
286
        path.setAltitudeMode(getAltitudeMode(this.parameters.getVectorElevationMode()));
287
        path.setAttributes(getNormalShapAttributes(legend));
288
        return path;
289
    }
290

    
291
    private BasicShapeAttributes getNormalShapAttributes(IVectorLegend legend) {
292

    
293
        if (this.lineShapeAttributes != null) {
294
            return lineShapeAttributes;
295
        }
296

    
297
        Material outlineMaterial;
298
        double outlineOpacity = 1.0;
299
        double outlineWidth = 3.0;
300

    
301
        ISymbol symbol = legend.getDefaultSymbol();
302
        if (symbol instanceof ILineSymbol) {
303
            ILineSymbol lineSymbol = (ILineSymbol) symbol;
304
            Color lineColor = lineSymbol.getColor();
305
            int lineAlpha = lineSymbol.getAlpha();
306
            outlineMaterial = new Material(lineColor);
307
            outlineOpacity = lineAlpha / 255.0;
308
            outlineWidth = lineSymbol.getLineWidth();
309
        } else {
310
            outlineMaterial = new Material(this.parameters.getDefaultColor());
311
            outlineOpacity = 1.0;
312
            outlineWidth = 3.0;
313
        }
314
        lineShapeAttributes = new BasicShapeAttributes();
315
        lineShapeAttributes.setOutlineMaterial(outlineMaterial);
316
        lineShapeAttributes.setOutlineWidth(outlineWidth);
317
        lineShapeAttributes.setOutlineOpacity(outlineOpacity);
318
        return lineShapeAttributes;
319
    }
320

    
321
    @Override
322
    protected void clearRenderAttributes() {
323
        // Sets normal shape attributes to null to force create it again
324
        this.polygonShapeAttributes = null;
325
        this.lineShapeAttributes = null;
326
        this.markAttributes = null;
327
    }
328
}