comparison src/java/de/intevation/mxd/writer/MapScriptWriter.java @ 42:395307e8b7ee

First MapScript Writer that generates valid mapfiles.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 15 Apr 2011 14:14:49 +0200
parents 60ed2164035a
children ef7ca23c4233
comparison
equal deleted inserted replaced
41:60ed2164035a 42:395307e8b7ee
2 2
3 import org.apache.log4j.Logger; 3 import org.apache.log4j.Logger;
4 4
5 import org.w3c.dom.Document; 5 import org.w3c.dom.Document;
6 import org.w3c.dom.Element; 6 import org.w3c.dom.Element;
7 import org.w3c.dom.Node;
8 import org.w3c.dom.NodeList; 7 import org.w3c.dom.NodeList;
8
9 import javax.xml.xpath.XPathConstants; 9 import javax.xml.xpath.XPathConstants;
10 import java.awt.Color;
10 11
11 import edu.umn.gis.mapscript.mapObj; 12 import edu.umn.gis.mapscript.mapObj;
12 import edu.umn.gis.mapscript.layerObj; 13 import edu.umn.gis.mapscript.layerObj;
14 import edu.umn.gis.mapscript.classObj;
15 import edu.umn.gis.mapscript.styleObj;
16 import edu.umn.gis.mapscript.colorObj;
17 import edu.umn.gis.mapscript.symbolObj;
18 import edu.umn.gis.mapscript.symbolSetObj;
19 import edu.umn.gis.mapscript.lineObj;
20 import edu.umn.gis.mapscript.pointObj;
21
13 import edu.umn.gis.mapscript.MS_UNITS; 22 import edu.umn.gis.mapscript.MS_UNITS;
23 import edu.umn.gis.mapscript.MS_LAYER_TYPE;
24 import edu.umn.gis.mapscript.MS_SYMBOL_TYPE;
14 25
15 import de.intevation.mxd.utils.XMLUtils; 26 import de.intevation.mxd.utils.XMLUtils;
16 27
17 /** 28 /**
18 * The MXD file reader. 29 * The MXD file reader.
48 if(filename.endsWith(".mxd")) { 59 if(filename.endsWith(".mxd")) {
49 filename = filename.replace(".mxd", ".map"); 60 filename = filename.replace(".mxd", ".map");
50 } 61 }
51 //Write the map attributes. 62 //Write the map attributes.
52 writeMap(); 63 writeMap();
53 64 //Write the layers.
65 writeLayer();
54 //Save the map. 66 //Save the map.
55 map.save(filename); 67 mapObj cloneMap = map.cloneMap();
68 cloneMap.save(filename);
56 return true; 69 return true;
57 } 70 }
58 71
59 private void writeMap() { 72 private void writeMap() {
60 logger.debug("writeMap()"); 73 logger.debug("writeMap()");
62 Element mapNode = (Element)XMLUtils.xpath( 75 Element mapNode = (Element)XMLUtils.xpath(
63 root, 76 root,
64 "/mxd/map", 77 "/mxd/map",
65 XPathConstants.NODE); 78 XPathConstants.NODE);
66 79
80 //TODO Get the following values from a template.
81 map.setShapepath("/home/intevation/mxd-testbed/testdata-frida");
82 map.setHeight(700);
83 map.setWidth(800);
84 map.setProjection("EPSG:4326");
85
67 //Set the name. 86 //Set the name.
68 map.setName(mapNode.getAttribute("name")); 87 map.setName(mapNode.getAttribute("name"));
69 88 map.setMetaData("wms_title", mapNode.getAttribute("name"));
70 //Set the extent. 89 //Set the extent.
71 map.setExtent( 90 map.setExtent(
72 Double.parseDouble(mapNode.getAttribute("extent_min_x")), 91 Double.parseDouble(mapNode.getAttribute("extent_min_x")),
73 Double.parseDouble(mapNode.getAttribute("extent_min_y")), 92 Double.parseDouble(mapNode.getAttribute("extent_min_y")),
74 Double.parseDouble(mapNode.getAttribute("extent_max_x")), 93 Double.parseDouble(mapNode.getAttribute("extent_max_x")),
106 //TODO: Find out whats the correct scale value. 125 //TODO: Find out whats the correct scale value.
107 //map.setScaledenom(Double.parseDouble(mapNode.getAttribute("scale"))); 126 //map.setScaledenom(Double.parseDouble(mapNode.getAttribute("scale")));
108 } 127 }
109 128
110 private void writeLayer() { 129 private void writeLayer() {
111 130 logger.debug("writeLayer()");
112 } 131 Element mapNode = (Element)XMLUtils.xpath(
113 132 root,
114 private void writeClass() { 133 "/mxd/map",
115 134 XPathConstants.NODE);
116 } 135 NodeList list = mapNode.getElementsByTagName("layer");
117 136 for(int i = 0; i < list.getLength(); i++) {
118 private void writeSymbol() { 137 Element layerElement = (Element)list.item(i);
119 138 layerObj layer = new layerObj(map);
120 } 139
121 140 //The layer name.
141 layer.setName(layerElement.getAttribute("name"));
142
143 //The layer status.
144 String stat = layerElement.getAttribute("status");
145 if (stat.equals("on")) {
146 layer.setStatus(1);
147 }
148 else {
149 layer.setStatus(0);
150 }
151
152 //The scale.
153 double maxScale =
154 Double.parseDouble(layerElement.getAttribute("max_scale"));
155 double minScale =
156 Double.parseDouble(layerElement.getAttribute("min_scale"));
157 layer.setMaxscaledenom(maxScale);
158 layer.setMinscaledenom(minScale);
159
160 //The layer type.
161 String type = layerElement.getAttribute("type");
162 if(type.equals("point")) {
163 layer.setType(MS_LAYER_TYPE.MS_LAYER_POINT);
164 }
165
166 //The layer datasource.
167 layer.setData(layerElement.getAttribute("data_source"));
168
169 //Write classes.
170 writeClass(layer, layerElement);
171 }
172
173 }
174
175 /**
176 * Adds the classes to the layer.
177 * @param layer Mapscript layer object.
178 * @param layerElement Dom element containing the class attributes.
179 */
180 private void writeClass(layerObj layer, Element layerElement) {
181 //Get all renderer elements (renderer in arcgis equals class in the
182 //mapfile.)
183 NodeList list = layerElement.getElementsByTagName("renderer");
184
185 //Create all found class objects and write the symbols and styles for
186 //each class.
187 for(int i = 0; i < list.getLength(); i++) {
188 Element classElement = (Element)list.item(i);
189 classObj co = new classObj(layer);
190 co.setName(classElement.getAttribute("name"));
191 //Write symbols and styles.
192 writeSymbol(co, classElement);
193 }
194 }
195
196 /**
197 * Adds teh symbols and styles to the mapfile.
198 * @param co Mapscript class object.
199 * @param classElement Dom element containing the style and symbol
200 * attributes.
201 */
202 private void writeSymbol(classObj co, Element classElement) {
203 //Get all symbol elements from dom.
204 NodeList list = classElement.getElementsByTagName("symbol");
205
206 //Get the symbol set from the map object.
207 symbolSetObj symbolSet = map.getSymbolset();
208
209 //
210 for(int i = 0; i < list.getLength(); i++){
211 Element symbolElement = (Element) list.item(i);
212 styleObj style = new styleObj(co);
213 style.setAngle(
214 Double.parseDouble(symbolElement.getAttribute("angle")));
215 String c = symbolElement.getAttribute("color");
216 Color col = Color.decode(c);
217 colorObj color = new colorObj(
218 col.getRed(),
219 col.getGreen(),
220 col.getBlue(),
221 -4);
222 style.setColor(color);
223 style.setSize(Double.parseDouble(symbolElement.getAttribute("size")));
224 Color oCol = Color.decode(
225 symbolElement.getAttribute("outline_color"));
226 colorObj outlineColor = new colorObj(
227 oCol.getRed(),
228 oCol.getGreen(),
229 oCol.getBlue(),
230 -4);
231 style.setOutlinecolor(outlineColor);
232 style.setOutlinewidth(
233 Double.parseDouble(symbolElement.getAttribute("outline_size")));
234 style.setSymbolByName(map, symbolElement.getAttribute("name"));
235
236 symbolObj sym = symbolSet.getSymbolByName(symbolElement.getAttribute("name"));
237 String symType = symbolElement.getAttribute("style");
238 if(symType.equals("point")) {
239 lineObj points = new lineObj();
240 points.add(new pointObj(1,1,0));
241 sym.setType(MS_SYMBOL_TYPE.MS_SYMBOL_ELLIPSE.swigValue());
242 sym.setPoints(points);
243 sym.setFilled(1);
244 }
245 }
246 saveSymbolSet(symbolSet);
247 }
248
249 /**
250 * Save the symbol set.
251 * @param
252 */
253 private void saveSymbolSet(symbolSetObj symbols) {
254 Element fileNode = (Element)XMLUtils.xpath(
255 root,
256 "/mxd/file",
257 XPathConstants.NODE);
258 String filename = fileNode.getAttribute("name");
259 String path = "";
260 if(filename.contains("/")) {
261 path = filename.substring(0, filename.lastIndexOf("/"));
262 }
263 else if(filename.contains("\\")) {
264 path = filename.substring(0, filename.lastIndexOf("\\"));
265 }
266 symbols.save(path + "/symbols.sym");
267 }
122 } 268 }
123 269
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)