comparison flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java @ 1781:ef2300b450bf

Modified the process to create mapfiles. flys-artifacts/trunk@3103 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 28 Oct 2011 09:26:54 +0000
parents 0156105222c9
children 49ad801076e4
comparison
equal deleted inserted replaced
1780:b503d92dd709 1781:ef2300b450bf
1 package de.intevation.flys.utils; 1 package de.intevation.flys.utils;
2 2
3 import java.io.File; 3 import java.io.File;
4 import java.io.FilenameFilter;
4 import java.io.FileNotFoundException; 5 import java.io.FileNotFoundException;
5 import java.io.FileWriter; 6 import java.io.FileWriter;
6 import java.io.IOException; 7 import java.io.IOException;
7 import java.io.StringWriter; 8 import java.io.StringWriter;
8 import java.io.Writer; 9 import java.io.Writer;
159 */ 160 */
160 protected void generate() 161 protected void generate()
161 throws FileNotFoundException, IOException 162 throws FileNotFoundException, IOException
162 { 163 {
163 File[] userDirs = getUserDirs(); 164 File[] userDirs = getUserDirs();
164 List<LayerInfo> layers = parseLayers(userDirs); 165
166 List<String> layers = parseLayers(userDirs);
165 167
166 logger.info("Found " + layers.size() + " layers for user mapfile."); 168 logger.info("Found " + layers.size() + " layers for user mapfile.");
167 169
168 writeMapfile(layers); 170 writeMapfile(layers);
169 } 171 }
305 return artifactDirs; 307 return artifactDirs;
306 } 308 }
307 309
308 310
309 311
310 protected List<LayerInfo> parseLayers(File[] dirs) { 312 protected List<String> parseLayers(File[] dirs) {
311 List<LayerInfo> layers = new ArrayList<LayerInfo>(); 313 List<String> layers = new ArrayList<String>();
312 314
313 for (File dir: dirs) { 315 for (File dir: dirs) {
314 LayerInfo layer = parseUeskLayer(dir); 316 File[] layerFiles = dir.listFiles(new FilenameFilter() {
315 if (layer != null && layer.getData() != null) { 317 @Override
316 logger.debug(" Add WSPLGEN layer."); 318 public boolean accept(File directory, String name) {
317 layers.add(layer); 319 return name.startsWith(MS_LAYER_PREFIX);
318 } 320 }
319 321 });
320 List<LayerInfo> barriers = parseBarriersLayers(dir); 322
321 int num = barriers != null ? barriers.size() : 0; 323 for (File layer: layerFiles) {
322 if (num > 0) { 324 try {
323 if (barriers.get(0).getData() != null) { 325 layers.add(layer.getCanonicalPath());
324 logger.debug(" Add " + num + " BARRIERS layers."); 326 }
325 layers.addAll(barriers); 327 catch (IOException ioe) {
328 logger.warn(ioe, ioe);
326 } 329 }
327 } 330 }
328 } 331 }
329 332
330 return layers; 333 return layers;
331 }
332
333
334 protected LayerInfo parseUeskLayer(File dir) {
335 File uesk = new File(dir, WSPLGEN_RESULT_SHAPE);
336
337 if (!uesk.exists() || !uesk.isFile()) {
338 return null;
339 }
340
341 return new LayerInfo(
342 dir.getName() + MS_WSPLGEN_POSTFIX,
343 "POLYGON",
344 dir.getName(),
345 WSPLGEN_RESULT_SHAPE,
346 "I18N_WSPLGEN_RESULT");
347 } 334 }
348 335
349 336
350 /** 337 /**
351 * Creates a layer file used for Mapserver's mapfile which represents the 338 * Creates a layer file used for Mapserver's mapfile which represents the
446 logger.warn("Unable to write layer: " + nameLines); 433 logger.warn("Unable to write layer: " + nameLines);
447 } 434 }
448 } 435 }
449 436
450 437
451 protected List<LayerInfo> parseBarriersLayers(File dir) { 438 /**
452 List<LayerInfo> barriers = new ArrayList<LayerInfo>(2); 439 * Creates a layer snippet which might be included in the mapfile.
453 440 *
454 String group = dir.getName() + MS_BARRIERS_POSTFIX; 441 * @param layerinfo A LayerInfo object that contains all necessary
455 String groupTitle = "I18N_BARRIERS_TITLE"; 442 * information to build a Mapserver LAYER section.
456 443 * @param dir The base dir for the LAYER snippet.
457 File lines = new File(dir, WSPLGEN_LINES_SHAPE); 444 * @param filename The name of the file that is written.
458 File polygons = new File(dir, WSPLGEN_POLYGONS_SHAPE); 445 * @param tpl The Velocity template which is used to create the LAYER
459 446 * section.
460 if (lines.exists() || lines.isFile()) { 447 */
461 barriers.add(
462 new LayerInfo(
463 dir.getName() + MS_LINE_POSTFIX,
464 "LINE",
465 dir.getName(),
466 WSPLGEN_LINES_SHAPE,
467 "I18N_LINE_SHAPE",
468 group,
469 groupTitle));
470 }
471
472 if (polygons.exists() || polygons.isFile()) {
473 barriers.add(
474 new LayerInfo(
475 dir.getName() + MS_POLYGONS_POSTFIX,
476 "POLYGON",
477 dir.getName(),
478 WSPLGEN_POLYGONS_SHAPE,
479 "I18N_POLYGON_SHAPE",
480 group,
481 groupTitle));
482 }
483
484 return barriers;
485 }
486
487
488 protected void writeLayer( 448 protected void writeLayer(
489 LayerInfo layerinfo, 449 LayerInfo layerinfo,
490 File dir, 450 File dir,
491 String name, 451 String filename,
492 Template tpl 452 Template tpl
493 ) 453 )
494 throws FileNotFoundException 454 throws FileNotFoundException
495 { 455 {
496 if (logger.isDebugEnabled()) { 456 if (logger.isDebugEnabled()) {
497 logger.debug("Write layer for:"); 457 logger.debug("Write layer for:");
498 logger.debug(" directory: " + dir.getName()); 458 logger.debug(" directory: " + dir.getName());
499 logger.debug(" name: " + name); 459 logger.debug(" name: " + filename);
500 } 460 }
501 461
502 File layer = new File(dir, name); 462 File layer = new File(dir, filename);
503 Writer writer = null; 463 Writer writer = null;
504 464
505 try { 465 try {
506 writer = new FileWriter(layer); 466 writer = new FileWriter(layer);
507 467
535 /** 495 /**
536 * Creates a mapfile with the layer information stored in <i>layers</i>. 496 * Creates a mapfile with the layer information stored in <i>layers</i>.
537 * 497 *
538 * @param layers Layer information. 498 * @param layers Layer information.
539 */ 499 */
540 protected void writeMapfile(List<LayerInfo> layers) { 500 protected void writeMapfile(List<String> layers) {
541 String tmpMapName = "mapfile" + new Date().getTime(); 501 String tmpMapName = "mapfile" + new Date().getTime();
542 502
543 File mapfile = new File( 503 File mapfile = new File(
544 FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPFILE_PATH)); 504 FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPFILE_PATH));
545 505
557 FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPSERVER_URL)); 517 FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPSERVER_URL));
558 context.put("SHAPEFILEPATH", 518 context.put("SHAPEFILEPATH",
559 getShapefileBaseDir().getCanonicalPath()); 519 getShapefileBaseDir().getCanonicalPath());
560 context.put("CONFIGDIR", 520 context.put("CONFIGDIR",
561 Config.getConfigDirectory().getCanonicalPath()); 521 Config.getConfigDirectory().getCanonicalPath());
562 context.put("LAYERS", fillLayerTemplates(layers)); 522 context.put("LAYERS", layers);
563 523
564 Template mapTemplate = getMapfileTemplate(); 524 Template mapTemplate = getMapfileTemplate();
565 if (mapTemplate == null) { 525 if (mapTemplate == null) {
566 logger.warn("No mapfile template found."); 526 logger.warn("No mapfile template found.");
567 return; 527 return;
596 catch (IOException ioe) { 556 catch (IOException ioe) {
597 logger.debug(ioe, ioe); 557 logger.debug(ioe, ioe);
598 } 558 }
599 } 559 }
600 } 560 }
601
602
603 protected List<String> fillLayerTemplates(List<LayerInfo> layers) {
604 List<String> evaluated = new ArrayList<String>(layers.size());
605
606 for (LayerInfo layer: layers) {
607 StringWriter writer = new StringWriter();
608
609 VelocityContext context = new VelocityContext();
610 context.put("LAYER", layer);
611
612 Template t = getTemplateByName("layer.vm");
613 if (t == null) {
614 logger.warn("No 'layer.vm' template found.");
615 return evaluated;
616 }
617
618 t.merge(context, writer);
619
620 evaluated.add(writer.toString());
621 }
622
623 return evaluated;
624 }
625 } 561 }
626 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 562 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org