changeset 6385:ef08c4f57ede

Artifacts: First part of the official lines guessing.
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 21 Jun 2013 12:46:59 +0200
parents 2987d81ea719
children 3eff8efb4354
files artifacts/doc/conf/cache.xml artifacts/src/main/java/org/dive4elements/river/artifacts/model/OfficialLineFinder.java
diffstat 2 files changed, 166 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/doc/conf/cache.xml	Fri Jun 21 12:15:30 2013 +0200
+++ b/artifacts/doc/conf/cache.xml	Fri Jun 21 12:46:59 2013 +0200
@@ -142,6 +142,13 @@
            memoryStoreEvictionPolicy="LRU"
            overflowToDisk="true"
            diskPersistent="true"
+       />
+
+    <!-- This one is used to associate the offical lines to the respective
+         main values. -->
+    <cache name="official-lines"
+           maxElementsInMemory="2"
+           timeToLiveSeconds="14400"
            />
 
     <!-- This one is used for the cross section lookup
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/OfficialLineFinder.java	Fri Jun 21 12:46:59 2013 +0200
@@ -0,0 +1,159 @@
+/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+
+package org.dive4elements.river.artifacts.model;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.river.artifacts.cache.CacheFactory;
+import org.dive4elements.river.model.Gauge;
+import org.dive4elements.river.model.MainValue;
+import org.dive4elements.river.model.NamedMainValue;
+import org.dive4elements.river.model.OfficialLine;
+import org.dive4elements.river.model.River;
+import org.dive4elements.river.model.Wst;
+import org.dive4elements.river.model.WstColumn;
+
+public class OfficialLineFinder
+{
+    public static final String CACHE_NAME = "official-lines";
+
+    // We will only have one entry in this cache.
+    public static final String CACHE_KEY = CACHE_NAME;
+
+    public static final double EPSILON = 1e-4;
+
+
+    public static class ValueRange extends Range {
+
+        private double value;
+        private int    wstId;
+        private int    columnPos;
+
+        public ValueRange(
+            double start,
+            double end, 
+            double value,
+            int    wstId,
+            int    columnPos
+        ) {
+            super(start, end);
+            this.value     = value;
+            this.wstId     = wstId;
+            this.columnPos = columnPos;
+        }
+
+        public boolean sameValue(double value) {
+            return Math.abs(value - this.value) < EPSILON;
+        }
+
+        public int getWstId() {
+            return wstId;
+        }
+
+        public int getColumnPos() {
+            return columnPos;
+        }
+    }
+
+    public OfficialLineFinder() {
+    }
+
+    public static Map<String, List<ValueRange>> getAll() {
+        Cache cache = CacheFactory.getCache(CACHE_NAME);
+
+        if (cache == null) {
+            return getAllUncached();
+        }
+
+        Element element  = cache.get(CACHE_KEY);
+
+        if (element != null) {
+            return (Map<String, List<ValueRange>>)element.getValue();
+        }
+
+        Map<String, List<ValueRange>> result = getAllUncached();
+        if (result != null) {
+            cache.put(new Element(CACHE_KEY, result));
+        }
+        return result;
+
+    }
+
+    public static Map<String, List<ValueRange>> getAllUncached() {
+
+        Map<String, List<ValueRange>> rivers2officialLines =
+            new HashMap<String, List<ValueRange>>();
+
+
+        for (OfficialLine line: OfficialLine.fetchAllOfficalLines()) {
+            String   name = line.getNamedMainValue().getName();
+            WstColumn wc  = line.getWstColumn();
+            Wst       wst = wc.getWst();
+
+            List<ValueRange> ranges = new ArrayList<ValueRange>();
+
+            River river = wst.getRiver();
+            List<Gauge> gauges = river.getGauges();
+            for (Gauge gauge: gauges) {
+                List<MainValue> mainValues = gauge.getMainValues();
+                for (MainValue mainValue: mainValues) {
+                    NamedMainValue nmv = mainValue.getMainValue();
+                    if (nmv.getName().equalsIgnoreCase(name)) {
+                        // found gauge with this main value
+
+                        double from = gauge.getRange().getA().doubleValue();
+                        double to   = gauge.getRange().getA().doubleValue();
+                        double value = mainValue.getValue().doubleValue();
+                        int    wstId = wst.getId();
+                        int    pos   = wc.getPosition();
+                        ValueRange range =
+                            new ValueRange(from, to, value, wstId, pos);
+                        ranges.add(range);
+                        break;
+                    }
+                }
+            }
+
+            if (!ranges.isEmpty()) {
+                rivers2officialLines.put(river.getName(), ranges);
+            }
+        }
+
+        return rivers2officialLines;
+    }
+
+    public static List<OfficialLine> findOfficialLines(D4EArtifact artifact) {
+
+        Map<String, List<ValueRange>> rivers2officialLines = getAll();
+
+        String riverName = artifact.getDataAsString("river");
+
+        if (riverName == null) {
+            return Collections.<OfficialLine>emptyList();
+        }
+
+        List<ValueRange> ranges = rivers2officialLines.get(riverName);
+
+        if (ranges.isEmpty()) {
+            return Collections.<OfficialLine>emptyList();
+        }
+
+        // TODO: Figure out all the cases here.
+
+        return Collections.<OfficialLine>emptyList();
+    }
+}

http://dive4elements.wald.intevation.org