[PATCH] (issue1754) Fix Radius calculation for filtered (smoothed) facets

Wald Commits scm-commit at wald.intevation.org
Wed Apr 29 11:56:10 CEST 2015


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1430301364 -7200
# Node ID 686d8876edf9c1d63e11a1647766a7cce716aa67
# Parent  a83d519155abfc24c2a1c15034fb21674da4f7b2
(issue1754) Fix Radius calculation for filtered (smoothed) facets

    To know the acutal extend of the Domain axis shown we need
    to know all data in the diagram as the acutal extend is
    the data point needed to calculate a Radius confusingly configured
    in zoom-scales we have to add all data first and
    then add the real filtered facets in a postprocessing step.

diff -r a83d519155ab -r 686d8876edf9 artifacts/src/main/java/org/dive4elements/river/artifacts/model/FacetTypes.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/FacetTypes.java	Tue Apr 28 14:22:47 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/FacetTypes.java	Wed Apr 29 11:56:04 2015 +0200
@@ -57,6 +57,9 @@
         public static boolean MANUALLINE(String type) {
             return type.endsWith("manualline");
         }
+        public static boolean FILTERED(String type) {
+            return type.endsWith("filtered");
+        }
         public static boolean SQ_CURVE(String type) {
             if (type.equals(SQ_A_CURVE)
                 || type.equals(SQ_B_CURVE)
diff -r a83d519155ab -r 686d8876edf9 artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java	Tue Apr 28 14:22:47 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java	Wed Apr 29 11:56:04 2015 +0200
@@ -166,6 +166,10 @@
         }
     }
 
+    protected void postProcess() {
+        return;
+    }
+
     /**
      * Generate the chart anew (including localized axis and all).
      */
