changeset 1198:661a9304f2f5

PRFParser: Extracted the data. All BfG PRFs are parsed correctly, now. flys-backend/trunk@2300 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 07 Jul 2011 14:09:54 +0000
parents ce3dacc6ea92
children cc8f770796cb
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java
diffstat 2 files changed, 119 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Thu Jul 07 09:29:31 2011 +0000
+++ b/flys-backend/ChangeLog	Thu Jul 07 14:09:54 2011 +0000
@@ -1,3 +1,8 @@
+2011-07-07	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/PRFParser.java:
+	  Extracted the data. All BfG PRFs are parsed correctly, now.
+
 2011-07-07	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/importer/PRFParser.java:
--- a/flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java	Thu Jul 07 09:29:31 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java	Thu Jul 07 14:09:54 2011 +0000
@@ -3,6 +3,9 @@
 import java.util.Map;
 import java.util.Stack;
 import java.util.TreeMap;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
 
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
@@ -30,6 +33,34 @@
     public static final Pattern KM_PATTERN =
         Pattern.compile("\\((\\d+)x\\s*,\\s*f(\\d+)\\.(\\d+)\\s*\\)?");
 
+    public static final double X_EPSILON = 1e-4;
+
+    public static final class XY
+    implements Comparable<XY>
+    {
+        protected double x;
+        protected double y;
+        protected int    index;
+
+        public XY() {
+        }
+
+        public XY(double x, double y, int index) {
+            this.x     = x;
+            this.y     = y;
+            this.index = index;
+        }
+
+        @Override
+        public int compareTo(XY other) {
+            if (x + X_EPSILON < other.x) return -1;
+            if (x > other.x + X_EPSILON) return +1;
+            if (index < other.index)     return -1;
+            if (index > other.index)     return +1;
+            return 0;
+        }
+    } // class XY
+
     public static class DataFormat {
 
         protected int deleteChars;
@@ -39,6 +70,9 @@
         protected int secondIntegerPlaces;
         protected int secondFractionPlaces;
 
+        protected double firstShift;
+        protected double secondShift;
+
         public DataFormat() {
         }
 
@@ -49,22 +83,75 @@
             firstFractionPlaces  = Integer.parseInt(m.group(4));
             secondIntegerPlaces  = Integer.parseInt(m.group(5));
             secondFractionPlaces = Integer.parseInt(m.group(6));
+
+            firstShift  = Math.pow(10, firstFractionPlaces);
+            secondShift = Math.pow(10, secondFractionPlaces);
         }
 
-        public boolean extractData(String line, Map<Double, Double> dest) 
-        throws NumberFormatException
-        {
-            //TODO: Implement me!
-            return true;
+        public int extractData(String line, List<XY> kmData) {
+            int L = line.length();
+            if (L <= deleteChars) {
+                return -1;
+            }
+
+            int pos = deleteChars;
+
+            boolean debug = log.isDebugEnabled();
+
+
+            int rep = 0;
+            for (;rep < maxRepetitions; ++rep) {
+                if (pos >= L || pos + firstIntegerPlaces >= L) {
+                    break;
+                }
+                String first = line.substring(
+                    pos, pos + firstIntegerPlaces);
+
+                String second = line.substring(
+                    pos + firstIntegerPlaces, 
+                    Math.min(L, pos+firstIntegerPlaces+secondIntegerPlaces));
+
+                double x, y;
+                try {
+                    x = Double.parseDouble(first);
+                    y = Double.parseDouble(second);
+                }
+                catch (NumberFormatException nfe) {
+                    // broken line -> substract from dataset skip
+                    return -1;
+                }
+
+                if (first.indexOf('.') < 0) {
+                    x /= firstShift;
+                }
+
+                if (firstFractionPlaces > 0) {
+                    x = (int)(x*firstShift)/firstShift;
+                }
+
+                if (second.indexOf('.') < 0) {
+                    y /= secondShift;
+                }
+
+                if (secondFractionPlaces > 0) {
+                    y = (int)(y*secondShift)/secondShift;
+                }
+
+                kmData.add(new XY(x, y, kmData.size()));
+
+                pos += firstIntegerPlaces + secondIntegerPlaces;
+            }
+
+            return rep == maxRepetitions ? 1 : 0;
         }
     } // class DataFormat
 
     public static class KMFormat {
+
         protected int deleteChars;
         protected int integerPlaces;
         protected int fractionPlaces;
 
-        protected double scale;
         protected double shift;
 
         public KMFormat() {
@@ -76,7 +163,6 @@
             fractionPlaces = Integer.parseInt(m.group(3));
 
             shift = Math.pow(10, fractionPlaces);
-            scale = 1d/shift;
         }
 
         public double extractKm(String line) throws NumberFormatException {
@@ -90,18 +176,27 @@
 
             double km = Double.parseDouble(kmS.trim());
 
+            if (kmS.indexOf('.') < 0) {
+                km /= shift;
+            }
+
             return fractionPlaces > 0
-                ? ((int)((scale*km)*shift))/shift
+                ? ((int)(km*shift))/shift
                 : km;
         }
     } // class KMFormat
 
-    protected Map<Double, Map<Double, Double>> data;
+    protected Map<Double, List<XY>> data;
 
     public PRFParser() {
-        data = new TreeMap<Double, Map<Double, Double>>();
+        data = new TreeMap<Double, List<XY>>();
     }
 
+    protected void sortLists() {
+        for (List<XY> xy: data.values()) {
+            Collections.sort(xy);
+        }
+    }
 
     public boolean parse(File file) {
 
@@ -206,24 +301,22 @@
 
                 Double station = Double.valueOf(km);
 
-                Map<Double, Double> kmData = data.get(station);
+                List<XY> kmData = data.get(station);
 
                 if (kmData == null) {
-                    log.debug("found new km: " + station);
-                    kmData = new TreeMap<Double, Double>();
+                    //log.debug("found new km: " + station);
+                    kmData = new ArrayList<XY>();
                     data.put(station, kmData);
                 }
 
-                try {
-                    if (!dataFormat.extractData(line, kmData)) {
-                        skip = lineSkipCount;
-                    }
-                }
-                catch (NumberFormatException nfe) {
-                    log.warn("cannot extract data from line " + in.getLineNumber());
-                    return false;
+                int c = dataFormat.extractData(line, kmData);
+                if (c < 1) {
+                    skip = lineSkipCount + c;
                 }
             }
+
+            // sort all the lists by x and index
+            sortLists();
         }
         catch (IOException ioe) {
             log.error(ioe);

http://dive4elements.wald.intevation.org