changeset 769:321b9e480f72

Added a method to river to find the gauge with the max overlap with a given interval. flys-backend/trunk@2235 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 26 Jun 2011 16:13:08 +0000
parents 87ac543c7ab9
children 33a79496ba9e
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/model/River.java
diffstat 2 files changed, 40 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Sun Jun 26 12:16:36 2011 +0000
+++ b/flys-backend/ChangeLog	Sun Jun 26 16:13:08 2011 +0000
@@ -1,3 +1,10 @@
+2011-06-26	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/model/River.java:
+	  Added a method maxOverlap to determine the gauge which has
+	  the max common length to a given interval. This is for
+	  numerical stability in slightly overlapping gauge ranges.
+
 2011-06-26	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/model/River.java: When
--- a/flys-backend/src/main/java/de/intevation/flys/model/River.java	Sun Jun 26 12:16:36 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/River.java	Sun Jun 26 16:13:08 2011 +0000
@@ -113,12 +113,43 @@
             "from Gauge where river=:river " +
             "and not (range.a > :b or range.b < :a) order by a");
         query.setParameter("river", this);
-        query.setParameter("a", new BigDecimal(a));
-        query.setParameter("b", new BigDecimal(b));
+        query.setParameter("a", new BigDecimal(a, PRECISION));
+        query.setParameter("b", new BigDecimal(b, PRECISION));
 
         return query.list();
     }
 
+    public Gauge maxOverlap(double a, double b) {
+        List<Gauge> gauges = determineGauges(a, b);
+        if (gauges == null) {
+            return null;
+        }
+
+        if (a > b) { double t = a; a = b; b = t; }
+
+        double max = -Double.MAX_VALUE;
+
+        Gauge result = null;
+
+        for (Gauge gauge: gauges) {
+            Range  r = gauge.getRange();
+            double c = r.getA().doubleValue();
+            double d = r.getB().doubleValue();
+
+            double start = c >= a ? c : a;
+            double stop  = d <= b ? d : b;
+
+            double length = stop - start;
+
+            if (length > max) {
+                max = length;
+                result = gauge;
+            }
+        }
+
+        return result;
+    }
+
     public Gauge determineGaugeByName(String name) {
         Session session = SessionHolder.HOLDER.get();
         Query query = session.createQuery(

http://dive4elements.wald.intevation.org