9014
|
1 /* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde |
|
2 * Software engineering by |
|
3 * Björnsen Beratende Ingenieure GmbH |
|
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt |
|
5 * |
|
6 * This file is Free Software under the GNU AGPL (>=v3) |
|
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
|
8 * documentation coming with Dive4Elements River for details. |
|
9 */ |
|
10 |
|
11 package org.dive4elements.river.importer.uinfo.parsers; |
|
12 |
|
13 import java.io.File; |
|
14 import java.util.ArrayList; |
|
15 import java.util.EnumMap; |
|
16 import java.util.List; |
|
17 import java.util.regex.Matcher; |
|
18 import java.util.regex.Pattern; |
|
19 |
|
20 import org.apache.log4j.Logger; |
|
21 import org.dive4elements.river.importer.Config; |
|
22 import org.dive4elements.river.importer.ImportRiver; |
|
23 import org.dive4elements.river.importer.common.AbstractParser; |
|
24 import org.dive4elements.river.importer.common.ParsingState; |
|
25 import org.dive4elements.river.importer.uinfo.importitem.VegetationSeriesImport; |
|
26 import org.dive4elements.river.importer.uinfo.importitem.VegetationZoneImport; |
|
27 import org.dive4elements.river.model.uinfo.Vegetation; |
|
28 import org.dive4elements.river.model.uinfo.VegetationZone; |
|
29 |
|
30 /** |
|
31 * Reads and parses a vegetation zones file |
|
32 * |
|
33 * @author Matthias Schäfer |
|
34 * |
|
35 */ |
|
36 public class VegetationParser extends AbstractParser<Vegetation, VegetationZone, VegetationZoneImport, VegetationSeriesImport> { |
|
37 |
|
38 /***** FIELDS *****/ |
|
39 |
|
40 private static final Logger log = Logger.getLogger(VegetationParser.class); |
|
41 |
|
42 private static final Pattern META_NAME = Pattern.compile("^#\\sEinteilung:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE); |
|
43 |
|
44 private static final Pattern META_COLUMNTITLES = Pattern.compile("^#*\\s*Vegetationstyp\\s*;.+", Pattern.CASE_INSENSITIVE); |
|
45 |
|
46 private enum ColTitlePattern { |
|
47 OVERFLOW_LIMIT("((.)|(Ue))berflutungsdauer-bis\\s*\\[(.*)\\].*"), // |
|
48 CLASSNO("Vegetationsklasse.*"); |
|
49 |
|
50 private final Pattern pattern; |
|
51 |
|
52 ColTitlePattern(final String regexp) { |
|
53 this.pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE); |
|
54 } |
|
55 |
|
56 public Pattern getPattern() { |
|
57 return this.pattern; |
|
58 } |
|
59 } |
|
60 |
|
61 private final EnumMap<ColTitlePattern, Integer> cols = new EnumMap<>(ColTitlePattern.class); |
|
62 |
|
63 private int previousClassNo; |
|
64 |
|
65 private int previousDaysLimit; |
|
66 |
|
67 |
|
68 /***** CONSTRUCTORS *****/ |
|
69 |
|
70 public VegetationParser(final File importPath, final File rootRelativePath, final ImportRiver river) { |
|
71 super(importPath, rootRelativePath, river); |
|
72 this.previousClassNo = 0; |
|
73 this.previousDaysLimit = -1; |
|
74 } |
|
75 |
|
76 |
|
77 /***** METHODS *****/ |
|
78 |
|
79 @Override |
|
80 protected Logger getLog() { |
|
81 return log; |
|
82 } |
|
83 |
|
84 /** |
|
85 * Whether this import type shall be skipped |
|
86 */ |
|
87 public static boolean shallSkip() { |
|
88 return Config.INSTANCE.skipUInfoVegetation(); |
|
89 } |
|
90 |
|
91 /** |
|
92 * Creates a list of parsers for all vegetation import files in a directory |
|
93 */ |
|
94 public static List<VegetationParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) { |
|
95 final List<VegetationParser> parsers = new ArrayList<>(); |
|
96 if (importDir.exists()) { |
|
97 for (final File file : listFiles(importDir, ".csv")) |
|
98 parsers.add(new VegetationParser(file, new File(relativeDir, file.getName()), river)); |
|
99 } |
|
100 return parsers; |
|
101 } |
|
102 |
|
103 @Override |
|
104 protected KmMode kmMode() { |
|
105 return KmMode.NONE; |
|
106 } |
|
107 |
|
108 @Override |
|
109 protected VegetationSeriesImport createSeriesImport(final String filename) { |
|
110 return new VegetationSeriesImport(filename); |
|
111 } |
|
112 |
|
113 @Override |
|
114 protected boolean handleMetaOther() { |
|
115 if (handleMetaName()) |
|
116 return true; |
|
117 else |
|
118 return false; |
|
119 } |
|
120 |
|
121 private boolean handleMetaName() { |
|
122 final Matcher m = META_NAME.matcher(this.currentLine); |
|
123 if (m.matches()) { |
|
124 this.metaPatternsMatched.add(META_NAME); |
|
125 this.seriesHeader.setName(parseMetaInfo(m.group(1).trim())); |
|
126 return true; |
|
127 } |
|
128 return false; |
|
129 } |
|
130 |
|
131 @Override |
|
132 protected boolean handleMetaColumnTitles() { |
|
133 if (!META_COLUMNTITLES.matcher(this.currentLine).matches()) |
|
134 return false; |
|
135 this.metaPatternsMatched.add(META_COLUMNTITLES); |
|
136 this.columnTitles.clear(); |
|
137 final String[] titles = this.currentLine.split(SEPARATOR_CHAR, 0); |
|
138 for (int i = 0; i <= titles.length - 1; i++) |
|
139 this.columnTitles.add(titles[i].trim()); |
|
140 for (final ColTitlePattern col : ColTitlePattern.values()) |
|
141 this.cols.put(col, -1); |
|
142 for (int i = 1; i <= this.columnTitles.size() - 1; i++) { |
|
143 for (final ColTitlePattern col : ColTitlePattern.values()) { |
|
144 if (col.getPattern().matcher(this.columnTitles.get(i)).matches()) { |
|
145 this.cols.put(col, i); |
|
146 break; |
|
147 } |
|
148 } |
|
149 } |
|
150 if ((this.cols.get(ColTitlePattern.OVERFLOW_LIMIT) < 0) || (this.cols.get(ColTitlePattern.CLASSNO) < 0)) { |
|
151 logError("Column of the overflow duration limit and/or vegetation zone class could not be identified"); |
|
152 this.headerParsingState = ParsingState.STOP; |
|
153 return true; |
|
154 } |
|
155 this.previousClassNo = 0; |
|
156 this.previousDaysLimit = 0; |
|
157 return true; |
|
158 } |
|
159 |
|
160 @Override |
|
161 protected VegetationZoneImport createKmLineImport(final Double km, final String[] values) { |
|
162 int daysLimit = 367; |
|
163 int classNo = 0; |
|
164 try { |
|
165 if (!values[this.cols.get(ColTitlePattern.OVERFLOW_LIMIT)].trim().isEmpty()) |
|
166 daysLimit = Integer.parseInt(values[this.cols.get(ColTitlePattern.OVERFLOW_LIMIT)]); |
|
167 classNo = Integer.parseInt(values[this.cols.get(ColTitlePattern.CLASSNO)]); |
|
168 } |
|
169 catch (final Exception e) { |
|
170 logError("Overflow days limit and/or vegetation zone class could not be parsed: line " + this.in.getLineNumber()); |
|
171 return null; |
|
172 } |
|
173 // Check completeness of vegetation zone type set, if needed |
|
174 // if (classNo != this.previousClassNo + 1) { |
|
175 // logError("Wrong vegetation zone class number or wrong class order: line " + this.in.getLineNumber()); |
|
176 // return null; |
|
177 // } |
|
178 // if (!this.types.containsKey(Integer.valueOf(classNo))) { |
|
179 // logError("Unknown vegetation zone class: line " + this.in.getLineNumber()); |
|
180 // return null; |
|
181 // } |
|
182 this.previousClassNo = classNo; |
|
183 final Integer minDays = Integer.valueOf(this.previousDaysLimit); |
|
184 this.previousDaysLimit = daysLimit + 1; |
|
185 return new VegetationZoneImport(classNo, minDays, Integer.valueOf(this.previousDaysLimit)); |
|
186 } |
|
187 } |