diff backend/src/main/java/org/dive4elements/river/model/River.java @ 8724:47199406994a

(issue1801) Determine gauge at a station always with same tolerance.
author Tom Gottfried <tom@intevation.de>
date Wed, 29 Apr 2015 12:30:57 +0200
parents cfafe5764509
children 20b543616e6d
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/model/River.java	Wed Apr 29 11:56:04 2015 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/River.java	Wed Apr 29 12:30:57 2015 +0200
@@ -32,16 +32,22 @@
 import org.hibernate.Query;
 import org.hibernate.Session;
 
+import org.apache.log4j.Logger;
 
 @Entity
 @Table(name = "rivers")
 public class River
 implements   Serializable
 {
+    private static Logger log = Logger.getLogger(River.class);
+
     public static final MathContext PRECISION = new MathContext(6);
 
     public static final double EPSILON = 1e-5;
 
+    // Tolerance for determining whether we are at the station of a gauge
+    public static final double GAUGE_EPSILON = 0.1;
+
     public static final Comparator<Double> KM_CMP = new Comparator<Double>() {
         @Override
         public int compare(Double a, Double b) {
@@ -267,20 +273,27 @@
         return gauges.isEmpty() ? null : gauges.get(0);
     }
 
-    public Gauge determineGaugeByStation(double a, double b) {
 
-        if (a > b) { double t = a; a = b; b = t; }
-
+    /**
+     * @param s station at which the gauge is requested.
+     * @return Gauge within tolerance at given station. null if there is none.
+     */
+    public Gauge determineGaugeAtStation(double s) {
         Session session = SessionHolder.HOLDER.get();
 
         Query query = session.createQuery(
             "from Gauge where river.id=:river " +
             "and station between :a and :b");
         query.setParameter("river", getId());
-        query.setParameter("a", new BigDecimal(a));
-        query.setParameter("b", new BigDecimal(b));
+        query.setParameter("a", new BigDecimal(s - GAUGE_EPSILON));
+        query.setParameter("b", new BigDecimal(s + GAUGE_EPSILON));
 
         List<Gauge> gauges = query.list();
+        if (gauges.size() > 1) {
+            log.warn("More than one gauge found at km " + s +
+                " within +-" + GAUGE_EPSILON +
+                ". Returning arbitrary result.");
+        }
         return gauges.isEmpty() ? null : gauges.get(0);
     }
 

http://dive4elements.wald.intevation.org