changeset 2821:0a536eb5d668

Added parser for parsing morphological widths. flys-backend/trunk@4238 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 13 Apr 2012 13:22:54 +0000
parents 7dffd28271d0
children 212c33c9a5e2
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/ImportMorphWidth.java flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java flys-backend/src/main/java/de/intevation/flys/importer/parsers/MorphologicalWidthParser.java
diffstat 4 files changed, 212 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Fri Apr 13 13:05:14 2012 +0000
+++ b/flys-backend/ChangeLog	Fri Apr 13 13:22:54 2012 +0000
@@ -1,3 +1,14 @@
+2012-04-13  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/parsers/MorphologicalWidthParser.java:
+	  New parser for morphological widths files.
+
+	* src/main/java/de/intevation/flys/importer/ImportMorphWidth.java: Throw
+	  constraint violation exceptions.
+
+	* src/main/java/de/intevation/flys/importer/ImportRiver.java: Parse and
+	  store morphological widths.
+
 2012-04-13  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/importer/Config.java: Added a config
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportMorphWidth.java	Fri Apr 13 13:05:14 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportMorphWidth.java	Fri Apr 13 13:22:54 2012 +0000
@@ -1,5 +1,7 @@
 package de.intevation.flys.importer;
 
+import java.sql.SQLException;
+
 import java.util.ArrayList;
 import java.util.List;
 
@@ -7,6 +9,7 @@
 
 import org.hibernate.Session;
 import org.hibernate.Query;
+import org.hibernate.exception.ConstraintViolationException;
 
 import de.intevation.flys.model.MorphologicalWidth;
 import de.intevation.flys.model.River;
@@ -24,8 +27,7 @@
     protected List<ImportMorphWidthValue> values;
 
 
-    public ImportMorphWidth(ImportUnit unit) {
-        this.unit   = unit;
+    public ImportMorphWidth() {
         this.values = new ArrayList<ImportMorphWidthValue>();
     }
 
@@ -35,7 +37,14 @@
     }
 
 