@@ -173,6 +177,8 @@
     public JFreeChart generateChart() {
         log.debug("DiagramGenerator.generateChart");
 
+        postProcess();
+
         JFreeChart chart = ChartFactory.createXYLineChart(
             getChartTitle(),
             "",
diff -r a83d519155ab -r 686d8876edf9 artifacts/src/main/java/org/dive4elements/river/exports/LongitudinalSectionGenerator2.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/LongitudinalSectionGenerator2.java	Tue Apr 28 14:22:47 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/LongitudinalSectionGenerator2.java	Wed Apr 29 11:56:04 2015 +0200
@@ -14,9 +14,34 @@
 import org.dive4elements.river.jfree.Bounds;
 import org.dive4elements.river.jfree.DoubleBounds;
 import org.dive4elements.river.themes.ThemeDocument;
+import org.dive4elements.river.artifacts.model.FacetTypes;
+
+import org.jfree.data.Range;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.log4j.Logger;
 
 public class LongitudinalSectionGenerator2 extends DiagramGenerator
 {
+    private static Logger log = Logger.getLogger(LongitudinalSectionGenerator2.class);
+
+    /** Wrapper around the doOut info for postprocessing. */
+    protected static class SuperBundle
+    {
+        public ArtifactAndFacet bundle;
+        public ThemeDocument theme;
+        boolean visible;
+        public SuperBundle(ArtifactAndFacet bundle, ThemeDocument theme, boolean visible) {
+            this.bundle = bundle;
+            this.theme = theme;
+            this.visible = visible;
+        }
+    };
+
+    protected List<SuperBundle> postOutAF;
+
     public static final String I18N_CHART_SHORT_SUBTITLE =
         "chart.longitudinal.section.shortsubtitle";
 
@@ -46,6 +71,68 @@
         return super.getDefaultChartSubtitle();
     }
 
+    protected void calculateRadius() {
+        // Fixed range in settings is preferred
+        Range candidate = getRangeForAxisFromSettings("X");
+        Bounds dataBounds = getXBounds(0);
+        if (candidate == null) {
+            candidate = getDomainAxisRange(); // Diagram is zoomed
+            if (candidate != null && dataBounds == null) {
+                log.debug("Can't calculate the zoom without any X bounds.");
+                candidate = null;
+            } else if (candidate != null) {
+                // domainAxisRange is relative so we have to take
+                // this into account.
+                Bounds bounds =
+                    calculateZoom(dataBounds, candidate);
+                candidate = new Range(bounds.getLower().doubleValue(),
+                                      bounds.getUpper().doubleValue());
+                log.debug("Using X Range from zoom.");
+            }
+        } else {
+            log.debug("Using X Range from settings.");
+        }
+
+        if (candidate == null) {
+            if (dataBounds == null) {
+                // Diagram is empty.
+                candidate = new Range(0d, 0d);
+            }
+            // Diagram is not zoomed
+            candidate = new Range(dataBounds.getLower().doubleValue(),
+                                  dataBounds.getUpper().doubleValue());
+            log.debug("Using Full X Range.");
+        }
+        log.debug("startkm for Radius is: " + candidate.getLowerBound() +
+                  " endkm: " + candidate.getUpperBound());
+        context.putContextValue("startkm", candidate.getLowerBound());
+        context.putContextValue("endkm", candidate.getUpperBound());
+        context.putContextValue("bounds_defined", true);
+    }
+
+    @Override
+    protected void postProcess() {
+        if (postOutAF == null) {
+            log.debug("PostProcess without bundles to process");
+            return;
+        }
+
+        // fake startkm and endkm for the dry run
+        context.putContextValue("startkm", 0d);
+        context.putContextValue("endkm", 42d);
+        for (SuperBundle superbundle: postOutAF) {
+            // Dry run with fake start /end 
+            // to get the filtered facets also included
+            // in the x axis range.
+            super.doOut(superbundle.bundle, superbundle.theme, false);
+        }
+
+        calculateRadius(); // This calculates the real start and end km's
+        for (SuperBundle superbundle: postOutAF) {
+            super.doOut(superbundle.bundle, superbundle.theme, superbundle.visible);
+        }
+    }
+
     /* We override doOut here to save the startkm and endkm in the
      * context. Some facets will deliver different data because of
      * that setting. It is mainly used in MINFO where it causes
@@ -57,46 +144,20 @@
         ThemeDocument    theme,
         boolean          visible
     ) {
-        /* Aheinecke (25.09.2013): I do not understand why this has to be
-         * done so difficult and if it really must be done for every
-         * facet. At least it has to be done _before_ the super class
-         * actually does the output and accesses the facet data.
-         */
-        D4EArtifact artifact = (D4EArtifact)bundle.getArtifact();
-        Object ctxV = context.getContextValue("bounds_defined");
-        if (ctxV != null && (Boolean)ctxV) {
-            super.doOut(bundle, theme, visible);
+        String facetName = bundle.getFacetName();
+        if (FacetTypes.IS.FILTERED(facetName)) {
+            // We can only process the filtered (smoothed) facets
+            // after we know the diagram's extend to correctly calculate
+            // the radius of the filter / smoothing operation. So
+            // we postprocess them.
+
+            SuperBundle superbundle = new SuperBundle(bundle, theme, visible);
+            if (postOutAF == null) {
+                postOutAF = new ArrayList<SuperBundle>();
+            }
+            postOutAF.add(superbundle);
             return;
         }
-        if (getXBounds(0) != null && getDomainAxisRange() != null) {
-            Bounds bounds =
-                calculateZoom(getXBounds(0), getDomainAxisRange());
-            context.putContextValue("startkm", bounds.getLower());
-            context.putContextValue("endkm", bounds.getUpper());
-            context.putContextValue("bounds_defined", true);
-        }
-        else if (getXBounds(0) != null && getDomainAxisRange() == null) {
-            context.putContextValue("startkm", getXBounds(0).getLower());
-            context.putContextValue("endkm", getXBounds(0).getUpper());
-            context.putContextValue("bounds_defined", true);
-        }
-        else if (getXBounds(0) == null && getDomainAxisRange() != null){
-            RangeAccess access = new RangeAccess(artifact);
-            if (access.hasFrom() && access.hasTo()) {
-                Bounds b = new DoubleBounds(access.getFrom(), access.getTo());
-                Bounds bounds =
-                    calculateZoom(b, getDomainAxisRange());
-                context.putContextValue("startkm", bounds.getLower());
-                context.putContextValue("endkm", bounds.getUpper());
-                context.putContextValue("bounds_defined", true);
-            }
-        }
-        /*
-        else if (getXBounds(0) == null && getDomainAxisRange() == null) {
-            .. In this case the automatic zoom of the diagram will provide
-            decent values for the zooom calculation.
-        }
-        */
         super.doOut(bundle, theme, visible);
     }
 }


More information about the Dive4Elements-commits mailing list