[PATCH 14 of 45] (Issue1754) Make BedDifference calculation range dependent

Wald Commits scm-commit at wald.intevation.org
Tue Mar 10 17:05:42 CET 2015


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1423840666 -3600
# Node ID 76113b9758298a3d32e7458d89b802787c568709
# Parent  9a5b3079aad49fc55a1f238ffbea20798904b06e
(Issue1754) Make BedDifference calculation range dependent.

diff -r 9a5b3079aad4 -r 76113b975829 artifacts/doc/conf/artifacts/minfo.xml
--- a/artifacts/doc/conf/artifacts/minfo.xml	Fri Feb 13 14:59:16 2015 +0100
+++ b/artifacts/doc/conf/artifacts/minfo.xml	Fri Feb 13 16:17:46 2015 +0100
@@ -35,7 +35,7 @@
 
         <transition transition="org.dive4elements.river.artifacts.transitions.ValueCompareTransition">
             <from state="state.minfo.calculation_mode"/>
-            <to state="state.minfo.bed.difference_select"/>
+            <to state="state.minfo.distance_only"/>
             <condition data="calculation_mode" value="calc.bed.diff" operator="equal"/>
         </transition>
 
@@ -45,7 +45,6 @@
             <condition data="calculation_mode" value="calc.bed.quality" operator="equal"/>
         </transition>
 
-
         <state id="state.minfo.distance_only" description="state.minfo.distance_only" state="org.dive4elements.river.artifacts.states.DistanceOnlySelect" helpText="help.state.minfo.distance_only">
             <data name="ld_from" type="Double" />
             <data name="ld_to"   type="Double" />
@@ -72,6 +71,12 @@
             <condition data="calculation_mode" value="calc.bed.middle" operator="equal"/>
         </transition>
 
+        <transition transition="org.dive4elements.river.artifacts.transitions.ValueCompareTransition">
+            <from state="state.minfo.distance_only"/>
+            <to state="state.minfo.bed.difference_select"/>
+            <condition data="calculation_mode" value="calc.bed.diff" operator="equal"/>
+        </transition>
+
         <transition transition="org.dive4elements.river.artifacts.transitions.DefaultTransition">
             <from state="state.minfo.sq.location"/>
             <to state="state.minfo.sq.period"/>
diff -r 9a5b3079aad4 -r 76113b975829 artifacts/doc/conf/meta-data.xml
--- a/artifacts/doc/conf/meta-data.xml	Fri Feb 13 14:59:16 2015 +0100
+++ b/artifacts/doc/conf/meta-data.xml	Fri Feb 13 16:17:46 2015 +0100
@@ -1873,8 +1873,9 @@
                             bhs.year
                      FROM   bed_height_single bhs
                             JOIN bed_height_single_values bhsv
