changeset 184:4ab2c3bd474c

Added parsing of PEGEL.GLT files. flys-backend/trunk@1501 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 17 Mar 2011 15:21:50 +0000
parents 222f4db3430a
children a60edcfe5f53
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/ImportGauge.java flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.java flys-backend/src/main/java/de/intevation/flys/importer/PegelGltParser.java flys-backend/src/main/java/de/intevation/flys/utils/DBCPConnectionProvider.java
diffstat 6 files changed, 202 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Thu Mar 17 10:47:31 2011 +0000
+++ b/flys-backend/ChangeLog	Thu Mar 17 15:21:50 2011 +0000
@@ -1,3 +1,20 @@
+2011-03-17	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/PegelGltParser.java:
+	  New. Parser for PEGEL.GLT files.
+
+	* src/main/java/de/intevation/flys/importer/ImportGauge.java:
+	  New. Import model for gauges.
+
+	* src/main/java/de/intevation/flys/utils/DBCPConnectionProvider.java:
+	  Removed needless imports.
+
+	* src/main/java/de/intevation/flys/importer/ImportRiver.java:
+	  Added method to parse the gauges.
+
+	* src/main/java/de/intevation/flys/importer/InfoGewParser.java:
+	  Trigger pegel glt file parsing.
+
 2011-03-17	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/importer/Importer.java:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportGauge.java	Thu Mar 17 15:21:50 2011 +0000
@@ -0,0 +1,55 @@
+package de.intevation.flys.importer;
+
+import java.io.File;
+
+public class ImportGauge
+{
+    protected double from;
+    protected double to;
+
+    protected File   staFile;
+    protected File   atFile;
+
+    public ImportGauge() {
+    }
+
+    public ImportGauge(double from, double to, File staFile, File atFile) {
+        this.from    = from;
+        this.to      = to;
+        this.staFile = staFile;
+        this.atFile  = atFile;
+    }
+
+    public void setFrom(double from) {
+        this.from = from;
+    }
+
+    public double getFrom() {
+        return from;
+    }
+
+    public void setTo(double to) {
+        this.to = to;
+    }
+
+    public double getTo() {
+        return to;
+    }
+
+    public void setStaFile(File staFile) {
+        this.staFile = staFile;
+    }
+
+    public File getStaFile() {
+        return staFile;
+    }
+
+    public void setAtFile(File atFile) {
+        this.atFile = atFile;
+    }
+
+    public File getAtFile() {
+        return atFile;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Thu Mar 17 10:47:31 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Thu Mar 17 15:21:50 2011 +0000
@@ -1,15 +1,28 @@
 package de.intevation.flys.importer;
 
+import java.util.List;
+
 import java.io.File;
+import java.io.IOException;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.utils.FileTools;
 
 public class ImportRiver
 {
+    private static Logger log = Logger.getLogger(ImportRiver.class);
+
+    public static final String PEGEL_GLT = "PEGEL.GLT";
+
     protected String name;
 
     protected File   wstFile;
 
     protected File   bbInfoFile;
 
+    protected List<ImportGauge> gauges;
+
     public ImportRiver() {
     }
 
@@ -42,5 +55,20 @@
     public void setBBInfo(File bbInfoFile) {
         this.bbInfoFile = bbInfoFile;
     }
+
+    public void parseGauges() throws IOException {
+        File gltFile = new File(wstFile.getParentFile(), PEGEL_GLT);
+        gltFile = FileTools.repair(gltFile);
+
+        if (!gltFile.isFile() || !gltFile.canRead()) {
+            log.warn("cannot read gauges from '" + gltFile + "'");
+            return;
+        }
+
+        PegelGltParser pgltp = new PegelGltParser();
+        pgltp.parse(gltFile);
+
+        gauges = pgltp.getGauges();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.java	Thu Mar 17 10:47:31 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.java	Thu Mar 17 15:21:50 2011 +0000
@@ -110,6 +110,10 @@
                 in.close();
             }
         }
+
+        for (ImportRiver river: rivers) {
+            river.parseGauges();
+        }
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/PegelGltParser.java	Thu Mar 17 15:21:50 2011 +0000
@@ -0,0 +1,98 @@
+package de.intevation.flys.importer;
+
+import java.io.File;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+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.utils.FileTools;
+
+public class PegelGltParser
+{
+    private static Logger log = Logger.getLogger(InfoGewParser.class);
+
+    public static final String ENCODING = "ISO-8859-1";
+
+    public static final String KM = "km:";
+
+    protected List<ImportGauge> gauges;
+
+    public PegelGltParser() {
+        gauges = new ArrayList<ImportGauge>();
+    }
+
+    public List<ImportGauge> getGauges() {
+        return gauges;
+    }
+
+    public void parse(File file) throws IOException {
+
+        File parent = file.getParentFile();
+
+        log.info("parsing GLT file '" + file + "'");
+        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;
+                }
+
+                int kmPos = line.indexOf(KM);
+                if (kmPos < 0) {
+                    log.warn("no gauge found in line " + in.getLineNumber());
+                    continue;
+                }
+
+                String gaugeName = line.substring(0, kmPos).trim();
+                log.info("Found gauge '" + gaugeName + "'");
+
+                line = line.substring(kmPos + KM.length()).trim();
+
+                String [] parts = line.split("\\s+");
+                if (parts.length < 4) {
+                    log.warn("line " + in.getLineNumber() 
+                        + " has not enough columns");
+                    continue;
+                }
+
+                double from = Double.parseDouble(parts[0].replace(",", "."));
+                double to   = Double.parseDouble(parts[1].replace(",", "."));
+                if (to < from) { double t = from; from = to; to = t; }
+                File staFile = FileTools.repair(new File(parent, parts[2]));
+                File atFile  = FileTools.repair(new File(parent, parts[3]));
+
+                if (log.isDebugEnabled()) {
+                    log.debug("\tfrom: " + from);
+                    log.debug("\tto: " + to);
+                    log.debug("\tsta: " + staFile);
+                    log.debug("\tat: " + atFile);
+                }
+
+                gauges.add(new ImportGauge(from, to, staFile, atFile));
+            }
+        }
+        finally {
+            if (in != null) {
+                in.close();
+            }
+        }
+    }
+
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/utils/DBCPConnectionProvider.java	Thu Mar 17 10:47:31 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/utils/DBCPConnectionProvider.java	Thu Mar 17 15:21:50 2011 +0000
@@ -15,9 +15,6 @@
  */
 package org.hibernate.connection;
 
-import java.io.PrintWriter;
-import java.io.StringWriter;
-
 import java.sql.Connection;
 import java.sql.SQLException;
 

http://dive4elements.wald.intevation.org