[PATCH 1 of 2] (issue1750) New methods for finding the actual location and companion station of a measurement station

Wald Commits scm-commit at wald.intevation.org
Mon Apr 20 10:26:48 CEST 2015


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1429518105 -7200
# Node ID 851ea37d35f3fcfa675b339e159dd80cf90cfb24
# Parent  67e53dbe05d2c43203e91a5c6fc0be04816fb329
(issue1750) New methods for finding the actual location and companion station of a measurement station.

diff -r 67e53dbe05d2 -r 851ea37d35f3 backend/src/main/java/org/dive4elements/river/model/MeasurementStation.java
--- a/backend/src/main/java/org/dive4elements/river/model/MeasurementStation.java	Fri Apr 17 15:04:16 2015 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/MeasurementStation.java	Mon Apr 20 10:21:45 2015 +0200
@@ -29,11 +29,14 @@
 
 import org.dive4elements.river.backend.SessionHolder;
 
+import org.apache.log4j.Logger;
+
 import static org.dive4elements.river.backend.utils.EpsilonComparator.CMP;
 
 @Entity
 @Table(name = "measurement_station")
 public class MeasurementStation {
+    private static final Logger log = Logger.getLogger(MeasurementStation.class);
 
     private Integer id;
 
@@ -70,15 +73,7 @@
                     "Stations not at same river");
             }
 
-            boolean kmUp = m1.getRange().getRiver().getKmUp();
-            double s1 = kmUp && m1.getRange().getB() != null
-                ? m1.getRange().getB().doubleValue()
-                : m1.getRange().getA().doubleValue();
-            double s2 = kmUp && m2.getRange().getB() != null
-                ? m2.getRange().getB().doubleValue()
-                : m2.getRange().getA().doubleValue();
-
-            int cmpStations = CMP.compare(s1, s2);
+            int cmpStations = CMP.compare(m1.retrieveKm(), m2.retrieveKm());
             if (cmpStations == 0) {
                 if (m1.getMeasurementType().equals(m2.getMeasurementType())) {
                     throw new IllegalArgumentException(
@@ -208,6 +203,30 @@
         this.comment = comment;
     }
 
+    /* Get the actual location of the measurement station at the river */
+    public double retrieveKm() {
+        // In case river is km_up, station is at larger value of range
+        return getRange().getRiver().getKmUp() && getRange().getB() != null
+            ? getRange().getB().doubleValue()
+            : getRange().getA().doubleValue();
+    }
+
+    /* Get measurement station of other type at the same location */
+    public MeasurementStation findCompanionStation() {
+        River  river = getRange().getRiver();
+        double km    = retrieveKm();
+        List<MeasurementStation> stations = getStationsAtKM(river, km);
+
+        for (MeasurementStation station: stations) {
+            if (!station.getMeasurementType().equals(getMeasurementType())) {
+                return station;
+            }
+        }
+        log.debug("No additional stations found at km " + km +
+            " at river " + river);
+        return null;
+    }
+
     public static List<MeasurementStation> getStationsAtRiver(River river) {
         Session session = SessionHolder.HOLDER.get();
 
@@ -234,19 +253,17 @@
         List<MeasurementStation> result = new ArrayList<MeasurementStation>();
         for (Iterator iter = query.iterate(); iter.hasNext();) {
             MeasurementStation st = (MeasurementStation)iter.next();
-            Double a = st.getRange().getA().doubleValue();
-            Double b = st.getRange().getB() == null
-                ? null
-                : st.getRange().getB().doubleValue();
-
-            // In case river is km_up, station is at larger value of range
-            if (river.getKmUp() && b != null && CMP.compare(b, river_km) == 0
-                || !river.getKmUp() && CMP.compare(a, river_km) == 0
-                || b == null && CMP.compare(a, river_km) == 0 // no end km given
-            ) {
+            if (CMP.compare(st.retrieveKm(), river_km) == 0) {
                 result.add(st);
             }
         }
+
+        if (result.size() > 2) {
+            // TODO: database schema should prevent this
+            log.warn("More than two measurement stations at km " + river_km +
+                " at river " + river.getName());
+        }
+
         return result;
     }
 }


More information about the Dive4Elements-commits mailing list