comparison src/java/de/intevation/mxd/writer/MapScriptWriter.java @ 181:0bde090506f9

Added comments.
author Raimund Renkert <rrenkert@intevation.de>
date Mon, 11 Jul 2011 14:28:38 +0200
parents f4eb506499f5
children 1391f911ee39
comparison
equal deleted inserted replaced
180:f4eb506499f5 181:0bde090506f9
50 private String mapFilename; 50 private String mapFilename;
51 private String MS_BINDIR = "c:/ms_6.1-dev/bin"; 51 private String MS_BINDIR = "c:/ms_6.1-dev/bin";
52 private String prefix = ""; 52 private String prefix = "";
53 private MS_UNITS units = MS_UNITS.MS_METERS; 53 private MS_UNITS units = MS_UNITS.MS_METERS;
54 54
55 /**
56 * Default constructor.
57 * Creates a mapscript writer object with an empty map.
58 */
55 public MapScriptWriter() { 59 public MapScriptWriter() {
56 map = new mapObj(""); 60 map = new mapObj("");
57 mapFilename = ""; 61 mapFilename = "";
58 } 62 }
59 63
64 /**
65 * Contructor with template and output filename.
66 * Creates a mapscript writer object with the given template that saves the
67 * map to the filename.
68 *
69 * @param templ Mapfile template.
70 * @param filename Output file name.
71 */
60 public MapScriptWriter(String templ, String filename) { 72 public MapScriptWriter(String templ, String filename) {
61 String path = System.getProperty("user.dir"); 73 String path = System.getProperty("user.dir");
62 map = new mapObj(templ); 74 map = new mapObj(templ);
63 File f = new File(filename); 75 File f = new File(filename);
64 mapFilename = filename; 76 mapFilename = filename;
229 layer.setTileitem(""); 241 layer.setTileitem("");
230 242
231 //The layer datasource. 243 //The layer datasource.
232 String con_type = layerElement.getAttribute("connection_type"); 244 String con_type = layerElement.getAttribute("connection_type");
233 if(con_type.equals("local")) { 245 if(con_type.equals("local")) {
246 // The data source is a local file. (.shp)
234 String datasource = ""; 247 String datasource = "";
235 if(layerElement.hasAttribute("workspace")) { 248 if(layerElement.hasAttribute("workspace")) {
236 datasource = layerElement.getAttribute("workspace"); 249 datasource = layerElement.getAttribute("workspace");
237 datasource += File.separator; 250 datasource += File.separator;
238 } 251 }
239 datasource += layerElement.getAttribute("data_source"); 252 datasource += layerElement.getAttribute("data_source");
240 datasource = datasource.replaceAll("\\\\", "/"); 253 datasource = datasource.replaceAll("\\\\", "/");
241 layer.setData(datasource); 254 layer.setData(datasource);
242 } 255 }
243 else if(con_type.equals("SDE")) { 256 else if(con_type.equals("SDE")) {
257 // The data source is a sde database.
244 logger.info( 258 logger.info(
245 "SDE datasource found." + 259 "SDE datasource found." +
246 " Please edit password in mapfile."); 260 " Please edit password in mapfile.");
247 layer.setConnectionType( 261 layer.setConnectionType(
248 MS_CONNECTION_TYPE.MS_PLUGIN.swigValue(), 262 MS_CONNECTION_TYPE.MS_PLUGIN.swigValue(),
281 } 295 }
282 } 296 }
283 } 297 }
284 layer.setTemplate("PleaseInsertAValidTemplateForGFI"); 298 layer.setTemplate("PleaseInsertAValidTemplateForGFI");
285 299
300 // Create labelitem
286 NodeList labels = layerElement.getElementsByTagName("label"); 301 NodeList labels = layerElement.getElementsByTagName("label");
287 if(labels.getLength() > 0) { 302 if(labels.getLength() > 0) {
288 Element label = (Element)labels.item(0); 303 Element label = (Element)labels.item(0);
289 String expr = label.getAttribute("expression"); 304 String expr = label.getAttribute("expression");
290 expr = expr.replaceAll("\\[", ""); 305 expr = expr.replaceAll("\\[", "");
296 } 311 }
297 312
298 //Write classes. 313 //Write classes.
299 writeClass(layer, layerElement); 314 writeClass(layer, layerElement);
300 } 315 }
301
302 } 316 }
303 317
304 /** 318 /**
305 * Adds the classes to the layer. 319 * Adds the classes to the layer.
306 * @param layer Mapscript layer object. 320 * @param layer Mapscript layer object.
324 name += "-" + i; 338 name += "-" + i;
325 } 339 }
326 } 340 }
327 co.setName (name); 341 co.setName (name);
328 342
343 //Create label object.
329 NodeList labels = layerElement.getElementsByTagName("label"); 344 NodeList labels = layerElement.getElementsByTagName("label");
330 if(labels.getLength() > 0) { 345 if(labels.getLength() > 0) {
331 Element labelElement = (Element)labels.item(0); 346 Element labelElement = (Element)labels.item(0);
332 String layerType = layerElement.getAttribute("type"); 347 String layerType = layerElement.getAttribute("type");
333 writeLabel(co, labelElement, layerType); 348 writeLabel(co, labelElement, layerType);
334 } 349 }
350
351 //Create definition expression.
335 if(classElement.hasAttribute("field_count")) { 352 if(classElement.hasAttribute("field_count")) {
336 co.setExpression(createExpression(classElement, i)); 353 co.setExpression(createExpression(classElement, i));
337 } 354 }
355
338 //Write symbols and styles. 356 //Write symbols and styles.
339 NodeList l = classElement.getChildNodes(); 357 NodeList l = classElement.getChildNodes();
340 for (int j = l.getLength() - 1; j >= 0; j--) { 358 for (int j = l.getLength() - 1; j >= 0; j--) {
341 Element elem = (Element)l.item(j); 359 Element elem = (Element)l.item(j);
342 360
361 } 379 }
362 } 380 }
363 } 381 }
364 } 382 }
365 383
366 384 /**
385 * Create a label object in a class object.
386 *
387 * @param co Class object.
388 * @param labelElement DOM element containing label attributes.
389 * @param layertype The layer type. Used to define the label position.
390 */
367 private void writeLabel( 391 private void writeLabel(
368 classObj co, 392 classObj co,
369 Element labelElement, 393 Element labelElement,
370 String layerType) { 394 String layerType) {
371 395
396 //Get the label object. Each class has a predefined labelobject.
372 labelObj label = co.getLabel(); 397 labelObj label = co.getLabel();
398
399 //Get the label text symbol attributes from DOM.
373 Element symbol = (Element)labelElement.getFirstChild(); 400 Element symbol = (Element)labelElement.getFirstChild();
374 if(symbol != null && symbol.getTagName().equals("symbol")) { 401 if(symbol != null && symbol.getTagName().equals("symbol")) {
375 String type = symbol.getAttribute("type"); 402 String type = symbol.getAttribute("type");
376 if(type.equals("text")) { 403 if(type.equals("text")) {
377 label.setType(MS_FONT_TYPE.MS_TRUETYPE); 404 label.setType(MS_FONT_TYPE.MS_TRUETYPE);
417 if(max > 0) { 444 if(max > 0) {
418 label.setMaxscaledenom(max); 445 label.setMaxscaledenom(max);
419 } 446 }
420 } 447 }
421 448
449 //Set the label position.
422 if(layerType.equals("point")) { 450 if(layerType.equals("point")) {
423 label.setPosition(MS_POSITIONS_ENUM.MS_UC.swigValue()); 451 label.setPosition(MS_POSITIONS_ENUM.MS_UC.swigValue());
424 } 452 }
425 } 453 }
426 } 454 }
427 455
428 456 /**
457 * Create definition expression.
458 *
459 * @param ce DOM elementcontaining the class expression information.
460 * @param index Index to determine the correct expression operator.
461 */
429 private String createExpression(Element ce, int index) { 462 private String createExpression(Element ce, int index) {
430 String expression = "("; 463 String expression = "(";
431 int count = 0; 464 int count = 0;
432 try { 465 try {
433 count = Integer.parseInt(ce.getAttribute("field_count")); 466 count = Integer.parseInt(ce.getAttribute("field_count"));
434 } 467 }
435 catch(NumberFormatException nfe) { 468 catch(NumberFormatException nfe) {
436 return ""; 469 return "";
437 } 470 }
471
438 if(count == 1) { 472 if(count == 1) {
439 try { 473 //Create single field expression.
474 try {
475 //If no exception is thrown, the expression value is a number.
440 Double.parseDouble(ce.getAttribute("value")); 476 Double.parseDouble(ce.getAttribute("value"));
441 String exp = ce.getAttribute("expression_field_0"); 477 String exp = ce.getAttribute("expression_field_0");
442 String pre = ""; 478 String pre = "";
443 if (!prefix.equals("") && !exp.startsWith(prefix)) { 479 if (!prefix.equals("") && !exp.startsWith(prefix)) {
444 pre = prefix + "."; 480 pre = prefix + ".";
450 expression += " >= " + ce.getAttribute("min_value"); 486 expression += " >= " + ce.getAttribute("min_value");
451 } 487 }
452 else { 488 else {
453 expression += " > " + ce.getAttribute("min_value"); 489 expression += " > " + ce.getAttribute("min_value");
454 } 490 }
455 expression += " AND [" + pre; 491 expression += " AND [" + pre;
456 expression += ce.getAttribute("expression_field_0"); 492 expression += ce.getAttribute("expression_field_0");
457 expression += "]"; 493 expression += "]";
458 } 494 }
459 expression += " " + ce.getAttribute("expression_operator"); 495 expression += " " + ce.getAttribute("expression_operator");
460 expression += " " + ce.getAttribute("value") + ")"; 496 expression += " " + ce.getAttribute("value") + ")";
461 } 497 }
462 catch(NumberFormatException nfe) { 498 catch(NumberFormatException nfe) {
499 //The expression value is a strings.
463 String exp = ce.getAttribute("expression_field_0"); 500 String exp = ce.getAttribute("expression_field_0");
464 String pre = ""; 501 String pre = "";
465 if (!prefix.equals("") && !exp.startsWith(prefix)) { 502 if (!prefix.equals("") && !exp.startsWith(prefix)) {
466 pre = prefix + "."; 503 pre = prefix + ".";
467 } 504 }
473 catch(Exception e) { 510 catch(Exception e) {
474 e.printStackTrace(); 511 e.printStackTrace();
475 } 512 }
476 } 513 }
477 else { 514 else {
515 //Create a multi field expression.
478 for (int i = 0; i < count; i++) { 516 for (int i = 0; i < count; i++) {
479 try { 517 try {
518 //If no exception is thrown, the expression values are
519 //numbers.
480 Double.parseDouble(ce.getAttribute("value_" + i)); 520 Double.parseDouble(ce.getAttribute("value_" + i));
481 String exp = ce.getAttribute("expression_field_" + i); 521 String exp = ce.getAttribute("expression_field_" + i);
482 String pre = ""; 522 String pre = "";
483 if (!prefix.equals("") && !exp.startsWith(prefix)) { 523 if (!prefix.equals("") && !exp.startsWith(prefix)) {
484 pre = prefix + "."; 524 pre = prefix + ".";
494 else { 534 else {
495 expression += ")"; 535 expression += ")";
496 } 536 }
497 } 537 }
498 catch (NumberFormatException nfe) { 538 catch (NumberFormatException nfe) {
539 //The expression values are strings.
499 String exp = ce.getAttribute("expression_field_" + i); 540 String exp = ce.getAttribute("expression_field_" + i);
500 String pre = ""; 541 String pre = "";
501 if (!prefix.equals("") && !exp.startsWith(prefix)) { 542 if (!prefix.equals("") && !exp.startsWith(prefix)) {
502 pre = prefix + "."; 543 pre = prefix + ".";
503 } 544 }
516 } 557 }
517 } 558 }
518 return expression; 559 return expression;
519 } 560 }
520 } 561 }
521
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)