-                              ON bhsv.bed_height_single_id = bhs.id
-                     WHERE  bhs.river_id = ${river_id}),
+                                ON bhsv.bed_height_single_id = bhs.id
+                     WHERE  bhs.river_id = ${river_id}
+                     AND bhsv.station BETWEEN ${fromkm} AND ${tokm}),
                  csta
                  AS (SELECT b1.id          AS b1id,
                             b1.description AS b1desc,
diff -r 9a5b3079aad4 -r 76113b975829 artifacts/src/main/java/org/dive4elements/river/artifacts/access/BedDifferencesAccess.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/access/BedDifferencesAccess.java	Fri Feb 13 14:59:16 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/access/BedDifferencesAccess.java	Fri Feb 13 16:17:46 2015 +0100
@@ -19,7 +19,7 @@
 
 
 public class BedDifferencesAccess
-extends RiverAccess
+extends RangeAccess
 {
     private static Logger log = Logger.getLogger(BedDifferencesAccess.class);
 
diff -r 9a5b3079aad4 -r 76113b975829 artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java	Fri Feb 13 14:59:16 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java	Fri Feb 13 16:17:46 2015 +0100
@@ -37,10 +37,12 @@
 
         this.river     = access.getRiverName();
         this.heightIds = access.extractHeightIds(context);
+        double fromKm  = access.getFrom();
+        double toKm    = access.getTo();
 
         BedDiffYearResult [] results = new BedDiffYearResult[heightIds.length];
         for (int i = 0; i < heightIds.length; i++) {
-            BedHeightSingleData [] pair = getHeightPair(heightIds[i]);
+            BedHeightSingleData [] pair = getHeightPair(heightIds[i], fromKm, toKm);
             if (pair[0].getYear() == null || pair[1].getYear() == null) {
                 addProblem("beddiff.missing.year");
             }
@@ -51,10 +53,10 @@
     }
 
     /** Get two BedHeights from factory. */
-    private static BedHeightSingleData [] getHeightPair(int [] ids) {
+    private static BedHeightSingleData [] getHeightPair(int [] ids, double from, double to) {
         return new BedHeightSingleData [] {
-            (BedHeightSingleData)BedHeightFactory.getHeight("single", ids[0]),
-            (BedHeightSingleData)BedHeightFactory.getHeight("single", ids[1])
+            (BedHeightSingleData)BedHeightFactory.getHeight("single", ids[0], from, to),
+            (BedHeightSingleData)BedHeightFactory.getHeight("single", ids[1], from, to)
         };
     }
 
diff -r 9a5b3079aad4 -r 76113b975829 artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightFactory.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightFactory.java	Fri Feb 13 14:59:16 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightFactory.java	Fri Feb 13 16:17:46 2015 +0100
@@ -33,10 +33,16 @@
         "SELECT bhsv.height, bhsv.station, bhsv.data_gap, bhsv.sounding_width," +
         "       bhs.year, bhsv.width" +
         "   FROM bed_height_single bhs" +
-        "       JOIN bed_height_single_values bhsv on bhsv.bed_height_single_id = bhs.id" +
+        "       JOIN bed_height_single_values bhsv on bhsv.bed_height_single_id = bhs.id";
+
+    public static final String ID_CLAUSE =
         "   WHERE bhs.id = :height_id" +
         "       ORDER BY bhsv.station";
 
+    public static final String ID_STATION_CLAUSE =
+        "   WHERE bhs.id = :height_id AND" +
+        "         bhsv.station BETWEEN :fromkm AND :tokm" +
+        "       ORDER BY bhsv.station";
 
     /** Query to get name (description) for wst_id. */
     public static final String SQL_SELECT_DESCR_SINGLE =
@@ -46,17 +52,23 @@
     private BedHeightFactory() {
     }
 
-
     /**
      * Get BedHeightData for given type and height_id, caring about the cache.
+     * If from or to are NaN all values are returned. Otherwise only get
+     * values with stations between from and to.
      */
-    public static BedHeightData getHeight(String type, int height_id) {
+    public static BedHeightData getHeight(String type, int height_id, double from, double to) {
         log.debug("BedHeightFactory.getHeight");
         Cache cache = CacheFactory.getCache("bedheight-value-table-static");
 
-        String cacheKey = Integer.toString(height_id);
+        String cacheKey = Integer.toString(height_id) + ":" +
+            Double.toString(from) + ":" + Double.toString(to);
 
         if (cache != null) {
+            /* We could be more intelligent here and reuse cached values for
+             * a complete river and filter the other stations out afterwards.
+             * It might even be better to cache all values first and filter
+             * later. */
             Element element = cache.get(cacheKey);
             if (element != null) {
                 log.debug("Got static bedheight values from cache");
@@ -67,7 +79,7 @@
             cacheKey = null;
         }
 
-        BedHeightData values = getBedHeightUncached(type, height_id);
+        BedHeightData values = getBedHeightUncached(type, height_id, from, to);
 
         if (values != null && cacheKey != null) {
             log.debug("Store static bed height values in cache.");
@@ -77,6 +89,13 @@
         return values;
     }
 
+    /**
+     * Get BedHeightData for given type and height_id, caring about the cache.
+     */
+    public static BedHeightData getHeight(String type, int height_id) {
+        return getHeight(type, height_id, Double.NaN, Double.NaN);
+    }
+
     /** Get name for a BedHeight. */
     public static String getHeightName(String type, int height_id) {
         log.debug("BedHeightFactory.getHeightName height_id/" + height_id);
@@ -104,12 +123,19 @@
 
     /**
      * Get BedHeightData from db.
+     *
+     * If from or to are negative all stations are returned. Otherwise
+     * only the values with a station betweend from and to.
      * @param height_id database id of the bed_height
+     * @param from minimum station value or NaN
+     * @param to maximum station value or NaN
      * @return according BedHeight.
      */
     public static BedHeightData getBedHeightUncached(
         String type,
-        int height_id)
+        int height_id,
+        double from,
+        double to)
     {
         if (log.isDebugEnabled()) {
             log.debug("BedHeightFactory.getBedHeightUncached");
@@ -120,7 +146,13 @@
         if (type.equals("single")) {
             BedHeightSingleData height =
                 new BedHeightSingleData(getHeightName(type, height_id));
-            sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLE)
+            String queryString = SQL_SELECT_SINGLE;
+            if (Double.isNaN(from) || Double.isNaN(to)) {
+                queryString += ID_CLAUSE;
+            } else {
+                queryString += ID_STATION_CLAUSE;
+            }
+            sqlQuery = session.createSQLQuery(queryString)
                 .addScalar("height", StandardBasicTypes.DOUBLE)
                 .addScalar("station", StandardBasicTypes.DOUBLE)
                 .addScalar("data_gap", StandardBasicTypes.DOUBLE)
@@ -128,6 +160,10 @@
                 .addScalar("width", StandardBasicTypes.DOUBLE)
                 .addScalar("year", StandardBasicTypes.INTEGER);
             sqlQuery.setInteger("height_id", height_id);
+            if (!Double.isNaN(from) && !Double.isNaN(to)) {
+                sqlQuery.setDouble("fromkm", from);
+                sqlQuery.setDouble("tokm", to);
+            }
             List<Object []> results = sqlQuery.list();
 
             for (Object [] row: results) {


More information about the Dive4Elements-commits mailing list