root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / SLDUtils.java @ 9641
History | View | Annotate | Download (3.71 KB)
1 |
package com.iver.cit.gvsig.fmap.core; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.util.StringTokenizer; |
5 |
|
6 |
import org.geotools.styling.Fill; |
7 |
import org.geotools.styling.GraphicImpl; |
8 |
import org.geotools.styling.LineSymbolizer; |
9 |
import org.geotools.styling.Mark; |
10 |
import org.geotools.styling.PointSymbolizer; |
11 |
import org.geotools.styling.PolygonSymbolizer; |
12 |
import org.geotools.styling.StrokeImpl; |
13 |
import org.geotools.styling.StyleBuilder; |
14 |
import org.geotools.styling.StyleFactory; |
15 |
import org.geotools.styling.Symbolizer; |
16 |
|
17 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
18 |
import com.iver.cit.gvsig.fmap.core.v02.FConstant; |
19 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
20 |
|
21 |
/**
|
22 |
* utility class for SLD functionality
|
23 |
* @author laura
|
24 |
*/
|
25 |
public class SLDUtils { |
26 |
|
27 |
public static Color convertHexStringToColor(String str) throws NumberFormatException{ |
28 |
int multiplier = 1; |
29 |
StringTokenizer tokenizer = new StringTokenizer(str, " \t\r\n\b:;[]()+"); |
30 |
while (tokenizer.hasMoreTokens()) {
|
31 |
multiplier = 1;
|
32 |
String token = tokenizer.nextToken();
|
33 |
if (null == token) { |
34 |
throw new NumberFormatException(str); |
35 |
} |
36 |
if (token.startsWith("-")) { |
37 |
multiplier = -1;
|
38 |
token = token.substring(1);
|
39 |
} |
40 |
int point_index = token.indexOf("."); |
41 |
if (point_index > 0) { |
42 |
token = token.substring(0, point_index);
|
43 |
} else if (point_index == 0) { |
44 |
return new Color(0); |
45 |
} |
46 |
try {
|
47 |
if (token.startsWith("0x")) { |
48 |
return new Color(multiplier |
49 |
* Integer.parseInt(token.substring(2), 16)); |
50 |
} else if (token.startsWith("#")) { |
51 |
return new Color(multiplier |
52 |
* Integer.parseInt(token.substring(1), 16)); |
53 |
} else if (token.startsWith("0") && !token.equals("0")) { |
54 |
return new Color(multiplier * Integer.parseInt(token.substring(1), 8)); |
55 |
} else {
|
56 |
return new Color(multiplier * Integer.parseInt(token)); |
57 |
} |
58 |
} catch (NumberFormatException e) { |
59 |
continue;
|
60 |
} |
61 |
} |
62 |
throw new NumberFormatException(str); |
63 |
|
64 |
} |
65 |
public static String convertColorToHexString(java.awt.Color c) |
66 |
{ |
67 |
String str = Integer.toHexString( c.getRGB() & 0xFFFFFF ); |
68 |
return ( "#" + "000000".substring( str.length() ) + str.toUpperCase() ); |
69 |
} |
70 |
public String getWellKnownName(int symbolType) |
71 |
{ |
72 |
return ""; |
73 |
} |
74 |
|
75 |
public static Symbolizer toGeotoolsSymbol(ISymbol sym) |
76 |
{ |
77 |
FSymbol symbol = (FSymbol) sym; |
78 |
StyleFactory styleFactory = StyleFactory.createStyleFactory(); |
79 |
StyleBuilder styleBuilder = new StyleBuilder();
|
80 |
StrokeImpl theStroke = (StrokeImpl)styleBuilder.createStroke(); |
81 |
GraphicImpl graphic = (GraphicImpl)styleBuilder.createGraphic(); |
82 |
try
|
83 |
{ |
84 |
switch (symbol.getSymbolType())
|
85 |
{ |
86 |
case FConstant.SYMBOL_TYPE_POINT:
|
87 |
PointSymbolizer point = styleFactory.createPointSymbolizer(); |
88 |
graphic.setSize(symbol.getSize()); |
89 |
Mark[] mark = new Mark[1]; |
90 |
mark[0] = styleFactory.createMark();
|
91 |
//
|
92 |
// mark[0].setWellKnownName(
|
93 |
// getWellKnownName(symbol.getStyle()));
|
94 |
graphic.setMarks(mark); |
95 |
|
96 |
// add, color, graphic, size, ...
|
97 |
return point;
|
98 |
case FConstant.SYMBOL_TYPE_LINE:
|
99 |
LineSymbolizer line = styleFactory.createLineSymbolizer(); |
100 |
theStroke.setColor(convertColorToHexString(symbol.getColor())); |
101 |
line.setStroke(theStroke); |
102 |
return line;
|
103 |
case FConstant.SYMBOL_TYPE_FILL:
|
104 |
PolygonSymbolizer polygon = styleFactory.createPolygonSymbolizer(); |
105 |
Fill theFill = styleBuilder.createFill(symbol.getColor()); |
106 |
//theFill.setOpacity();
|
107 |
polygon.setFill( theFill ); |
108 |
//theStroke.setColor(symbol.getOutlineColor());
|
109 |
return polygon;
|
110 |
case FShape.MULTI:
|
111 |
// To implement: ver que clase de simbolo corresponde
|
112 |
return null; |
113 |
} |
114 |
return null; |
115 |
} |
116 |
catch(Exception e) |
117 |
{ |
118 |
e.printStackTrace(); |
119 |
return null; |
120 |
} |
121 |
} |
122 |
} |