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 / extrusion / DefaultVectorExtrusionPointRenderableLayer.java @ 773

History | View | Annotate | Download (6.68 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.extrusion;
25

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

    
30
import org.apache.commons.lang3.StringUtils;
31
import org.cresques.cts.ICoordTrans;
32

    
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.feature.Feature;
35
import org.gvsig.fmap.dal.feature.FeatureQuery;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.fmap.geom.Geometry;
38
import org.gvsig.fmap.geom.Geometry.TYPES;
39
import org.gvsig.fmap.geom.aggregate.MultiPoint;
40
import org.gvsig.fmap.geom.primitive.Point;
41
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
42
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
43
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.ISimpleMarkerSymbol;
44
import org.gvsig.view3d.vector.lib.api.VectorElevationMode;
45
import org.gvsig.view3d.vector.lib.api.VectorExtrusionLoaderParameters;
46
import org.gvsig.view3d.vector.lib.impl.AbstractRenderableLayer;
47

    
48
import gov.nasa.worldwind.geom.Position;
49
import gov.nasa.worldwind.render.Material;
50
import gov.nasa.worldwind.render.PointPlacemark;
51
import gov.nasa.worldwind.render.PointPlacemarkAttributes;
52
import gov.nasa.worldwind.render.Renderable;
53

    
54
/**
55
 * 
56
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
57
 *
58
 */
59
public class DefaultVectorExtrusionPointRenderableLayer extends AbstractRenderableLayer {
60
    
61
    private PointPlacemarkAttributes markAttributes;
62

    
63
    public DefaultVectorExtrusionPointRenderableLayer(VectorExtrusionLoaderParameters parameters, FeatureStore featureStore,
64
        IVectorLegend legend) {
65
        super(parameters, featureStore, legend);
66
    }
67

    
68
    @Override
69
    protected List<Renderable> getRenderables(Feature feature) {
70

    
71
        double heightFieldValue = 0;
72
        String heightField = ((VectorExtrusionLoaderParameters) parameters).getHeightField();
73
        if (StringUtils.isNoneBlank(heightField)) {
74
            heightFieldValue = feature.getDouble(heightField);
75
        }
76

    
77
        Geometry geom = feature.getDefaultGeometry();
78
        List<Renderable> renderables = new ArrayList<>();
79
        if (geom.getGeometryType().isTypeOf(TYPES.MULTIPOINT)) {
80
            MultiPoint multiPoint = (MultiPoint) geom;
81
            for (int i = 0; i < multiPoint.getPrimitivesNumber(); i++) {
82
                renderables.add(getRenderable(multiPoint.getPointAt(i), heightFieldValue));
83
            }
84
        } else {
85
            renderables.add(getRenderable((Point) geom, heightFieldValue));
86
        }
87
        return renderables;
88
    }
89

    
90
    private PointPlacemark getRenderable(Point point, double heightFieldValue) {
91
        boolean hasZ = point.getDimension() > 2;
92
        double h = 0.0;
93
        
94
        ICoordTrans coordTrans = getCoordTrans(this.parameters.getLayer().getProjection());
95
        if(coordTrans != null) {
96
            point.reProject(coordTrans);
97
        }
98
        
99
        double y = point.getY();
100
        double x = point.getX();
101
        
102
        if(VectorElevationMode.CLAMP_TO_GROUND.equals(parameters.getVectorElevationMode())) {
103
            hasZ = false;
104
        } else {
105
            if(VectorElevationMode.RELATIVE_TO_GROUND.equals(parameters.getVectorElevationMode())) {
106
                hasZ = false;
107
            }
108
            if (parameters.getConstantHeight() != null) {
109
                h = parameters.getConstantHeight();
110
            }
111
        }
112
        h += heightFieldValue;
113
        h *= ((VectorExtrusionLoaderParameters) parameters).getVerticalExaggeration();
114
        PointPlacemark marker;
115
        if (hasZ) {
116
            double z = point.getCoordinateAt(2);
117
            marker = new PointPlacemark(Position.fromDegrees(y, x, z + h));
118
        } else {
119
            Position position = Position.fromDegrees(y, x, h);
120
            marker = new PointPlacemark(position);
121
        }
122
        marker.setAltitudeMode(getAltitudeMode(parameters.getVectorElevationMode()));
123
        marker.setLineEnabled(true);
124
        marker.setAttributes(this.getBasicMarkerAttributes(legend));
125
        return marker;
126
    }
127

    
128
    @Override
129
    protected FeatureQuery getFeatureQuery() throws DataException {
130
        FeatureQuery featureQuery = super.getFeatureQuery();
131
        // Add field height to feature query
132
        String fieldHeight = ((VectorExtrusionLoaderParameters) parameters).getHeightField();
133
        if (StringUtils.isNotBlank(fieldHeight)) {
134
            featureQuery.addAttributeName(fieldHeight);
135
        }
136
        return featureQuery;
137
    }
138
    
139
    private PointPlacemarkAttributes getBasicMarkerAttributes(IVectorLegend legend) {
140

    
141
        if (this.markAttributes != null) {
142
            return this.markAttributes;
143
        }
144

    
145
        double markerSize = 0;
146
        Material fillMaterial = null;
147

    
148
        if (legend == null) {
149
            fillMaterial = new Material(parameters.getDefaultColor());
150
            markerSize = 0.3;
151
        } else {
152
            ISymbol symbol = legend.getDefaultSymbol();
153
            if (symbol instanceof ISimpleMarkerSymbol) {
154
                ISimpleMarkerSymbol markerSymbol = (ISimpleMarkerSymbol) symbol;
155
                Color fillColor = markerSymbol.getColor();
156
                markerSize = markerSymbol.getSize();
157
                fillMaterial = new Material(fillColor);
158
            } else {
159
                fillMaterial = new Material(parameters.getDefaultColor());
160
                markerSize = 4;
161
            }
162
        }
163
        markAttributes = new PointPlacemarkAttributes();
164
        Color color = fillMaterial.getDiffuse();
165
        markAttributes.setImageColor(color);
166
        markAttributes.setLineMaterial(fillMaterial);
167
        markAttributes.setLineWidth(1d);
168
        markAttributes.setUsePointAsDefaultImage(true);
169
        markAttributes.setScale(markerSize);
170
        return markAttributes;
171
    }
172

    
173
    @Override
174
    protected void clearRenderAttributes() {
175
        this.markAttributes = null;
176
    }
177

    
178
}