comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java @ 5379:61bf64b102bc mapgenfix

Merge with default branch
author Christian Lins <christian.lins@intevation.de>
date Fri, 22 Mar 2013 11:25:54 +0100
parents 2b24073e80d9 d43ef9f709cd
children
comparison
equal deleted inserted replaced
5175:cfc5540a4eec 5379:61bf64b102bc
15 import de.intevation.flys.artifacts.access.RangeAccess; 15 import de.intevation.flys.artifacts.access.RangeAccess;
16 import de.intevation.flys.artifacts.context.FLYSContext; 16 import de.intevation.flys.artifacts.context.FLYSContext;
17 import de.intevation.flys.artifacts.model.CalculationMessage; 17 import de.intevation.flys.artifacts.model.CalculationMessage;
18 import de.intevation.flys.artifacts.model.CalculationResult; 18 import de.intevation.flys.artifacts.model.CalculationResult;
19 import de.intevation.flys.artifacts.model.FacetTypes; 19 import de.intevation.flys.artifacts.model.FacetTypes;
20 import de.intevation.flys.artifacts.model.LayerInfo;
20 import de.intevation.flys.artifacts.model.WQKms; 21 import de.intevation.flys.artifacts.model.WQKms;
22 import de.intevation.flys.artifacts.model.map.HWS;
23 import de.intevation.flys.artifacts.model.map.HWSContainer;
24 import de.intevation.flys.artifacts.model.map.HWSFactory;
21 import de.intevation.flys.artifacts.model.map.WMSLayerFacet; 25 import de.intevation.flys.artifacts.model.map.WMSLayerFacet;
22 import de.intevation.flys.artifacts.model.map.WSPLGENCalculation; 26 import de.intevation.flys.artifacts.model.map.WSPLGENCalculation;
23 import de.intevation.flys.artifacts.model.map.WSPLGENJob; 27 import de.intevation.flys.artifacts.model.map.WSPLGENJob;
24 import de.intevation.flys.artifacts.model.map.WSPLGENReportFacet; 28 import de.intevation.flys.artifacts.model.map.WSPLGENReportFacet;
25 import de.intevation.flys.artifacts.resources.Resources; 29 import de.intevation.flys.artifacts.resources.Resources;
40 import java.io.File; 44 import java.io.File;
41 import java.io.FileNotFoundException; 45 import java.io.FileNotFoundException;
42 import java.io.FileOutputStream; 46 import java.io.FileOutputStream;
43 import java.io.IOException; 47 import java.io.IOException;
44 import java.util.ArrayList; 48 import java.util.ArrayList;
49 import java.util.Arrays;
45 import java.util.List; 50 import java.util.List;
46 51
47 import org.apache.log4j.Logger; 52 import org.apache.log4j.Logger;
53 import org.apache.velocity.Template;
48 import org.geotools.feature.FeatureCollection; 54 import org.geotools.feature.FeatureCollection;
49 import org.geotools.feature.FeatureCollections; 55 import org.geotools.feature.FeatureCollections;
50 import org.geotools.feature.simple.SimpleFeatureBuilder; 56 import org.geotools.feature.simple.SimpleFeatureBuilder;
57 import org.hibernate.HibernateException;
51 import org.opengis.feature.simple.SimpleFeature; 58 import org.opengis.feature.simple.SimpleFeature;
52 import org.opengis.feature.simple.SimpleFeatureType; 59 import org.opengis.feature.simple.SimpleFeatureType;
53
54 60
55 public class FloodMapState 61 public class FloodMapState
56 extends DefaultState 62 extends DefaultState
57 implements FacetTypes 63 implements FacetTypes
58 { 64 {
84 90
85 public static final String WSPLGEN_QPS_NAME = "qps"; 91 public static final String WSPLGEN_QPS_NAME = "qps";
86 92
87 public static final int WSPLGEN_DEFAULT_OUTPUT = 0; 93 public static final int WSPLGEN_DEFAULT_OUTPUT = 0;
88 94
95 private static final String HWS_LINES_SHAPE = "hws-lines.shp";
96
97 private static final String I18N_HWS_POINTS = "floodmap.hws.points";
98 private static final String I18N_HWS_LINES = "floodmap.hws.lines";
99 private static final String HWS_LINES = "hws-lines";
100 private static final String HWS_POINT_SHAPE = "hws-points.shp";
101 private static final String HWS_POINTS = "hws-points";
89 102
90 /** 103 /**
91 * @param orig 104 * @param orig
92 * @param owner 105 * @param owner
93 * @param context 106 * @param context
287 File artifactDir, 300 File artifactDir,
288 CallContext context, 301 CallContext context,
289 WSPLGENCalculation calculation 302 WSPLGENCalculation calculation
290 ) { 303 ) {
291 logger.debug("FloodMapState.prepareWSPLGENJob"); 304 logger.debug("FloodMapState.prepareWSPLGENJob");
305 String scenario = artifact.getDataAsString("scenario");
292 306
293 WSPLGENJob job = new WSPLGENJob( 307 WSPLGENJob job = new WSPLGENJob(
294 artifact, 308 artifact,
295 artifactDir, 309 artifactDir,
296 facetCreator, 310 facetCreator,
302 setOut(artifact, job); 316 setOut(artifact, job);
303 setRange(artifact, job); 317 setRange(artifact, job);
304 setDelta(artifact, job); 318 setDelta(artifact, job);
305 setGel(artifact, job); 319 setGel(artifact, job);
306 setDist(artifact, job); 320 setDist(artifact, job);
307 setLine(artifact, facetCreator, artifactDir, job);
308 setUserShape(artifact, facetCreator, artifactDir, job);
309 setAxis(artifact, artifactDir, job); 321 setAxis(artifact, artifactDir, job);
310 setPro(artifact, artifactDir, job); 322 setPro(artifact, artifactDir, job);
311 setDgm(artifact, job); 323 setDgm(artifact, job, context);
312 setArea(artifact, artifactDir, job); 324 setArea(artifact, artifactDir, job);
313 setOutFile(artifact, job); 325 setOutFile(artifact, job);
314 setWsp(artifact, context, artifactDir, job); // WSP 326 setWsp(artifact, context, artifactDir, job); // WSP
315 327 if (scenario.equals("scenario.current")) {
328 setOfficialHWS(artifact, facetCreator, artifactDir, job);
329 }
330 else if (scenario.equals("scenario.scenario")) {
331 setAdditionalHWS(artifact, facetCreator, artifactDir, job);
332 setLine(artifact, facetCreator, artifactDir, job);
333 setUserShape(artifact, facetCreator, artifactDir, job);
334 }
316 // TODO 335 // TODO
317 // setWspTag(artifact, job); 336 // setWspTag(artifact, job);
318 337
319 try { 338 try {
320 job.toFile(paraFile); 339 job.toFile(paraFile);
327 catch (IllegalArgumentException iae) { 346 catch (IllegalArgumentException iae) {
328 logger.warn("Cannot write PAR file: " + iae.getMessage()); 347 logger.warn("Cannot write PAR file: " + iae.getMessage());
329 } 348 }
330 349
331 return null; 350 return null;
351 }
352
353
354 private void setAdditionalHWS(
355 FLYSArtifact artifact,
356 FacetCreator facetCreator,
357 File dir,
358 WSPLGENJob job) {
359 File line = new File(dir, HWS_LINES_SHAPE);
360 boolean lines = line.exists();
361 logger.debug("shp file exists: " + lines);
362 if (lines) {
363 job.addLin(dir + "/" + HWS_LINES_SHAPE);
364 facetCreator.createShapeFacet(I18N_HWS_LINES,
365 MapfileGenerator.MS_LAYER_PREFIX + HWS_LINES,
366 FLOODMAP_LINES, 2);
367 }
368 File point = new File(dir, HWS_POINT_SHAPE);
369 boolean points = point.exists();
370 logger.debug("shp file exists: " + points);
371 if (points) {
372 facetCreator.createShapeFacet(I18N_HWS_POINTS,
373 MapfileGenerator.MS_LAYER_PREFIX + HWS_POINTS,
374 FLOODMAP_FIXPOINTS, 3);
375 }
376 }
377
378
379 private void setOfficialHWS(
380 FLYSArtifact artifact,
381 FacetCreator facetCreator,
382 File artifactDir,
383 WSPLGENJob job) {
384 String river = artifact.getDataAsString("river");
385
386 HWSContainer hwsLines = HWSFactory.getHWSLines(river);
387 List<HWS> selectedLines = hwsLines.getOfficialHWS();
388
389 FeatureCollection collectionLines = FeatureCollections.newCollection();
390 SimpleFeatureType lineType = null;
391 for (HWS h : selectedLines) {
392 lineType = h.getFeatureType();
393 collectionLines.add(h.getFeature());
394 }
395 boolean successLines = false;
396 if (lineType != null && collectionLines.size() > 0) {
397 File shapeLines = new File(artifactDir, HWS_LINES_SHAPE);
398 successLines = GeometryUtils.writeShapefile(
399 shapeLines, lineType, collectionLines);
400 }
401 if (successLines) {
402 createMapfile(
403 artifact,
404 artifactDir,
405 MapfileGenerator.MS_LAYER_PREFIX + "hws-lines",
406 HWS_LINES_SHAPE,
407 "LINE",
408 "31467",
409 "hws");
410 job.addLin(artifactDir + "/" + HWS_LINES_SHAPE);
411 facetCreator.createShapeFacet(I18N_HWS_LINES,
412 MapfileGenerator.MS_LAYER_PREFIX + HWS_LINES,
413 FLOODMAP_HWS_LINES,2);
414 }
415 }
416
417
418 private void createMapfile(
419 FLYSArtifact artifact,
420 File artifactDir,
421 String name,
422 String hwsShapefile,
423 String type,
424 String srid,
425 String group
426 ) {
427 LayerInfo info = new LayerInfo();
428 info.setName(name + artifact.identifier());
429 info.setType(type);
430 info.setDirectory(artifact.identifier());
431 info.setTitle(name);
432 info.setData(hwsShapefile);
433 info.setSrid(srid);
434 info.setGroupTitle(group);
435 MapfileGenerator generator = new ArtifactMapfileGenerator();
436 Template tpl = generator.getTemplateByName(MapfileGenerator.SHP_LAYER_TEMPLATE);
437 try {
438 File layer = new File(artifactDir.getCanonicalPath() + "/" + name);
439 generator.writeLayer(info, layer, tpl);
440 List<String> layers = new ArrayList<String>();
441 layers.add(layer.getAbsolutePath());
442 generator.generate();
443 }
444 catch(FileNotFoundException fnfe) {
445 logger.warn("Could not find mapfile for hws layer");
446 }
447 catch (Exception ioe) {
448 logger.warn("Could not create mapfile for hws layer");
449 logger.warn(Arrays.toString(ioe.getStackTrace()));
450 }
332 } 451 }
333 452
334 453
335 protected void setOut(FLYSArtifact artifact, WSPLGENJob job) { 454 protected void setOut(FLYSArtifact artifact, WSPLGENJob job) {
336 job.setOut(WSPLGEN_DEFAULT_OUTPUT); 455 job.setOut(WSPLGEN_DEFAULT_OUTPUT);
446 565
447 if (l) { 566 if (l) {
448 logger.debug( 567 logger.debug(
449 "Successfully created barrier line shapefile. " + 568 "Successfully created barrier line shapefile. " +
450 "Write shapefile path into WSPLGEN job."); 569 "Write shapefile path into WSPLGEN job.");
570 createMapfile(
571 artifact,
572 dir,
573 MapfileGenerator.MS_LAYER_PREFIX + "barriers-lines",
574 WSPLGEN_BARRIERS_LINES,
575 "LINE",
576 srid,
577 "barriers");
451 578
452 if (scenario.equals(WSPLGENJob.GEL_NOSPERRE)) { 579 if (scenario.equals(WSPLGENJob.GEL_NOSPERRE)) {
453 logger.debug("WSPLGEN will not use barrier features."); 580 logger.debug("WSPLGEN will not use barrier features.");
454 } 581 }
455 else { 582 else {
460 boolean p = GeometryUtils.writeShapefile( 587 boolean p = GeometryUtils.writeShapefile(
461 shapePolys, 588 shapePolys,
462 GeometryUtils.buildFeatureType("polygons", srs, Polygon.class, obj), 589 GeometryUtils.buildFeatureType("polygons", srs, Polygon.class, obj),
463 fcs[1]); 590 fcs[1]);
464 591
592
465 if (p) { 593 if (p) {
466 logger.debug( 594 logger.debug(
467 "Successfully created barrier polygon shapefile. " + 595 "Successfully created barrier polygon shapefile. " +
468 "Write shapefile path into WSPLGEN job."); 596 "Write shapefile path into WSPLGEN job.");
597 createMapfile(
598 artifact,
599 dir,
600 MapfileGenerator.MS_LAYER_PREFIX + "barriers-poly",
601 shapePolys.getAbsolutePath(),
602 "POLYGON",
603 srid,
604 "barriers");
469 605
470 if (scenario.equals(WSPLGENJob.GEL_NOSPERRE)) { 606 if (scenario.equals(WSPLGENJob.GEL_NOSPERRE)) {
471 logger.debug("WSPLGEN will not use barrier features."); 607 logger.debug("WSPLGEN will not use barrier features.");
472 } 608 }
473 else { 609 else {
485 FLYSArtifact artifact, 621 FLYSArtifact artifact,
486 FacetCreator facetCreator, 622 FacetCreator facetCreator,
487 File dir, 623 File dir,
488 WSPLGENJob job 624 WSPLGENJob job
489 ) { 625 ) {
490 File archive = new File(dir, WSPLGEN_USER_ZIP); 626 File archive = new File(dir, WSPLGEN_USER_SHAPE);
491 boolean exists = archive.exists(); 627 boolean exists = archive.exists();
492 logger.debug("Zip file exists: " + exists); 628 logger.debug("shp file exists: " + exists);
493 if (exists) { 629 if (exists) {
494 FileUtils.extractZipfile(archive, dir); 630 FileUtils.extractZipfile(archive, dir);
495 631
496 job.addLin(dir + "/" + WSPLGEN_USER_SHAPE); 632 job.addLin(dir + "/" + WSPLGEN_USER_SHAPE);
497 facetCreator.createUserShapeFacet(); 633 facetCreator.createShapeFacet(FacetCreator.I18N_USERSHAPE,
634 MapfileGenerator.MS_LAYER_PREFIX + "user-rgd",
635 FLOODMAP_USERSHAPE,
636 4);
498 } 637 }
499 } 638 }
500 639
501 protected SimpleFeatureType getBarriersFeatureType( 640 protected SimpleFeatureType getBarriersFeatureType(
502 String name, 641 String name,
604 743
605 protected void setAxis(FLYSArtifact artifact, File dir, WSPLGENJob job) { 744 protected void setAxis(FLYSArtifact artifact, File dir, WSPLGENJob job) {
606 String river = artifact.getDataAsString("river"); 745 String river = artifact.getDataAsString("river");
607 String srid = FLYSUtils.getRiverDGMSrid(river); 746 String srid = FLYSUtils.getRiverDGMSrid(river);
608 String srs = "EPSG:" + srid; 747 String srs = "EPSG:" + srid;
609 logger.debug("srs: " + srs); 748
610 List<RiverAxis> axes = RiverAxis.getRiverAxis(river); 749 List<RiverAxis> axes = null;
750 try {
751 axes = RiverAxis.getRiverAxis(river);
752 }
753 catch (HibernateException iae) {
754 logger.warn("No valid river axis found for " + river);
755 return;
756 }
611 if (axes == null || axes.isEmpty()) { 757 if (axes == null || axes.isEmpty()) {
612 logger.warn("Could not find river axis for: '" + river + "'"); 758 logger.warn("Could not find river axis for: '" + river + "'");
613 return; 759 return;
614 } 760 }
615 761
682 job.setPro(qpsShape.getAbsolutePath()); 828 job.setPro(qpsShape.getAbsolutePath());
683 } 829 }
684 } 830 }
685 831
686 832
687 protected void setDgm(FLYSArtifact artifact, WSPLGENJob job) { 833 protected void setDgm(
834 FLYSArtifact artifact,
835 WSPLGENJob job,
836 CallContext context
837 ) {
688 String dgm_id = artifact.getDataAsString("dgm"); 838 String dgm_id = artifact.getDataAsString("dgm");
689 839
690 int id = -1; 840 int id = -1;
691 try { 841 try {
692 id = Integer.parseInt(dgm_id); 842 id = Integer.parseInt(dgm_id);
699 logger.warn("Could not find specified DGM."); 849 logger.warn("Could not find specified DGM.");
700 850
701 return; 851 return;
702 } 852 }
703 853
704 job.setDgm(dgm.getPath()); 854 File dgmPath = new File (dgm.getPath());
855 if (dgmPath.isAbsolute()) {
856 job.setDgm(dgm.getPath());
857 }
858 else {
859 FLYSContext fc = (FLYSContext)context.globalContext();
860 String prefix = (String) fc.get("dgm-path");
861 job.setDgm(prefix.trim() + dgm.getPath().trim());
862 }
705 } 863 }
706 864
707 865
708 protected void setArea(FLYSArtifact artifact, File dir, WSPLGENJob job) { 866 protected void setArea(FLYSArtifact artifact, File dir, WSPLGENJob job) {
709 String useFloodplain = artifact.getDataAsString("use_floodplain"); 867 String useFloodplain = artifact.getDataAsString("use_floodplain");

http://dive4elements.wald.intevation.org