Mercurial > mxd2map
annotate src/java/de/intevation/mxd/writer/FillStyleWriter.java @ 120:11d63bf00326
Changed exception handling and logging in writer classes.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Thu, 16 Jun 2011 14:49:45 +0200 |
parents | 461ee9193097 |
children | 3c792458a716 |
rev | line source |
---|---|
97 | 1 package de.intevation.mxd.writer; |
2 | |
3 import java.io.IOException; | |
4 import java.awt.Color; | |
5 import org.apache.log4j.Logger; | |
6 | |
7 import org.w3c.dom.Element; | |
8 import org.w3c.dom.NodeList; | |
9 | |
10 import edu.umn.gis.mapscript.mapObj; | |
11 import edu.umn.gis.mapscript.layerObj; | |
12 import edu.umn.gis.mapscript.classObj; | |
13 import edu.umn.gis.mapscript.styleObj; | |
14 import edu.umn.gis.mapscript.colorObj; | |
15 import edu.umn.gis.mapscript.symbolObj; | |
16 import edu.umn.gis.mapscript.symbolSetObj; | |
17 import edu.umn.gis.mapscript.lineObj; | |
18 import edu.umn.gis.mapscript.pointObj; | |
19 | |
20 | |
21 /** | |
22 * The interface to the mapfile writer. | |
23 * | |
24 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> | |
25 */ | |
26 public class FillStyleWriter { | |
27 | |
28 /** | |
29 * The Logger. | |
30 */ | |
31 private static final Logger logger = | |
32 Logger.getLogger(FillStyleWriter.class); | |
33 | |
34 private mapObj map; | |
35 private classObj cl; | |
36 private styleObj style; | |
37 | |
38 public FillStyleWriter (mapObj map, classObj cl) { | |
39 logger.debug("contructor(mapObj, classObj)"); | |
40 this.map = map; | |
41 this.cl = cl; | |
42 this.style = new styleObj(cl); | |
43 } | |
44 | |
45 /** | |
46 * Write the content. | |
47 */ | |
120
11d63bf00326
Changed exception handling and logging in writer classes.
Raimund Renkert <rrenkert@intevation.de>
parents:
101
diff
changeset
|
48 public boolean write(Element symbolElement) { |
97 | 49 symbolSetObj symbolSet = map.getSymbolset(); |
50 | |
51 if(symbolElement.hasChildNodes()) { | |
52 NodeList symbols = symbolElement.getChildNodes(); | |
53 for (int i = 0; i < symbols.getLength(); i++) { | |
54 Element nextSym = (Element)symbols.item(i); | |
55 String type = nextSym.getAttribute("type"); | |
56 | |
57 if(((symbols.getLength() > 1 && i == 0) || | |
58 (symbols.getLength() == 1 && | |
59 !symbolElement.hasAttribute("hatch"))) && | |
60 type.equals("line")) { | |
61 writeOutline(nextSym); | |
62 if (symbols.getLength() == 1) { | |
63 writeSimple(symbolElement); | |
64 } | |
65 } | |
66 else if(nextSym.getTagName().equals("symbol") && | |
67 !symbolElement.hasAttribute("hatch") || | |
68 (i == 1 && type.equals("marker"))) { | |
69 double gap = 0; | |
70 if(symbolElement.hasAttribute("xseparation")) { | |
71 gap = Double.parseDouble( | |
72 symbolElement.getAttribute("xseparation")); | |
73 } | |
74 writeMarker(nextSym, gap); | |
75 } | |
76 else if (nextSym.getTagName().equals("symbol") && | |
77 symbolElement.hasAttribute("hatch")) { | |
78 if(symbolElement.hasAttribute("angle")) { | |
79 nextSym.setAttribute( | |
80 "angle", | |
81 symbolElement.getAttribute("angle")); | |
82 } | |
83 if(symbolElement.hasAttribute("separation")) { | |
84 nextSym.setAttribute( | |
85 "size", | |
86 symbolElement.getAttribute("separation")); | |
87 } | |
88 writeMarker(nextSym, -1); | |
89 } | |
90 else { | |
91 writeSimple(symbolElement); | |
92 } | |
93 } | |
94 } | |
95 else { | |
96 writeSimple(symbolElement); | |
97 if(symbolElement.hasAttribute("outline_color")) { | |
98 Color oCol = Color.decode( | |
99 symbolElement.getAttribute("outline_color")); | |
100 colorObj outlineColor = new colorObj( | |
101 oCol.getRed(), | |
102 oCol.getGreen(), | |
103 oCol.getBlue(), | |
104 -4); | |
105 style.setOutlinecolor(outlineColor); | |
106 style.setOutlinewidth(Double.parseDouble( | |
107 symbolElement.getAttribute("outline_size"))); | |
108 } | |
109 | |
110 } | |
111 | |
112 return true; | |
113 } | |
114 | |
115 /** | |
116 * Write the outline for a polygon. | |
117 */ | |
120
11d63bf00326
Changed exception handling and logging in writer classes.
Raimund Renkert <rrenkert@intevation.de>
parents:
101
diff
changeset
|
118 private void writeOutline(Element symbolElement) { |
97 | 119 logger.debug("writeOutline()"); |
120 //write transparent outline | |
121 colorObj color = new colorObj(-1, -1, -1, -4); | |
122 | |
123 //write new style for the outline | |
124 //TODO write further attribute like pattern etc. | |
125 Color oCol = Color.decode( | |
126 symbolElement.getAttribute("color")); | |
127 | |
128 styleObj outline = new styleObj (cl); | |
129 | |
130 colorObj outlinecolor = new colorObj( | |
131 oCol.getRed(), | |
132 oCol.getGreen(), | |
133 oCol.getBlue(), | |
134 -4); | |
135 outline.setOutlinecolor(outlinecolor); | |
136 outline.setOutlinewidth(Double.parseDouble( | |
137 symbolElement.getAttribute("width"))); | |
138 } | |
139 | |
140 /** | |
141 * Write marker attributes and a symbol for the polygon fill. | |
142 */ | |
120
11d63bf00326
Changed exception handling and logging in writer classes.
Raimund Renkert <rrenkert@intevation.de>
parents:
101
diff
changeset
|
143 private void writeMarker(Element symbolElement, double gap) { |
97 | 144 logger.debug("writeMarker()"); |
145 String name = symbolElement.getAttribute("name"); | |
146 String type = symbolElement.getAttribute("type"); | |
147 if (symbolElement.hasAttribute("angle")) { | |
148 style.setAngle( | |
149 Double.parseDouble(symbolElement.getAttribute("angle"))); | |
150 } | |
151 if(symbolElement.hasAttribute("color")) { | |
152 String c = symbolElement.getAttribute("color"); | |
153 Color col = Color.decode(c); | |
154 colorObj color = new colorObj( | |
155 col.getRed(), | |
156 col.getGreen(), | |
157 col.getBlue(), | |
158 -4); | |
159 style.setColor(color); | |
160 } | |
161 if (symbolElement.hasAttribute ("size")) { | |
162 double size = Double.parseDouble( | |
163 symbolElement.getAttribute("size")); | |
164 style.setSize(size); | |
165 //In arcgis the separation goes from center to center, so the gap is | |
166 //the separation - size | |
167 if (gap > 0) { | |
168 style.setGap(gap - size); | |
169 } | |
170 } | |
171 if(symbolElement.hasAttribute("outline_color")) { | |
172 Color oCol = Color.decode( | |
173 symbolElement.getAttribute("outline_color")); | |
174 colorObj outlineColor = new colorObj( | |
175 oCol.getRed(), | |
176 oCol.getGreen(), | |
177 oCol.getBlue(), | |
178 -4); | |
179 style.setOutlinecolor(outlineColor); | |
180 style.setOutlinewidth(Double.parseDouble( | |
181 symbolElement.getAttribute("outline_size"))); | |
182 } | |
101
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
183 if(symbolElement.hasAttribute("linestyle")) { |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
184 String ls = symbolElement.getAttribute("linestyle"); |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
185 double[] vals; |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
186 if(ls.equals("dash")) { |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
187 style.setPatternlength(2); |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
188 vals = new double[] {2.0, 2.0}; |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
189 style.setPattern(vals); |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
190 } |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
191 else if(ls.equals("dot")) { |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
192 style.setPatternlength(2); |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
193 vals = new double[] {1.0, 2.0}; |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
194 style.setPattern(vals); |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
195 } |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
196 else if(ls.equals("dashdot")) { |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
197 style.setPatternlength(4); |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
198 vals = new double[] {2.0, 2.0, 1.0, 2.0}; |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
199 style.setPattern(vals); |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
200 } |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
201 else if (ls.equals("dashdotdot")) { |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
202 style.setPatternlength(6); |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
203 vals = new double[] {2.0, 2.0, 1.0, 2.0, 1.0, 2.0}; |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
204 style.setPattern(vals); |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
205 } |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
206 } |
97 | 207 |
208 if(type.equals("marker")) { | |
209 style.setSymbolByName(map, name); | |
210 SymbolWriter sw = new SymbolWriter(this.map, this.cl); | |
211 sw.write(symbolElement); | |
212 } | |
213 else if(type.equals("line")) { | |
214 style.setSymbolByName(map, "hatch"); | |
215 SymbolWriter sw = new SymbolWriter(this.map, this.cl); | |
216 symbolElement.setAttribute("name", "hatch"); | |
217 sw.write(symbolElement); | |
218 } | |
219 } | |
220 | |
221 /** | |
222 * Write simple fill attributes. | |
223 */ | |
224 private void writeSimple(Element symbolElement) { | |
225 logger.debug("writeSimple(Element)"); | |
226 if(symbolElement.hasAttribute("transparency")) { | |
227 double value = Double.parseDouble( | |
228 symbolElement.getAttribute("transparency")); | |
229 int opacity = (int)(value/255) * 100; | |
230 if(value >= 0) { | |
231 style.setOpacity(opacity); | |
232 } | |
233 } | |
234 if(symbolElement.hasAttribute("color")) { | |
235 String c = symbolElement.getAttribute("color"); | |
236 Color col = Color.decode(c); | |
237 colorObj color = new colorObj( | |
238 col.getRed(), | |
239 col.getGreen(), | |
240 col.getBlue(), | |
241 -4); | |
242 style.setColor(color); | |
243 } | |
244 } | |
245 } | |
246 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |