changeset 1651:a7def20539fb

flys/issue317: Removed dependency from cross section demo app. flys-artifacts/trunk@2840 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 27 Sep 2011 13:45:13 +0000
parents aaf8d32f85bd
children 46a2a5dc98b7
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/charts/CrossSectionApp.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/geom/Lines.java
diffstat 4 files changed, 83 insertions(+), 193 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Sep 27 12:40:42 2011 +0000
+++ b/flys-artifacts/ChangeLog	Tue Sep 27 13:45:13 2011 +0000
@@ -1,3 +1,15 @@
+2011-09-27	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	flys/issue317: (Querprofil-Diagramm: Referenzen auf CrossSectionApp entfernen)
+
+	* src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java:
+	  Removed dependency to demo app.
+
+	* src/main/java/de/intevation/flys/artifacts/charts/CrossSectionApp.java:
+	  Refactored to use logic from the models.
+
+	* src/main/java/de/intevation/flys/artifacts/geom/Lines.java:
+	  Moved some logic from the demo app to this model.
 2011-09-27  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/wsplgen/FacetCreator.java: New. Code
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Tue Sep 27 12:40:42 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Tue Sep 27 13:45:13 2011 +0000
@@ -40,11 +40,12 @@
 import de.intevation.flys.artifacts.states.DefaultState;
 import de.intevation.flys.artifacts.states.LocationDistanceSelect;
 
+import de.intevation.flys.geom.Lines;
+
 import de.intevation.flys.model.Gauge;
 import de.intevation.flys.model.River;
 import de.intevation.flys.model.CrossSection;
 import de.intevation.flys.model.CrossSectionLine;
-import de.intevation.flys.model.CrossSectionPoint;
 
 import de.intevation.flys.utils.DoubleUtil;
 import de.intevation.flys.utils.FLYSUtils;
