Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/BedHeightSingleParser.java @ 2811:8926571e47fb
Finished importing MINFO bed heights (single and epoch).
flys-backend/trunk@4225 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 13 Apr 2012 07:24:55 +0000 |
parents | f283212966e8 |
children | 0d27d02b1208 |
comparison
equal
deleted
inserted
replaced
2810:04eeb45df27b | 2811:8926571e47fb |
---|---|
1 package de.intevation.flys.importer.parsers; | 1 package de.intevation.flys.importer.parsers; |
2 | 2 |
3 import java.io.File; | |
4 | 3 |
5 import java.math.BigDecimal; | 4 import java.math.BigDecimal; |
6 | 5 |
7 import java.text.NumberFormat; | |
8 import java.text.ParseException; | 6 import java.text.ParseException; |
9 | 7 |
10 import java.util.ArrayList; | |
11 import java.util.List; | |
12 import java.util.Locale; | |
13 | 8 |
14 import java.util.regex.Matcher; | |
15 import java.util.regex.Pattern; | |
16 | 9 |
17 import java.io.IOException; | |
18 import java.io.LineNumberReader; | |
19 import java.io.FileInputStream; | |
20 import java.io.InputStreamReader; | |
21 | 10 |
22 import org.apache.log4j.Logger; | 11 import org.apache.log4j.Logger; |
23 | 12 |
13 import de.intevation.flys.importer.ImportBedHeight; | |
24 import de.intevation.flys.importer.ImportBedHeightSingle; | 14 import de.intevation.flys.importer.ImportBedHeightSingle; |
25 import de.intevation.flys.importer.ImportBedHeightSingleValue; | 15 import de.intevation.flys.importer.ImportBedHeightSingleValue; |
26 import de.intevation.flys.importer.ImportBedHeightType; | |
27 import de.intevation.flys.importer.ImportElevationModel; | |
28 import de.intevation.flys.importer.ImportLocationSystem; | |
29 import de.intevation.flys.importer.ImportRange; | |
30 import de.intevation.flys.importer.ImportUnit; | |
31 import de.intevation.flys.model.BedHeightType; | |
32 | 16 |
33 | 17 |
34 public class BedHeightSingleParser { | 18 public class BedHeightSingleParser extends BedHeightParser { |
35 | |
36 public static final String ENCODING = "ISO-8859-1"; | |
37 | |
38 public static final Locale DEFAULT_LOCALE = Locale.GERMAN; | |
39 | |
40 public static final String START_META_CHAR = "#"; | |
41 public static final String SEPERATOR_CHAR = ";"; | |
42 | |
43 public static final Pattern META_YEAR = | |
44 Pattern.compile("^Jahr: (\\d*).*"); | |
45 | |
46 public static final Pattern META_TYPE = | |
47 Pattern.compile("^Aufnahmeart: (.*).*"); | |
48 | |
49 public static final Pattern META_LOCATION_SYSTEM = | |
50 Pattern.compile("^Lagesystem: (.*).*"); | |
51 | |
52 public static final Pattern META_CUR_ELEVATION_SYSTEM = | |
53 Pattern.compile("^H.hensystem:\\s(\\w++) (.* )??\\[(.*)\\].*"); | |
54 | |
55 public static final Pattern META_OLD_ELEVATION_SYSTEM = | |
56 Pattern.compile("^urspr.ngliches H.hensystem:\\s(\\w++) (.* )??\\[(.*)\\].*"); | |
57 | |
58 public static final Pattern META_SOUNDING_WIDTH = | |
59 Pattern.compile("^ausgewertete Peilbreite: (\\d*).*"); | |
60 | |
61 public static final Pattern META_RANGE = | |
62 Pattern.compile("^Strecke:\\D*(\\d++.\\d*)-(\\d++.\\d*).*"); | |
63 | |
64 public static final Pattern META_EVALUATION_BY = | |
65 Pattern.compile("^Auswerter: (.*).*"); | |
66 | |
67 public static final Pattern META_COMMENTS = | |
68 Pattern.compile("^Weitere Bemerkungen: (.*).*"); | |
69 | 19 |
70 private static final Logger log = | 20 private static final Logger log = |
71 Logger.getLogger(BedHeightSingleParser.class); | 21 Logger.getLogger(BedHeightSingleParser.class); |
72 | 22 |
73 | 23 |
74 protected static NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); | |
75 | 24 |
76 | 25 @Override |
77 protected List<ImportBedHeightSingle> bedHeights; | 26 protected ImportBedHeight newImportBedHeight(String description) { |
78 | 27 return new ImportBedHeightSingle(description); |
79 | |
80 public BedHeightSingleParser() { | |
81 bedHeights = new ArrayList<ImportBedHeightSingle>(); | |
82 } | 28 } |
83 | 29 |
84 | 30 |
85 public List<ImportBedHeightSingle> getBedHeights() { | |
86 return bedHeights; | |
87 } | |
88 | 31 |
89 | 32 @Override |
90 public void parse(File file) throws IOException { | 33 protected void handleDataLine(ImportBedHeight obj, String line) { |
91 log.info("Parsing bed height single file '" + file + "'"); | |
92 | |
93 ImportBedHeightSingle obj = new ImportBedHeightSingle(file.getName()); | |
94 | |
95 LineNumberReader in = null; | |
96 try { | |
97 in = | |
98 new LineNumberReader( | |
99 new InputStreamReader( | |
100 new FileInputStream(file), ENCODING)); | |
101 | |
102 String line = null; | |
103 while ((line = in.readLine()) != null) { | |
104 if ((line = line.trim()).length() == 0) { | |
105 continue; | |
106 } | |
107 | |
108 if (line.startsWith(START_META_CHAR)) { | |
109 handleMetaLine(obj, line); | |
110 } | |
111 else { | |
112 handleDataLine(obj, line); | |
113 } | |
114 } | |
115 | |
116 log.info("File contained " + obj.getValues().size() + " values."); | |
117 bedHeights.add(obj); | |
118 } | |
119 finally { | |
120 if (in != null) { | |
121 in.close(); | |
122 } | |
123 } | |
124 } | |
125 | |
126 | |
127 protected void handleMetaLine(ImportBedHeightSingle obj, String line) { | |
128 String meta = stripMetaLine(line); | |
129 | |
130 if (handleMetaYear(obj, meta)) { | |
131 return; | |
132 } | |
133 else if (handleMetaSoundingWidth(obj, meta)) { | |
134 return; | |
135 } | |
136 else if (handleMetaComment(obj, meta)) { | |
137 return; | |
138 } | |
139 else if (handleMetaEvaluationBy(obj, meta)) { | |
140 return; | |
141 } | |
142 else if (handleMetaRange(obj, meta)) { | |
143 return; | |
144 } | |
145 else if (handleMetaType(obj, meta)) { | |
146 return; | |
147 } | |
148 else if (handleMetaLocationSystem(obj, meta)) { | |
149 return; | |
150 } | |
151 else if (handleMetaCurElevationModel(obj, meta)) { | |
152 return; | |
153 } | |
154 else if (handleMetaOldElevationModel(obj, meta)) { | |
155 return; | |
156 } | |
157 else { | |
158 log.warn("Meta line did not match any known type: " + line); | |
159 } | |
160 } | |
161 | |
162 | |
163 protected boolean handleMetaYear(ImportBedHeightSingle obj, String line) { | |
164 Matcher m = META_YEAR.matcher(line); | |
165 | |
166 if (m.matches()) { | |
167 String tmp = m.group(1); | |
168 | |
169 try { | |
170 obj.setYear(Integer.valueOf(tmp)); | |
171 return true; | |
172 } | |
173 catch (NumberFormatException e) { | |
174 log.warn("Error while parsing year!", e); | |
175 } | |
176 } | |
177 | |
178 return false; | |
179 } | |
180 | |
181 | |
182 protected boolean handleMetaSoundingWidth(ImportBedHeightSingle obj, String line) { | |
183 Matcher m = META_SOUNDING_WIDTH.matcher(line); | |
184 | |
185 if (m.matches()) { | |
186 String tmp = m.group(1); | |
187 | |
188 try { | |
189 obj.setSoundingWidth(Integer.valueOf(tmp)); | |
190 return true; | |
191 } | |
192 catch (NumberFormatException e) { | |
193 log.warn("Error while parsing sounding width!", e); | |
194 } | |
195 } | |
196 | |
197 return false; | |
198 } | |
199 | |
200 | |
201 protected boolean handleMetaComment(ImportBedHeightSingle obj, String line) { | |
202 Matcher m = META_COMMENTS.matcher(line); | |
203 | |
204 if (m.matches()) { | |
205 String tmp = m.group(1); | |
206 | |
207 obj.setDescription(tmp); | |
208 | |
209 return true; | |
210 } | |
211 | |
212 return false; | |
213 } | |
214 | |
215 | |
216 protected boolean handleMetaEvaluationBy( | |
217 ImportBedHeightSingle obj, | |
218 String line | |
219 ) { | |
220 Matcher m = META_EVALUATION_BY.matcher(line); | |
221 | |
222 if (m.matches()) { | |
223 String tmp = m.group(1); | |
224 tmp = tmp.replace(";", ""); | |
225 | |
226 obj.setEvaluationBy(tmp); | |
227 | |
228 return true; | |
229 } | |
230 | |
231 return false; | |
232 } | |
233 | |
234 | |
235 protected boolean handleMetaRange(ImportBedHeightSingle obj, String line) { | |
236 Matcher m = META_RANGE.matcher(line); | |
237 | |
238 if (m.matches() && m.groupCount() >= 2) { | |
239 String a = m.group(1).replace(";", ""); | |
240 String b = m.group(2).replace(";", ""); | |
241 | |
242 try { | |
243 BigDecimal lower = new BigDecimal(nf.parse(a).doubleValue()); | |
244 BigDecimal upper = new BigDecimal(nf.parse(b).doubleValue()); | |
245 | |
246 obj.setRange(new ImportRange(lower, upper)); | |
247 | |
248 return true; | |
249 } | |
250 catch (ParseException e) { | |
251 log.warn("Error while parsing range!", e); | |
252 } | |
253 } | |
254 | |
255 return false; | |
256 } | |
257 | |
258 | |
259 protected boolean handleMetaType(ImportBedHeightSingle obj, String line) { | |
260 Matcher m = META_TYPE.matcher(line); | |
261 | |
262 if (m.matches()) { | |
263 String tmp = m.group(1).replace(";", ""); | |
264 | |
265 obj.setType(new ImportBedHeightType( | |
266 BedHeightType.getBedHeightName(tmp), | |
267 tmp)); | |
268 | |
269 return true; | |
270 } | |
271 | |
272 return false; | |
273 } | |
274 | |
275 | |
276 protected boolean handleMetaLocationSystem( | |
277 ImportBedHeightSingle obj, | |
278 String line | |
279 ) { | |
280 Matcher m = META_LOCATION_SYSTEM.matcher(line); | |
281 | |
282 if (m.matches()) { | |
283 String tmp = m.group(1).replace(";", ""); | |
284 | |
285 obj.setLocationSystem(new ImportLocationSystem(tmp, tmp)); | |
286 | |
287 return true; | |
288 } | |
289 | |
290 return false; | |
291 } | |
292 | |
293 | |
294 protected boolean handleMetaCurElevationModel( | |
295 ImportBedHeightSingle obj, | |
296 String line | |
297 ) { | |
298 Matcher m = META_CUR_ELEVATION_SYSTEM.matcher(line); | |
299 | |
300 if (m.matches()) { | |
301 String name = m.group(1); | |
302 String num = m.group(2); | |
303 String unit = m.group(3); | |
304 | |
305 obj.setCurElevationModel(new ImportElevationModel( | |
306 name + " " + num, | |
307 new ImportUnit(unit) | |
308 )); | |
309 | |
310 return true; | |
311 } | |
312 | |
313 return false; | |
314 } | |
315 | |
316 | |
317 protected boolean handleMetaOldElevationModel( | |
318 ImportBedHeightSingle obj, | |
319 String line | |
320 ) { | |
321 Matcher m = META_OLD_ELEVATION_SYSTEM.matcher(line); | |
322 | |
323 if (m.matches()) { | |
324 String name = m.group(1); | |
325 String num = m.group(2); | |
326 String unit = m.group(3); | |
327 | |
328 obj.setOldElevationModel(new ImportElevationModel( | |
329 name + " " + num, | |
330 new ImportUnit(unit) | |
331 )); | |
332 | |
333 return true; | |
334 } | |
335 | |
336 return false; | |
337 } | |
338 | |
339 | |
340 protected void handleDataLine(ImportBedHeightSingle obj, String line) { | |
341 String[] values = line.split(SEPERATOR_CHAR); | 34 String[] values = line.split(SEPERATOR_CHAR); |
342 | 35 |
343 if (values == null || values.length < 6) { | 36 if (values == null || values.length < 6) { |
344 //log.warn("Error while parsing data line: '" + line + "'"); | 37 //log.warn("Error while parsing data line: '" + line + "'"); |
345 return; | 38 return; |
346 } | 39 } |
347 | 40 |
348 | |
349 try { | 41 try { |
350 ImportBedHeightSingleValue value = new ImportBedHeightSingleValue( | 42 ImportBedHeightSingleValue value = new ImportBedHeightSingleValue( |
351 obj, | 43 (ImportBedHeightSingle) obj, |
352 new BigDecimal(nf.parse(values[0]).doubleValue()), | 44 new BigDecimal(nf.parse(values[0]).doubleValue()), |
353 new BigDecimal(nf.parse(values[1]).doubleValue()), | 45 new BigDecimal(nf.parse(values[1]).doubleValue()), |
354 new BigDecimal(nf.parse(values[2]).doubleValue()), | 46 new BigDecimal(nf.parse(values[2]).doubleValue()), |
355 new BigDecimal(nf.parse(values[3]).doubleValue()), | 47 new BigDecimal(nf.parse(values[3]).doubleValue()), |
356 new BigDecimal(nf.parse(values[4]).doubleValue()), | 48 new BigDecimal(nf.parse(values[4]).doubleValue()), |
361 } | 53 } |
362 catch (ParseException e) { | 54 catch (ParseException e) { |
363 log.warn("Error while parsing data row.", e); | 55 log.warn("Error while parsing data row.", e); |
364 } | 56 } |
365 } | 57 } |
366 | |
367 | |
368 protected static String stripMetaLine(String line) { | |
369 String tmp = line.substring(1, line.length()-1); | |
370 | |
371 if (tmp.startsWith(" ")) { | |
372 return tmp.substring(1, tmp.length()-1); | |
373 } | |
374 else { | |
375 return tmp; | |
376 } | |
377 } | |
378 } | 58 } |
379 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 59 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |