comparison src/java/de/intevation/mxd/writer/FillStyleWriter.java @ 106:609c234e8e23

merged with reimunds changes
author Stephan Holl <stephan.holl@intevation.de>
date Tue, 07 Jun 2011 15:50:43 +0200
parents 18e4f143896b
children 461ee9193097
comparison
equal deleted inserted replaced
105:db18c1b33936 106:609c234e8e23
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 }
186
187 if(type.equals("marker")) {
188 style.setSymbolByName(map, name);
189 SymbolWriter sw = new SymbolWriter(this.map, this.cl);
190 sw.write(symbolElement);
191 }
192 else if(type.equals("line")) {
193 style.setSymbolByName(map, "hatch");
194 SymbolWriter sw = new SymbolWriter(this.map, this.cl);
195 symbolElement.setAttribute("name", "hatch");
196 sw.write(symbolElement);
197 }
198 }
199
200 /**
201 * Write simple fill attributes.
202 */
203 private void writeSimple(Element symbolElement) {
204 logger.debug("writeSimple(Element)");
205 if(symbolElement.hasAttribute("transparency")) {
206 double value = Double.parseDouble(
207 symbolElement.getAttribute("transparency"));
208 int opacity = (int)(value/255) * 100;
209 if(value >= 0) {
210 style.setOpacity(opacity);
211 }
212 }
213 if(symbolElement.hasAttribute("color")) {
214 String c = symbolElement.getAttribute("color");
215 Color col = Color.decode(c);
216 colorObj color = new colorObj(
217 col.getRed(),
218 col.getGreen(),
219 col.getBlue(),
220 -4);
221 style.setColor(color);
222 }
223 }
224 }
225 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)