comparison 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
comparison
equal deleted inserted replaced
8723:686d8876edf9 8724:47199406994a
30 import javax.persistence.Table; 30 import javax.persistence.Table;
31 31
32 import org.hibernate.Query; 32 import org.hibernate.Query;
33 import org.hibernate.Session; 33 import org.hibernate.Session;
34 34
35 import org.apache.log4j.Logger;
35 36
36 @Entity 37 @Entity
37 @Table(name = "rivers") 38 @Table(name = "rivers")
38 public class River 39 public class River
39 implements Serializable 40 implements Serializable
40 { 41 {
42 private static Logger log = Logger.getLogger(River.class);
43
41 public static final MathContext PRECISION = new MathContext(6); 44 public static final MathContext PRECISION = new MathContext(6);
42 45
43 public static final double EPSILON = 1e-5; 46 public static final double EPSILON = 1e-5;
47
48 // Tolerance for determining whether we are at the station of a gauge
49 public static final double GAUGE_EPSILON = 0.1;
44 50
45 public static final Comparator<Double> KM_CMP = new Comparator<Double>() { 51 public static final Comparator<Double> KM_CMP = new Comparator<Double>() {
46 @Override 52 @Override
47 public int compare(Double a, Double b) { 53 public int compare(Double a, Double b) {
48 double diff = a - b; 54 double diff = a - b;
265 query.setParameter("p", new BigDecimal(p, PRECISION)); 271 query.setParameter("p", new BigDecimal(p, PRECISION));
266 List<Gauge> gauges = query.list(); 272 List<Gauge> gauges = query.list();
267 return gauges.isEmpty() ? null : gauges.get(0); 273 return gauges.isEmpty() ? null : gauges.get(0);
268 } 274 }
269 275
270 public Gauge determineGaugeByStation(double a, double b) { 276
271 277 /**
272 if (a > b) { double t = a; a = b; b = t; } 278 * @param s station at which the gauge is requested.
273 279 * @return Gauge within tolerance at given station. null if there is none.
280 */
281 public Gauge determineGaugeAtStation(double s) {
274 Session session = SessionHolder.HOLDER.get(); 282 Session session = SessionHolder.HOLDER.get();
275 283
276 Query query = session.createQuery( 284 Query query = session.createQuery(
277 "from Gauge where river.id=:river " + 285 "from Gauge where river.id=:river " +
278 "and station between :a and :b"); 286 "and station between :a and :b");
279 query.setParameter("river", getId()); 287 query.setParameter("river", getId());
280 query.setParameter("a", new BigDecimal(a)); 288 query.setParameter("a", new BigDecimal(s - GAUGE_EPSILON));
281 query.setParameter("b", new BigDecimal(b)); 289 query.setParameter("b", new BigDecimal(s + GAUGE_EPSILON));
282 290
283 List<Gauge> gauges = query.list(); 291 List<Gauge> gauges = query.list();
292 if (gauges.size() > 1) {
293 log.warn("More than one gauge found at km " + s +
294 " within +-" + GAUGE_EPSILON +
295 ". Returning arbitrary result.");
296 }
284 return gauges.isEmpty() ? null : gauges.get(0); 297 return gauges.isEmpty() ? null : gauges.get(0);
285 } 298 }
286 299
287 public double[] determineMinMaxQ() { 300 public double[] determineMinMaxQ() {
288 Session session = SessionHolder.HOLDER.get(); 301 Session session = SessionHolder.HOLDER.get();

http://dive4elements.wald.intevation.org