Mercurial > mxd2map
comparison src/java/de/intevation/mxd/writer/MapScriptWriter.java @ 180:f4eb506499f5
Done some code styling and removed TODOs.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Mon, 11 Jul 2011 12:11:08 +0200 |
parents | f3a91cd7440b |
children | 0bde090506f9 |
comparison
equal
deleted
inserted
replaced
179:f3a91cd7440b | 180:f4eb506499f5 |
---|---|
56 map = new mapObj(""); | 56 map = new mapObj(""); |
57 mapFilename = ""; | 57 mapFilename = ""; |
58 } | 58 } |
59 | 59 |
60 public MapScriptWriter(String templ, String filename) { | 60 public MapScriptWriter(String templ, String filename) { |
61 String path = System.getProperty("user.dir"); | 61 String path = System.getProperty("user.dir"); |
62 map = new mapObj(templ); | 62 map = new mapObj(templ); |
63 File f = new File(filename); | 63 File f = new File(filename); |
64 mapFilename = filename; | 64 mapFilename = filename; |
65 if(f.isAbsolute()) { | 65 if(f.isAbsolute()) { |
66 map.setMappath(mapFilename); | 66 map.setMappath(mapFilename); |
67 } | 67 } |
68 else { | 68 else { |
69 map.setMappath( | 69 map.setMappath( |
70 System.getProperty("user.dir") + | 70 System.getProperty("user.dir") + |
71 File.separator + | 71 File.separator + |
72 mapFilename); | 72 mapFilename); |
73 mapFilename = System.getProperty("user.dir") + | 73 mapFilename = System.getProperty("user.dir") + |
74 File.separator + | 74 File.separator + |
75 mapFilename; | 75 mapFilename; |
76 } | 76 } |
77 String fontSetPath = map.getFontset().getFilename(); | 77 String fontSetPath = map.getFontset().getFilename(); |
78 File fonts = new File(fontSetPath); | 78 File fonts = new File(fontSetPath); |
79 String fontSet = fonts.getAbsolutePath(); | 79 String fontSet = fonts.getAbsolutePath(); |
80 fontSet = fontSet.replaceAll("\\\\", "/"); | 80 fontSet = fontSet.replaceAll("\\\\", "/"); |
81 map.setFontSet(fontSet); | 81 map.setFontSet(fontSet); |
82 } | 82 } |
83 | 83 |
84 /** | 84 /** |
85 * Write the mapfile. | 85 * Write the mapfile. |
86 * @param doc The root document containin the map attributes. | 86 * @param doc The root document containin the map attributes. |
101 //Write the layers. | 101 //Write the layers. |
102 writeLayer(); | 102 writeLayer(); |
103 //Save the map. | 103 //Save the map. |
104 mapObj cloneMap = map.cloneMap(); | 104 mapObj cloneMap = map.cloneMap(); |
105 | 105 |
106 cloneMap.save(mapFilename); | 106 cloneMap.save(mapFilename); |
107 logger.info("Mapfile created: " + mapFilename); | 107 logger.info("Mapfile created: " + mapFilename); |
108 return true; | 108 return true; |
109 } | 109 } |
110 | 110 |
111 /** | 111 /** |
128 Double.parseDouble(mapNode.getAttribute("extent_min_y")), | 128 Double.parseDouble(mapNode.getAttribute("extent_min_y")), |
129 Double.parseDouble(mapNode.getAttribute("extent_max_x")), | 129 Double.parseDouble(mapNode.getAttribute("extent_max_x")), |
130 Double.parseDouble(mapNode.getAttribute("extent_max_y"))); | 130 Double.parseDouble(mapNode.getAttribute("extent_max_y"))); |
131 | 131 |
132 //Set the units and projection. | 132 //Set the units and projection. |
133 int proj = Integer.parseInt(mapNode.getAttribute("projection")); | 133 int proj = Integer.parseInt(mapNode.getAttribute("projection")); |
134 if(proj != 0) { | 134 if(proj != 0) { |
135 map.setProjection("epsg:" + mapNode.getAttribute("projection")); | 135 map.setProjection("epsg:" + mapNode.getAttribute("projection")); |
136 } | 136 } |
137 String u = mapNode.getAttribute("units"); | 137 String u = mapNode.getAttribute("units"); |
138 if(u.equals("feet")) { | 138 if(u.equals("feet")) { |
139 units = MS_UNITS.MS_FEET; | 139 units = MS_UNITS.MS_FEET; |
140 } | 140 } |
141 else if(u.equals("inches")) { | 141 else if(u.equals("inches")) { |
154 units = MS_UNITS.MS_NAUTICALMILES; | 154 units = MS_UNITS.MS_NAUTICALMILES; |
155 } | 155 } |
156 else if(u.equals("points")) { | 156 else if(u.equals("points")) { |
157 units = MS_UNITS.MS_PIXELS; | 157 units = MS_UNITS.MS_PIXELS; |
158 } | 158 } |
159 else if(u.equals("degree")) { | 159 else if(u.equals("degree")) { |
160 units = MS_UNITS.MS_DD; | 160 units = MS_UNITS.MS_DD; |
161 } | 161 } |
162 else { | 162 else { |
163 units = MS_UNITS.MS_METERS; | 163 units = MS_UNITS.MS_METERS; |
164 } | 164 } |
165 map.setUnits(units); | 165 map.setUnits(units); |
166 | |
167 //TODO: Find out whats the correct scale value. | |
168 //map.setScaledenom(Double.parseDouble(mapNode.getAttribute("scale"))); | |
169 } | 166 } |
170 | 167 |
171 /** | 168 /** |
172 * Create layer objects and set the attributes. | 169 * Create layer objects and set the attributes. |
173 */ | 170 */ |
184 | 181 |
185 //The layer name. | 182 //The layer name. |
186 String lname = layerElement.getAttribute("name"); | 183 String lname = layerElement.getAttribute("name"); |
187 lname = lname.replaceAll(" ", ""); | 184 lname = lname.replaceAll(" ", ""); |
188 layer.setName(lname); | 185 layer.setName(lname); |
189 layer.setUnits(units.swigValue()); | 186 layer.setUnits(units.swigValue()); |
190 layer.setMetaData("wms_title", layerElement.getAttribute("name")); | 187 layer.setMetaData("wms_title", layerElement.getAttribute("name")); |
191 if(layerElement.hasAttribute("group")) { | 188 if(layerElement.hasAttribute("group")) { |
192 layer.setGroup(layerElement.getAttribute("group")); | 189 layer.setGroup(layerElement.getAttribute("group")); |
193 } | 190 } |
191 | |
194 //The layer status. | 192 //The layer status. |
195 String stat = layerElement.getAttribute("status"); | 193 String stat = layerElement.getAttribute("status"); |
196 if (stat.equals("on")) { | 194 if (stat.equals("on")) { |
197 layer.setStatus(1); | 195 layer.setStatus(1); |
198 } | 196 } |
237 if(layerElement.hasAttribute("workspace")) { | 235 if(layerElement.hasAttribute("workspace")) { |
238 datasource = layerElement.getAttribute("workspace"); | 236 datasource = layerElement.getAttribute("workspace"); |
239 datasource += File.separator; | 237 datasource += File.separator; |
240 } | 238 } |
241 datasource += layerElement.getAttribute("data_source"); | 239 datasource += layerElement.getAttribute("data_source"); |
242 datasource = datasource.replaceAll("\\\\", "/"); | 240 datasource = datasource.replaceAll("\\\\", "/"); |
243 layer.setData(datasource); | 241 layer.setData(datasource); |
244 } | 242 } |
245 else if(con_type.equals("SDE")) { | 243 else if(con_type.equals("SDE")) { |
246 logger.info( | 244 logger.info( |
247 "SDE datasource found." + | 245 "SDE datasource found." + |
273 layerElement.getAttribute("join_table") + "." + | 271 layerElement.getAttribute("join_table") + "." + |
274 layerElement.getAttribute("join_field")); | 272 layerElement.getAttribute("join_field")); |
275 } | 273 } |
276 else { | 274 else { |
277 prefix = layerElement.getAttribute("data_source"); | 275 prefix = layerElement.getAttribute("data_source"); |
278 if(layerElement.hasAttribute("definition_query") && | 276 if(layerElement.hasAttribute("definition_query") && |
279 !layerElement.getAttribute("definition_query").equals("")) { | 277 !layerElement.getAttribute("definition_query") |
280 layer.setFilter("where " + | 278 .equals("")) { |
281 layerElement.getAttribute("definition_query")); | 279 layer.setFilter("where " + |
282 } | 280 layerElement.getAttribute("definition_query")); |
283 } | 281 } |
284 | 282 } |
285 } | 283 } |
286 layer.setTemplate("PleaseInsertAValidTemplateForGFI"); | 284 layer.setTemplate("PleaseInsertAValidTemplateForGFI"); |
287 | 285 |
288 NodeList labels = layerElement.getElementsByTagName("label"); | 286 NodeList labels = layerElement.getElementsByTagName("label"); |
289 if(labels.getLength() > 0) { | 287 if(labels.getLength() > 0) { |
290 Element label = (Element)labels.item(0); | 288 Element label = (Element)labels.item(0); |
291 String expr = label.getAttribute("expression"); | 289 String expr = label.getAttribute("expression"); |
292 expr = expr.replaceAll("\\[", ""); | 290 expr = expr.replaceAll("\\[", ""); |
293 expr = expr.replaceAll("\\]", ""); | 291 expr = expr.replaceAll("\\]", ""); |
294 if(!prefix.equals("")) { | 292 if(!prefix.equals("")) { |
295 expr = prefix + "." + expr; | 293 expr = prefix + "." + expr; |
296 } | 294 } |
297 layer.setLabelitem(expr); | 295 layer.setLabelitem(expr); |
298 } | 296 } |
297 | |
299 //Write classes. | 298 //Write classes. |
300 writeClass(layer, layerElement); | 299 writeClass(layer, layerElement); |
301 } | 300 } |
302 | 301 |
303 } | 302 } |
325 name += "-" + i; | 324 name += "-" + i; |
326 } | 325 } |
327 } | 326 } |
328 co.setName (name); | 327 co.setName (name); |
329 | 328 |
330 NodeList labels = layerElement.getElementsByTagName("label"); | 329 NodeList labels = layerElement.getElementsByTagName("label"); |
331 if(labels.getLength() > 0) { | 330 if(labels.getLength() > 0) { |
332 Element labelElement = (Element)labels.item(0); | 331 Element labelElement = (Element)labels.item(0); |
333 String layerType = layerElement.getAttribute("type"); | 332 String layerType = layerElement.getAttribute("type"); |
334 writeLabel(co, labelElement, layerType); | 333 writeLabel(co, labelElement, layerType); |
335 } | 334 } |
336 if(classElement.hasAttribute("field_count")) { | 335 if(classElement.hasAttribute("field_count")) { |
337 co.setExpression(createExpression(classElement, i)); | 336 co.setExpression(createExpression(classElement, i)); |
338 } | 337 } |
339 //Write symbols and styles. | 338 //Write symbols and styles. |
340 NodeList l = classElement.getChildNodes(); | 339 NodeList l = classElement.getChildNodes(); |
341 for (int j = l.getLength() - 1; j >= 0; j--) { | 340 for (int j = l.getLength() - 1; j >= 0; j--) { |
342 Element elem = (Element)l.item(j); | 341 Element elem = (Element)l.item(j); |
343 | 342 |
344 String type = layerElement.getAttribute("type"); | 343 String type = layerElement.getAttribute("type"); |
345 if(type.equals("point") && elem.getTagName().equals("symbol")) { | 344 if(type.equals("point") && |
346 MarkerStyleWriter swriter = new MarkerStyleWriter (this.map, co); | 345 elem.getTagName().equals("symbol")) { |
346 MarkerStyleWriter swriter = | |
347 new MarkerStyleWriter (this.map, co); | |
347 swriter.write (elem); | 348 swriter.write (elem); |
348 } | 349 } |
349 else if(type.equals("line") && elem.getTagName().equals("symbol")) { | 350 else if(type.equals("line") && |
350 LineStyleWriter swriter = new LineStyleWriter (this.map, co); | 351 elem.getTagName().equals("symbol")) { |
352 LineStyleWriter swriter = | |
353 new LineStyleWriter (this.map, co); | |
351 swriter.write (elem); | 354 swriter.write (elem); |
352 } | 355 } |
353 else if(type.equals("polygon") && elem.getTagName().equals("symbol")) { | 356 else if(type.equals("polygon") && |
354 FillStyleWriter swriter = new FillStyleWriter (this.map, co); | 357 elem.getTagName().equals("symbol")) { |
358 FillStyleWriter swriter = | |
359 new FillStyleWriter (this.map, co); | |
355 swriter.write (elem); | 360 swriter.write (elem); |
356 } | 361 } |
357 } | 362 } |
358 } | 363 } |
359 } | 364 } |
360 | 365 |
361 | 366 |
362 private void writeLabel( | 367 private void writeLabel( |
363 classObj co, | 368 classObj co, |
364 Element labelElement, | 369 Element labelElement, |
365 String layerType) { | 370 String layerType) { |
366 | 371 |
367 labelObj label = co.getLabel(); | 372 labelObj label = co.getLabel(); |
368 Element symbol = (Element)labelElement.getFirstChild(); | 373 Element symbol = (Element)labelElement.getFirstChild(); |
369 if(symbol != null && symbol.getTagName().equals("symbol")) { | 374 if(symbol != null && symbol.getTagName().equals("symbol")) { |
370 String type = symbol.getAttribute("type"); | 375 String type = symbol.getAttribute("type"); |
371 if(type.equals("text")) { | 376 if(type.equals("text")) { |
372 label.setType(MS_FONT_TYPE.MS_TRUETYPE); | 377 label.setType(MS_FONT_TYPE.MS_TRUETYPE); |
373 } | 378 } |
374 label.setSize(Double.parseDouble(symbol.getAttribute("size"))); | 379 label.setSize(Double.parseDouble(symbol.getAttribute("size"))); |
375 | 380 |
376 if(symbol.hasAttribute("color")) { | 381 if(symbol.hasAttribute("color")) { |
377 String c = symbol.getAttribute("color"); | 382 String c = symbol.getAttribute("color"); |
378 Color col = Color.decode(c); | 383 Color col = Color.decode(c); |
379 colorObj color = new colorObj( | 384 colorObj color = new colorObj( |
380 col.getRed(), | 385 col.getRed(), |
381 col.getGreen(), | 386 col.getGreen(), |
382 col.getBlue(), | 387 col.getBlue(), |
383 -4); | 388 -4); |
384 label.setColor(color); | 389 label.setColor(color); |
385 } | 390 } |
386 if(symbol.hasAttribute("font")) { | 391 if(symbol.hasAttribute("font")) { |
387 String font = symbol.getAttribute("font"); | 392 String font = symbol.getAttribute("font"); |
388 fontSetObj fso = this.map.getFontset(); | 393 fontSetObj fso = this.map.getFontset(); |
389 hashTableObj fonts = fso.getFonts(); | 394 hashTableObj fonts = fso.getFonts(); |
390 String mapFont = fonts.get(font,""); | 395 String mapFont = fonts.get(font,""); |
391 if(mapFont != null && !mapFont.equals("")) { | 396 if(mapFont != null && !mapFont.equals("")) { |
392 label.setFont(font); | 397 label.setFont(font); |
393 } | 398 } |
394 else { | 399 else { |
395 logger.info( | 400 logger.info( |
396 "Could not find font " + font + " in font set. " + | 401 "Could not find font " + font + " in font set. " + |
397 "Using FreeSans for labels."); | 402 "Using FreeSans for labels."); |
398 label.setFont("FreeSans"); | 403 label.setFont("FreeSans"); |
399 } | 404 } |
400 } | 405 } |
401 | 406 |
402 if(labelElement.hasAttribute("max_scale")) { | 407 if(labelElement.hasAttribute("max_scale")) { |
403 double min = | 408 double min = |
404 Double.parseDouble(labelElement.getAttribute("max_scale")); | 409 Double.parseDouble(labelElement.getAttribute("max_scale")); |
405 if(min > 0) { | 410 if(min > 0) { |
406 label.setMinscaledenom(min); | 411 label.setMinscaledenom(min); |
407 } | 412 } |
408 } | 413 } |
409 if(labelElement.hasAttribute("min_scale")) { | 414 if(labelElement.hasAttribute("min_scale")) { |
410 double max = | 415 double max = |
411 Double.parseDouble(labelElement.getAttribute("min_scale")); | 416 Double.parseDouble(labelElement.getAttribute("min_scale")); |
412 if(max > 0) { | 417 if(max > 0) { |
413 label.setMaxscaledenom(max); | 418 label.setMaxscaledenom(max); |
414 } | 419 } |
415 } | 420 } |
416 | 421 |
417 if(layerType.equals("point")) { | 422 if(layerType.equals("point")) { |
418 label.setPosition(MS_POSITIONS_ENUM.MS_UC.swigValue()); | 423 label.setPosition(MS_POSITIONS_ENUM.MS_UC.swigValue()); |
419 } | 424 } |
420 } | 425 } |
421 } | 426 } |
422 | 427 |
423 | 428 |
424 private String createExpression(Element ce, int index) { | 429 private String createExpression(Element ce, int index) { |
425 String expression = "("; | 430 String expression = "("; |
439 pre = prefix + "."; | 444 pre = prefix + "."; |
440 } | 445 } |
441 expression += "[" + pre; | 446 expression += "[" + pre; |
442 expression += ce.getAttribute("expression_field_0") + "]"; | 447 expression += ce.getAttribute("expression_field_0") + "]"; |
443 if(ce.hasAttribute("min_value")) { | 448 if(ce.hasAttribute("min_value")) { |
444 if(index == 0) { | 449 if(index == 0) { |
445 expression += " >= " + ce.getAttribute("min_value"); | 450 expression += " >= " + ce.getAttribute("min_value"); |
446 } | 451 } |
447 else { | 452 else { |
448 expression += " > " + ce.getAttribute("min_value"); | 453 expression += " > " + ce.getAttribute("min_value"); |
449 } | 454 } |
450 expression += " AND [" + pre; | 455 expression += " AND [" + pre; |
451 expression += ce.getAttribute("expression_field_0")+ "]"; | 456 expression += ce.getAttribute("expression_field_0"); |
457 expression += "]"; | |
452 } | 458 } |
453 expression += " " + ce.getAttribute("expression_operator"); | 459 expression += " " + ce.getAttribute("expression_operator"); |
454 expression += " " + ce.getAttribute("value") + ")"; | 460 expression += " " + ce.getAttribute("value") + ")"; |
455 } | 461 } |
456 catch(NumberFormatException nfe) { | 462 catch(NumberFormatException nfe) { |
476 String pre = ""; | 482 String pre = ""; |
477 if (!prefix.equals("") && !exp.startsWith(prefix)) { | 483 if (!prefix.equals("") && !exp.startsWith(prefix)) { |
478 pre = prefix + "."; | 484 pre = prefix + "."; |
479 } | 485 } |
480 expression += "[" + pre; | 486 expression += "[" + pre; |
481 expression += ce.getAttribute("expression_field_" + i) + "]"; | 487 expression += ce.getAttribute("expression_field_" + i); |
488 expression += "]"; | |
482 expression += " " + ce.getAttribute("expression_operator"); | 489 expression += " " + ce.getAttribute("expression_operator"); |
483 expression += " " + ce.getAttribute("value_" + i); | 490 expression += " " + ce.getAttribute("value_" + i); |
484 if (i < count - 1) { | 491 if (i < count - 1) { |
485 expression += " AND "; | 492 expression += " AND "; |
486 } | 493 } |
493 String pre = ""; | 500 String pre = ""; |
494 if (!prefix.equals("") && !exp.startsWith(prefix)) { | 501 if (!prefix.equals("") && !exp.startsWith(prefix)) { |
495 pre = prefix + "."; | 502 pre = prefix + "."; |
496 } | 503 } |
497 expression += "\"[" + pre; | 504 expression += "\"[" + pre; |
498 expression += ce.getAttribute("expression_field_" + i) + "]\""; | 505 expression += ce.getAttribute("expression_field_" + i); |
506 expression += "]\""; | |
499 expression += " " + ce.getAttribute("expression_operator"); | 507 expression += " " + ce.getAttribute("expression_operator"); |
500 expression += " " + ce.getAttribute("value_" + i); | 508 expression += " " + ce.getAttribute("value_" + i); |
501 if (i < count - 1) { | 509 if (i < count - 1) { |
502 expression += " AND "; | 510 expression += " AND "; |
503 } | 511 } |