Statistics
| Revision:

gvsig-sldtools / org.gvsig.sld / org.gvsig.sldconverter / org.gvsig.sldconverter.lib / org.gvsig.sldconverter.lib.impl / src / main / java / org / gvsig / sldconverter / impl / symbol / PointSymbolUtils.java @ 46

History | View | Annotate | Download (10.7 KB)

1
/*******************************************************************************
2
 *
3
 *   gvSIG. Desktop Geographic Information System.
4
 *  
5
 *   Copyright (C) 2007-2013 gvSIG Association.
6
 *  
7
 *   This program is free software; you can redistribute it and/or
8
 *   modify it under the terms of the GNU General Public License
9
 *   as published by the Free Software Foundation; either version 3
10
 *   of the License, or (at your option) any later version.
11
 *  
12
 *   This program is distributed in the hope that it will be useful,
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *   GNU General Public License for more details.
16
 *  
17
 *   You should have received a copy of the GNU General Public License
18
 *   along with this program; if not, write to the Free Software
19
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
 *   MA  02110-1301, USA.
21
 *  
22
 *   For any additional information, do not hesitate to contact us
23
 *   at info AT gvsig.com, or visit our website www.gvsig.com.
24
 *   
25
 *******************************************************************************/
26
package org.gvsig.sldconverter.impl.symbol;
27

    
28
import java.awt.Color;
29
import java.net.URL;
30
import java.util.List;
31

    
32
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
34
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
35
import org.gvsig.sldconverter.exception.UnsupportedSymbolException;
36
import org.gvsig.sldconverter.impl.util.BasicUtils;
37
import org.gvsig.sldsupport.exception.UnsupportedSLDObjectException;
38
import org.gvsig.sldsupport.sld.filter.expression.operator.SLDLiteral;
39
import org.gvsig.sldsupport.sld.graphic.SLDExternalGraphic;
40
import org.gvsig.sldsupport.sld.graphic.SLDGraphic;
41
import org.gvsig.sldsupport.sld.graphic.SLDGraphicStackElement;
42
import org.gvsig.sldsupport.sld.graphic.SLDMark;
43
import org.gvsig.sldsupport.sld.symbol.SLDPointSymbol;
44
import org.gvsig.sldsupport.sld.symbol.misc.SLDFill;
45
import org.gvsig.sldsupport.sld.symbol.misc.SLDParameterValue;
46
import org.gvsig.sldsupport.sld.symbol.misc.SLDStroke;
47
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
48
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IPictureMarkerSymbol;
49
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.ISimpleMarkerSymbol;
50

    
51
public class PointSymbolUtils {
52
        
53
        public static ISymbol toMarkerSymbol(SLDPointSymbol sym)
54
                        throws UnsupportedSLDObjectException {
55
                
56
                SLDGraphic gra = sym.getGraphic();
57
                List<SLDGraphicStackElement> list = gra.getElementStack();
58
                if (list.size() == 1) {
59
                        
60
                        Integer alpha = BasicUtils.getAlphaValue(gra.getOpacity());
61
                        Double sz = BasicUtils.getDouble(gra.getSize());
62
                        IMarkerSymbol msym = toMarkerSymbol(list.get(0));
63
                        if (alpha != null) {
64
                                msym.setAlpha(alpha.intValue());
65
                        }
66
                        if (sz != null) {
67
                                msym.setSize(sz.doubleValue());
68
                        }
69
                        return msym;
70
                } else {
71

    
72
                        IMultiLayerSymbol resp = BasicUtils.syMan().createMultiLayerSymbol(
73
                                        Geometry.TYPES.POINT);
74
                        for (int i=0; i<list.size(); i++) {
75
                                resp.addLayer(toMarkerSymbol(list.get(i)));
76
                        }
77
                        return resp;
78
                }
79
        }
80
        
81
        
82
        private static IMarkerSymbol toMarkerSymbol(SLDExternalGraphic extgra)
83
                        throws UnsupportedSLDObjectException {
84

    
85
                if (extgra.isOnlineResource()) {
86
                        URL u = null;
87
                        IPictureMarkerSymbol pms = null;
88
                        try {
89
                                u = new URL(extgra.getOnlineResource()); 
90
                                pms = BasicUtils.symMan().createPictureMarkerSymbol(u,u);
91
                        } catch (Exception exc) {
92
                                throw new UnsupportedSLDObjectException(exc,
93
                                                "SLDExternalGraphic", "Creating picture marker symbol from URL");
94
                        }
95
                        return pms;
96
                } else {
97
                        // TODO Not supported yet
98
                        throw new UnsupportedSLDObjectException(
99
                                        "SLDExternalGraphic",
100
                                        "Inline content not supported.");
101
                }
102
        }
103

    
104
        private static IMarkerSymbol toMarkerSymbol(SLDGraphicStackElement elem)
105
                        throws UnsupportedSLDObjectException {
106
                
107
                if (elem == null) {
108
                        throw new UnsupportedSLDObjectException("SLDGraphicStackElement", "Null");
109
                }
110
                
111
                if (elem instanceof SLDExternalGraphic) {
112
                        return toMarkerSymbol((SLDExternalGraphic) elem);
113
                } else {
114
                        if (elem instanceof SLDMark) {
115
                                return toMarkerSymbol((SLDMark) elem);
116
                        } else {
117
                                throw new UnsupportedSLDObjectException(
118
                                                "SLDGraphicStackElement",
119
                                                "Unexpected class: " + elem.getClass().getName());
120
                        }
121
                }
122
        }
123
        
124
        
125
        private static IMarkerSymbol toMarkerSymbol(SLDMark mk)
126
                        throws UnsupportedSLDObjectException {
127

    
128
                switch (mk.getMarkType()) {
129
                case SLDMark.MARK_TYPE_INLINE_CONTENT:
130
                        throw new UnsupportedSLDObjectException(
131
                                        "SLDMark",
132
                                        "Inline content not supported.");
133
                        // ============================================
134
                case SLDMark.MARK_TYPE_ONLINE_RESOURCE:
135
                        URL u = null;
136
                        IPictureMarkerSymbol pms = null;
137
                        try {
138
                                u = new URL(mk.getOnlineResource()); 
139
                                pms = BasicUtils.symMan().createPictureMarkerSymbol(u,u);
140
                        } catch (Exception exc) {
141
                                throw new UnsupportedSLDObjectException(exc, "SLDMark",
142
                                                "Creating picture marker symbol from URL");
143
                        }
144
                        return pms;                        
145
                        // ============================================
146
                case SLDMark.MARK_TYPE_WELL_KNOWN_NAME:
147
                        String wkn = mk.getWellKnownName();
148
                        int t = getMarkerSymbolType(wkn);
149
                        ISimpleMarkerSymbol resp = BasicUtils.symMan().createSimpleMarkerSymbol();
150
                        resp.setStyle(t);
151
                        
152
                        SLDFill fill = mk.getFill();
153
                        Color co = null;
154
                        Double dob = null;
155
                        if (fill != null) {
156
                                co = BasicUtils.toColor(fill.getFillColor());
157
                                if (co != null) {
158
                                        resp.setColor(co);
159
                                }
160
                        }
161
                        SLDStroke stro = mk.getStroke();
162
                        if (stro != null) {
163
                                co = BasicUtils.toColor(stro.getColor());
164
                                if (co != null) {
165
                                        resp.setOutlineColor(co);
166
                                }
167
                                dob = BasicUtils.toDouble(stro.getWidth());
168
                                if (dob != null) {
169
                                        resp.setOutlineSize(dob.doubleValue());
170
                                }
171
                        }
172
                        return resp;
173
                        // ============================================
174
                default:
175
                        IMarkerSymbol ms = BasicUtils.symMan().createSimpleMarkerSymbol();
176
                        SLDStroke str = mk.getStroke();
177
                        Color col = BasicUtils.getColor(str.getColor());
178
                        if (col != null) {
179
                                ms.setColor(col);
180
                        }
181
                        Double sz = BasicUtils.toDouble(str.getWidth());
182
                        if (sz != null) {
183
                                ms.setSize(sz.doubleValue());
184
                        }
185
                        return ms;
186
                }
187

    
188
        }
189
        
190
        private static int getMarkerSymbolType(String wkn) {
191
                
192
                if (wkn.compareToIgnoreCase("circle") == 0)
193
                        return IMarkerSymbol.CIRCLE_STYLE;
194
                else if (wkn.compareToIgnoreCase("x") == 0)
195
                        return IMarkerSymbol.X_STYLE;
196
                else if (wkn.compareToIgnoreCase("cross") == 0)
197
                        return IMarkerSymbol.CROSS_STYLE;
198
                else if (wkn.compareToIgnoreCase("triangle") == 0)
199
                        return IMarkerSymbol.TRIANGLE_STYLE;
200
                else if (wkn.compareToIgnoreCase("star") == 0)
201
                        return IMarkerSymbol.STAR_STYLE;
202
                return IMarkerSymbol.SQUARE_STYLE;
203
        }
204

    
205
        public static ISymbol toMarkerSymbol(SLDGraphic gra) 
206
                        throws UnsupportedSLDObjectException {
207
                
208
                IMarkerSymbol resp = null;
209
                List<SLDGraphicStackElement> list = gra.getElementStack();
210
                
211
                Integer alpha = BasicUtils.getAlphaValue(gra.getOpacity());
212
                Double sz = BasicUtils.getDouble(gra.getSize());
213

    
214
                if (list.size() == 0) {
215
                        // Get data from css/svg params
216
                        ISimpleMarkerSymbol sms = BasicUtils.symMan().createSimpleMarkerSymbol();
217
                        if (alpha != null) {
218
                                sms.setAlpha(alpha.intValue());
219
                        }
220
                        if (sz != null) {
221
                                sms.setSize(sz.doubleValue());
222
                        }
223
                        // color? default?
224
                        return sms;
225

    
226
                } else {
227
                        
228
                        if (list.size() == 1) {
229
                                IMarkerSymbol msym = toMarkerSymbol(list.get(0));
230
                                if (alpha != null) {
231
                                        msym.setAlpha(alpha.intValue());
232
                                }
233
                                if (sz != null) {
234
                                        msym.setSize(sz.doubleValue());
235
                                }
236
                                return msym;
237
                        } else {
238
                                
239
                                IMultiLayerSymbol mls = BasicUtils.syMan().createMultiLayerSymbol(Geometry.TYPES.POINT);
240
                                for (int i=0; i<list.size(); i++) {
241
                                        mls.addLayer(toMarkerSymbol(list.get(i)));
242
                                }
243
                                return mls;
244
                        }
245
                }
246
        }
247
        
248
        
249
        
250
        private static String getMarkWellKnownName(int style) {
251
                
252
                if (style == IMarkerSymbol.CIRCLE_STYLE)
253
                        return "circle";
254
                else if (style == IMarkerSymbol.CROSS_STYLE)
255
                        return "cross";
256
                else if (style == IMarkerSymbol.SQUARE_STYLE)
257
                        return "square";
258
                else if (style == IMarkerSymbol.TRIANGLE_STYLE)
259
                        return "triangle";
260
                else if (style == IMarkerSymbol.STAR_STYLE)
261
                        return "star";
262
                else if (style == IMarkerSymbol.X_STYLE)
263
                        return "x";
264
                
265
                return "square";        
266
        }        
267
        
268
        public static SLDPointSymbol toSLDPointSymbol(IMarkerSymbol sym)
269
                        throws UnsupportedSymbolException {
270
                
271
                if (sym instanceof IPictureMarkerSymbol) {
272
                        
273
                        IPictureMarkerSymbol aux = (IPictureMarkerSymbol) sym;
274
                        SLDExternalGraphic egra = new SLDExternalGraphic();
275
                        egra.setIsOnline(true);
276
                        egra.setOnlineResource(aux.getSource().toString());
277
                        egra.setFormat(BasicUtils.getFormat(aux.getSource()));
278
                        SLDGraphic gra = new SLDGraphic();
279
                        gra.getElementStack().add(egra);
280
                        SLDPointSymbol resp = new SLDPointSymbol();
281
                        resp.setGraphic(gra);
282
                        return resp;
283
                        
284
                } else {
285
                        if (sym instanceof ISimpleMarkerSymbol) {
286
                                
287
                                ISimpleMarkerSymbol aux = (ISimpleMarkerSymbol) sym;
288
                                
289
                                SLDMark mark = new SLDMark();
290
                                mark.setMarkType(SLDMark.MARK_TYPE_WELL_KNOWN_NAME);
291
                                String wkn = getMarkWellKnownName(aux.getStyle());
292
                                mark.setWellKnownName(wkn);
293
                                
294
                                Color fillColor = aux.getColor();
295
                                Color borderColor = aux.getOutlineColor();
296
                                double size = aux.getSize();
297
                                double borderw = aux.getOutlineSize();
298
                                double rot = aux.getRotation();
299
                                // from (radians,anticlockwise) to (degrees,clockwise) 
300
                                rot = -rot * 180.0 / Math.PI;
301
                                // ===============
302
                                SLDFill fill = new SLDFill();
303
                                if (fillColor == null) {
304
                                        fillColor = SLDFill.DEFAULT_FILL_COLOR;
305
                                }
306

    
307
                                fill.setFillColor(fillColor);
308
                                mark.setFill(fill);
309
                                // ===
310
                                SLDStroke stro = new SLDStroke();
311
                                stro.setColor(borderColor);
312
                                
313
                                stro.setWidth(new SLDLiteral(BasicUtils.df.format(borderw)));
314
                                mark.setStroke(stro);
315
                                // ===============
316
                                SLDGraphic gra = new SLDGraphic();
317
                                // ===============
318
                                SLDParameterValue pv = new SLDParameterValue();
319
                                pv.getExpressionList().add(new SLDLiteral(BasicUtils.df.format(size)));
320
                                gra.setSize(pv);
321
                                // ===
322
                                pv = new SLDParameterValue();
323
                                pv.getExpressionList().add(new SLDLiteral(BasicUtils.df.format(rot)));
324
                                gra.setRotation(pv);
325
                                // ===============
326
                                gra.getElementStack().add(mark);
327
                                SLDPointSymbol resp = new SLDPointSymbol();
328
                                resp.setGraphic(gra);
329
                                return resp;
330
                                
331
                        } else {
332
                                throw new UnsupportedSymbolException(
333
                                                sym.getClass().getName(),
334
                                                "Unsupported class for conversion to SLD");
335
                        }
336
                }
337
        }
338
        
339
        
340
        
341
}