Mercurial > mxd2map
annotate src/java/de/intevation/mxd/writer/FillStyleWriter.java @ 123:3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Fri, 17 Jun 2011 13:36:33 +0200 |
parents | 11d63bf00326 |
children | cd18c61cbcf6 |
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) { |
123
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
49 logger.debug("write()Element"); |
97 | 50 symbolSetObj symbolSet = map.getSymbolset(); |
51 | |
52 if(symbolElement.hasChildNodes()) { | |
53 NodeList symbols = symbolElement.getChildNodes(); | |
54 for (int i = 0; i < symbols.getLength(); i++) { | |
55 Element nextSym = (Element)symbols.item(i); | |
56 String type = nextSym.getAttribute("type"); | |
57 | |
58 if(((symbols.getLength() > 1 && i == 0) || | |
59 (symbols.getLength() == 1 && | |
60 !symbolElement.hasAttribute("hatch"))) && | |
61 type.equals("line")) { | |
62 writeOutline(nextSym); | |
63 if (symbols.getLength() == 1) { | |
64 writeSimple(symbolElement); | |
65 } | |
66 } | |
67 else if(nextSym.getTagName().equals("symbol") && | |
68 !symbolElement.hasAttribute("hatch") || | |
69 (i == 1 && type.equals("marker"))) { | |
70 double gap = 0; | |
71 if(symbolElement.hasAttribute("xseparation")) { | |
72 gap = Double.parseDouble( | |
73 symbolElement.getAttribute("xseparation")); | |
74 } | |
75 writeMarker(nextSym, gap); | |
76 } | |
77 else if (nextSym.getTagName().equals("symbol") && | |
78 symbolElement.hasAttribute("hatch")) { | |
79 if(symbolElement.hasAttribute("angle")) { | |
80 nextSym.setAttribute( | |
81 "angle", | |
82 symbolElement.getAttribute("angle")); | |
83 } | |
84 if(symbolElement.hasAttribute("separation")) { | |
85 nextSym.setAttribute( | |
86 "size", | |
87 symbolElement.getAttribute("separation")); | |
88 } | |
89 writeMarker(nextSym, -1); | |
90 } | |
91 else { | |
92 writeSimple(symbolElement); | |
93 } | |
94 } | |
95 } | |
96 else { | |
97 writeSimple(symbolElement); | |
98 if(symbolElement.hasAttribute("outline_color")) { | |
99 Color oCol = Color.decode( | |
100 symbolElement.getAttribute("outline_color")); | |
101 colorObj outlineColor = new colorObj( | |
102 oCol.getRed(), | |
103 oCol.getGreen(), | |
104 oCol.getBlue(), | |
105 -4); | |
106 style.setOutlinecolor(outlineColor); | |
107 style.setOutlinewidth(Double.parseDouble( | |
108 symbolElement.getAttribute("outline_size"))); | |
109 } | |
110 | |
111 } | |
112 | |
113 return true; | |
114 } | |
115 | |
116 /** | |
117 * Write the outline for a polygon. | |
118 */ | |
120
11d63bf00326
Changed exception handling and logging in writer classes.
Raimund Renkert <rrenkert@intevation.de>
parents:
101
diff
changeset
|
119 private void writeOutline(Element symbolElement) { |
97 | 120 logger.debug("writeOutline()"); |
121 //write transparent outline | |
122 colorObj color = new colorObj(-1, -1, -1, -4); | |
123 | |
124 //write new style for the outline | |
125 //TODO write further attribute like pattern etc. | |
126 Color oCol = Color.decode( | |
127 symbolElement.getAttribute("color")); | |
128 | |
129 styleObj outline = new styleObj (cl); | |
130 | |
131 colorObj outlinecolor = new colorObj( | |
132 oCol.getRed(), | |
133 oCol.getGreen(), | |
134 oCol.getBlue(), | |
135 -4); | |
136 outline.setOutlinecolor(outlinecolor); | |
137 outline.setOutlinewidth(Double.parseDouble( | |
138 symbolElement.getAttribute("width"))); | |
123
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
139 if(symbolElement.hasAttribute("linestyle")) { |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
140 String ls = symbolElement.getAttribute("linestyle"); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
141 double[] vals; |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
142 if(ls.equals("dash")) { |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
143 outline.setPatternlength(2); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
144 vals = new double[] {4.0, 4.0, 0, 0, 0, 0, 0, 0, 0, 0}; |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
145 outline.setPattern(vals); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
146 } |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
147 else if(ls.equals("dot")) { |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
148 outline.setPatternlength(2); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
149 vals = new double[] {1.0, 3.0, 0, 0, 0, 0, 0, 0, 0, 0}; |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
150 outline.setPattern(vals); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
151 } |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
152 else if(ls.equals("dashdot")) { |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
153 outline.setPatternlength(4); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
154 vals = new double[] {4.0, 3.0, 1.0, 3.0, 0, 0, 0, 0, 0, 0, 0}; |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
155 outline.setPattern(vals); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
156 } |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
157 else if (ls.equals("dashdotdot")) { |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
158 outline.setPatternlength(6); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
159 vals = new double[] {5.0, 3.0, 1.0, 3.0, 1.0, 4.0, 0, 0, 0, 0}; |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
160 outline.setPattern(vals); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
161 } |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
162 } |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
163 |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
164 |
97 | 165 } |
166 | |
167 /** | |
168 * Write marker attributes and a symbol for the polygon fill. | |
169 */ | |
120
11d63bf00326
Changed exception handling and logging in writer classes.
Raimund Renkert <rrenkert@intevation.de>
parents:
101
diff
changeset
|
170 private void writeMarker(Element symbolElement, double gap) { |
97 | 171 logger.debug("writeMarker()"); |
172 String name = symbolElement.getAttribute("name"); | |
173 String type = symbolElement.getAttribute("type"); | |
174 if (symbolElement.hasAttribute("angle")) { | |
175 style.setAngle( | |
176 Double.parseDouble(symbolElement.getAttribute("angle"))); | |
177 } | |
178 if(symbolElement.hasAttribute("color")) { | |
179 String c = symbolElement.getAttribute("color"); | |
180 Color col = Color.decode(c); | |
181 colorObj color = new colorObj( | |
182 col.getRed(), | |
183 col.getGreen(), | |
184 col.getBlue(), | |
185 -4); | |
186 style.setColor(color); | |
187 } | |
188 if (symbolElement.hasAttribute ("size")) { | |
189 double size = Double.parseDouble( | |
190 symbolElement.getAttribute("size")); | |
191 style.setSize(size); | |
192 //In arcgis the separation goes from center to center, so the gap is | |
193 //the separation - size | |
194 if (gap > 0) { | |
195 style.setGap(gap - size); | |
196 } | |
197 } | |
198 if(symbolElement.hasAttribute("outline_color")) { | |
199 Color oCol = Color.decode( | |
200 symbolElement.getAttribute("outline_color")); | |
201 colorObj outlineColor = new colorObj( | |
202 oCol.getRed(), | |
203 oCol.getGreen(), | |
204 oCol.getBlue(), | |
205 -4); | |
206 style.setOutlinecolor(outlineColor); | |
207 style.setOutlinewidth(Double.parseDouble( | |
208 symbolElement.getAttribute("outline_size"))); | |
209 } | |
210 if(type.equals("marker")) { | |
211 style.setSymbolByName(map, name); | |
212 SymbolWriter sw = new SymbolWriter(this.map, this.cl); | |
213 sw.write(symbolElement); | |
214 } | |
215 else if(type.equals("line")) { | |
216 style.setSymbolByName(map, "hatch"); | |
217 SymbolWriter sw = new SymbolWriter(this.map, this.cl); | |
218 symbolElement.setAttribute("name", "hatch"); | |
219 sw.write(symbolElement); | |
123
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
220 if(symbolElement.hasAttribute("linestyle")) { |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
221 String ls = symbolElement.getAttribute("linestyle"); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
222 double[] vals; |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
223 if(ls.equals("dash")) { |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
224 style.setPatternlength(2); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
225 vals = new double[] {4.0, 4.0, 0, 0, 0, 0, 0, 0, 0, 0}; |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
226 style.setPattern(vals); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
227 } |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
228 else if(ls.equals("dot")) { |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
229 style.setPatternlength(2); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
230 vals = new double[] {1.0, 3.0, 0, 0, 0, 0, 0, 0, 0, 0}; |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
231 style.setPattern(vals); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
232 } |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
233 else if(ls.equals("dashdot")) { |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
234 style.setPatternlength(4); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
235 vals = new double[] {4.0, 3.0, 1.0, 3.0, 0, 0, 0, 0, 0, 0, 0}; |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
236 style.setPattern(vals); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
237 } |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
238 else if (ls.equals("dashdotdot")) { |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
239 style.setPatternlength(6); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
240 vals = new double[] {5.0, 3.0, 1.0, 3.0, 1.0, 4.0, 0, 0, 0, 0}; |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
241 style.setPattern(vals); |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
242 } |
3c792458a716
Write PATTERN for polygon outlines and hatch symbols.
Raimund Renkert <rrenkert@intevation.de>
parents:
120
diff
changeset
|
243 } |
97 | 244 } |
245 } | |
246 | |
247 /** | |
248 * Write simple fill attributes. | |
249 */ | |
250 private void writeSimple(Element symbolElement) { | |
251 logger.debug("writeSimple(Element)"); | |
252 if(symbolElement.hasAttribute("transparency")) { | |
253 double value = Double.parseDouble( | |
254 symbolElement.getAttribute("transparency")); | |
255 int opacity = (int)(value/255) * 100; | |
256 if(value >= 0) { | |
257 style.setOpacity(opacity); | |
258 } | |
259 } | |
260 if(symbolElement.hasAttribute("color")) { | |
261 String c = symbolElement.getAttribute("color"); | |
262 Color col = Color.decode(c); | |
263 colorObj color = new colorObj( | |
264 col.getRed(), | |
265 col.getGreen(), | |
266 col.getBlue(), | |
267 -4); | |
268 style.setColor(color); | |
269 } | |
270 } | |
271 } | |
272 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |