annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/SoilKindKmValueFinder.java @ 8952:1a8f19f3b776

Connection to new database table bed_mobility implemented
author mschaefer
date Mon, 19 Mar 2018 09:12:17 +0100
parents d9dbf0b74bc2
children 45f1ad66560e
rev   line source
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
1 /* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
2 * Software engineering by
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
3 * Björnsen Beratende Ingenieure GmbH
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
5 *
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
6 * This file is Free Software under the GNU AGPL (>=v3)
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
8 * documentation coming with Dive4Elements River for details.
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
9 */
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
10
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
11 package org.dive4elements.river.artifacts.sinfo.tkhcalculation;
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
12
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
13 import java.util.List;
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
14 import java.util.Map.Entry;
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
15 import java.util.NavigableMap;
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
16 import java.util.TreeMap;
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
17
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
18 import org.apache.commons.lang.math.DoubleRange;
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
19 import org.apache.commons.math.ArgumentOutsideDomainException;
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
20 import org.dive4elements.river.backend.SessionHolder;
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
21 import org.dive4elements.river.model.River;
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
22 import org.hibernate.SQLQuery;
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
23 import org.hibernate.Session;
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
24 import org.hibernate.type.StandardBasicTypes;
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
25
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
26
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
27 /**
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
28 * @author Matthias Schäfer
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
29 */
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
30 final class SoilKindKmValueFinder {
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
31
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
32 /***** FIELDS *****/
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
33
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
34 // private static Logger log = Logger.getLogger(SoilKindKmValueFinder.class);
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
35
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
36 /**
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
37 * Query selecting the bed mobility attribute for a range of stations of a river
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
38 */
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
39 private static final String SQL_BED_MOBILITY = "SELECT bmv.station, bmv.moving"
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
40 + " FROM bed_mobility bm INNER JOIN bed_mobility_values bmv ON bm.id = bmv.bed_mobility_id"
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
41 + " WHERE (bm.river_id=:river_id) AND (bmv.station BETWEEN (:fromkm-0.0001) AND (:tokm+0.0001))"
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
42 + " ORDER BY bmv.station ASC";
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
43
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
44 private final NavigableMap<Double, SoilKind> kmMobility;
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
45
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
46
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
47 /***** CONSTRUCTORS *****/
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
48
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
49 private SoilKindKmValueFinder() {
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
50 /* only instantiate me via static constructor */
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
51 this.kmMobility = null;
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
52 }
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
53
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
54 private SoilKindKmValueFinder(final List<Object[]> queryRows) {
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
55 this.kmMobility = new TreeMap<>();
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
56 for (int i = 0; i <= queryRows.size() - 1; i++) {
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
57 this.kmMobility.put(Double.valueOf((double) queryRows.get(i)[0]), (((int) queryRows.get(i)[1]) == 1) ? SoilKind.mobil : SoilKind.starr);
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
58 }
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
59 }
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
60
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
61 /***** METHODS *****/
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
62
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
63 /**
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
64 * Loads the range of the river's kms with their soil kind.
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
65 *
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
66 * @return Whether the load has been successful
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
67 */
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
68 public static SoilKindKmValueFinder loadValues(final River river, final DoubleRange kmRange) {
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
69 final Session session = SessionHolder.HOLDER.get();
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
70 final SQLQuery sqlQuery = session.createSQLQuery(SQL_BED_MOBILITY).addScalar("station", StandardBasicTypes.DOUBLE)
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
71 .addScalar("moving", StandardBasicTypes.INTEGER);
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
72 sqlQuery.setInteger("river_id", river.getId().intValue());
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
73 sqlQuery.setDouble("fromkm", kmRange.getMinimumDouble());
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
74 sqlQuery.setDouble("tokm", kmRange.getMaximumDouble());
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
75 final List<Object[]> rows = sqlQuery.list();
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
76 if (rows.size() >= 1)
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
77 return new SoilKindKmValueFinder(rows);
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
78 else
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
79 return null;
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
80 }
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
81
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
82 /***** METHODS *****/
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
83
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
84 /**
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
85 * Searches a km with its soil kind
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
86 */
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
87 public SoilKind findSoilKind(final double km) throws ArgumentOutsideDomainException {
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
88 if ((this.kmMobility == null) || this.kmMobility.isEmpty())
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
89 throw new ArgumentOutsideDomainException(km, Double.NaN, Double.NaN);
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
90 else if (this.kmMobility.containsKey(Double.valueOf(km)))
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
91 return this.kmMobility.get(Double.valueOf(km));
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
92 else {
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
93 // FIXME: Assert minimum distance between neighbouring stations and candidate km?
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
94 final Entry<Double, SoilKind> streamUp = this.kmMobility.floorEntry(Double.valueOf(km));
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
95 if (streamUp == null)
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
96 throw new ArgumentOutsideDomainException(km, Double.NaN, Double.NaN);
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
97 else {
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
98 // Return the soil kind of the neighbouring station with the shorter distance to the candidate.
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
99 final Entry<Double, SoilKind> streamDown = this.kmMobility.ceilingEntry(Double.valueOf(km));
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
100 if (streamDown == null)
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
101 return streamUp.getValue();
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
102 else if ((streamUp.getKey().doubleValue() + streamDown.getKey().doubleValue()) / 2 <= km)
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
103 return streamUp.getValue();
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
104 else
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
105 return streamDown.getValue();
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
106 }
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
107 }
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
108 }
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
109 }

http://dive4elements.wald.intevation.org