[Dive4elements-commits] [PATCH 2 of 2] WaterlevelFacet: Slight refactoring (more needed), fix flys/issue1149

Wald Commits scm-commit at wald.intevation.org
Wed Mar 27 09:55:17 CET 2013


# HG changeset patch
# User Felix Wolfsteller <felix.wolfsteller at intevation.de>
# Date 1364374261 -3600
# Node ID 3c7e7d47939f935d4abb7b0d35a64fcad79846a7
# Parent  70e0732bd4dd066741f7fe479ad65af2470ea940
WaterlevelFacet: Slight refactoring (more needed), fix flys/issue1149.

diff -r 70e0732bd4dd -r 3c7e7d47939f flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java	Wed Mar 27 09:49:04 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java	Wed Mar 27 09:51:01 2013 +0100
@@ -76,47 +76,73 @@
 
         WQKms [] wqkms = getWQKms(res);
         Object KM = context.getContextValue("currentKm");
+
+        // Interpolation.
         if (KM != null) {
-            logger.debug("interpolate at given km");
-            // TODO handle exact match.
+            linearInterpolate(wqkms[index], (Double) KM);
+        }
+        else {
+            logger.debug("Do not interpolate.");
+        }
 
-            WQKms wqkmsI = wqkms[index];
-            double km = (Double)KM;
+        return wqkms != null ? wqkms[index] : null;
+    }
 
-            // TODO employ DataUtils interface to TDoubleArraList
-            int size = wqkmsI.size();
-            boolean kmIncreasing = wqkmsI.getKm(0) < wqkmsI.getKm(size-1);
-            int mod = kmIncreasing ? +1 : -1;
-            int idx = 0;
-            if (!kmIncreasing) {
-                while (idx < size && wqkmsI.getKm(idx) < km) {
-                    idx++;
+
+    /**
+     * Linear interpolation of WQKms.
+     * TODO rewrite.
+     * @return [w, q, km]
+     */
+    public WQKms linearInterpolate(WQKms wqkms, double km) {
+        logger.debug("interpolate at given km (" + km + ")");
+
+        WQKms resultWQKms = new WQKms();
+        int size = wqkms.size();
+        boolean kmIncreasing = wqkms.getKm(0) < wqkms.getKm(size-1);
+        int mod = kmIncreasing ? +1 : -1;
+        int idx = 0;
+        // Move idx to closest from one direction, check for match.
+        if (!kmIncreasing) {
+            while (idx < size && wqkms.getKm(idx) < km) {
+                if (Math.abs(wqkms.getKm(idx) - km) < 0.01d) {
+                    resultWQKms.add(wqkms.getW(idx), wqkms.getQ(idx), wqkms.getKm(idx));
+                    return resultWQKms;
                 }
+                idx++;
             }
-            else {
-                idx = size-1;
-                while (idx > 0 && wqkmsI.getKm(idx) > km) {
-                    idx--;
+        }
+        else {
+            idx = size-1;
+            while (idx > 0 && wqkms.getKm(idx) > km) {
+                if (Math.abs(wqkms.getKm(idx) - km) < 0.01d) {
+                    resultWQKms.add(wqkms.getW(idx), wqkms.getQ(idx), wqkms.getKm(idx));
+                    return resultWQKms;
                 }
+                idx--;
             }
-
-            WQKms resultWQKms = new WQKms();
-            if ((idx != -1) && (idx < size) && (idx - mod != -1) && (idx - mod < size)) {
-                double inW = Linear.linear(
-                    km,
-                    wqkmsI.getKm(idx), wqkmsI.getKm(idx - mod),
-                    wqkmsI.getW(idx), wqkmsI.getW(idx - mod));
-                double inQ = Linear.linear(
-                    km,
-                    wqkmsI.getKm(idx), wqkmsI.getKm(idx - mod),
-                    wqkmsI.getQ(idx), wqkmsI.getQ(idx - mod));
-                resultWQKms.add(inW, inQ, km);
-            }
-
+        }
+        if (Math.abs(wqkms.getKm(idx) - km) < 0.01d) {
+            resultWQKms.add(wqkms.getW(idx), wqkms.getQ(idx), wqkms.getKm(idx));
             return resultWQKms;
         }
 
-        return wqkms != null ? wqkms[index] : null;
+        if ((idx != -1) && (idx < size) && (idx - mod != -1) && (idx - mod < size)) {
+            double inW = Linear.linear(
+                km,
+                wqkms.getKm(idx), wqkms.getKm(idx - mod),
+                wqkms.getW(idx), wqkms.getW(idx - mod));
+            double inQ = Linear.linear(
+                km,
+                wqkms.getKm(idx), wqkms.getKm(idx - mod),
+                wqkms.getQ(idx), wqkms.getQ(idx - mod));
+            resultWQKms.add(inW, inQ, km);
+        }
+        else {
+            logger.debug("waterlevelfacet stuff " + idx + " size " + size + " mod: " + mod);
+        }
+
+        return resultWQKms;
     }
 
 


More information about the Dive4elements-commits mailing list