[PATCH 1 of 2] (issue1801) Return defined gauge at limit between two gauge ranges

Wald Commits scm-commit at wald.intevation.org
Wed Apr 29 18:21:31 CEST 2015


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1430324273 -7200
# Node ID 20b543616e6d4326740a180edc01578eee836109
# Parent  7f1fb07e4e3dc6686561e3ef6e5c7fad098b6522
(issue1801) Return defined gauge at limit between two gauge ranges.

diff -r 7f1fb07e4e3d -r 20b543616e6d backend/src/main/java/org/dive4elements/river/model/River.java
--- 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;
     }
 
 


More information about the Dive4Elements-commits mailing list