changeset 6395:d2803cc7a338

Artifacts: Official lines: Only apply in Q-Calculations and filter against the ranges of the gauges.
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 21 Jun 2013 16:28:47 +0200
parents cf4889a257cb
children f1018fd85183
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/OfficialLineFinder.java
diffstat 1 files changed, 65 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/OfficialLineFinder.java	Fri Jun 21 15:37:32 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/OfficialLineFinder.java	Fri Jun 21 16:28:47 2013 +0200
@@ -136,22 +136,84 @@
         return rivers2officialLines;
     }
 
+    public static final Range MAX_RANGE = new Range(-Double.MAX_VALUE, +Double.MAX_VALUE);
+
+    private static final String nn(String s) {
+        return s != null ? s : "";
+    }
+
+    public static Range extractRange(D4EArtifact artifact) {
+        String mode      = nn(artifact.getDataAsString("ld_mode"));
+        String locations = nn(artifact.getDataAsString("ld_locations"));
+        String from      = nn(artifact.getDataAsString("ld_from"));
+        String to        = nn(artifact.getDataAsString("ld_to"));
+
+        if (mode.equals("location")) {
+            try {
+                String loc = locations.replace(" ", "");
+                String[] split = loc.split(",");
+                if (split.length < 1) {
+                    return MAX_RANGE;
+                }
+                double min = Double.parseDouble(split[0]);
+                double max = min;
+                for (int i = 1; i < split.length; ++i) {
+                    double v = Double.parseDouble(split[i]);
+                    if (v > max) max = v;
+                    if (v < min) min = v;
+                }
+                return new Range(min, max);
+            }
+            catch (NumberFormatException nfe) {
+                return MAX_RANGE;
+            }
+        }
+        try {
+            return new Range(Double.parseDouble(from), Double.parseDouble(to));
+        }
+        catch (NumberFormatException nfe) {
+            return MAX_RANGE;
+        }
+    }
+
+    private static List<ValueRange> filterByRange(Range range, List<ValueRange> ranges) {
+        List<ValueRange> list = new ArrayList<ValueRange>(ranges.size());
+        for (ValueRange r: ranges) {
+            if (r.intersects(range)) {
+                list.add(r);
+            }
+        }
+        return list;
+    }
+
+    private static boolean isQ(D4EArtifact artifact) {
+        Boolean b = artifact.getDataAsBoolean("wq_isq");
+        return b != null && b;
+    }
+
     public static List<OfficialLine> findOfficialLines(D4EArtifact artifact) {
 
+        if (!isQ(artifact)) { // Only handle Q calculations
+            return Collections.<OfficialLine>emptyList();
+        }
+
         Map<String, List<ValueRange>> rivers2officialLines = getAll();
 
-        String riverName = artifact.getDataAsString("river");
+        String riverName = nn(artifact.getDataAsString("river"));
 
-        if (riverName == null) {
+        List<ValueRange> ranges = rivers2officialLines.get(riverName);
+
+        if (ranges == null) {
             return Collections.<OfficialLine>emptyList();
         }
 
-        List<ValueRange> ranges = rivers2officialLines.get(riverName);
+        ranges = filterByRange(extractRange(artifact), ranges);
 
         if (ranges.isEmpty()) {
             return Collections.<OfficialLine>emptyList();
         }
 
+
         // TODO: Figure out all the cases here.
 
         return Collections.<OfficialLine>emptyList();

http://dive4elements.wald.intevation.org