-    public void storeDependencies(River river) {
+    public void setUnit(ImportUnit unit) {
+        this.unit = unit;
+    }
+
+
+    public void storeDependencies(River river)
+    throws SQLException, ConstraintViolationException
+    {
         log.info("store dependencies");
 
         MorphologicalWidth peer = getPeer(river);
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Fri Apr 13 13:05:14 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Fri Apr 13 13:22:54 2012 +0000
@@ -27,6 +27,7 @@
 import de.intevation.flys.importer.parsers.BedHeightSingleParser;
 import de.intevation.flys.importer.parsers.PRFParser;
 import de.intevation.flys.importer.parsers.HYKParser;
+import de.intevation.flys.importer.parsers.MorphologicalWidthParser;
 import de.intevation.flys.importer.parsers.AnnotationsParser;
 import de.intevation.flys.importer.parsers.AnnotationClassifier;
 import de.intevation.flys.importer.parsers.PegelGltParser;
@@ -70,6 +71,8 @@
 
     public static final String SEDIMENT_DENSITY_DIR = "Sedimentdichte";
 
+    public static final String MORPHOLOGICAL_WIDTH_DIR = "morphologische_Breite";
+
 
     protected String name;
 
@@ -101,6 +104,8 @@
 
     protected List<ImportSedimentDensity> sedimentDensities;
 
+    protected List<ImportMorphWidth> morphologicalWidths;
+
     protected ImportWst wst;
 
     protected ImportUnit wstUnit;
@@ -110,14 +115,15 @@
     protected River peer;
 
     public ImportRiver() {
-        hyks              = new ArrayList<ImportHYK>();
-        crossSections     = new ArrayList<ImportCrossSection>();
-        extraWsts         = new ArrayList<ImportWst>();
-        fixations         = new ArrayList<ImportWst>();
-        officialLines     = new ArrayList<ImportWst>();
-        floodWater        = new ArrayList<ImportWst>();
-        floodProtection   = new ArrayList<ImportWst>();
-        sedimentDensities = new ArrayList<ImportSedimentDensity>();
+        hyks                = new ArrayList<ImportHYK>();
+        crossSections       = new ArrayList<ImportCrossSection>();
+        extraWsts           = new ArrayList<ImportWst>();
+        fixations           = new ArrayList<ImportWst>();
+        officialLines       = new ArrayList<ImportWst>();
+        floodWater          = new ArrayList<ImportWst>();
+        floodProtection     = new ArrayList<ImportWst>();
+        sedimentDensities   = new ArrayList<ImportSedimentDensity>();
+        morphologicalWidths = new ArrayList<ImportMorphWidth>();
     }
 
     public ImportRiver(
@@ -183,6 +189,7 @@
         parseFloodProtection();
         parseBedHeight();
         parseSedimentDensity();
+        parseMorphologicalWidth();
     }
 
     public void parseFloodProtection() throws IOException {
@@ -284,6 +291,36 @@
     }
 
 
+    protected void parseMorphologicalWidth() throws IOException {
+        log.debug("Parse morphological width");
+
+        if (Config.INSTANCE.skipMorphologicalWidth()) {
+            log.info("skip parsing morphological width.");
+            return;
+        }
+
+        File minfoDir = getMinfoDir();
+        File morphDir = new File(minfoDir, MORPHOLOGICAL_WIDTH_DIR);
+
+        File[] files = morphDir.listFiles();
+
+        if (files == null) {
+            log.warn("Cannot parse directory '" + morphDir + "'");
+            return;
+        }
+
+        MorphologicalWidthParser parser = new MorphologicalWidthParser();
+
+        for (File file: files) {
+            parser.parse(file);
+        }
+
+        morphologicalWidths = parser.getMorphologicalWidths();
+
+        log.info("Parsed " + morphologicalWidths.size() + " morph. widths files.");
+    }
+
+
     protected void parseBedHeightSingles(File dir) throws IOException {
         log.debug("Parse bed height singles");
 
@@ -650,6 +687,7 @@
         storeFloodProtection();
         storeBedHeight();
         storeSedimentDensity();
+        storeMorphologicalWidth();
     }
 
     public void storeWstUnit() {
@@ -836,6 +874,26 @@
         }
     }
 
+    public void storeMorphologicalWidth() {
+        if (!Config.INSTANCE.skipMorphologicalWidth()) {
+            log.info("store morphological width");
+
+            River river = getPeer();
+
+            for (ImportMorphWidth width: morphologicalWidths) {
+                try {
+                    width.storeDependencies(river);
+                }
+                catch (SQLException sqle) {
+                    log.error("Error while parsing file for morph. width.");
+                }
+                catch (ConstraintViolationException cve) {
+                    log.error("Error while parsing file for morph. width.");
+                }
+            }
+        }
+    }
+
     public void storeAnnotations() {
         if (!Config.INSTANCE.skipAnnotations()) {
             River river = getPeer();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/MorphologicalWidthParser.java	Fri Apr 13 13:22:54 2012 +0000
@@ -0,0 +1,123 @@
+package de.intevation.flys.importer.parsers;
+
+import java.math.BigDecimal;
+
+import java.text.NumberFormat;
+import java.text.ParseException;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.importer.ImportMorphWidth;
+import de.intevation.flys.importer.ImportMorphWidthValue;
+import de.intevation.flys.importer.ImportUnit;
+
+
+public class MorphologicalWidthParser extends LineParser {
+
+    private static final Logger log =
+        Logger.getLogger(MorphologicalWidthParser.class);
+
+
+    public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE);
+
+
+    public static final Pattern META_UNIT =
+        Pattern.compile("^Einheit: \\[(.*)\\].*");
+
+
+    protected List<ImportMorphWidth> morphWidths;
+
+    protected ImportMorphWidth current;
+
+
+    public MorphologicalWidthParser() {
+        morphWidths = new ArrayList<ImportMorphWidth>();
+    }
+
+
+    @Override
+    protected void reset() {
+        current = new ImportMorphWidth();
+    }
+
+
+    @Override
+    protected void finish() {
+        if (current != null) {
+            morphWidths.add(current);
+        }
+    }
+
+
+    @Override
+    protected void handleLine(String line) {
+        if (line.startsWith(START_META_CHAR)) {
+            handleMetaLine(stripMetaLine(line));
+        }
+        else {
+            handleDataLine(line);
+        }
+    }
+
+
+    protected void handleMetaLine(String line) {
+        if (handleMetaUnit(line)) {
+            return;
+        }
+        else {
+            log.warn("Unknown meta line: '" + line + "'");
+        }
+    }
+
+
+    protected boolean handleMetaUnit(String line) {
+        Matcher m = META_UNIT.matcher(line);
+
+        if (m.matches()) {
+            String unit = m.group(1);
+
+            current.setUnit(new ImportUnit(unit));
+
+            return true;
+        }
+
+        return false;
+    }
+
+
+    protected void handleDataLine(String line) {
+        String[] vals = line.split(SEPERATOR_CHAR);
+
+        if (vals == null || vals.length < 2) {
+            log.warn("skip invalid data line: '" + line + "'");
+            return;
+        }
+
+        try {
+            BigDecimal km    = new BigDecimal(nf.parse(vals[0]).doubleValue());
+            BigDecimal width = new BigDecimal(nf.parse(vals[1]).doubleValue());
+
+            String desc = vals.length > 2 ? vals[2] : null;
+
+            current.addValue(new ImportMorphWidthValue(
+                km,
+                width,
+                desc
+            ));
+        }
+        catch (ParseException pe) {
+            log.warn("Error while parsing numbers in '" + line + "'");
+        }
+    }
+
+
+    public List<ImportMorphWidth> getMorphologicalWidths() {
+        return morphWidths;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org