svn-gvsig-desktop / branches / v02_desarrollo / libraries / sld / using-sld-model / org.gvsig.sldsupport / org.gvsig.sldsupport.basic / src / main / java / org / gvsig / sldsupport / basic / sld1_0_0 / BasicSLDUtils.java @ 40758
History | View | Annotate | Download (41 KB)
1 |
package org.gvsig.sldsupport.basic.sld1_0_0; |
---|---|
2 |
|
3 |
import java.awt.BasicStroke; |
4 |
import java.awt.Color; |
5 |
import java.io.File; |
6 |
import java.io.IOException; |
7 |
import java.io.OutputStream; |
8 |
import java.io.OutputStreamWriter; |
9 |
import java.net.URL; |
10 |
import java.net.UnknownHostException; |
11 |
import java.text.DecimalFormat; |
12 |
import java.text.DecimalFormatSymbols; |
13 |
import java.text.NumberFormat; |
14 |
import java.util.ArrayList; |
15 |
import java.util.HashMap; |
16 |
import java.util.List; |
17 |
|
18 |
import org.gvsig.fmap.dal.exception.DataException; |
19 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
20 |
import org.gvsig.fmap.dal.feature.FeatureType; |
21 |
import org.gvsig.fmap.geom.Geometry; |
22 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
23 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
24 |
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer; |
25 |
import org.gvsig.fmap.mapcontext.rendering.legend.IInterval; |
26 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
27 |
import org.gvsig.fmap.mapcontext.rendering.legend.ISingleSymbolLegend; |
28 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialIntervalLegend; |
29 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend; |
30 |
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol; |
31 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
32 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
33 |
import org.gvsig.sldsupport.SLDSupportLocator; |
34 |
import org.gvsig.sldsupport.SLDSupportManager; |
35 |
import org.gvsig.sldsupport.exception.SLDReadException; |
36 |
import org.gvsig.sldsupport.exception.SLDWriteException; |
37 |
import org.gvsig.sldsupport.exception.UnsupportedSLDException; |
38 |
import org.gvsig.sldsupport.exception.UnsupportedSLDVersionException; |
39 |
import org.gvsig.sldsupport.filterencoding.FExpression; |
40 |
import org.gvsig.sldsupport.filterencoding.Filter; |
41 |
import org.gvsig.sldsupport.filterencoding.FilterTags; |
42 |
import org.gvsig.sldsupport.graphic.SLDExternalGraphic; |
43 |
import org.gvsig.sldsupport.graphic.SLDFill; |
44 |
import org.gvsig.sldsupport.graphic.SLDGraphic; |
45 |
import org.gvsig.sldsupport.graphic.SLDMark; |
46 |
import org.gvsig.sldsupport.graphic.SLDStroke; |
47 |
import org.gvsig.sldsupport.layer.ISLDLayer; |
48 |
import org.gvsig.sldsupport.symbolizer.ISLDSymbolizer; |
49 |
import org.gvsig.sldsupport.symbolizer.SLDLineSymbolizer; |
50 |
import org.gvsig.sldsupport.symbolizer.SLDMultiLineSymbolizer; |
51 |
import org.gvsig.sldsupport.symbolizer.SLDMultiPointSymbolizer; |
52 |
import org.gvsig.sldsupport.symbolizer.SLDMultiPolygonSymbolizer; |
53 |
import org.gvsig.sldsupport.symbolizer.SLDPointSymbolizer; |
54 |
import org.gvsig.sldsupport.symbolizer.SLDPolygonSymbolizer; |
55 |
import org.gvsig.sldsupport.util.SLDTags; |
56 |
import org.gvsig.sldsupport.util.SLDUtils; |
57 |
import org.gvsig.sldsupport.util.XmlBuilder; |
58 |
import org.gvsig.symbology.SymbologyLocator; |
59 |
import org.gvsig.symbology.SymbologyManager; |
60 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.IMultiShapeSymbol; |
61 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol; |
62 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IMarkerFillSymbol; |
63 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IMultiLayerFillSymbol; |
64 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IPictureFillSymbol; |
65 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.ISimpleFillSymbol; |
66 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
67 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.IMultiLayerLineSymbol; |
68 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.IPictureLineSymbol; |
69 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol; |
70 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol; |
71 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMultiLayerMarkerSymbol; |
72 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IPictureMarkerSymbol; |
73 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.ISimpleMarkerSymbol; |
74 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ISimpleLineStyle; |
75 |
import org.slf4j.Logger; |
76 |
import org.slf4j.LoggerFactory; |
77 |
|
78 |
/**
|
79 |
*
|
80 |
* Basic symbology utility methods for SLD version 1.0.0
|
81 |
*
|
82 |
* @author jldominguez
|
83 |
*
|
84 |
*/
|
85 |
public class BasicSLDUtils { |
86 |
|
87 |
private static Logger logger = LoggerFactory.getLogger(BasicSLDUtils.class); |
88 |
|
89 |
private static final String DEFAULT_ENCODING = "ISO-8859-1"; |
90 |
|
91 |
private static MapContextManager mcoMan = null; |
92 |
private static SymbologyManager symMan = null; |
93 |
private static SymbolManager syMan = null; |
94 |
|
95 |
private static SLDSupportManager sldMan = |
96 |
SLDSupportLocator.getInstance().getSLDSupportManager(); |
97 |
|
98 |
|
99 |
public static ILegend createLegendFor(ISLDLayer sldLayer, VectorLayer vect, |
100 |
String v)
|
101 |
throws NumberFormatException, SLDReadException, DataException { |
102 |
|
103 |
ArrayList<Filter> filters = sldLayer.getLayerFilters(); |
104 |
String[] fNames = sldLayer.getFieldNames(); |
105 |
|
106 |
if (!checkFieldNames(fNames, vect)) {
|
107 |
throw new SLDReadException(new Exception( |
108 |
"SLD layer uses fields which are not available in layer: " + vect.getName()));
|
109 |
|
110 |
} |
111 |
|
112 |
//Multishape not yet supported
|
113 |
if (vect.getShapeType() == Geometry.TYPES.GEOMETRY ||
|
114 |
vect.getShapeType() == Geometry.TYPES.AGGREGATE) { |
115 |
throw new SLDReadException(new Exception( |
116 |
"Multi/aggregate geometry type layers not supported."));
|
117 |
} |
118 |
|
119 |
//SingleSymbolLegend
|
120 |
else if (!sldLayer.layerHasFilterForSymbolizers(vect.getShapeType())) { |
121 |
|
122 |
ISingleSymbolLegend leg = (ISingleSymbolLegend) |
123 |
mcoMan().createLegend(ISingleSymbolLegend.LEGEND_NAME); |
124 |
|
125 |
ArrayList<ISLDSymbolizer> symbolizers;
|
126 |
symbolizers = sldLayer.getSymbolizersByShapeType(vect.getShapeType()); |
127 |
|
128 |
IMultiLayerSymbol msym = null;
|
129 |
msym = syMan().createMultiLayerSymbol(vect.getShapeType()); |
130 |
|
131 |
if (msym != null) { |
132 |
for (int i = 0; i < symbolizers.size(); i++) { |
133 |
ISymbol sym = SLDSymbolizer2ISymbol(symbolizers.get(i), v); |
134 |
msym.addLayer(sym); |
135 |
} |
136 |
ISymbol defaultSym = (msym.getLayerCount() > 1) ?
|
137 |
msym : msym.getLayer(0);
|
138 |
|
139 |
defaultSym.setDescription("Default");
|
140 |
leg.setDefaultSymbol(defaultSym); |
141 |
return leg;
|
142 |
} |
143 |
throw new SLDReadException(new Exception( |
144 |
"Unable to parse symbol in SingleSymbolLegend."));
|
145 |
} |
146 |
|
147 |
throw new SLDReadException(new Exception( |
148 |
"Unsupported SLD legend."));
|
149 |
/*
|
150 |
*
|
151 |
//VectorialFilterExpressionLegend
|
152 |
else {
|
153 |
IClassifiedVectorLegend lege = (IClassifiedVectorLegend)
|
154 |
mcoMan().createLegend("FILTER_EXPRESSION_LEGEND_NAME");
|
155 |
|
156 |
if (lege == null) {
|
157 |
throw new SLDReadException(new Exception("Filter expression legend not available."));
|
158 |
}
|
159 |
for (int i = 0; i < filters.size(); i++) {
|
160 |
for (int j = 0; j < filters.get(i).getSymbolizers().size(); j++) {
|
161 |
|
162 |
ISLDSymbolizer sldSym = filters.get(i).getSymbolizers().get(j);
|
163 |
ISymbol sym = SLDSymbolizer2ISymbol(sldSym);
|
164 |
sym.setDescription(filters.get(i).getExpression().toString());
|
165 |
lege.addSymbol(filters.get(i).getExpression().toString(), sym);
|
166 |
}
|
167 |
}
|
168 |
return lege;
|
169 |
}
|
170 |
*/
|
171 |
} |
172 |
|
173 |
|
174 |
private static boolean checkFieldNames(String[] fieldNames, VectorLayer lyr) |
175 |
throws DataException {
|
176 |
|
177 |
FeatureStore sto = lyr.getFeatureStore(); |
178 |
FeatureType fty = sto.getDefaultFeatureType(); |
179 |
|
180 |
for (int i = 0; i < fieldNames.length; i++) { |
181 |
if (fty.getAttributeDescriptor(fieldNames[i]) == null) { |
182 |
return false; |
183 |
} |
184 |
} |
185 |
return true; |
186 |
} |
187 |
|
188 |
private static SymbolManager syMan() { |
189 |
if (syMan == null) { |
190 |
syMan = MapContextLocator.getSymbolManager(); |
191 |
} |
192 |
return syMan;
|
193 |
} |
194 |
|
195 |
private static MapContextManager mcoMan() { |
196 |
if (mcoMan == null) { |
197 |
mcoMan = MapContextLocator.getMapContextManager(); |
198 |
} |
199 |
return mcoMan;
|
200 |
} |
201 |
|
202 |
private static SymbologyManager symMan() { |
203 |
if (symMan == null) { |
204 |
symMan = SymbologyLocator.getSymbologyManager(); |
205 |
} |
206 |
return symMan;
|
207 |
} |
208 |
|
209 |
|
210 |
/**
|
211 |
* Transforms a SLD Symbolizer into an ISymbol.
|
212 |
*
|
213 |
* @param symbolizer to be transfomed into an ISymbol
|
214 |
* @return ISymbol
|
215 |
* @throws NumberFormatException
|
216 |
* @throws LegendDriverException
|
217 |
* @throws UnknownHostException
|
218 |
*/
|
219 |
public static ISymbol SLDSymbolizer2ISymbol( |
220 |
ISLDSymbolizer symbolizer, String v)
|
221 |
throws NumberFormatException, SLDReadException { |
222 |
|
223 |
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
224 |
//LINE//
|
225 |
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
226 |
if (symbolizer instanceof SLDLineSymbolizer) { |
227 |
SLDLineSymbolizer myLine = (SLDLineSymbolizer) symbolizer; |
228 |
|
229 |
ISimpleLineStyle simplLine = symMan().createSimpleLineStyle(); |
230 |
int lineCap;
|
231 |
if (myLine.getStroke().getExpressionLineCap().getLiteral() != null) |
232 |
lineCap = myLine.getStroke().getLineCap(); |
233 |
else lineCap = BasicStroke.CAP_SQUARE; |
234 |
|
235 |
int lineJoin;
|
236 |
if (myLine.getStroke().getExpressionLineJoin().getLiteral() != null) |
237 |
lineJoin = myLine.getStroke().getLineJoin(); |
238 |
else lineJoin = BasicStroke.JOIN_MITER; |
239 |
|
240 |
Float dashOffset;
|
241 |
if (myLine.getStroke().getExpressionDashOffset().getLiteral() != null) |
242 |
dashOffset = Float.valueOf(myLine.getStroke().getExpressionDashOffset().getLiteral());
|
243 |
else dashOffset = 0f; |
244 |
|
245 |
Float width;
|
246 |
if (myLine.getStroke().getExpressionWidth().getLiteral()!= null) |
247 |
width = myLine.getStroke().getStrokeWidth(); |
248 |
else width = 0f; |
249 |
|
250 |
float[] dash; |
251 |
if(myLine.getStroke().getFloatDashArray().length == 0) { |
252 |
dash = null;
|
253 |
} |
254 |
else dash = myLine.getStroke().getFloatDashArray();
|
255 |
|
256 |
BasicStroke str = new BasicStroke(width, |
257 |
lineCap, |
258 |
lineJoin, |
259 |
10,
|
260 |
dash, |
261 |
dashOffset); |
262 |
|
263 |
|
264 |
simplLine.setStroke(str); |
265 |
|
266 |
//SimpleLineSymbol
|
267 |
if ( !myLine.getStroke().isHasGraphicFill() && !myLine.getStroke().isHasGraphicStroke() ) {
|
268 |
|
269 |
ISimpleLineSymbol line = symMan().createSimpleLineSymbol(); |
270 |
line.setLineStyle(simplLine); |
271 |
line.setUnit(-1);
|
272 |
line.setLineColor(myLine.getStroke().getStrokeColor()); |
273 |
if(myLine.getStroke().getExpressionOpacity().getLiteral() != null) |
274 |
line.setAlpha((int)(255 * Float.valueOf(myLine.getStroke().getExpressionOpacity().getLiteral()))); |
275 |
if (myLine.getStroke().getExpressionWidth().getLiteral() != null) |
276 |
line.setLineWidth(myLine.getStroke().getStrokeWidth()); |
277 |
return line;
|
278 |
} |
279 |
else {
|
280 |
//MarkerLineSymbol
|
281 |
IPictureLineSymbol line = null;
|
282 |
//If the marker is an instance of SimpleMarkerSymbol
|
283 |
if(myLine.getStroke().getGraphic().getMarks().size() > 0) { |
284 |
/*
|
285 |
* TODO We need MarkerLineSymbol in gvSIG 2
|
286 |
*
|
287 |
SLDMark myMark = myLine.getStroke().getGraphic().getMarks().get(0);
|
288 |
FExpression size = myLine.getStroke().getGraphic().getExpressionSize();
|
289 |
FExpression rotation = myLine.getStroke().getGraphic().getExpressionRotation();
|
290 |
ISimpleMarkerSymbol marker = createMarker(myMark,size,rotation);
|
291 |
line.setLineWidth(marker.getSize());
|
292 |
marker.setUnit(-1);
|
293 |
marker.setStyle(SLDUtils.setMarkerStyle(myMark.getWellKnownName().getLiteral()));
|
294 |
line.s.setMarker(marker);
|
295 |
line.setAlpha(marker.getColor().getAlpha());
|
296 |
if(str.getDashArray() != null)
|
297 |
line.setSeparation(str.getDashArray()[1]);
|
298 |
return line;
|
299 |
*/
|
300 |
} |
301 |
//If the marker is an instance of PictureMarkerSymbol
|
302 |
else if (myLine.getStroke().getGraphic().getExternalGraphics().size() > 0) { |
303 |
|
304 |
SLDExternalGraphic myExtGraphic = myLine.getStroke().getGraphic().getExternalGraphics().get(0);
|
305 |
FExpression size = myLine.getStroke().getGraphic().getExpressionSize(); |
306 |
FExpression rotation = myLine.getStroke().getGraphic().getExpressionRotation(); |
307 |
IPictureMarkerSymbol marker = createMarker(myExtGraphic, size, rotation); |
308 |
if (marker != null) { |
309 |
|
310 |
marker.setUnit(-1);
|
311 |
try {
|
312 |
line = symMan().createPictureLineSymbol( |
313 |
marker.getSource(), marker.getSelectedSource()); |
314 |
} catch (IOException e) { |
315 |
throw new SLDReadException(e); |
316 |
} |
317 |
line.setLineStyle(simplLine); |
318 |
line.setUnit(-1);
|
319 |
line.setLineWidth(marker.getSize()); |
320 |
line.setAlpha(marker.getColor().getAlpha()); |
321 |
return line;
|
322 |
} else {
|
323 |
throw new SLDReadException(new Exception( |
324 |
"Unable to create picture marker symbol"));
|
325 |
} |
326 |
} |
327 |
} |
328 |
} |
329 |
|
330 |
|
331 |
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
332 |
//POLYGON//
|
333 |
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
334 |
else if (symbolizer instanceof SLDPolygonSymbolizer) { |
335 |
|
336 |
SLDPolygonSymbolizer myPolygon = (SLDPolygonSymbolizer)symbolizer; |
337 |
//SimpleFillSymbol
|
338 |
if ( myPolygon.getFill() != null && myPolygon.getFill().getFillGraphic() == null) { |
339 |
|
340 |
ISimpleFillSymbol simpleFill = symMan().createSimpleFillSymbol(); |
341 |
|
342 |
if( myPolygon.getFill().getExpressionColor().getLiteral() != null) { |
343 |
Color color = myPolygon.getFill().getFillColor();
|
344 |
if(myPolygon.getFill().getExpressionOpacity().getLiteral() != null) { |
345 |
simpleFill.setHasFill(true);
|
346 |
int alpha = (int) ((myPolygon.getFill().getFillOpacity()) * 255); |
347 |
Color myColor = new Color(color.getRed(),color.getGreen(),color.getBlue(),alpha); |
348 |
simpleFill.setFillColor(myColor); |
349 |
} |
350 |
else
|
351 |
simpleFill.setFillColor(color); |
352 |
} |
353 |
|
354 |
//Outline of the symbol
|
355 |
if(myPolygon.getStroke() != null) { |
356 |
simpleFill.setHasOutline(true);
|
357 |
simpleFill.setOutline(createOutline(myPolygon.getStroke(), v)); |
358 |
} |
359 |
return simpleFill;
|
360 |
} |
361 |
else if (myPolygon.getFill() != null && myPolygon.getFill().getFillGraphic() != null) { |
362 |
|
363 |
SLDGraphic myFill = myPolygon.getFill().getFillGraphic(); |
364 |
|
365 |
//MarkerFillSymbol
|
366 |
if(myFill.getMarks().size() > 0 || myFill.getExternalGraphics().size() > 0) { |
367 |
|
368 |
IMarkerFillSymbol markerFill = symMan().createMarkerFillSymbol(); |
369 |
// IMarkerFillPropertiesStyle markerProperties = markerFill.getMarkerFillProperties();
|
370 |
// markerProperties.setFillStyle(markerFill.GRID_FILL);
|
371 |
// markerFill.setMarkerFillProperties(markerProperties);
|
372 |
|
373 |
FExpression size = myFill.getExpressionSize(); |
374 |
FExpression rotation = myFill.getExpressionRotation(); |
375 |
|
376 |
|
377 |
//Outline of the symbol
|
378 |
if(myPolygon.getStroke() != null) { |
379 |
markerFill.setHasOutline(true);
|
380 |
markerFill.setOutline(createOutline(myPolygon.getStroke(), v)); |
381 |
} |
382 |
//Marker->SimpleMarkerSymbol
|
383 |
if(myFill.getMarks().size() > 0) { |
384 |
ISimpleMarkerSymbol marker = null;
|
385 |
SLDMark myMark = myFill.getMarks().get(0);
|
386 |
marker = createMarker(myMark, size, rotation); |
387 |
if (marker != null) { |
388 |
markerFill.setMarker(marker); |
389 |
if(marker.getColor() != null ) |
390 |
markerFill.setFillColor(marker.getColor()); |
391 |
} |
392 |
return markerFill;
|
393 |
} |
394 |
//Marker->PictureMarkerSymbol
|
395 |
else if (myFill.getExternalGraphics().size() > 0) { |
396 |
|
397 |
IPictureMarkerSymbol marker = null;
|
398 |
SLDExternalGraphic myExtGraphic = myFill.getExternalGraphics().get(0);
|
399 |
marker = createMarker(myExtGraphic, size, rotation); |
400 |
if (marker != null) { |
401 |
markerFill.setMarker(marker); |
402 |
if(marker.getColor() != null) |
403 |
markerFill.setFillColor(marker.getColor()); |
404 |
} |
405 |
return markerFill;
|
406 |
} |
407 |
} |
408 |
} |
409 |
} |
410 |
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
411 |
//POINT//
|
412 |
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
413 |
else if (symbolizer instanceof SLDPointSymbolizer) { |
414 |
|
415 |
SLDPointSymbolizer myPoint = (SLDPointSymbolizer)symbolizer; |
416 |
//Marker->SimpleMarkerSymbol
|
417 |
if(myPoint.getGraphic().getMarks().size() > 0) { |
418 |
SLDMark myMark; |
419 |
myMark = myPoint.getGraphic().getMarks().get(0);
|
420 |
FExpression size = myPoint.getGraphic().getExpressionSize(); |
421 |
FExpression rotation = myPoint.getGraphic().getExpressionRotation(); |
422 |
|
423 |
ISimpleMarkerSymbol marker = createMarker(myMark,size,rotation); |
424 |
marker.setUnit(-1);
|
425 |
return marker;
|
426 |
} |
427 |
//Marker->PictureMarkerSymbol
|
428 |
else if (myPoint.getGraphic().getExternalGraphics().size() > 0) { |
429 |
SLDExternalGraphic myExtGraphic = myPoint.getGraphic().getExternalGraphics().get(0);
|
430 |
FExpression size = myPoint.getGraphic().getExpressionSize(); |
431 |
FExpression rotation = myPoint.getGraphic().getExpressionRotation(); |
432 |
IPictureMarkerSymbol marker = createMarker(myExtGraphic, size, rotation); |
433 |
marker.setUnit(-1);
|
434 |
return marker;
|
435 |
} |
436 |
} |
437 |
return null; |
438 |
|
439 |
} |
440 |
|
441 |
|
442 |
// =======================================
|
443 |
|
444 |
private static ISimpleMarkerSymbol createMarker( |
445 |
SLDMark myMark, FExpression size, FExpression rotation) |
446 |
throws NumberFormatException, SLDReadException { |
447 |
|
448 |
ISimpleMarkerSymbol marker = symMan().createSimpleMarkerSymbol(); |
449 |
String name = myMark.getWellKnownName().getLiteral();
|
450 |
marker.setStyle(SLDUtils.getMarkerStyle(name)); |
451 |
|
452 |
|
453 |
if(myMark.getFill() != null) { |
454 |
if( myMark.getFill().getExpressionColor().getLiteral() != null) { |
455 |
Color color = myMark.getFill().getFillColor();
|
456 |
if(myMark.getFill().getExpressionOpacity().getLiteral() != null) { |
457 |
int alpha = (int) ((myMark.getFill().getFillOpacity()) * 255); |
458 |
Color myColor = new Color(color.getRed(),color.getGreen(),color.getBlue(),alpha); |
459 |
marker.setColor(myColor); |
460 |
} |
461 |
else
|
462 |
marker.setColor(color); |
463 |
} |
464 |
} |
465 |
if (myMark.getStroke() != null) { |
466 |
marker.setOutlined(false);
|
467 |
if( myMark.getStroke().getExpressionColor().getLiteral() != null && |
468 |
myMark.getStroke().getExpressionWidth().getLiteral() != null ) {
|
469 |
marker.setOutlineColor(myMark.getStroke().getStrokeColor()); |
470 |
marker.setOutlineSize(myMark.getStroke().getStrokeWidth()); |
471 |
marker.setOutlined(true);
|
472 |
} |
473 |
} |
474 |
if( size.getLiteral() != null) { |
475 |
marker.setSize(Float.valueOf(size.getLiteral()));
|
476 |
} |
477 |
if( rotation.getLiteral() != null) |
478 |
marker.setRotation(Float.valueOf(rotation.getLiteral()));
|
479 |
|
480 |
return marker;
|
481 |
} |
482 |
|
483 |
|
484 |
private static IPictureMarkerSymbol createMarker( |
485 |
SLDExternalGraphic myExtGraphic,FExpression size, FExpression rotation) |
486 |
throws SLDReadException {
|
487 |
|
488 |
IPictureMarkerSymbol marker = null;
|
489 |
|
490 |
//We take the first URL by default
|
491 |
URL url = myExtGraphic.getOnlineResource().get(0); |
492 |
try {
|
493 |
if (url != null) { |
494 |
marker = symMan().createPictureMarkerSymbol(url,url); |
495 |
if( size.getLiteral() != null) { |
496 |
marker.setUnit(-1);
|
497 |
marker.setSize(Float.valueOf(size.getLiteral()));
|
498 |
} |
499 |
if( rotation.getLiteral() != null) |
500 |
marker.setRotation(Float.valueOf(rotation.getLiteral()));
|
501 |
} |
502 |
} catch (IOException e1) { |
503 |
throw new SLDReadException(e1); |
504 |
} |
505 |
|
506 |
|
507 |
return marker;
|
508 |
} |
509 |
|
510 |
|
511 |
private static ILineSymbol createOutline(SLDStroke stroke, String v) |
512 |
throws NumberFormatException, SLDReadException { |
513 |
|
514 |
SLDLineSymbolizer line; |
515 |
try {
|
516 |
line = sldMan.createSLDLineSymbolizer(v); |
517 |
line.setStroke(stroke); |
518 |
return (ILineSymbol) SLDSymbolizer2ISymbol(line, v);
|
519 |
} catch (Exception e) { |
520 |
throw new SLDReadException(e); |
521 |
} |
522 |
} |
523 |
|
524 |
// ====================================================
|
525 |
|
526 |
public static void writeLegend( |
527 |
String layer_name, ILegend legend, OutputStream outstream, |
528 |
String version)
|
529 |
throws SLDWriteException {
|
530 |
|
531 |
DecimalFormatSymbols dformater_rules = new DecimalFormatSymbols (); |
532 |
dformater_rules.setDecimalSeparator ('.');
|
533 |
DecimalFormat df = new DecimalFormat("##########.0##########",dformater_rules); |
534 |
|
535 |
HashMap<String, String> attributes = new HashMap<String, String>(); |
536 |
attributes.put("xsi:schemaLocation","http://www.opengis.net/sld StyledLayerDescriptor.xsd"); |
537 |
attributes.put("xmlns","http://www.opengis.net/sld"); |
538 |
attributes.put("xmlns:ogc","http://www.opengis.net/ogc"); |
539 |
attributes.put("xmlns:xlink","http://www.w3.org/1999/xlink"); |
540 |
attributes.put("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance"); |
541 |
attributes.put(SLDTags.VERSION_ATTR, version); |
542 |
|
543 |
XmlBuilder xmlBuilder = new XmlBuilder();
|
544 |
xmlBuilder.setEncoding(DEFAULT_ENCODING); |
545 |
xmlBuilder.writeHeader(); |
546 |
xmlBuilder.openTag(SLDTags.SLD_ROOT, attributes); |
547 |
xmlBuilder.openTag(SLDTags.NAMEDLAYER); |
548 |
xmlBuilder.writeTag(SLDTags.NAME, layer_name); |
549 |
xmlBuilder.openTag(SLDTags.USERSTYLE); |
550 |
xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE); |
551 |
try {
|
552 |
|
553 |
//VectorialIntervalLegend
|
554 |
if (legend instanceof IVectorialIntervalLegend) { |
555 |
|
556 |
IVectorialIntervalLegend myLegend = (IVectorialIntervalLegend) legend; |
557 |
xmlBuilder.writeTag(SLDTags.FEATURETYPENAME, "Feature");
|
558 |
|
559 |
ISymbol[] symbols = myLegend.getSymbols();
|
560 |
if (symbols.length==0) { |
561 |
throw new SLDWriteException(new Exception( |
562 |
"VectorialIntervalLegend has no symbols."));
|
563 |
} |
564 |
|
565 |
if (symbols[0] != null && symbols[0] instanceof IMultiShapeSymbol) { |
566 |
throw new SLDWriteException(new Exception( |
567 |
"VectorialIntervalLegend has symbol of unsupported type (MultiShapeSymbol)."));
|
568 |
} |
569 |
|
570 |
Object[] values = myLegend.getValues(); |
571 |
NumberFormat nf=NumberFormat.getInstance(); |
572 |
IInterval interval; |
573 |
ISymbol def_sym = null;
|
574 |
|
575 |
for(int i = 0; i < symbols.length; i++ ) |
576 |
{ |
577 |
if (values[i] == null || values[i] instanceof String) { |
578 |
/*
|
579 |
* We'll consider this is the default symbol
|
580 |
*/
|
581 |
def_sym = symbols[i]; |
582 |
continue;
|
583 |
} |
584 |
|
585 |
interval = (IInterval) values[i]; |
586 |
xmlBuilder.openTag(SLDTags.RULE); |
587 |
xmlBuilder.writeTag(FilterTags.NAME, ""+interval.getMin() +" - " +interval.getMax()); |
588 |
xmlBuilder.openTag(FilterTags.FILTER); |
589 |
xmlBuilder.openTag(FilterTags.AND); |
590 |
xmlBuilder.openTag(FilterTags.PROPERTYISGREATEROREQUALTHAN); |
591 |
xmlBuilder.writeTag(FilterTags.PROPERTYNAME,myLegend.getClassifyingFieldNames()[0]);
|
592 |
double f = (double) interval.getMin(); |
593 |
|
594 |
xmlBuilder.writeTag(FilterTags.LITERAL, ""+df.format(f));
|
595 |
xmlBuilder.closeTag(); |
596 |
xmlBuilder.openTag(FilterTags.PROPERTYISLESSOREQUALTHAN); |
597 |
xmlBuilder.writeTag(FilterTags.PROPERTYNAME,myLegend.getClassifyingFieldNames()[0]);
|
598 |
f= (double)interval.getMax();
|
599 |
xmlBuilder.writeTag(FilterTags.LITERAL, ""+df.format(f));
|
600 |
xmlBuilder.closeTag(); |
601 |
xmlBuilder.closeTag(); |
602 |
xmlBuilder.closeTag(); |
603 |
|
604 |
ISLDSymbolizer symSLD = (ISLDSymbolizer)ISymbol2SLDSymbolizer( |
605 |
symbols[i], version); |
606 |
xmlBuilder.writeRaw(symSLD.toXML()); |
607 |
|
608 |
xmlBuilder.closeTag(); |
609 |
} |
610 |
/*
|
611 |
* Write the default symbol in a "ElseFilter" rule
|
612 |
*/
|
613 |
if (def_sym != null) { |
614 |
xmlBuilder.openTag(SLDTags.RULE); |
615 |
xmlBuilder.openTag(SLDTags.ELSEFILTER); |
616 |
xmlBuilder.closeTag(); // close else
|
617 |
ISLDSymbolizer symSLD = (ISLDSymbolizer) |
618 |
ISymbol2SLDSymbolizer(def_sym, version); |
619 |
xmlBuilder.writeRaw(symSLD.toXML()); |
620 |
xmlBuilder.closeTag(); |
621 |
} |
622 |
} |
623 |
else if (legend instanceof IVectorialUniqueValueLegend) { |
624 |
|
625 |
IVectorialUniqueValueLegend myLegend = (IVectorialUniqueValueLegend)legend; |
626 |
xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"Feature");
|
627 |
ISymbol defSym = null;
|
628 |
ISymbol[] symbols = myLegend.getSymbols();
|
629 |
if (symbols.length==0){ |
630 |
throw new SLDWriteException(new Exception( |
631 |
"VectorialUniqueValueLegend has no symbols."));
|
632 |
} |
633 |
|
634 |
if(symbols[0] != null && symbols[0] instanceof IMultiShapeSymbol) { |
635 |
throw new SLDWriteException(new Exception( |
636 |
"VectorialUniqueValueLegend has symbol of unsupported type (MultiShapeSymbol)."));
|
637 |
} |
638 |
|
639 |
Object[] values = myLegend.getValues(); |
640 |
Object myVal;
|
641 |
|
642 |
for(int i = 0; i < symbols.length; i++ ) |
643 |
{ |
644 |
if (values[i] == null) { |
645 |
continue;
|
646 |
} |
647 |
|
648 |
myVal = values[i]; |
649 |
|
650 |
if (myVal.toString().compareTo("Default") == 0) { |
651 |
defSym = symbols[i]; |
652 |
} |
653 |
else {
|
654 |
xmlBuilder.openTag(SLDTags.RULE); |
655 |
if(myVal instanceof String) |
656 |
xmlBuilder.writeTag(FilterTags.NAME,"\""+myVal.toString()+"\""); |
657 |
else
|
658 |
xmlBuilder.writeTag(FilterTags.NAME, ""+df.format(Double.valueOf(myVal.toString()))); |
659 |
|
660 |
xmlBuilder.openTag(FilterTags.FILTER); |
661 |
|
662 |
xmlBuilder.openTag(FilterTags.PROPERTYISEQUALTO); |
663 |
xmlBuilder.writeTag(FilterTags.PROPERTYNAME,myLegend.getClassifyingFieldNames()[0]);
|
664 |
if(myVal instanceof String) |
665 |
xmlBuilder.writeTag(FilterTags.LITERAL, ""+"\""+myVal.toString()+"\""); |
666 |
else
|
667 |
xmlBuilder.writeTag(FilterTags.LITERAL, ""+df.format(Double.valueOf(myVal.toString()))); |
668 |
|
669 |
xmlBuilder.closeTag(); |
670 |
xmlBuilder.closeTag(); |
671 |
|
672 |
ISLDSymbolizer symSLD = (ISLDSymbolizer)ISymbol2SLDSymbolizer( |
673 |
symbols[i], version); |
674 |
xmlBuilder.writeRaw(symSLD.toXML()); |
675 |
|
676 |
xmlBuilder.closeTag(); |
677 |
} |
678 |
} |
679 |
} |
680 |
/*
|
681 |
else if (legend instanceof VectorFilterExpressionLegend) {
|
682 |
VectorFilterExpressionLegend myLegend = (VectorFilterExpressionLegend)legend;
|
683 |
xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"Feature");
|
684 |
|
685 |
ISymbol[] symbols = myLegend.getSymbols();
|
686 |
if (symbols.length==0){
|
687 |
NotificationManager.showMessageInfo(PluginServices.getText(this, "not_save_empty_legend"), null);
|
688 |
return;
|
689 |
}
|
690 |
Object[] values = myLegend.getValues();
|
691 |
String[] descriptions = myLegend.getDescriptions();
|
692 |
for(int i = 0; i < symbols.length; i++ ){
|
693 |
if (!(values[i] instanceof Expression)){
|
694 |
throw new LegendDriverException(LegendDriverException.UNSUPPORTED_LEGEND_CREATION);
|
695 |
}
|
696 |
Expression filter4Symbol = (Expression) values[i];
|
697 |
FilterFactory filterFactory = new FilterFactory();
|
698 |
|
699 |
xmlBuilder.openTag(SLDTags.RULE);
|
700 |
xmlBuilder.writeTag(FilterTags.NAME, descriptions[i]);
|
701 |
xmlBuilder.openTag(FilterTags.FILTER);
|
702 |
xmlBuilder.writeRaw(filterFactory.createXMLFromExpression(filter4Symbol));
|
703 |
xmlBuilder.closeTag();
|
704 |
|
705 |
ISLDSymbolizer symSLD = (ISLDSymbolizer)ISymbol2SLDSymbolizer(symbols[i], sldVersion);
|
706 |
xmlBuilder.writeRaw(symSLD.toXML());
|
707 |
xmlBuilder.closeTag();
|
708 |
|
709 |
}
|
710 |
}
|
711 |
*/
|
712 |
//SingleSymbolLegend
|
713 |
else if (legend instanceof ISingleSymbolLegend ) { |
714 |
ISingleSymbolLegend myLegend = (ISingleSymbolLegend)legend; |
715 |
xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"Feature");
|
716 |
xmlBuilder.openTag(SLDTags.RULE); |
717 |
ISLDSymbolizer symSLD = (ISLDSymbolizer) |
718 |
ISymbol2SLDSymbolizer( |
719 |
myLegend.getDefaultSymbol(), version); |
720 |
xmlBuilder.writeRaw(symSLD.toXML()); |
721 |
xmlBuilder.closeTag(); |
722 |
} |
723 |
else {
|
724 |
|
725 |
throw new SLDWriteException(new Exception( |
726 |
"Unsupported legend type: " + legend.getClass().getName()));
|
727 |
} |
728 |
xmlBuilder.closeTag(); |
729 |
xmlBuilder.closeTag(); |
730 |
xmlBuilder.closeTag(); |
731 |
xmlBuilder.closeTag(); |
732 |
|
733 |
OutputStreamWriter writer = new OutputStreamWriter(outstream, DEFAULT_ENCODING); |
734 |
// FileWriter writer = new FileWriter(file.getAbsolutePath());
|
735 |
writer.write( xmlBuilder.getXML()); |
736 |
writer.close(); |
737 |
|
738 |
} catch (Exception e) { |
739 |
throw new SLDWriteException(e); |
740 |
} |
741 |
} |
742 |
|
743 |
// private static String classNameForVersion(Class sldClass, String sldVersion) {
|
744 |
// String versionString = sldVersion.replaceAll("\\.", "_");
|
745 |
//// String packageName = sldClass.getPackage().getName();
|
746 |
//// String packageName = "org.gvsig.remoteClient.sld.";
|
747 |
//// String subPackage = packageName.substring(packageName.lastIndexOf(".")+1, packageName.length());
|
748 |
//// packageName = packageName.substring(0, packageName.lastIndexOf('.')+1);
|
749 |
//// String cName = packageName + "sld" + versionString + "." +
|
750 |
//// subPackage + sldClass.getName().substring(
|
751 |
//// sldClass.getName().lastIndexOf(".", sldClass.getName().length())) + versionString;
|
752 |
//
|
753 |
// String path = sldClass.toString();
|
754 |
// path = sldClass.toString().substring("class ".length(),path.length());
|
755 |
// path = path.replace(".sld.", ".sld.sld"+versionString+".");
|
756 |
// String cName = path+versionString;
|
757 |
//
|
758 |
// return cName;
|
759 |
// }
|
760 |
/**
|
761 |
* Transforms an ISymbol into a SLD Symbolizer
|
762 |
*
|
763 |
* @param symbol ISymbol to be transformed
|
764 |
* @return SLD Symbolizer
|
765 |
* @throws LegendDriverException
|
766 |
* @throws ClassNotFoundException
|
767 |
* @throws IllegalAccessException
|
768 |
* @throws InstantiationException
|
769 |
*/
|
770 |
public static ISLDSymbolizer ISymbol2SLDSymbolizer( |
771 |
ISymbol symbol, String v)
|
772 |
throws SLDWriteException, UnsupportedSLDVersionException {
|
773 |
//MultiLayer Symbol
|
774 |
if(symbol instanceof IMultiLayerSymbol) { |
775 |
//Made up of lines
|
776 |
if(symbol instanceof IMultiLayerLineSymbol) { |
777 |
IMultiLayerLineSymbol multiLine = (IMultiLayerLineSymbol) symbol; |
778 |
SLDMultiLineSymbolizer multiSldLine = |
779 |
sldMan.createSLDMultiLineSymbolizer(v); |
780 |
for (int i = 0; i < multiLine.getLayerCount(); i++) { |
781 |
multiSldLine.addSldLine((SLDLineSymbolizer)ISymbol2SLDSymbolizer( |
782 |
multiLine.getLayer(i), v)); |
783 |
} |
784 |
return (ISLDSymbolizer) multiSldLine;
|
785 |
} |
786 |
//Made up of polygons
|
787 |
else if (symbol instanceof IMultiLayerFillSymbol) { |
788 |
IMultiLayerFillSymbol multiFill = (IMultiLayerFillSymbol) symbol; |
789 |
SLDMultiPolygonSymbolizer multiSldPolygon = |
790 |
sldMan.createSLDMultiPolygonSymbolizer(v); |
791 |
for (int i = 0; i < multiFill.getLayerCount(); i++) { |
792 |
multiSldPolygon.addSldPolygon((SLDPolygonSymbolizer) |
793 |
ISymbol2SLDSymbolizer( |
794 |
multiFill.getLayer(i), v)); |
795 |
} |
796 |
return (ISLDSymbolizer) multiSldPolygon;
|
797 |
} |
798 |
//Made up of markers
|
799 |
else if (symbol instanceof IMultiLayerMarkerSymbol) { |
800 |
IMultiLayerMarkerSymbol multiMarker = (IMultiLayerMarkerSymbol) symbol; |
801 |
SLDMultiPointSymbolizer multiSldPoint = |
802 |
sldMan.createSLDMultiPointSymbolizer(v); |
803 |
for (int i = 0; i < multiMarker.getLayerCount(); i++) { |
804 |
multiSldPoint.addSldPoint((SLDPointSymbolizer)ISymbol2SLDSymbolizer( |
805 |
multiMarker.getLayer(i), v)); |
806 |
} |
807 |
return (ISLDSymbolizer) multiSldPoint;
|
808 |
} |
809 |
} |
810 |
// MultishapeSymbol not yet supported
|
811 |
// else if(symbol instanceof MultiShapeSymbol) {
|
812 |
// MultiShapeSymbol multiShape = (MultiShapeSymbol) symbol;
|
813 |
// SLDMultiShapeSymbolizer sldMultiShape = new SLDMultiShapeSymbolizer();
|
814 |
|
815 |
// sldMultiShape.addSldLine((SLDLineSymbolizer)ISymbol2SLDSymbolizer(multiShape.getLineSymbol()));
|
816 |
// sldMultiShape.addSldPolygon((SLDPolygonSymbolizer)ISymbol2SLDSymbolizer(multiShape.getFillSymbol()));
|
817 |
// sldMultiShape.addSldPoint((SLDPointSymbolizer)ISymbol2SLDSymbolizer(multiShape.getMarkerSymbol()));
|
818 |
|
819 |
// return sldMultiShape;
|
820 |
// }
|
821 |
//Single symbol
|
822 |
else {
|
823 |
//Made up of a line
|
824 |
if (symbol instanceof ILineSymbol) { |
825 |
ILineSymbol myLine = (ILineSymbol)symbol; |
826 |
SLDLineSymbolizer mySldLine = |
827 |
sldMan.createSLDLineSymbolizer(v); |
828 |
if (myLine.getLineStyle() != null) |
829 |
mySldLine.setStroke(createStroke(myLine, v)); |
830 |
return mySldLine;
|
831 |
} |
832 |
//Made up of a polygon
|
833 |
else if (symbol instanceof IFillSymbol) { |
834 |
IFillSymbol myFill = (IFillSymbol)symbol; |
835 |
SLDPolygonSymbolizer mySldPolygon = |
836 |
sldMan.createSLDPolygonSymbolizer(v); |
837 |
SLDFill fill = sldMan.createSLDFill(v); |
838 |
SLDGraphic graphic = sldMan.createSLDGraphic(v); |
839 |
|
840 |
if (myFill.getOutline() != null) { |
841 |
mySldPolygon.setStroke(createStroke(myFill.getOutline(), v)); |
842 |
} |
843 |
|
844 |
if (myFill instanceof IMarkerFillSymbol) { |
845 |
IMarkerFillSymbol markerFill = (IMarkerFillSymbol) symbol; |
846 |
fill.setFillGraphic(createMarkerFill( |
847 |
markerFill.getMarker(), graphic, v)); |
848 |
} |
849 |
else if (myFill instanceof IPictureFillSymbol) { |
850 |
IPictureFillSymbol picFill = (IPictureFillSymbol) symbol; |
851 |
graphic.addExternalGraphic(createPictureFill( |
852 |
picFill.getSource(), v)); |
853 |
fill.setFillGraphic(graphic); |
854 |
} |
855 |
else if (!(myFill instanceof IMarkerFillSymbol) |
856 |
&& !(myFill instanceof IPictureFillSymbol)){
|
857 |
|
858 |
if (myFill.getFillColor() != null && myFill.hasFill()){ |
859 |
|
860 |
FExpression expressionColor = new FExpression();
|
861 |
expressionColor.setLiteral(SLDUtils.convertColorToHexString(myFill.getFillColor())); |
862 |
fill.setExpressionColor(expressionColor); |
863 |
|
864 |
FExpression expressionOpacity = new FExpression();
|
865 |
float opacity =(float ) myFill.getFillColor().getAlpha() / (float) 255; |
866 |
expressionOpacity.setLiteral(String.valueOf(opacity));
|
867 |
fill.setExpressionOpacity(expressionOpacity); |
868 |
|
869 |
} |
870 |
else if (myFill.getFillColor() != null && !myFill.hasFill()){ |
871 |
|
872 |
FExpression expressionColor = new FExpression();
|
873 |
expressionColor.setLiteral(SLDUtils.convertColorToHexString(Color.white));
|
874 |
fill.setExpressionColor(expressionColor); |
875 |
|
876 |
FExpression expressionOpacity = new FExpression();
|
877 |
expressionOpacity.setLiteral(String.valueOf(255)); |
878 |
fill.setExpressionOpacity(expressionOpacity); |
879 |
} |
880 |
} |
881 |
|
882 |
mySldPolygon.setFill(fill); |
883 |
|
884 |
return mySldPolygon;
|
885 |
} |
886 |
//Made up of a marker
|
887 |
else if (symbol instanceof IMarkerSymbol) { |
888 |
IMarkerSymbol myMarker = (IMarkerSymbol)symbol; |
889 |
SLDPointSymbolizer mySldPoint = |
890 |
sldMan.createSLDPointSymbolizer(v); |
891 |
mySldPoint.setGraphic(createGraphic(myMarker, v)); |
892 |
return mySldPoint;
|
893 |
} |
894 |
} |
895 |
|
896 |
throw new SLDWriteException(new Exception( |
897 |
"Unsupported symbol: " + symbol.getClass().getName()));
|
898 |
} |
899 |
|
900 |
private static SLDGraphic createMarkerFill( |
901 |
IMarkerSymbol markerFill, |
902 |
SLDGraphic graphic, String v)
|
903 |
throws UnsupportedSLDVersionException {
|
904 |
|
905 |
SLDGraphic myGraphic = sldMan.createSLDGraphic(v); |
906 |
SLDFill fill = sldMan.createSLDFill(v); |
907 |
boolean hasOpacity = false; |
908 |
float opacity = 0 ; |
909 |
|
910 |
myGraphic = graphic; |
911 |
|
912 |
if (markerFill.getColor() != null) { |
913 |
FExpression expressionColor = new FExpression();
|
914 |
expressionColor.setLiteral(SLDUtils.convertColorToHexString(markerFill.getColor())); |
915 |
fill.setExpressionColor(expressionColor); |
916 |
|
917 |
FExpression expressionOpacity = new FExpression();
|
918 |
opacity =(float ) markerFill.getColor().getAlpha() / (float) 255; |
919 |
expressionOpacity.setLiteral(String.valueOf(opacity));
|
920 |
|
921 |
hasOpacity = true;
|
922 |
} |
923 |
|
924 |
if(markerFill instanceof IMultiLayerMarkerSymbol) { |
925 |
IMultiLayerMarkerSymbol multi = (IMultiLayerMarkerSymbol)markerFill; |
926 |
IMarkerSymbol marker = null;
|
927 |
double max = 0; |
928 |
for (int i = 0; i < multi.getLayerCount(); i++) { |
929 |
if (max < ((IMarkerSymbol)multi.getLayer(i)).getSize()) {
|
930 |
marker = (IMarkerSymbol) multi.getLayer(i); |
931 |
max = ((IMarkerSymbol)multi.getLayer(i)).getSize(); |
932 |
} |
933 |
} |
934 |
myGraphic = createMarkerFill(marker, myGraphic, v); |
935 |
} |
936 |
else if (markerFill instanceof ISimpleMarkerSymbol) { |
937 |
ISimpleMarkerSymbol myMarker = (ISimpleMarkerSymbol) markerFill; |
938 |
SLDMark mark = sldMan.createSLDMark(v); |
939 |
FExpression wellKnownName = new FExpression();
|
940 |
wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(myMarker.getStyle())); |
941 |
mark.setWellKnownName(wellKnownName); |
942 |
mark.setFill(fill); |
943 |
myGraphic.addMark(mark); |
944 |
} |
945 |
else if (markerFill instanceof IPictureMarkerSymbol) { |
946 |
IPictureMarkerSymbol myMarkerSymbol = (IPictureMarkerSymbol) markerFill; |
947 |
myGraphic.addExternalGraphic( |
948 |
createPictureFill(myMarkerSymbol.getSource(), v)); |
949 |
} |
950 |
/*
|
951 |
else if (markerFill instanceof ICharacterMarkerSymbol) {
|
952 |
SLDMark mark = (SLDMark) Class.forName(
|
953 |
classNameForVersion(SLDMark.class, sldVersion)).newInstance();
|
954 |
FExpression wellKnownName = new FExpression();
|
955 |
wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(SimpleMarkerSymbol.SQUARE_STYLE));
|
956 |
mark.setWellKnownName(wellKnownName);
|
957 |
mark.setFill(fill);
|
958 |
myGraphic.addMark(mark);
|
959 |
}
|
960 |
*/
|
961 |
|
962 |
FExpression expressionSize = new FExpression();
|
963 |
expressionSize.setLiteral(String.valueOf(markerFill.getSize()));
|
964 |
myGraphic.setExpressionSize(expressionSize); |
965 |
|
966 |
if(hasOpacity) {
|
967 |
|
968 |
FExpression expressionOpacity = new FExpression();
|
969 |
expressionOpacity.setLiteral(String.valueOf(opacity));
|
970 |
myGraphic.setExpressionOpacity(expressionOpacity); |
971 |
|
972 |
} |
973 |
return myGraphic;
|
974 |
} |
975 |
|
976 |
|
977 |
|
978 |
private static SLDExternalGraphic createPictureFill(URL img_url, String v) |
979 |
throws UnsupportedSLDVersionException {
|
980 |
|
981 |
SLDExternalGraphic extGra = sldMan.createSLDExternalGraphic(v); |
982 |
if (img_url != null) { |
983 |
extGra.addOnlineResource(img_url); |
984 |
String fpath = img_url.getFile();
|
985 |
String ext = fpath.substring(fpath.lastIndexOf(".")+1,fpath.length()); |
986 |
extGra.setFormat("image/" + ext);
|
987 |
} |
988 |
return extGra;
|
989 |
} |
990 |
|
991 |
private static SLDStroke createStroke(ILineSymbol myLine, |
992 |
String v) throws UnsupportedSLDVersionException { |
993 |
|
994 |
SLDStroke stroke = sldMan.createSLDStroke(v); |
995 |
SLDLineSymbolizer mySldLine = sldMan.createSLDLineSymbolizer(v); |
996 |
|
997 |
//If the outline symbol is a multilayer symbol the line is made up of the first
|
998 |
//layer of the multilayer symbol
|
999 |
if (myLine instanceof IMultiLayerLineSymbol) { |
1000 |
IMultiLayerLineSymbol multiLine = (IMultiLayerLineSymbol) myLine; |
1001 |
for (int i = 0; i < multiLine.getLayerCount(); i++) { |
1002 |
ILineSymbol myNewLine = (ILineSymbol) multiLine.getLayer(i); |
1003 |
if (myLine.getLineWidth() < myNewLine.getLineWidth())
|
1004 |
myLine = myNewLine; |
1005 |
} |
1006 |
myLine = (ILineSymbol) multiLine.getLayer(0);
|
1007 |
} |
1008 |
|
1009 |
BasicStroke str= (BasicStroke) myLine.getLineStyle().getStroke(); |
1010 |
|
1011 |
if (myLine.getColor() != null) { |
1012 |
stroke.setExpressionColor(SLDUtils.convertColorToHexString(myLine.getColor())); |
1013 |
float opacity =(float ) myLine.getColor().getAlpha() / (float) 255; |
1014 |
stroke.setExpressionOpacity(Double.toString(opacity));
|
1015 |
} |
1016 |
if(str != null) { |
1017 |
stroke.setFloatDashArray(str.getDashArray()); |
1018 |
stroke.setExpressionLineCap(SLDUtils.convertLineCapToString(str.getEndCap())); |
1019 |
stroke.setExpressionLineJoin(SLDUtils.convertLineJoinToString(str.getLineJoin())); |
1020 |
stroke.setExpressionWidth(Double.toString(myLine.getLineWidth()));
|
1021 |
stroke.setExpressionDashOffset(Double.toString(str.getDashPhase()));
|
1022 |
} |
1023 |
/*
|
1024 |
if(myLine instanceof IMarkerLineSymbol) {
|
1025 |
MarkerLineSymbol myMarkerLine = (MarkerLineSymbol) myLine;
|
1026 |
float[] dash = {Float.valueOf(String.valueOf(myLine.getLineWidth())),
|
1027 |
Float.valueOf(String.valueOf(myMarkerLine.getSeparation()))};
|
1028 |
stroke.setFloatDashArray(dash);
|
1029 |
stroke.setGraphic(createGraphic(myMarkerLine.getMarker(), sldVersion));
|
1030 |
}
|
1031 |
else
|
1032 |
*/
|
1033 |
if (myLine instanceof IPictureLineSymbol) { |
1034 |
IPictureLineSymbol picLine = (IPictureLineSymbol)myLine; |
1035 |
SLDGraphic graphic = sldMan.createSLDGraphic(v); |
1036 |
graphic.addExternalGraphic(createPictureFill( |
1037 |
picLine.getSource(), v)); |
1038 |
stroke.setGraphic(graphic); |
1039 |
} |
1040 |
|
1041 |
return stroke;
|
1042 |
} |
1043 |
|
1044 |
|
1045 |
|
1046 |
private static SLDGraphic createGraphic( |
1047 |
IMarkerSymbol myMarker, String v)
|
1048 |
throws UnsupportedSLDVersionException {
|
1049 |
|
1050 |
SLDGraphic graphic = sldMan.createSLDGraphic(v); |
1051 |
graphic.getExpressionSize().setLiteral(String.valueOf(myMarker.getSize()));
|
1052 |
graphic.getExpressionRotation().setLiteral(String.valueOf(myMarker.getRotation()));
|
1053 |
graphic.getExpressionOpacity().setLiteral(String.valueOf(myMarker.getColor().getAlpha()/255)); |
1054 |
|
1055 |
if (myMarker instanceof ISimpleMarkerSymbol) { |
1056 |
ISimpleMarkerSymbol marker = (ISimpleMarkerSymbol) myMarker; |
1057 |
|
1058 |
SLDStroke str = sldMan.createSLDStroke(v); |
1059 |
SLDMark mark = sldMan.createSLDMark(v); |
1060 |
if (marker.getOutlineColor() != null) |
1061 |
str.setExpressionColor(SLDUtils.convertColorToHexString( |
1062 |
marker.getOutlineColor())); |
1063 |
|
1064 |
str.setExpressionWidth(String.valueOf(marker.getOutlineSize()));
|
1065 |
mark.setStroke(str); |
1066 |
if (marker.getColor() != null) { |
1067 |
|
1068 |
SLDFill fill = sldMan.createSLDFill(v); |
1069 |
FExpression expressionColor = new FExpression();
|
1070 |
expressionColor.setLiteral(SLDUtils.convertColorToHexString( |
1071 |
marker.getColor())); |
1072 |
fill.setExpressionColor(expressionColor); |
1073 |
|
1074 |
FExpression expressionOpacity = new FExpression();
|
1075 |
float opacity =(float ) marker.getColor().getAlpha() / (float) 255; |
1076 |
expressionOpacity.setLiteral(String.valueOf(opacity));
|
1077 |
fill.setExpressionOpacity(expressionOpacity); |
1078 |
|
1079 |
mark.setFill(fill); |
1080 |
|
1081 |
} |
1082 |
FExpression wellKnownName = new FExpression();
|
1083 |
wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(marker.getStyle())); |
1084 |
mark.setWellKnownName(wellKnownName); |
1085 |
graphic.addMark(mark); |
1086 |
} |
1087 |
else if (myMarker instanceof IPictureMarkerSymbol) { |
1088 |
IPictureMarkerSymbol marker = (IPictureMarkerSymbol) myMarker; |
1089 |
SLDExternalGraphic ext = sldMan.createSLDExternalGraphic(v); |
1090 |
if (marker.getSource() != null) { |
1091 |
ext.addOnlineResource(marker.getSource()); |
1092 |
} |
1093 |
graphic.addExternalGraphic(ext); |
1094 |
} |
1095 |
/*
|
1096 |
else if (myMarker instanceof CharacterMarkerSymbol) {
|
1097 |
SLDMark mark = (SLDMark) Class.forName(
|
1098 |
classNameForVersion(SLDMark.class, sldVersion)).newInstance();
|
1099 |
FExpression wellKnownName = new FExpression();
|
1100 |
wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(SimpleMarkerSymbol.SQUARE_STYLE));
|
1101 |
mark.setWellKnownName(wellKnownName);
|
1102 |
|
1103 |
if (myMarker.getColor() != null) {
|
1104 |
SLDFill fill = (SLDFill) Class.forName(
|
1105 |
classNameForVersion(SLDFill.class, sldVersion)).newInstance();
|
1106 |
|
1107 |
FExpression expressionColor = new FExpression();
|
1108 |
expressionColor.setLiteral(SLDUtils.convertColorToHexString(myMarker.getColor()));
|
1109 |
fill.setExpressionColor(expressionColor);
|
1110 |
|
1111 |
FExpression expressionOpacity = new FExpression();
|
1112 |
float opacity =(float ) myMarker.getColor().getAlpha() / (float) 255;
|
1113 |
expressionOpacity.setLiteral(String.valueOf(opacity));
|
1114 |
fill.setExpressionOpacity(expressionOpacity);
|
1115 |
|
1116 |
mark.setFill(fill);
|
1117 |
}
|
1118 |
graphic.addMark(mark);
|
1119 |
}
|
1120 |
*/
|
1121 |
|
1122 |
return graphic;
|
1123 |
} |
1124 |
|
1125 |
} |