changeset 8727:20b543616e6d

(issue1801) Return defined gauge at limit between two gauge ranges.
author Tom Gottfried <tom@intevation.de>
date Wed, 29 Apr 2015 18:17:53 +0200
parents 7f1fb07e4e3d
children f51b9305d338
files backend/src/main/java/org/dive4elements/river/model/River.java
diffstat 1 files changed, 32 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/model/River.java	Wed Apr 29 16:36:29 2015 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/River.java	Wed Apr 29 18:17:53 2015 +0200
@@ -261,6 +261,16 @@
     }
 
     public Gauge determineGaugeByPosition(double p) {
+        // Per default, we prefer the gauge downstream
+        return determineGaugeByPosition(p, getKmUp());
+    }
+
+    /**
+     * @param p Station on this river for which the gauge is searched
+     * @param kmLower At boundary of two gauge ranges, should gauge at lower
+     * km be returned?
+     */
+    public Gauge determineGaugeByPosition(double p, boolean kmLower) {
         Session session = SessionHolder.HOLDER.get();
         Query query = session.createQuery(
             "from Gauge g where river=:river "  +
@@ -270,7 +280,28 @@
         query.setParameter("river", this);
         query.setParameter("p", new BigDecimal(p, PRECISION));
         List<Gauge> gauges = query.list();
-        return gauges.isEmpty() ? null : gauges.get(0);
+        if (gauges.isEmpty()) {
+            return null;
+        }
+        if (gauges.size() == 1) {
+            return gauges.get(0);
+        }
+        if (gauges.size() > 2) {
+            // TODO: database schema should prevent this.
+            log.warn("More than two gauge ranges overlap km " + p +
+                ". Returning arbitrary result.");
+        }
+        Gauge g0 = gauges.get(0);
+        Gauge g1 = gauges.get(1);
+        if (kmLower) {
+            return
+                g0.getStation().doubleValue() < g1.getStation().doubleValue()
+                ? g0
+                : g1;
+        }
+        return g0.getStation().doubleValue() > g1.getStation().doubleValue()
+            ? g0
+            : g1;
     }
 
 

http://dive4elements.wald.intevation.org