Mercurial > dive4elements > river
annotate backend/src/main/java/org/dive4elements/river/importer/uinfo/parsers/VegetationParser.java @ 9401:361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
author | mschaefer |
---|---|
date | Tue, 14 Aug 2018 14:02:26 +0200 |
parents | df6f1b5806f6 |
children | 6146358c4842 |
rev | line source |
---|---|
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*\\[(.*)\\].*"), // | |
9401
361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
mschaefer
parents:
9359
diff
changeset
|
48 CLASSNO("Vegetationsklasse.*"), // |
361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
mschaefer
parents:
9359
diff
changeset
|
49 COLOR_R("Rot"), // |
361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
mschaefer
parents:
9359
diff
changeset
|
50 COLOR_G("Gr((.)|(ue))n"), // |
361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
mschaefer
parents:
9359
diff
changeset
|
51 COLOR_B("Blau"); |
9014 | 52 |
53 private final Pattern pattern; | |
54 | |
55 ColTitlePattern(final String regexp) { | |
56 this.pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE); | |
57 } | |
58 | |
59 public Pattern getPattern() { | |
60 return this.pattern; | |
61 } | |
62 } | |
63 | |
64 private final EnumMap<ColTitlePattern, Integer> cols = new EnumMap<>(ColTitlePattern.class); | |
65 | |
66 private int previousClassNo; | |
67 | |
68 private int previousDaysLimit; | |
69 | |
70 | |
71 /***** CONSTRUCTORS *****/ | |
72 | |
73 public VegetationParser(final File importPath, final File rootRelativePath, final ImportRiver river) { | |
74 super(importPath, rootRelativePath, river); | |
75 this.previousClassNo = 0; | |
76 this.previousDaysLimit = -1; | |
77 } | |
78 | |
79 | |
80 /***** METHODS *****/ | |
81 | |
82 @Override | |
83 protected Logger getLog() { | |
84 return log; | |
85 } | |
86 | |
87 /** | |
88 * Whether this import type shall be skipped | |
89 */ | |
90 public static boolean shallSkip() { | |
91 return Config.INSTANCE.skipUInfoVegetation(); | |
92 } | |
93 | |
94 /** | |
95 * Creates a list of parsers for all vegetation import files in a directory | |
96 */ | |
97 public static List<VegetationParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) { | |
98 final List<VegetationParser> parsers = new ArrayList<>(); | |
99 if (importDir.exists()) { | |
100 for (final File file : listFiles(importDir, ".csv")) | |
101 parsers.add(new VegetationParser(file, new File(relativeDir, file.getName()), river)); | |
102 } | |
103 return parsers; | |
104 } | |
105 | |
106 @Override | |
107 protected KmMode kmMode() { | |
108 return KmMode.NONE; | |
109 } | |
110 | |
111 @Override | |
112 protected VegetationSeriesImport createSeriesImport(final String filename) { | |
113 return new VegetationSeriesImport(filename); | |
114 } | |
115 | |
116 @Override | |
117 protected boolean handleMetaOther() { | |
118 if (handleMetaName()) | |
119 return true; | |
120 else | |
121 return false; | |
122 } | |
123 | |
124 private boolean handleMetaName() { | |
125 final Matcher m = META_NAME.matcher(this.currentLine); | |
126 if (m.matches()) { | |
127 this.metaPatternsMatched.add(META_NAME); | |
128 this.seriesHeader.setName(parseMetaInfo(m.group(1).trim())); | |
129 return true; | |
130 } | |
131 return false; | |
132 } | |
133 | |
134 @Override | |
135 protected boolean handleMetaColumnTitles() { | |
136 if (!META_COLUMNTITLES.matcher(this.currentLine).matches()) | |
137 return false; | |
138 this.metaPatternsMatched.add(META_COLUMNTITLES); | |
139 this.columnTitles.clear(); | |
140 final String[] titles = this.currentLine.split(SEPARATOR_CHAR, 0); | |
141 for (int i = 0; i <= titles.length - 1; i++) | |
142 this.columnTitles.add(titles[i].trim()); | |
143 for (final ColTitlePattern col : ColTitlePattern.values()) | |
144 this.cols.put(col, -1); | |
145 for (int i = 1; i <= this.columnTitles.size() - 1; i++) { | |
146 for (final ColTitlePattern col : ColTitlePattern.values()) { | |
147 if (col.getPattern().matcher(this.columnTitles.get(i)).matches()) { | |
148 this.cols.put(col, i); | |
149 break; | |
150 } | |
151 } | |
152 } | |
153 if ((this.cols.get(ColTitlePattern.OVERFLOW_LIMIT) < 0) || (this.cols.get(ColTitlePattern.CLASSNO) < 0)) { | |
154 logError("Column of the overflow duration limit and/or vegetation zone class could not be identified"); | |
155 this.headerParsingState = ParsingState.STOP; | |
156 return true; | |
157 } | |
158 this.previousClassNo = 0; | |
9401
361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
mschaefer
parents:
9359
diff
changeset
|
159 this.previousDaysLimit = -1; |
9014 | 160 return true; |
161 } | |
162 | |
163 @Override | |
164 protected VegetationZoneImport createKmLineImport(final Double km, final String[] values) { | |
9401
361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
mschaefer
parents:
9359
diff
changeset
|
165 int daysLimit = 366; |
9014 | 166 int classNo = 0; |
167 try { | |
168 if (!values[this.cols.get(ColTitlePattern.OVERFLOW_LIMIT)].trim().isEmpty()) | |
169 daysLimit = Integer.parseInt(values[this.cols.get(ColTitlePattern.OVERFLOW_LIMIT)]); | |
170 classNo = Integer.parseInt(values[this.cols.get(ColTitlePattern.CLASSNO)]); | |
171 } | |
172 catch (final Exception e) { | |
173 logError("Overflow days limit and/or vegetation zone class could not be parsed: line " + this.in.getLineNumber()); | |
174 return null; | |
175 } | |
176 // Check completeness of vegetation zone type set, if needed | |
177 // if (classNo != this.previousClassNo + 1) { | |
178 // logError("Wrong vegetation zone class number or wrong class order: line " + this.in.getLineNumber()); | |
179 // return null; | |
180 // } | |
181 // if (!this.types.containsKey(Integer.valueOf(classNo))) { | |
182 // logError("Unknown vegetation zone class: line " + this.in.getLineNumber()); | |
183 // return null; | |
184 // } | |
185 this.previousClassNo = classNo; | |
9401
361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
mschaefer
parents:
9359
diff
changeset
|
186 final int minDays = this.previousDaysLimit + 1; |
361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
mschaefer
parents:
9359
diff
changeset
|
187 this.previousDaysLimit = daysLimit; |
361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
mschaefer
parents:
9359
diff
changeset
|
188 final int red = (this.cols.get(ColTitlePattern.COLOR_R) >= 0) ? Integer.parseInt(values[this.cols.get(ColTitlePattern.COLOR_R)]) : 0; |
361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
mschaefer
parents:
9359
diff
changeset
|
189 final int green = (this.cols.get(ColTitlePattern.COLOR_G) >= 0) ? Integer.parseInt(values[this.cols.get(ColTitlePattern.COLOR_G)]) : 0; |
361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
mschaefer
parents:
9359
diff
changeset
|
190 final int blue = (this.cols.get(ColTitlePattern.COLOR_B) >= 0) ? Integer.parseInt(values[this.cols.get(ColTitlePattern.COLOR_B)]) : 0; |
361de818f76e
Added color fields to the vegetation zone database table and importer, max days field changed from exclusive to inclusive
mschaefer
parents:
9359
diff
changeset
|
191 return new VegetationZoneImport(classNo, minDays, daysLimit, red, green, blue); |
9014 | 192 } |
193 } |