diff artifacts/src/main/java/org/dive4elements/river/artifacts/access/RangeAccess.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/access/RangeAccess.java@bd047b71ab37
children 4897a58c8746
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/access/RangeAccess.java	Thu Apr 25 15:23:37 2013 +0200
@@ -0,0 +1,193 @@
+package org.dive4elements.river.artifacts.access;
+
+import org.apache.log4j.Logger;
+
+import gnu.trove.TDoubleArrayList;
+import org.dive4elements.artifacts.CallContext;
+import org.dive4elements.river.artifacts.FLYSArtifact;
+import org.dive4elements.river.artifacts.WINFOArtifact;
+
+import org.dive4elements.river.utils.FLYSUtils;
+
+
+/** For the moment, light-weight wrapper around FLYSUtils. */
+// TODO employ 'Caching' like other Accesses, remove usage of FLYSUtils.
+public class RangeAccess
+extends RiverAccess
+{
+    private static Logger logger = Logger.getLogger(RangeAccess.class);
+
+    private CallContext context;
+
+    public static enum KM_MODE { RANGE, LOCATIONS, NONE };
+
+    double[] kmRange;
+
+    Double from;
+
+    Double to;
+
+    Double step;
+
+    private KM_MODE mode;
+
+
+    public RangeAccess(FLYSArtifact artifact, CallContext context) {
+        super(artifact);
+        this.context = context;
+    }
+
+
+    /** Evaluate the ld_mode data of artifact. */
+    public KM_MODE getKmRangeMode() {
+        if (mode != null) {
+            return mode;
+        }
+        String modeData = getString("ld_mode");
+
+        if (modeData == null || modeData.length() == 0) {
+            mode = KM_MODE.NONE;
+        }
+        else if (modeData.equals("distance"))  {
+            mode = KM_MODE.RANGE;
+        }
+        else if (modeData.equals("locations")) {
+            mode = KM_MODE.LOCATIONS;
+        }
+        else {
+            mode = KM_MODE.NONE;
+        }
+
+        return mode;
+    }
+
+    /**
+     * Return sorted array of locations at which stuff was calculated
+     * (from ld_locations data), null if not parameterized this way.
+     */
+    public double[] getLocations() {
+        String locationStr = getString("ld_locations");
+
+        if (locationStr == null || locationStr.length() == 0) {
+            if (getArtifact() instanceof WINFOArtifact) {
+                WINFOArtifact winfo = (WINFOArtifact) getArtifact();
+                if (winfo.getReferenceStartKm() != null && winfo.getReferenceEndKms() != null) {
+                    return new double[]
+                        {
+                            winfo.getReferenceStartKm().doubleValue(),
+                            winfo.getReferenceEndKms()[0]
+                        };
+                }
+                else if (winfo.getReferenceStartKm() != null) {
+                    return new double[]
+                        {
+                            winfo.getReferenceStartKm().doubleValue(),
+                            winfo.getReferenceStartKm().doubleValue()
+                        };
+                }
+            }
+            return null;
+        }
+
+        String[] tmp               = locationStr.split(" ");
+        TDoubleArrayList locations = new TDoubleArrayList();
+
+        for (String l: tmp) {
+            try {
+                locations.add(Double.parseDouble(l));
+            }
+            catch (NumberFormatException nfe) {
+                logger.debug(nfe.getLocalizedMessage(), nfe);
+            }
+        }
+
+        locations.sort();
+
+        return locations.toNativeArray();
+    }
+
+
+    /** Return ld_from data (in km). */
+    public double getFrom() {
+        if (from == null) {
+            from = getDouble("ld_from");
+        }
+
+        if (logger.isDebugEnabled()) {
+            logger.debug("from: '" + from + "'");
+        }
+
+        return from.doubleValue();
+    }
+
+
+    /** Return ld_to data (in km). */
+    public double getTo() {
+        if (to == null) {
+            to = getDouble("ld_to");
+        }
+
+        if (logger.isDebugEnabled()) {
+            logger.debug("to: '" + to + "'");
+        }
+
+        return to.doubleValue();
+    }
+
+
+    /** Step width for calculation. */
+    public Double getStep() {
+
+        if (step == null) {
+            step = getDouble("ld_step");
+        }
+
+        if (logger.isDebugEnabled()) {
+            logger.debug("step: '" + step + "'");
+        }
+
+        return step;
+    }
+
+
+    /**
+     * Get min and max kilometer, independent of parametization
+     * (ld_from/to vs ld_locations).
+     */
+    public double[] getKmRange() {
+        // TODO store kmRange in field.
+        switch (getKmRangeMode()) {
+            case RANGE: {
+                return getKmFromTo();
+            }
+
+            case LOCATIONS: {
+                double[] locs = getLocations();
+                // if no locations, nPE.
+                if (locs == null) {
+                    logger.warn("no locations to get km range from.");
+                    return new double[] { Double.NaN, Double.NaN };
+                }
+                return new double[] { locs[0], locs[locs.length-1] };
+            }
+
+            case NONE: {
+                double[] locs = getLocations();
+                if (locs != null) {
+                    return new double[] { locs[0], locs[locs.length-1] };
+                }
+                else {
+                    return getKmFromTo();
+                }
+            }
+        }
+
+        return new double[] { Double.NaN, Double.NaN };
+    }
+
+
+    public double[] getKmFromTo() {
+         return FLYSUtils.getKmFromTo(this.getArtifact());
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org