# HG changeset patch # User Sascha L. Teichmann # Date 1309104788 0 # Node ID 321b9e480f723e8b21b63f0a2122aa5e7bc878b9 # Parent 87ac543c7ab95613b3878416483689c56b6643c3 Added a method to river to find the gauge with the max overlap with a given interval. flys-backend/trunk@2235 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 87ac543c7ab9 -r 321b9e480f72 flys-backend/ChangeLog --- a/flys-backend/ChangeLog Sun Jun 26 12:16:36 2011 +0000 +++ b/flys-backend/ChangeLog Sun Jun 26 16:13:08 2011 +0000 @@ -1,3 +1,10 @@ +2011-06-26 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/model/River.java: + Added a method maxOverlap to determine the gauge which has + the max common length to a given interval. This is for + numerical stability in slightly overlapping gauge ranges. + 2011-06-26 Sascha L. Teichmann * src/main/java/de/intevation/flys/model/River.java: When diff -r 87ac543c7ab9 -r 321b9e480f72 flys-backend/src/main/java/de/intevation/flys/model/River.java --- a/flys-backend/src/main/java/de/intevation/flys/model/River.java Sun Jun 26 12:16:36 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/River.java Sun Jun 26 16:13:08 2011 +0000 @@ -113,12 +113,43 @@ "from Gauge where river=:river " + "and not (range.a > :b or range.b < :a) order by a"); query.setParameter("river", this); - query.setParameter("a", new BigDecimal(a)); - query.setParameter("b", new BigDecimal(b)); + query.setParameter("a", new BigDecimal(a, PRECISION)); + query.setParameter("b", new BigDecimal(b, PRECISION)); return query.list(); } + public Gauge maxOverlap(double a, double b) { + List gauges = determineGauges(a, b); + if (gauges == null) { + return null; + } + + if (a > b) { double t = a; a = b; b = t; } + + double max = -Double.MAX_VALUE; + + Gauge result = null; + + for (Gauge gauge: gauges) { + Range r = gauge.getRange(); + double c = r.getA().doubleValue(); + double d = r.getB().doubleValue(); + + double start = c >= a ? c : a; + double stop = d <= b ? d : b; + + double length = stop - start; + + if (length > max) { + max = length; + result = gauge; + } + } + + return result; + } + public Gauge determineGaugeByName(String name) { Session session = SessionHolder.HOLDER.get(); Query query = session.createQuery(