@@ -64,7 +65,6 @@
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 
-import de.intevation.flys.artifacts.charts.CrossSectionApp;
 import de.intevation.flys.artifacts.model.CalculationMessage;
 
 /**
@@ -665,37 +665,6 @@
 
 
     /**
-     * Get sorted, valid Points of a CrossSectionLine.
-     *
-     * @param line line of interest.
-     *
-     * @return list of points of CrossSectionLine, sorted.
-     */
-    protected List<Point2D> getCrossSectionLinesPoints(CrossSectionLine line) {
-        List<Point2D> points = new ArrayList<Point2D>();
-
-        if (line == null) {
-            return points;
-        }
-
-        List<CrossSectionPoint> linePoints = line.getPoints();
-        if (linePoints.isEmpty()) {
-            logger.info("No points in selected CrossSectionLine.");
-            return points;
-        }
-        Collections.sort(linePoints, CrossSectionApp.COL_POS_CMP);
-        for (CrossSectionPoint p: linePoints) {
-            double x = p.getX().doubleValue();
-            double y = p.getY().doubleValue();
-            if (CrossSectionApp.isValid(x) && CrossSectionApp.isValid(y)) {
-                points.add(new Point2D.Double(x,y));
-            }
-        }
-        return points;
-    }
-
-
-    /**
      * Get CrossSectionLine spatially closest to what is specified in the data
      * "cross_section.km".
      *
@@ -707,7 +676,7 @@
             wishKM = Double.parseDouble(getDataAsString("cross_section.km"));
         }
         catch (Exception e) {
-            ;
+            logger.warn(e);
         }
 
         // Get the cross section closest to requested km.
@@ -761,7 +730,7 @@
         logger.info("getCrossSectionData() for cross_section.km "
             + getDataAsString("cross_section.km"));
         CrossSectionLine line = searchCrossSectionKmLine();
-        return getCrossSectionProfile(line);
+        return line != null ? line.fetchCrossSectionProfile() : null;
     }
 
 
@@ -774,49 +743,47 @@
     public double [][] getWaterLines() {
         logger.debug("getWaterLines()");
         CrossSectionLine csl = searchCrossSectionKmLine();
-        List<Point2D> points = getCrossSectionLinesPoints(csl);
+        List<Point2D> points = csl.fetchCrossSectionLinesPoints();
         // Need W at km
         WQKms [] wqkms = (WQKms[]) getWaterlevelData().getData();
         if (wqkms.length == 0) {
             logger.error("No WQKms found.");
-            return CrossSectionApp.createWaterLines(points, 0.0f);
+            return Lines.createWaterLines(points, 0.0f);
         }
-        else
-        {
-            if (wqkms.length > 1) {
-                logger.warn("More than one wqkms found, taking first one.");
-            }
-            // Find W at km, linear naive approach.
-            WQKms triple = wqkms[0];
-            // Find index of km.
-            double wishKM = 0.0f;
-            int old_idx = 0;
-            try {
-                wishKM = Double.parseDouble(getDataAsString("cross_section.km"));
-            }
-            catch (Exception e) {
-                ;
-            }
 
-            if (triple.size() == 0) {
-                logger.warn("Calculation of waterline is empty.");
-                return CrossSectionApp.createWaterLines(points, 0.0f);
-            }
-
-            // Linear seach in WQKms for closest km.
-            double old_dist_wish = Math.abs(wishKM - triple.getKm(0));
-            double last_w = triple.getW(0);
+        if (wqkms.length > 1) {
+            logger.warn("More than one wqkms found, taking first one.");
+        }
+        // Find W at km, linear naive approach.
+        WQKms triple = wqkms[0];
+        // Find index of km.
+        double wishKM = 0.0f;
+        int old_idx = 0;
+        try {
+            wishKM = Double.parseDouble(getDataAsString("cross_section.km"));
+        }
+        catch (Exception e) {
+            logger.warn(e);
+        }
 
-            for (int i = 0; i < triple.size(); i++) {
-                double diff = Math.abs(wishKM - triple.getKm(i));
-                if (diff > old_dist_wish) {
-                    break;
-                }
-                last_w = triple.getW(i);
-                old_dist_wish = diff;
+        if (triple.size() == 0) {
+            logger.warn("Calculation of waterline is empty.");
+            return Lines.createWaterLines(points, 0.0f);
+        }
+
+        // Linear seach in WQKms for closest km.
+        double old_dist_wish = Math.abs(wishKM - triple.getKm(0));
+        double last_w = triple.getW(0);
+
+        for (int i = 0, T = triple.size(); i < T; i++) {
+            double diff = Math.abs(wishKM - triple.getKm(i));
+            if (diff > old_dist_wish) {
+                break;
             }
-            return CrossSectionApp.createWaterLines(points, last_w);
+            last_w = triple.getW(i);
+            old_dist_wish = diff;
         }
+        return Lines.createWaterLines(points, last_w);
     }
 
 
@@ -836,35 +803,6 @@
 
 
     /**
-     * Get points of CrossSection Line.
-     * @param csl The crossSectionline of interest.
-     * @return x and y positions of cross section profile.
-     */
-    protected double [][] getCrossSectionProfile(CrossSectionLine csl) {
-        List<Point2D> points = getCrossSectionLinesPoints(csl);
-        double [] xs = new double[points.size()];
-        double [] ys = new double[points.size()];
-
-        if (points.isEmpty()) {
-            return new double [][] {xs, ys};
-        }
-
-        xs[0] = points.get(0).getX();
-        ys[0] = points.get(0).getY();
-
-        for (int i = 1; i < points.size(); i++) {
-            Point2D p = points.get(i);
-            xs[i] = p.getX();
-            if (xs[i] < xs[i-1]) {
-                xs[i] = xs[i-1] + CrossSectionApp.EPSILON;
-            }
-            ys[i] = p.getY(); 
-        }
-        return new double [][] { xs, ys };
-    }
-
-
-    /**
      * Returns the Qs for a number of Ws. This method makes use of
      * DischargeTables.getQForW().
      *
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/charts/CrossSectionApp.java	Tue Sep 27 12:40:42 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/charts/CrossSectionApp.java	Tue Sep 27 13:45:13 2011 +0000
@@ -18,13 +18,8 @@
 import java.awt.event.ActionEvent;
 
 import java.awt.geom.Point2D;
-import java.awt.geom.Line2D;
 
-import java.util.ArrayList;
 import java.util.List;
-import java.util.Comparator;
-import java.util.Collections;
-import java.util.Iterator;
 
 import java.io.File;
 import java.io.IOException;
@@ -59,8 +54,6 @@
 import org.hibernate.Session;
 import org.hibernate.Query;
 
-import gnu.trove.TDoubleArrayList;
-
 public class CrossSectionApp
 extends      ApplicationFrame
 {
@@ -68,30 +61,6 @@
 
     public static final double EPSILON   = 1e-4;
 
-    public static final double TOO_SMALL = 0.2;
-    public static final double TOO_BIG   = 500;
-
-    public static final Comparator<CrossSectionPoint> COL_POS_CMP =
-        new Comparator<CrossSectionPoint>() {
-            @Override
-            public int compare(CrossSectionPoint a, CrossSectionPoint b) {
-                // TODO evaluate: isnt it enough to
-                // return (|d| > |EPS|) ? d : diff
-                double xa = a.getX().doubleValue();
-                double xb = b.getX().doubleValue();
-                double d = xa - xb;
-                if (d < -EPSILON) return -1;
-                if (d > +EPSILON) return +1;
-                int diff = a.getColPos() - b.getColPos();
-                return diff < 0 ? -1 : diff > 0 ? +1 : 0;
-            }
-        };
-
-    public static final boolean isValid(double x) {
-        x = Math.abs(x);
-        return x > TOO_SMALL && x < TOO_BIG;
-    }
-
     protected Session session;
 
     protected JComboBox crossSectionsCB;
@@ -320,48 +289,14 @@
     ) {
         DefaultXYDataset dataset = new DefaultXYDataset();
 
-        List<CrossSectionPoint> ps = line.getPoints();
-
-        if (ps.isEmpty()) {
-            return dataset;
-        }
-
-        Collections.sort(ps, COL_POS_CMP);
-
-        List<Point2D> points = new ArrayList<Point2D>(ps.size());
-
-        for (CrossSectionPoint p: ps) {
-            double x = p.getX().doubleValue();
-            double y = p.getY().doubleValue();
-            if (isValid(x) && isValid(y)) {
-                points.add(new Point2D.Double(x, y));
-            }
-        }
+        List<Point2D> points = line.fetchCrossSectionLinesPoints();
 
         if (points.isEmpty()) {
             return dataset;
         }
 
-        double [] xs = new double[points.size()];
-        double [] ys = new double[xs.length];
-
-        xs[0] = points.get(0).getX();
-        ys[0] = points.get(0).getY();
-
-        for (int i = 1; i < xs.length; ++i) {
-            Point2D p = points.get(i);
-            double x = p.getX();
-            double y = p.getY();
-
-            if (x <= xs[i-1]) {
-                x = xs[i-1] + EPSILON;
-            }
-            xs[i] = x;
-            ys[i] = y;
-        }
-
         if (waterlevel != null) {
-            double [][] data = createWaterLines(points, waterlevel);
+            double [][] data = Lines.createWaterLines(points, waterlevel);
             dataset.addSeries(String.valueOf(waterlevel), data);
         }
 
@@ -370,37 +305,13 @@
         String legend = (cs != null ? cs.getDescription() : "???")
             + " " + Math.round(line.getKm().doubleValue() * 1000d)/1000d;
 
-        dataset.addSeries(legend, new double [][] { xs, ys });
+        double [][] values = CrossSectionLine.fetchCrossSectionProfile(points);
+
+        dataset.addSeries(legend, values);
 
         return dataset;
     }
 
-    public static double [][] createWaterLines(
-        List<Point2D> points,
-        double        waterlevel
-    ) {
-        List<Line2D> lines = Lines.fillWater(points, waterlevel);
-
-        TDoubleArrayList lxs = new TDoubleArrayList();
-        TDoubleArrayList lys = new TDoubleArrayList();
-
-        for (Iterator<Line2D> iter = lines.iterator(); iter.hasNext();) {
-            Line2D  l  = iter.next();
-            Point2D p1 = l.getP1();
-            Point2D p2 = l.getP2();
-            lxs.add(p1.getX());
-            lys.add(p1.getY());
-            lxs.add(p2.getX());
-            lys.add(p2.getY());
-            if (iter.hasNext()) {
-                lxs.add(Double.NaN);
-                lys.add(Double.NaN);
-            }
-        }
-
-        return new double [][] { lxs.toNativeArray(), lys.toNativeArray() };
-    }
-
     protected void updateCrossSection(CrossSection crossSection) {
         Object [] cslis = createCrossSectionLineItems(crossSection);
         DefaultComboBoxModel dcbm = new DefaultComboBoxModel(cslis);
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/geom/Lines.java	Tue Sep 27 12:40:42 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/geom/Lines.java	Tue Sep 27 13:45:13 2011 +0000
@@ -1,7 +1,8 @@
 package de.intevation.flys.geom;
 
+import java.util.ArrayList;
 import java.util.List;
-import java.util.ArrayList;
+import java.util.Iterator;
 
 import java.awt.geom.Point2D;
 import java.awt.geom.Line2D;
@@ -10,6 +11,8 @@
 
 import org.apache.log4j.Logger;
 
+import gnu.trove.TDoubleArrayList;
+
 public class Lines
 {
     private static Logger log = Logger.getLogger(Lines.class);
@@ -247,5 +250,31 @@
 
         return result;
     }
+
+    public static double [][] createWaterLines(
+        List<Point2D> points,
+        double        waterlevel
+    ) {
+        List<Line2D> lines = fillWater(points, waterlevel);
+
+        TDoubleArrayList lxs = new TDoubleArrayList();
+        TDoubleArrayList lys = new TDoubleArrayList();
+
+        for (Iterator<Line2D> iter = lines.iterator(); iter.hasNext();) {
+            Line2D  l  = iter.next();
+            Point2D p1 = l.getP1();
+            Point2D p2 = l.getP2();
+            lxs.add(p1.getX());
+            lys.add(p1.getY());
+            lxs.add(p2.getX());
+            lys.add(p2.getY());
+            if (iter.hasNext()) {
+                lxs.add(Double.NaN);
+                lys.add(Double.NaN);
+            }
+        }
+
+        return new double [][] { lxs.toNativeArray(), lys.toNativeArray() };
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org