Mercurial > dive4elements > river
diff flys-backend/src/main/java/de/intevation/flys/importer/parsers/BedHeightParser.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 | |
children | 0d27d02b1208 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/BedHeightParser.java Fri Apr 13 07:24:55 2012 +0000 @@ -0,0 +1,405 @@ +package de.intevation.flys.importer.parsers; + +import java.io.File; + +import java.math.BigDecimal; + +import java.text.NumberFormat; +import java.text.ParseException; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Locale; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.FileInputStream; +import java.io.InputStreamReader; + +import org.apache.log4j.Logger; + +import de.intevation.flys.importer.ImportBedHeight; +import de.intevation.flys.importer.ImportBedHeightType; +import de.intevation.flys.importer.ImportElevationModel; +import de.intevation.flys.importer.ImportLocationSystem; +import de.intevation.flys.importer.ImportRange; +import de.intevation.flys.importer.ImportTimeInterval; +import de.intevation.flys.importer.ImportUnit; +import de.intevation.flys.model.BedHeightType; + + +public abstract class BedHeightParser { + + private static final Logger log = + Logger.getLogger(BedHeightParser.class); + + public static final String ENCODING = "ISO-8859-1"; + + public static final Locale DEFAULT_LOCALE = Locale.GERMAN; + + public static final String START_META_CHAR = "#"; + public static final String SEPERATOR_CHAR = ";"; + + public static final Pattern META_YEAR = + Pattern.compile("^Jahr: (\\d*).*"); + + public static final Pattern META_TIMEINTERVAL = + Pattern.compile("^Zeitraum: Epoche (\\d*)-(\\d*).*"); + + public static final Pattern META_TYPE = + Pattern.compile("^Aufnahmeart: (.*).*"); + + public static final Pattern META_LOCATION_SYSTEM = + Pattern.compile("^Lagesystem: (.*).*"); + + public static final Pattern META_CUR_ELEVATION_SYSTEM = + Pattern.compile("^H.hensystem:\\s(\\w++) (.* )??\\[(.*)\\].*"); + + public static final Pattern META_OLD_ELEVATION_SYSTEM = + Pattern.compile("^urspr.ngliches H.hensystem:\\s(\\w++) (.* )??\\[(.*)\\].*"); + + public static final Pattern META_SOUNDING_WIDTH = + Pattern.compile("^ausgewertete Peilbreite: (\\d*).*"); + + public static final Pattern META_RANGE = + Pattern.compile("^Strecke:\\D*(\\d++.\\d*)-(\\d++.\\d*).*"); + + public static final Pattern META_EVALUATION_BY = + Pattern.compile("^Auswerter: (.*).*"); + + public static final Pattern META_COMMENTS = + Pattern.compile("^Weitere Bemerkungen: (.*).*"); + + + protected static NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); + + + protected List<ImportBedHeight> bedHeights; + + + protected abstract ImportBedHeight newImportBedHeight(String description); + + protected abstract void handleDataLine( + ImportBedHeight importBedHeight, + String line + ); + + + + public BedHeightParser() { + this.bedHeights = new ArrayList<ImportBedHeight>(); + } + + + public List<ImportBedHeight> getBedHeights() { + return bedHeights; + } + + + public void parse(File file) throws IOException { + log.info("Parsing bed height single file '" + file + "'"); + + ImportBedHeight obj = newImportBedHeight(file.getName()); + + LineNumberReader in = null; + try { + in = + new LineNumberReader( + new InputStreamReader( + new FileInputStream(file), ENCODING)); + + String line = null; + while ((line = in.readLine()) != null) { + if ((line = line.trim()).length() == 0) { + continue; + } + + if (line.startsWith(START_META_CHAR)) { + handleMetaLine(obj, line); + } + else { + handleDataLine(obj, line); + } + } + + log.info("File contained " + obj.getValueCount() + " values."); + bedHeights.add(obj); + } + finally { + if (in != null) { + in.close(); + } + } + } + + + protected static String stripMetaLine(String line) { + String tmp = line.substring(1, line.length()); + + if (tmp.startsWith(" ")) { + return tmp.substring(1, tmp.length()); + } + else { + return tmp; + } + } + + + public static Date getDateFromYear(int year) { + Calendar cal = Calendar.getInstance(); + cal.set(year, 0, 1); + + return cal.getTime(); + } + + + protected void handleMetaLine(ImportBedHeight obj, String line) { + String meta = stripMetaLine(line); + + if (handleMetaYear(obj, meta)) { + return; + } + else if (handleMetaTimeInterval(obj, meta)) { + return; + } + else if (handleMetaSoundingWidth(obj, meta)) { + return; + } + else if (handleMetaComment(obj, meta)) { + return; + } + else if (handleMetaEvaluationBy(obj, meta)) { + return; + } + else if (handleMetaRange(obj, meta)) { + return; + } + else if (handleMetaType(obj, meta)) { + return; + } + else if (handleMetaLocationSystem(obj, meta)) { + return; + } + else if (handleMetaCurElevationModel(obj, meta)) { + return; + } + else if (handleMetaOldElevationModel(obj, meta)) { + return; + } + else { + log.warn("Meta line did not match any known type: " + line); + } + } + + + protected boolean handleMetaYear(ImportBedHeight obj, String line) { + Matcher m = META_YEAR.matcher(line); + + if (m.matches()) { + String tmp = m.group(1); + + try { + obj.setYear(Integer.valueOf(tmp)); + return true; + } + catch (NumberFormatException e) { + log.warn("Error while parsing year!", e); + } + } + + return false; + } + + + protected boolean handleMetaTimeInterval(ImportBedHeight obj, String line) { + Matcher m = META_TIMEINTERVAL.matcher(line); + + if (m.matches()) { + String lo = m.group(1); + String up = m.group(2); + + log.debug("Found time interval: " + lo + " - " + up); + + try { + int lower = Integer.valueOf(lo); + int upper = Integer.valueOf(up); + + Date fromYear = getDateFromYear(lower); + Date toYear = getDateFromYear(upper); + + obj.setTimeInterval(new ImportTimeInterval(fromYear, toYear)); + } + catch (NumberFormatException e) { + log.warn("Error while parsing timeinterval!", e); + } + + return true; + } + + return false; + } + + + protected boolean handleMetaSoundingWidth(ImportBedHeight obj, String line) { + Matcher m = META_SOUNDING_WIDTH.matcher(line); + + if (m.matches()) { + String tmp = m.group(1); + + try { + obj.setSoundingWidth(Integer.valueOf(tmp)); + return true; + } + catch (NumberFormatException e) { + log.warn("Error while parsing sounding width!", e); + } + } + + return false; + } + + + protected boolean handleMetaComment(ImportBedHeight obj, String line) { + Matcher m = META_COMMENTS.matcher(line); + + if (m.matches()) { + String tmp = m.group(1); + + obj.setDescription(tmp); + + return true; + } + + return false; + } + + + protected boolean handleMetaEvaluationBy( + ImportBedHeight obj, + String line + ) { + Matcher m = META_EVALUATION_BY.matcher(line); + + if (m.matches()) { + String tmp = m.group(1); + tmp = tmp.replace(";", ""); + + obj.setEvaluationBy(tmp); + + return true; + } + + return false; + } + + + protected boolean handleMetaRange(ImportBedHeight obj, String line) { + Matcher m = META_RANGE.matcher(line); + + if (m.matches() && m.groupCount() >= 2) { + String a = m.group(1).replace(";", ""); + String b = m.group(2).replace(";", ""); + + try { + BigDecimal lower = new BigDecimal(nf.parse(a).doubleValue()); + BigDecimal upper = new BigDecimal(nf.parse(b).doubleValue()); + + obj.setRange(new ImportRange(lower, upper)); + + return true; + } + catch (ParseException e) { + log.warn("Error while parsing range!", e); + } + } + + return false; + } + + + protected boolean handleMetaType(ImportBedHeight obj, String line) { + Matcher m = META_TYPE.matcher(line); + + if (m.matches()) { + String tmp = m.group(1).replace(";", ""); + + obj.setType(new ImportBedHeightType( + BedHeightType.getBedHeightName(tmp), + tmp)); + + return true; + } + + return false; + } + + + protected boolean handleMetaLocationSystem( + ImportBedHeight obj, + String line + ) { + Matcher m = META_LOCATION_SYSTEM.matcher(line); + + if (m.matches()) { + String tmp = m.group(1).replace(";", ""); + + obj.setLocationSystem(new ImportLocationSystem(tmp, tmp)); + + return true; + } + + return false; + } + + + protected boolean handleMetaCurElevationModel( + ImportBedHeight obj, + String line + ) { + Matcher m = META_CUR_ELEVATION_SYSTEM.matcher(line); + + if (m.matches()) { + String name = m.group(1); + String num = m.group(2); + String unit = m.group(3); + + obj.setCurElevationModel(new ImportElevationModel( + name + " " + num, + new ImportUnit(unit) + )); + + return true; + } + + return false; + } + + + protected boolean handleMetaOldElevationModel( + ImportBedHeight obj, + String line + ) { + Matcher m = META_OLD_ELEVATION_SYSTEM.matcher(line); + + if (m.matches()) { + String name = m.group(1); + String num = m.group(2); + String unit = m.group(3); + + obj.setOldElevationModel(new ImportElevationModel( + name + " " + num, + new ImportUnit(unit) + )); + + return true; + } + + return false; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :