comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java @ 5312:2c1045a1e3fe

Added new states and transitions to UESK calculation and adjusted states and UI. TODO: Some refactoring and code cleanup.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 14 Mar 2013 17:25:00 +0100
parents cf7e0d9022fa
children a1cb9a734cc5
comparison
equal deleted inserted replaced
5311:ff9dfc58d0cb 5312:2c1045a1e3fe
41 import de.intevation.flys.artifacts.context.FLYSContext; 41 import de.intevation.flys.artifacts.context.FLYSContext;
42 42
43 import de.intevation.flys.artifacts.model.CalculationMessage; 43 import de.intevation.flys.artifacts.model.CalculationMessage;
44 import de.intevation.flys.artifacts.model.CalculationResult; 44 import de.intevation.flys.artifacts.model.CalculationResult;
45 import de.intevation.flys.artifacts.model.FacetTypes; 45 import de.intevation.flys.artifacts.model.FacetTypes;
46 import de.intevation.flys.artifacts.model.LayerInfo;
46 import de.intevation.flys.artifacts.model.WQKms; 47 import de.intevation.flys.artifacts.model.WQKms;
47 48 import de.intevation.flys.artifacts.model.map.HWS;
49 import de.intevation.flys.artifacts.model.map.HWSContainer;
50 import de.intevation.flys.artifacts.model.map.HWSFactory;
48 import de.intevation.flys.artifacts.model.map.WMSLayerFacet; 51 import de.intevation.flys.artifacts.model.map.WMSLayerFacet;
49 import de.intevation.flys.artifacts.model.map.WSPLGENCalculation; 52 import de.intevation.flys.artifacts.model.map.WSPLGENCalculation;
50 import de.intevation.flys.artifacts.model.map.WSPLGENJob; 53 import de.intevation.flys.artifacts.model.map.WSPLGENJob;
51 import de.intevation.flys.artifacts.model.map.WSPLGENReportFacet; 54 import de.intevation.flys.artifacts.model.map.WSPLGENReportFacet;
52 55
65 import de.intevation.flys.utils.MapfileGenerator; 68 import de.intevation.flys.utils.MapfileGenerator;
66 69
67 import de.intevation.flys.wsplgen.FacetCreator; 70 import de.intevation.flys.wsplgen.FacetCreator;
68 import de.intevation.flys.wsplgen.JobObserver; 71 import de.intevation.flys.wsplgen.JobObserver;
69 import de.intevation.flys.wsplgen.Scheduler; 72 import de.intevation.flys.wsplgen.Scheduler;
73
74 import java.io.File;
75 import java.io.FileNotFoundException;
76 import java.io.FileOutputStream;
77 import java.io.IOException;
78 import java.util.ArrayList;
79 import java.util.Arrays;
80 import java.util.List;
81
82 import javax.naming.Context;
83
84 import org.apache.log4j.Logger;
85 import org.apache.velocity.Template;
86 import org.geotools.feature.FeatureCollection;
87 import org.geotools.feature.FeatureCollections;
88 import org.geotools.feature.simple.SimpleFeatureBuilder;
89 import org.hibernate.HibernateException;
90 import org.opengis.feature.simple.SimpleFeature;
91 import org.opengis.feature.simple.SimpleFeatureType;
92
70 93
71 public class FloodMapState 94 public class FloodMapState
72 extends DefaultState 95 extends DefaultState
73 implements FacetTypes 96 implements FacetTypes
74 { 97 {
100 123
101 public static final String WSPLGEN_QPS_NAME = "qps"; 124 public static final String WSPLGEN_QPS_NAME = "qps";
102 125
103 public static final int WSPLGEN_DEFAULT_OUTPUT = 0; 126 public static final int WSPLGEN_DEFAULT_OUTPUT = 0;
104 127
128 private static final String HWS_LINES_SHAPE = "hws-lines.shp";
129
130 private static final String I18N_HWS_POINTS = "floodmap.hws.points";
131 private static final String I18N_HWS_LINES = "floodmap.hws.lines";
132 private static final String HWS_LINES = "hws-lines";
133 private static final String HWS_POINT_SHAPE = "hws-points.shp";
134 private static final String HWS_POINTS = "hws-points";
105 135
106 /** 136 /**
107 * @param orig 137 * @param orig
108 * @param owner 138 * @param owner
109 * @param context 139 * @param context
303 File artifactDir, 333 File artifactDir,
304 CallContext context, 334 CallContext context,
305 WSPLGENCalculation calculation 335 WSPLGENCalculation calculation
306 ) { 336 ) {
307 logger.debug("FloodMapState.prepareWSPLGENJob"); 337 logger.debug("FloodMapState.prepareWSPLGENJob");
338 String scenario = artifact.getDataAsString("scenario");
308 339
309 WSPLGENJob job = new WSPLGENJob( 340 WSPLGENJob job = new WSPLGENJob(
310 artifact, 341 artifact,
311 artifactDir, 342 artifactDir,
312 facetCreator, 343 facetCreator,
318 setOut(artifact, job); 349 setOut(artifact, job);
319 setRange(artifact, job); 350 setRange(artifact, job);
320 setDelta(artifact, job); 351 setDelta(artifact, job);
321 setGel(artifact, job); 352 setGel(artifact, job);
322 setDist(artifact, job); 353 setDist(artifact, job);
323 setLine(artifact, facetCreator, artifactDir, job);
324 setUserShape(artifact, facetCreator, artifactDir, job);
325 setAxis(artifact, artifactDir, job); 354 setAxis(artifact, artifactDir, job);
326 setPro(artifact, artifactDir, job); 355 setPro(artifact, artifactDir, job);
327 setDgm(artifact, job, context); 356 setDgm(artifact, job, context);
328 setArea(artifact, artifactDir, job); 357 setArea(artifact, artifactDir, job);
329 setOutFile(artifact, job); 358 setOutFile(artifact, job);
330 setWsp(artifact, context, artifactDir, job); // WSP 359 setWsp(artifact, context, artifactDir, job); // WSP
331 360 if (scenario.equals("scenario.current")) {
361 setOfficialHWS(artifact, facetCreator, artifactDir, job);
362 }
363 else if (scenario.equals("scenario.scenario")) {
364 setAdditionalHWS(artifact, facetCreator, artifactDir, job);
365 setLine(artifact, facetCreator, artifactDir, job);
366 setUserShape(artifact, facetCreator, artifactDir, job);
367 }
332 // TODO 368 // TODO
333 // setWspTag(artifact, job); 369 // setWspTag(artifact, job);
334 370
335 try { 371 try {
336 job.toFile(paraFile); 372 job.toFile(paraFile);
343 catch (IllegalArgumentException iae) { 379 catch (IllegalArgumentException iae) {
344 logger.warn("Cannot write PAR file: " + iae.getMessage()); 380 logger.warn("Cannot write PAR file: " + iae.getMessage());
345 } 381 }
346 382
347 return null; 383 return null;
384 }
385
386
387 private void setAdditionalHWS(
388 FLYSArtifact artifact,
389 FacetCreator facetCreator,
390 File dir,
391 WSPLGENJob job) {
392 File line = new File(dir, HWS_LINES_SHAPE);
393 boolean lines = line.exists();
394 logger.debug("shp file exists: " + lines);
395 if (lines) {
396 job.addLin(dir + "/" + HWS_LINES_SHAPE);
397 facetCreator.createShapeFacet(I18N_HWS_LINES,
398 MapfileGenerator.MS_LAYER_PREFIX + HWS_LINES,
399 FLOODMAP_LINES, 2);
400 }
401 File point = new File(dir, HWS_POINT_SHAPE);
402 boolean points = point.exists();
403 logger.debug("shp file exists: " + points);
404 if (points) {
405 facetCreator.createShapeFacet(I18N_HWS_POINTS,
406 MapfileGenerator.MS_LAYER_PREFIX + HWS_POINTS,
407 FLOODMAP_FIXPOINTS, 3);
408 }
409 }
410
411
412 private void setOfficialHWS(
413 FLYSArtifact artifact,
414 FacetCreator facetCreator,
415 File artifactDir,
416 WSPLGENJob job) {
417 String river = artifact.getDataAsString("river");
418
419 HWSContainer hwsLines = HWSFactory.getHWSLines(river);
420 List<HWS> selectedLines = hwsLines.getOfficialHWS();
421
422 FeatureCollection collectionLines = FeatureCollections.newCollection();
423 SimpleFeatureType lineType = null;
424 for (HWS h : selectedLines) {
425 lineType = h.getFeatureType();
426 collectionLines.add(h.getFeature());
427 }
428 boolean successLines = false;
429 if (lineType != null && collectionLines.size() > 0) {
430 File shapeLines = new File(artifactDir, HWS_LINES_SHAPE);
431 successLines = GeometryUtils.writeShapefile(
432 shapeLines, lineType, collectionLines);
433 }
434 if (successLines) {
435 createMapfile(
436 artifact,
437 artifactDir,
438 MapfileGenerator.MS_LAYER_PREFIX + "hws-lines",
439 HWS_LINES_SHAPE,
440 "LINE",
441 "31467",
442 "hws");
443 job.addLin(artifactDir + "/" + HWS_LINES_SHAPE);
444 facetCreator.createShapeFacet(I18N_HWS_LINES,
445 MapfileGenerator.MS_LAYER_PREFIX + HWS_LINES,
446 FLOODMAP_HWS,2);
447 }
448 }
449
450
451 private void createMapfile(
452 FLYSArtifact artifact,
453 File artifactDir,
454 String name,
455 String hwsShapefile,
456 String type,
457 String srid,
458 String group
459 ) {
460 LayerInfo info = new LayerInfo();
461 info.setName(name + artifact.identifier());
462 info.setType(type);
463 info.setDirectory(artifact.identifier());
464 info.setTitle(name);
465 info.setData(hwsShapefile);
466 info.setSrid(srid);
467 info.setGroupTitle(group);
468 MapfileGenerator generator = new ArtifactMapfileGenerator();
469 Template tpl = generator.getTemplateByName(MapfileGenerator.SHP_LAYER_TEMPLATE);
470 try {
471 File layer = new File(artifactDir.getCanonicalPath() + "/" + name);
472 generator.writeLayer(info, layer, tpl);
473 List<String> layers = new ArrayList<String>();
474 layers.add(layer.getAbsolutePath());
475 generator.generate();
476 }
477 catch(FileNotFoundException fnfe) {
478 logger.warn("Could not find mapfile for hws layer");
479 }
480 catch (Exception ioe) {
481 logger.warn("Could not create mapfile for hws layer");
482 logger.warn(Arrays.toString(ioe.getStackTrace()));
483 }
348 } 484 }
349 485
350 486
351 protected void setOut(FLYSArtifact artifact, WSPLGENJob job) { 487 protected void setOut(FLYSArtifact artifact, WSPLGENJob job) {
352 job.setOut(WSPLGEN_DEFAULT_OUTPUT); 488 job.setOut(WSPLGEN_DEFAULT_OUTPUT);
462 598
463 if (l) { 599 if (l) {
464 logger.debug( 600 logger.debug(
465 "Successfully created barrier line shapefile. " + 601 "Successfully created barrier line shapefile. " +
466 "Write shapefile path into WSPLGEN job."); 602 "Write shapefile path into WSPLGEN job.");
603 createMapfile(
604 artifact,
605 dir,
606 MapfileGenerator.MS_LAYER_PREFIX + "barriers-lines",
607 WSPLGEN_BARRIERS_LINES,
608 "LINE",
609 srid,
610 "barriers");
467 611
468 if (scenario.equals(WSPLGENJob.GEL_NOSPERRE)) { 612 if (scenario.equals(WSPLGENJob.GEL_NOSPERRE)) {
469 logger.debug("WSPLGEN will not use barrier features."); 613 logger.debug("WSPLGEN will not use barrier features.");
470 } 614 }
471 else { 615 else {
476 boolean p = GeometryUtils.writeShapefile( 620 boolean p = GeometryUtils.writeShapefile(
477 shapePolys, 621 shapePolys,
478 GeometryUtils.buildFeatureType("polygons", srs, Polygon.class, obj), 622 GeometryUtils.buildFeatureType("polygons", srs, Polygon.class, obj),
479 fcs[1]); 623 fcs[1]);
480 624
625
481 if (p) { 626 if (p) {
482 logger.debug( 627 logger.debug(
483 "Successfully created barrier polygon shapefile. " + 628 "Successfully created barrier polygon shapefile. " +
484 "Write shapefile path into WSPLGEN job."); 629 "Write shapefile path into WSPLGEN job.");
630 createMapfile(
631 artifact,
632 dir,
633 MapfileGenerator.MS_LAYER_PREFIX + "barriers-poly",
634 shapePolys.getAbsolutePath(),
635 "POLYGON",
636 srid,
637 "barriers");
485 638
486 if (scenario.equals(WSPLGENJob.GEL_NOSPERRE)) { 639 if (scenario.equals(WSPLGENJob.GEL_NOSPERRE)) {
487 logger.debug("WSPLGEN will not use barrier features."); 640 logger.debug("WSPLGEN will not use barrier features.");
488 } 641 }
489 else { 642 else {
501 FLYSArtifact artifact, 654 FLYSArtifact artifact,
502 FacetCreator facetCreator, 655 FacetCreator facetCreator,
503 File dir, 656 File dir,
504 WSPLGENJob job 657 WSPLGENJob job
505 ) { 658 ) {
506 File archive = new File(dir, WSPLGEN_USER_ZIP); 659 File archive = new File(dir, WSPLGEN_USER_SHAPE);
507 boolean exists = archive.exists(); 660 boolean exists = archive.exists();
508 logger.debug("Zip file exists: " + exists); 661 logger.debug("shp file exists: " + exists);
509 if (exists) { 662 if (exists) {
510 try {
511 File tmpDir = new File(dir, "usr_tmp");
512 FileTools.extractArchive(archive, tmpDir);
513 moveFiles(tmpDir, dir);
514 }
515 catch (IOException ioe) {
516 logger.warn("Zip archive " + dir + "/" + WSPLGEN_USER_ZIP + " could not be extracted.");
517 return;
518 }
519
520 job.addLin(dir + "/" + WSPLGEN_USER_SHAPE); 663 job.addLin(dir + "/" + WSPLGEN_USER_SHAPE);
521 facetCreator.createUserShapeFacet(); 664 facetCreator.createShapeFacet(FacetCreator.I18N_USERSHAPE,
665 MapfileGenerator.MS_LAYER_PREFIX + "user-rgd",
666 FLOODMAP_USERSHAPE,
667 4);
522 } 668 }
523 } 669 }
524 670
525 protected SimpleFeatureType getBarriersFeatureType( 671 protected SimpleFeatureType getBarriersFeatureType(
526 String name, 672 String name,
881 } 1027 }
882 } 1028 }
883 } 1029 }
884 1030
885 1031
886 protected void moveFiles(File source, final File target) 1032
887 throws IOException
888 {
889 if (!source.exists()) {
890 return;
891 }
892 if (!target.exists()) {
893 target.mkdir();
894 }
895 FileTools.walkTree(source, new FileTools.FileVisitor() {
896 @Override
897 public boolean visit(File file) {
898 if (!file.isDirectory()) {
899 String name = file.getName();
900 String suffix = "";
901 int pos = name.lastIndexOf('.');
902 if (pos > 0 && pos < name.length() - 1) {
903 suffix = name.substring(pos + 1);
904 }
905 else {
906 return true;
907 }
908 try {
909 FileTools.copyFile(file, new File(target, WSPLGEN_USER_FILENAME + "." + suffix));
910 }
911 catch (IOException ioe) {
912 logger.warn ("Error while copying file " + file.getName());
913 return true;
914 }
915 }
916 return true;
917 }
918 });
919
920 FileTools.deleteRecursive(source);
921 }
922 } 1033 }
923 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : 1034 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org