comparison flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java @ 1775:0156105222c9

Improved the MapfileGenerator. It offers methods to create barrier and wsplgen layer files for mapserver. flys-artifacts/trunk@3097 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 28 Oct 2011 08:36:32 +0000
parents 055f32a5388a
children ef2300b450bf
comparison
equal deleted inserted replaced
1774:092e1e5020bc 1775:0156105222c9
17 import org.apache.velocity.VelocityContext; 17 import org.apache.velocity.VelocityContext;
18 import org.apache.velocity.app.VelocityEngine; 18 import org.apache.velocity.app.VelocityEngine;
19 19
20 import de.intevation.artifacts.common.utils.Config; 20 import de.intevation.artifacts.common.utils.Config;
21 21
22 import de.intevation.flys.artifacts.FLYSArtifact;
22 import de.intevation.flys.artifacts.model.LayerInfo; 23 import de.intevation.flys.artifacts.model.LayerInfo;
24 import de.intevation.flys.artifacts.model.WMSLayerFacet;
23 25
24 /** 26 /**
25 * This class iterates over a bunch of directories, searches for meta 27 * This class iterates over a bunch of directories, searches for meta
26 * information coresponding to shapefiles and creates a mapfile which is used by 28 * information coresponding to shapefiles and creates a mapfile which is used by
27 * a <i>MapServer</i>. 29 * a <i>MapServer</i>.
33 { 35 {
34 public static final String WSPLGEN_RESULT_SHAPE = "wsplgen.shp"; 36 public static final String WSPLGEN_RESULT_SHAPE = "wsplgen.shp";
35 public static final String WSPLGEN_LINES_SHAPE = "barrier_lines.shp"; 37 public static final String WSPLGEN_LINES_SHAPE = "barrier_lines.shp";
36 public static final String WSPLGEN_POLYGONS_SHAPE = "barrier_polygons.shp"; 38 public static final String WSPLGEN_POLYGONS_SHAPE = "barrier_polygons.shp";
37 39
40 public static final String SHP_LAYER_TEMPLATE = "shapefile_layer.vm";
41
38 public static final String MS_WSPLGEN_POSTFIX = "-wsplgen"; 42 public static final String MS_WSPLGEN_POSTFIX = "-wsplgen";
39 public static final String MS_BARRIERS_POSTFIX = "-barriers"; 43 public static final String MS_BARRIERS_POSTFIX = "-barriers";
40 public static final String MS_LINE_POSTFIX = "-lines"; 44 public static final String MS_LINE_POSTFIX = "-lines";
41 public static final String MS_POLYGONS_POSTFIX = "-polygons"; 45 public static final String MS_POLYGONS_POSTFIX = "-polygons";
46 public static final String MS_LAYER_PREFIX = "ms_layer-";
42 47
43 protected static final long SLEEPTIME = 10 * 1000L; // 10 seconds 48 protected static final long SLEEPTIME = 10 * 1000L; // 10 seconds
44 49
45 private static Logger logger = Logger.getLogger(MapfileGenerator.class); 50 private static Logger logger = Logger.getLogger(MapfileGenerator.class);
46 51
126 logger.debug("MapfileGenerator thread got an interrupt."); 131 logger.debug("MapfileGenerator thread got an interrupt.");
127 } 132 }
128 catch (FileNotFoundException fnfe) { 133 catch (FileNotFoundException fnfe) {
129 logger.debug("Error while mapfile creation: " + fnfe.getMessage()); 134 logger.debug("Error while mapfile creation: " + fnfe.getMessage());
130 } 135 }
136 catch (IOException ioe) {
137 logger.error(ioe, ioe);
138 }
131 finally { 139 finally {
132 logger.debug("THREAD END"); 140 logger.debug("THREAD END");
133 } 141 }
134 } 142 }
135 143
148 /** 156 /**
149 * Method which starts searching for meta information file and mapfile 157 * Method which starts searching for meta information file and mapfile
150 * generation. 158 * generation.
151 */ 159 */
152 protected void generate() 160 protected void generate()
153 throws FileNotFoundException 161 throws FileNotFoundException, IOException
154 { 162 {
155 File[] userDirs = getUserDirs(); 163 File[] userDirs = getUserDirs();
156 List<LayerInfo> layers = parseLayers(userDirs); 164 List<LayerInfo> layers = parseLayers(userDirs);
157 165
158 logger.info("Found " + layers.size() + " layers for user mapfile."); 166 logger.info("Found " + layers.size() + " layers for user mapfile.");
261 * 269 *
262 * @throws FileNotFoundException if no shapefile path is found or 270 * @throws FileNotFoundException if no shapefile path is found or
263 * configured. 271 * configured.
264 */ 272 */
265 protected File getShapefileBaseDir() 273 protected File getShapefileBaseDir()
266 throws FileNotFoundException 274 throws FileNotFoundException, IOException
267 { 275 {
268 if (shapefileDirectory == null) { 276 if (shapefileDirectory == null) {
269 String path = FLYSUtils.getXPathString( 277 String path = FLYSUtils.getXPathString(
270 FLYSUtils.XPATH_SHAPEFILE_DIR); 278 FLYSUtils.XPATH_SHAPEFILE_DIR);
271 279
272 if (path != null) { 280 if (path != null) {
273 shapefileDirectory = new File(path); 281 shapefileDirectory = new File(path);
274 } 282 }
275 283
276 if (shapefileDirectory == null || !shapefileDirectory.exists()) { 284 if (shapefileDirectory == null) {
277 throw new FileNotFoundException("No shapefile directory given"); 285 throw new FileNotFoundException("No shapefile directory given");
278 } 286 }
287
288 if (!shapefileDirectory.exists()) {
289 shapefileDirectory.createNewFile();
290 }
279 } 291 }
280 292
281 return shapefileDirectory; 293 return shapefileDirectory;
282 } 294 }
283 295
284 296
285 protected File[] getUserDirs() 297 protected File[] getUserDirs()
286 throws FileNotFoundException 298 throws FileNotFoundException, IOException
287 { 299 {
288 File baseDir = getShapefileBaseDir(); 300 File baseDir = getShapefileBaseDir();
289 File[] artifactDirs = baseDir.listFiles(); 301 File[] artifactDirs = baseDir.listFiles();
290 302
291 // TODO ONLY RETURN DIRECTORIES OF THE SPECIFIED USER 303 // TODO ONLY RETURN DIRECTORIES OF THE SPECIFIED USER
330 dir.getName() + MS_WSPLGEN_POSTFIX, 342 dir.getName() + MS_WSPLGEN_POSTFIX,
331 "POLYGON", 343 "POLYGON",
332 dir.getName(), 344 dir.getName(),
333 WSPLGEN_RESULT_SHAPE, 345 WSPLGEN_RESULT_SHAPE,
334 "I18N_WSPLGEN_RESULT"); 346 "I18N_WSPLGEN_RESULT");
347 }
348
349
350 /**
351 * Creates a layer file used for Mapserver's mapfile which represents the
352 * floodmap.
353 *
354 * @param flys The FLYSArtifact that owns <i>wms</i>.
355 * @param wms The WMSLayerFacet that contains information for the layer.
356 */
357 public void createUeskLayer(FLYSArtifact flys, WMSLayerFacet wms)
358 throws FileNotFoundException, IOException
359 {
360 logger.debug("createUeskLayer");
361
362 LayerInfo layerinfo = new LayerInfo(
363 flys.identifier() + MS_WSPLGEN_POSTFIX,
364 "POLYGON",
365 flys.identifier(),
366 WSPLGEN_RESULT_SHAPE,
367 "I18N_WSPLGEN_RESULT");
368
369 String name = MS_LAYER_PREFIX + wms.getName();
370
371 Template template = getTemplateByName(SHP_LAYER_TEMPLATE);
372 if (template == null) {
373 logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
374 return;
375 }
376
377 try {
378 File dir = new File(getShapefileBaseDir(), flys.identifier());
379 writeLayer(layerinfo, dir, name, template);
380 }
381 catch (FileNotFoundException fnfe) {
382 logger.error(fnfe, fnfe);
383 logger.warn("Unable to write layer: " + name);
384 }
385 }
386
387
388 /**
389 * Creates a layer file used for Mapserver's mapfile which represents the
390 * user defined barriers.
391 *
392 * @param flys The FLYSArtifact that owns <i>wms</i>.
393 * @param wms The WMSLayerFacet that contains information for the layer.
394 */
395 public void createBarriersLayer(FLYSArtifact flys, WMSLayerFacet wms)
396 throws FileNotFoundException, IOException
397 {
398 logger.debug("createBarriersLayer");
399
400 String uuid = flys.identifier();
401 File dir = new File(getShapefileBaseDir(), uuid);
402
403 String group = uuid + MS_BARRIERS_POSTFIX;
404 String groupTitle = "I18N_BARRIERS_TITLE";
405
406 LayerInfo lineInfo = new LayerInfo(
407 uuid + MS_LINE_POSTFIX,
408 "LINE",
409 uuid,
410 WSPLGEN_LINES_SHAPE,
411 "I18N_LINE_SHAPE",
412 group,
413 groupTitle);
414
415 LayerInfo polygonInfo = new LayerInfo(
416 uuid + MS_POLYGONS_POSTFIX,
417 "POLYGON",
418 uuid,
419 WSPLGEN_POLYGONS_SHAPE,
420 "I18N_POLYGON_SHAPE",
421 group,
422 groupTitle);
423
424 String nameLines = MS_LAYER_PREFIX + wms.getName() + "-lines";
425 String namePolygons = MS_LAYER_PREFIX + wms.getName() + "-polygons";
426
427 Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE);
428 if (tpl == null) {
429 logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
430 return;
431 }
432
433 try {
434 writeLayer(lineInfo, dir, nameLines, tpl);
435 }
436 catch (FileNotFoundException fnfe) {
437 logger.error(fnfe, fnfe);
438 logger.warn("Unable to write layer: " + nameLines);
439 }
440
441 try {
442 writeLayer(polygonInfo, dir, namePolygons, tpl);
443 }
444 catch (FileNotFoundException fnfe) {
445 logger.error(fnfe, fnfe);
446 logger.warn("Unable to write layer: " + nameLines);
447 }
335 } 448 }
336 449
337 450
338 protected List<LayerInfo> parseBarriersLayers(File dir) { 451 protected List<LayerInfo> parseBarriersLayers(File dir) {
339 List<LayerInfo> barriers = new ArrayList<LayerInfo>(2); 452 List<LayerInfo> barriers = new ArrayList<LayerInfo>(2);
370 483
371 return barriers; 484 return barriers;
372 } 485 }
373 486
374 487
488 protected void writeLayer(
489 LayerInfo layerinfo,
490 File dir,
491 String name,
492 Template tpl
493 )
494 throws FileNotFoundException
495 {
496 if (logger.isDebugEnabled()) {
497 logger.debug("Write layer for:");
498 logger.debug(" directory: " + dir.getName());
499 logger.debug(" name: " + name);
500 }
501
502 File layer = new File(dir, name);
503 Writer writer = null;
504
505 try {
506 writer = new FileWriter(layer);
507
508 VelocityContext context = new VelocityContext();
509 context.put("LAYER", layerinfo);
510
511 tpl.merge(context, writer);
512 }
513 catch (FileNotFoundException fnfe) {
514 logger.error(fnfe, fnfe);
515 }
516 catch (IOException ioe) {
517 logger.error(ioe, ioe);
518 }
519 catch (Exception e) {
520 logger.error(e, e);
521 }
522 finally {
523 try {
524 if (writer != null) {
525 writer.close();
526 }
527 }
528 catch (IOException ioe) {
529 logger.debug(ioe, ioe);
530 }
531 }
532 }
533
534
375 /** 535 /**
376 * Creates a mapfile with the layer information stored in <i>layers</i>. 536 * Creates a mapfile with the layer information stored in <i>layers</i>.
377 * 537 *
378 * @param layers Layer information. 538 * @param layers Layer information.
379 */ 539 */

http://dive4elements.wald.intevation.org