# HG changeset patch # User Ingo Weinzierl # Date 1334323374 0 # Node ID 0a536eb5d668428f20c0c24fc484f16dd4cc346a # Parent 7dffd28271d0293e5f2d56321e0915a653fc7674 Added parser for parsing morphological widths. flys-backend/trunk@4238 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 7dffd28271d0 -r 0a536eb5d668 flys-backend/ChangeLog --- 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 + + * 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 * src/main/java/de/intevation/flys/importer/Config.java: Added a config diff -r 7dffd28271d0 -r 0a536eb5d668 flys-backend/src/main/java/de/intevation/flys/importer/ImportMorphWidth.java --- 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 values; - public ImportMorphWidth(ImportUnit unit) { - this.unit = unit; + public ImportMorphWidth() { this.values = new ArrayList(); } @@ -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); diff -r 7dffd28271d0 -r 0a536eb5d668 flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java --- 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 sedimentDensities; + protected List morphologicalWidths; + protected ImportWst wst; protected ImportUnit wstUnit; @@ -110,14 +115,15 @@ protected River peer; public ImportRiver() { - hyks = new ArrayList(); - crossSections = new ArrayList(); - extraWsts = new ArrayList(); - fixations = new ArrayList(); - officialLines = new ArrayList(); - floodWater = new ArrayList(); - floodProtection = new ArrayList(); - sedimentDensities = new ArrayList(); + hyks = new ArrayList(); + crossSections = new ArrayList(); + extraWsts = new ArrayList(); + fixations = new ArrayList(); + officialLines = new ArrayList(); + floodWater = new ArrayList(); + floodProtection = new ArrayList(); + sedimentDensities = new ArrayList(); + morphologicalWidths = new ArrayList(); } 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(); diff -r 7dffd28271d0 -r 0a536eb5d668 flys-backend/src/main/java/de/intevation/flys/importer/parsers/MorphologicalWidthParser.java --- /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 morphWidths; + + protected ImportMorphWidth current; + + + public MorphologicalWidthParser() { + morphWidths = new ArrayList(); + } + + + @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 getMorphologicalWidths() { + return morphWidths; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :