Mercurial > mxd2map
annotate src/java/de/intevation/mxd/writer/FillStyleWriter.java @ 113:0db6eacad0e6
Read SDE connection settings.
author | vc11884admin@VC11884.win.bsh.de |
---|---|
date | Tue, 14 Jun 2011 17:19:49 +0200 |
parents | 461ee9193097 |
children | 11d63bf00326 |
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 */ | |
48 public boolean write(Element symbolElement) | |
49 throws Exception { | |
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 */ | |
119 private void writeOutline(Element symbolElement) | |
120 throws Exception { | |
121 logger.debug("writeOutline()"); | |
122 //write transparent outline | |
123 colorObj color = new colorObj(-1, -1, -1, -4); | |
124 | |
125 //write new style for the outline | |
126 //TODO write further attribute like pattern etc. | |
127 Color oCol = Color.decode( | |
128 symbolElement.getAttribute("color")); | |
129 | |
130 styleObj outline = new styleObj (cl); | |
131 | |
132 colorObj outlinecolor = new colorObj( | |
133 oCol.getRed(), | |
134 oCol.getGreen(), | |
135 oCol.getBlue(), | |
136 -4); | |
137 outline.setOutlinecolor(outlinecolor); | |
138 outline.setOutlinewidth(Double.parseDouble( | |
139 symbolElement.getAttribute("width"))); | |
140 } | |
141 | |
142 /** | |
143 * Write marker attributes and a symbol for the polygon fill. | |
144 */ | |
145 private void writeMarker(Element symbolElement, double gap) | |
146 throws Exception { | |
147 logger.debug("writeMarker()"); | |
148 String name = symbolElement.getAttribute("name"); | |
149 String type = symbolElement.getAttribute("type"); | |
150 if (symbolElement.hasAttribute("angle")) { | |
151 style.setAngle( | |
152 Double.parseDouble(symbolElement.getAttribute("angle"))); | |
153 } | |
154 if(symbolElement.hasAttribute("color")) { | |
155 String c = symbolElement.getAttribute("color"); | |
156 Color col = Color.decode(c); | |
157 colorObj color = new colorObj( | |
158 col.getRed(), | |
159 col.getGreen(), | |
160 col.getBlue(), | |
161 -4); | |
162 style.setColor(color); | |
163 } | |
164 if (symbolElement.hasAttribute ("size")) { | |
165 double size = Double.parseDouble( | |
166 symbolElement.getAttribute("size")); | |
167 style.setSize(size); | |
168 //In arcgis the separation goes from center to center, so the gap is | |
169 //the separation - size | |
170 if (gap > 0) { | |
171 style.setGap(gap - size); | |
172 } | |
173 } | |
174 if(symbolElement.hasAttribute("outline_color")) { | |
175 Color oCol = Color.decode( | |
176 symbolElement.getAttribute("outline_color")); | |
177 colorObj outlineColor = new colorObj( | |
178 oCol.getRed(), | |
179 oCol.getGreen(), | |
180 oCol.getBlue(), | |
181 -4); | |
182 style.setOutlinecolor(outlineColor); | |
183 style.setOutlinewidth(Double.parseDouble( | |
184 symbolElement.getAttribute("outline_size"))); | |
185 } | |
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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 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
|
193 } |
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 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
|
195 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
|
196 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
|
197 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
|
198 } |
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 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
|
200 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
|
201 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
|
202 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
|
203 } |
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 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
|
205 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
|
206 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
|
207 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
|
208 } |
461ee9193097
Write PATTERN to the style if a hatch symbol is used to fill a
Raimund Renkert <rrenkert@intevation.de>
parents:
97
diff
changeset
|
209 } |
97 | 210 |
211 if(type.equals("marker")) { | |
212 style.setSymbolByName(map, name); | |
213 SymbolWriter sw = new SymbolWriter(this.map, this.cl); | |
214 sw.write(symbolElement); | |
215 } | |
216 else if(type.equals("line")) { | |
217 style.setSymbolByName(map, "hatch"); | |
218 SymbolWriter sw = new SymbolWriter(this.map, this.cl); | |
219 symbolElement.setAttribute("name", "hatch"); | |
220 sw.write(symbolElement); | |
221 } | |
222 } | |
223 | |
224 /** | |
225 * Write simple fill attributes. | |
226 */ | |
227 private void writeSimple(Element symbolElement) { | |
228 logger.debug("writeSimple(Element)"); | |
229 if(symbolElement.hasAttribute("transparency")) { | |
230 double value = Double.parseDouble( | |
231 symbolElement.getAttribute("transparency")); | |
232 int opacity = (int)(value/255) * 100; | |
233 if(value >= 0) { | |
234 style.setOpacity(opacity); | |
235 } | |
236 } | |
237 if(symbolElement.hasAttribute("color")) { | |
238 String c = symbolElement.getAttribute("color"); | |
239 Color col = Color.decode(c); | |
240 colorObj color = new colorObj( | |
241 col.getRed(), | |
242 col.getGreen(), | |
243 col.getBlue(), | |
244 -4); | |
245 style.setColor(color); | |
246 } | |
247 } | |
248 } | |
249 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |