Mercurial > dive4elements > river
view backend/src/main/java/org/dive4elements/river/importer/uinfo/parsers/VegetationParser.java @ 9661:9b8ba3b83a15
Importer (s/u-info) vegetation zones: new database column in vegetation_type table for german type name,
localized vegetation type names by querying the database instead of translating by resource property,
detecting and cancelling the import of a second vegetation zone file for a river,
detecting, logging, cancelling in case of wrong column titles,
detecting, logging and ignoring lines with missing (color) values,
comparing vegetation zone name and class with the database and logging+ignoring in case of inconsistencies,
starting the most elevated zone with 0 instead of -1 overflow days
author | mschaefer |
---|---|
date | Mon, 23 Mar 2020 16:38:12 +0100 |
parents | 6146358c4842 |
children |
line wrap: on
line source
/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde * Software engineering by * Björnsen Beratende Ingenieure GmbH * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt * * This file is Free Software under the GNU AGPL (>=v3) * and comes with ABSOLUTELY NO WARRANTY! Check out the * documentation coming with Dive4Elements River for details. */ package org.dive4elements.river.importer.uinfo.parsers; import java.io.File; import java.util.ArrayList; import java.util.EnumMap; import java.util.List; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; import org.dive4elements.river.importer.Config; import org.dive4elements.river.importer.ImportRiver; import org.dive4elements.river.importer.ImporterSession; import org.dive4elements.river.importer.common.AbstractParser; import org.dive4elements.river.importer.common.ParsingState; import org.dive4elements.river.importer.uinfo.importitem.VegetationSeriesImport; import org.dive4elements.river.importer.uinfo.importitem.VegetationZoneImport; import org.dive4elements.river.model.uinfo.Vegetation; import org.dive4elements.river.model.uinfo.VegetationType; import org.dive4elements.river.model.uinfo.VegetationZone; import org.hibernate.Session; /** * Reads and parses a vegetation zones file * * @author Matthias Schäfer * */ public class VegetationParser extends AbstractParser<Vegetation, VegetationZone, VegetationZoneImport, VegetationSeriesImport> { /***** FIELDS *****/ private static final Logger log = Logger.getLogger(VegetationParser.class); private static final String IMPORT_FILENAME = "Standardvegetationszonen.csv"; private static final Pattern META_FIRST = Pattern.compile("^#\\sVegetationszonen.*", Pattern.CASE_INSENSITIVE); private static final Pattern META_NAME = Pattern.compile("^#\\sEinteilung:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE); private static final Pattern META_COLUMNTITLES = Pattern.compile("^#*\\s*Vegetationstyp\\s*;.+", Pattern.CASE_INSENSITIVE); private enum ColTitlePattern { OVERFLOW_LIMIT("((.)|(Ue))berflutungsdauer-bis\\s*\\[(.*)\\].*"), // CLASSNO("Vegetationsklasse.*"), // COLOR_R("Rot"), // COLOR_G("Gr((.)|(ue))n"), // COLOR_B("Blau"); private final Pattern pattern; ColTitlePattern(final String regexp) { this.pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE); } public Pattern getPattern() { return this.pattern; } } private final EnumMap<ColTitlePattern, Integer> cols = new EnumMap<>(ColTitlePattern.class); private int previousClassNo; private int previousDaysLimit; /***** CONSTRUCTORS *****/ public VegetationParser(final File importPath, final File rootRelativePath, final ImportRiver river) { super(importPath, rootRelativePath, river); this.previousClassNo = 0; this.previousDaysLimit = -1; } /***** METHODS *****/ @Override protected Logger getLog() { return log; } /** * Whether this import type shall be skipped */ public static boolean shallSkip() { return Config.INSTANCE.skipUInfoVegetation(); } /** * Creates a list of parsers for all vegetation import files in a directory */ public static List<VegetationParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) { final List<VegetationParser> parsers = new ArrayList<>(); final File importFile = new File(importDir, IMPORT_FILENAME); if (importFile.exists()) parsers.add(new VegetationParser(importFile, new File(relativeDir, IMPORT_FILENAME), river)); return parsers; } @Override protected KmMode kmMode() { return KmMode.NONE; } @Override protected VegetationSeriesImport createSeriesImport(final String filename) { return new VegetationSeriesImport(filename); } @Override protected boolean handleMetaOther() { if (handleMetaFirst()) return true; else if (handleMetaName()) return true; else return false; } private boolean handleMetaFirst() { final Matcher m = META_FIRST.matcher(this.currentLine); if (m.matches()) { this.metaPatternsMatched.add(META_FIRST); return true; } return false; } private boolean handleMetaName() { final Matcher m = META_NAME.matcher(this.currentLine); if (m.matches()) { this.metaPatternsMatched.add(META_NAME); this.seriesHeader.setName(parseMetaInfo(m.group(1).trim())); return true; } return false; } @Override protected boolean handleMetaColumnTitles() { if (!META_COLUMNTITLES.matcher(this.currentLine).matches()) return false; this.metaPatternsMatched.add(META_COLUMNTITLES); this.columnTitles.clear(); final String[] titles = this.currentLine.split(SEPARATOR_CHAR, 0); for (int i = 0; i <= titles.length - 1; i++) this.columnTitles.add(titles[i].trim()); for (final ColTitlePattern col : ColTitlePattern.values()) this.cols.put(col, -1); for (int i = 1; i <= this.columnTitles.size() - 1; i++) { for (final ColTitlePattern col : ColTitlePattern.values()) { if (col.getPattern().matcher(this.columnTitles.get(i)).matches()) { this.cols.put(col, i); break; } } } this.previousClassNo = 0; this.previousDaysLimit = 0; return true; } @Override protected boolean checkMetaData() { if (super.checkMetaData() == false) return false; for (final ColTitlePattern value : ColTitlePattern.values()) { if (this.cols.get(value) < 0) { logError("No valid header line with the column titles found"); this.headerParsingState = ParsingState.STOP; return false; } } return true; } @Override protected VegetationZoneImport createKmLineImport(final Double km, final String[] values) { int daysLimit = 366; int classNo = 0; try { if (!values[this.cols.get(ColTitlePattern.OVERFLOW_LIMIT)].trim().isEmpty()) daysLimit = Integer.parseInt(values[this.cols.get(ColTitlePattern.OVERFLOW_LIMIT)]); classNo = Integer.parseInt(values[this.cols.get(ColTitlePattern.CLASSNO)]); } catch (final Exception e) { logLineWarning("Invalid overflow days limit and/or vegetation zone class (%s)", e.getMessage()); return null; } // Check completeness of vegetation zone type set, if needed // if (classNo != this.previousClassNo + 1) { // logError("Wrong vegetation zone class number or wrong class order: line " + this.in.getLineNumber()); // return null; // } // if (!this.types.containsKey(Integer.valueOf(classNo))) { // logError("Unknown vegetation zone class: line " + this.in.getLineNumber()); // return null; // } boolean classokay = false; final Session session = ImporterSession.getInstance().getDatabaseSession(); for (final VegetationType vt : VegetationType.getTypes(session)) { if (classNo == vt.getId().intValue()) { final String dbname = vt.getLocalizedName(Locale.GERMAN); if (!values[0].equalsIgnoreCase(dbname)) { logLineWarning("Wrong Vegetationstyp or Vegetationsklasse (%d is '%s' in the database)", classNo, dbname); return null; } classokay = true; break; } } if (!classokay) { logLineWarning("Unknown Vegetationsklasse"); return null; } this.previousClassNo = classNo; final int minDays = this.previousDaysLimit; this.previousDaysLimit = daysLimit; final Integer red = parseIntegerCheckNull(values, this.cols.get(ColTitlePattern.COLOR_R)); final Integer green = parseIntegerCheckNull(values, this.cols.get(ColTitlePattern.COLOR_G)); final Integer blue = parseIntegerCheckNull(values, this.cols.get(ColTitlePattern.COLOR_B)); if ((red == null) || (green == null) || (blue == null)) { logLineWarning("One or more color components invalid or missing"); return null; } if ((red.intValue() < 0) || (red.intValue() > 255)) { logLineWarning("Invalid red value"); return null; } if ((green.intValue() < 0) || (green.intValue() > 255)) { logLineError("Invalid green value"); return null; } if ((blue.intValue() < 0) || (blue.intValue() > 255)) { logLineError("Invalid blue value"); return null; } return new VegetationZoneImport(classNo, minDays, daysLimit, red.intValue(), green.intValue(), blue.intValue()); } }