changeset 429:bed9735adf84

Finished preprocessing data for interpolation in verticalcrosssection charts.ß gnv-artifacts/trunk@477 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 22 Dec 2009 17:19:10 +0000
parents 88cd37c3b5e4
children 23e9352df09b
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/math/AttributedXYColumns.java gnv-artifacts/src/main/java/de/intevation/gnv/math/HeightValue.java gnv-artifacts/src/main/java/de/intevation/gnv/math/XYColumn.java gnv-artifacts/src/main/java/de/intevation/gnv/state/OutputStateBase.java gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileOutputState.java gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/vertical/VerticalProfileOutputState.java gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java
diffstat 12 files changed, 306 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Tue Dec 22 13:18:07 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Tue Dec 22 17:19:10 2009 +0000
@@ -1,3 +1,29 @@
+2009-12-22  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java:
+	  Preprocessing of data used for interpolation.
+
+	* src/main/java/de/intevation/gnv/math/AttributedXYColumns.java: Data
+	  wrapper. Contains preprocessed data used for interpolation and some
+	  metadata used for chart creation.
+
+	* src/main/java/de/intevation/gnv/math/XYColumn.java,
+	  src/main/java/de/intevation/gnv/math/HeightValue.java: Made them
+	  serializable and added hashCode() and equals() to XYColumn to use it as
+	  key in HashMaps.
+
+	* src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java,
+	  src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java,
+	  src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileOutputState.java,
+	  src/main/java/de/intevation/gnv/state/profile/vertical/VerticalProfileOutputState.java,
+	  src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java,
+	  src/main/java/de/intevation/gnv/state/OutputStateBase.java: Changed some 
+	  method signatures.
+
+	* src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java:
+	  Improved error handling. Return empty statistic set if there are no input
+	  data.
+
 2009-12-22  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	* doc/conf/products/verticalcrosssection/conf_mesh.xml: Adjusted
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/math/AttributedXYColumns.java	Tue Dec 22 17:19:10 2009 +0000
@@ -0,0 +1,43 @@
+package de.intevation.gnv.math;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.io.Serializable;
+
+public class AttributedXYColumns
+implements   Serializable
+{
+    protected XYColumn[] columns;
+    protected Map        attributes;
+
+    public AttributedXYColumns() {
+    }
+
+    public AttributedXYColumns(XYColumn[] columns) {
+        this(columns, null);
+    }
+
+    public AttributedXYColumns(XYColumn[] columns, Map attributes) {
+        this.columns    = columns;
+        this.attributes = attributes;
+    }
+
+    public Object getAttribute(Object key) {
+        return attributes.get(key);
+    }
+
+    public void setAttribute(Object key, Object value) {
+        if (attributes == null)
+            attributes = new HashMap();
+
+        attributes.put(key, value);
+    }
+
+    public XYColumn[] getXYColumns() {
+        return columns;
+    }
+
+    public void setXYColumns(XYColumn[] columns) {
+        this.columns = columns;
+    }
+}
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/math/HeightValue.java	Tue Dec 22 13:18:07 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/math/HeightValue.java	Tue Dec 22 17:19:10 2009 +0000
@@ -1,10 +1,13 @@
 package de.intevation.gnv.math;
 
+import java.io.Serializable;
+
 /**
  * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
  */
-public class HeightValue {
-
+public class HeightValue
+implements   Serializable
+{
     private double z;
     private double v;
     private int    k;
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/math/XYColumn.java	Tue Dec 22 13:18:07 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/math/XYColumn.java	Tue Dec 22 17:19:10 2009 +0000
@@ -1,12 +1,15 @@
 package de.intevation.gnv.math;
 
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.List;
 
 /**
  * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
  */
-public class XYColumn {
+public class XYColumn
+implements   Serializable
+{
 
     private double x;
     private double y;
@@ -21,12 +24,13 @@
         this.y = y;
         this.i = i;
         this.j = j;
-
-        values = new ArrayList();
     }
 
 
     public void add(HeightValue value) {
+        if (values == null) {
+            values = new ArrayList();
+        }
         values.add(value);
     }
 
@@ -34,4 +38,16 @@
     public HeightValue[] getValues() {
         return (HeightValue[]) values.toArray(new HeightValue[values.size()]);
     }
+
+
+    public int hashCode() {
+        return (i << 16) | j;
+    }
+
+
+    public boolean equals(Object obj) {
+        XYColumn other = (XYColumn) obj;
+
+        return i == other.i && j == other.j && x == other.x && y == other.y;
+    }
 }
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/OutputStateBase.java	Tue Dec 22 13:18:07 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/OutputStateBase.java	Tue Dec 22 17:19:10 2009 +0000
@@ -142,7 +142,7 @@
     /**
      * @return
      */
-    protected Collection<Result> getChartResult(String uuid) {
+    protected Object getChartResult(String uuid) {
         log.debug("OutputStateBase.getChartResult");
         Collection<Result> result = null;
         if (CacheFactory.getInstance().isInitialized()) {
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java	Tue Dec 22 13:18:07 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java	Tue Dec 22 17:19:10 2009 +0000
@@ -88,7 +88,7 @@
         Collection   parameters,
         Collection   measurements,
         Collection   dates,
-        Collection   result,
+        Object       result,
         Locale       locale,
         String       uuid,
         boolean      linesVisible,
@@ -110,7 +110,7 @@
             parameters,
             measurements,
             dates,
-            result,
+            (Collection)result,
             null,
             locale,
             linesVisible,
@@ -127,7 +127,7 @@
     }
 
     @Override
-    protected Collection<Result> getChartResult(String uuid) {
+    protected Object getChartResult(String uuid) {
         log.debug("HorizontalProfileMeshCrossOutputState.getChartResult");
         Collection<Result> result = null;
         if (CacheFactory.getInstance().isInitialized()) {
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileOutputState.java	Tue Dec 22 13:18:07 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileOutputState.java	Tue Dec 22 17:19:10 2009 +0000
@@ -121,7 +121,7 @@
         Collection   parameters,
         Collection   measurements,
         Collection   dates,
-        Collection   result,
+        Object       result,
         Locale       locale,
         String       uuid,
         boolean      linesVisible,
@@ -144,7 +144,7 @@
             parameters,
             measurements,
             dates,
-            result,
+            (Collection)result,
             null,
             locale,
             linesVisible,
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java	Tue Dec 22 13:18:07 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java	Tue Dec 22 17:19:10 2009 +0000
@@ -53,7 +53,7 @@
         Collection   parameters,
         Collection   measurements,
         Collection   dates,
-        Collection   result,
+        Object       result,
         Locale       locale,
         String       uuid,
         boolean      linesVisible,
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/vertical/VerticalProfileOutputState.java	Tue Dec 22 13:18:07 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/vertical/VerticalProfileOutputState.java	Tue Dec 22 17:19:10 2009 +0000
@@ -93,7 +93,7 @@
         Collection  parameters,
         Collection  measurements,
         Collection  dates,
-        Collection  result,
+        Object      result,
         Locale      locale,
         String      uuid,
         boolean     linesVisible,
@@ -116,7 +116,7 @@
             parameters,
             measurements,
             dates,
-            result,
+            (Collection)result,
             null,
             locale,
             linesVisible,
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java	Tue Dec 22 13:18:07 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java	Tue Dec 22 17:19:10 2009 +0000
@@ -8,29 +8,47 @@
 import java.io.OutputStreamWriter;
 import java.io.UnsupportedEncodingException;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Locale;
+import java.util.Map;
 
 import org.apache.log4j.Logger;
 
 import org.jfree.chart.ChartTheme;
 
+import org.w3c.dom.Node;
+
+import net.sf.ehcache.Element;
+
 import au.com.bytecode.opencsv.CSVWriter;
 
 import com.vividsolutions.jts.geom.Point;
+import com.vividsolutions.jts.geom.Coordinate;
+import com.vividsolutions.jts.geom.LineString;
 import com.vividsolutions.jts.io.ParseException;
 import com.vividsolutions.jts.io.WKTReader;
 
+import de.intevation.artifactdatabase.Config;
+import de.intevation.gnv.artifacts.cache.CacheFactory;
 import de.intevation.gnv.chart.Chart;
 import de.intevation.gnv.chart.ChartLabels;
 import de.intevation.gnv.chart.ChartStyle;
 import de.intevation.gnv.chart.exception.TechnicalChartException;
 import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.geobackend.base.ResultDescriptor;
+import de.intevation.gnv.geobackend.base.query.QueryExecutor;
+import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
+import de.intevation.gnv.geobackend.base.query.exception.QueryException;
+import de.intevation.gnv.math.AttributedXYColumns;
+import de.intevation.gnv.math.HeightValue;
+import de.intevation.gnv.math.XYColumn;
 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
 import de.intevation.gnv.state.exception.StateException;
 import de.intevation.gnv.state.timeseries.TimeSeriesOutputState;
 import de.intevation.gnv.statistics.Statistics;
 import de.intevation.gnv.statistics.VerticalProfileStatistics;
+import de.intevation.gnv.utils.WKTUtils;
 
 /**
  * @author Tim Englich <tim.englich@intevation.de>
@@ -38,9 +56,20 @@
  */
 public class VerticalCrossSectionOutputState extends TimeSeriesOutputState {
 
-    private static Logger log = Logger
-    .getLogger(VerticalCrossSectionOutputState.class);
-    
+    public static final String[] ATTRIBUTE_LIST = {
+        "SHAPE",
+        "Z",
+        "YORDINATE",
+        "IPOSITION",
+        "JPOSITION",
+        "KPOSITION"
+    };
+
+    private static Logger log = Logger.getLogger(
+        VerticalCrossSectionOutputState.class);
+
+    private String ijkQueryID = "horizontalprofile_meshpoint_cross_ij";
+
     /**
      * The UID of this Class
      */
@@ -54,6 +83,155 @@
         super.domainLable = "chart.verticalcrosssection.title.xaxis";
     }
 
+
+    @Override
+    protected Object getChartResult(String uuid) {
+        log.debug("VerticalCrossSectionOutputState.getChartResult");
+        Collection<Result> result = null;
+        String key = uuid + super.getID();
+
+        Element element = CacheFactory.getInstance().getCache().get(key);
+        if (element != null)
+            return element.getObjectValue();
+
+        log.debug("No results in cache yet.");
+        if (inputData.containsKey("mesh_linestring")) {
+
+            try {
+                LineString ls = (LineString) new WKTReader().read(
+                    inputData.get("mesh_linestring").getValue());
+
+                Coordinate[] coords = ls.getCoordinates();
+
+                String additionWhere = WKTUtils.worldCoordinatesToIndex(
+                    result,
+                    inputData,
+                    ijkQueryID
+                );
+
+
+                String[] filterValues =
+                    generateFilterValuesFromInputData();
+                String[] addedFilterValues =
+                    new String[filterValues.length + 1];
+
+                System.arraycopy(
+                    filterValues, 0,
+                    addedFilterValues, 0,
+                    filterValues.length
+                );
+                addedFilterValues[filterValues.length] = additionWhere;
+
+                QueryExecutor exec = QueryExecutorFactory
+                    .getInstance()
+                    .getQueryExecutor();
+
+                result = exec.executeQuery(queryID, addedFilterValues);
+            }
+            catch (ParseException pe) {
+                log.error(pe, pe);
+            }
+            catch (QueryException qe) {
+                log.error(qe, qe);
+            }
+        }
+        else {
+            // TODO What should happen if there is no linestring?
+            log.warn("No linestring in inputData.");
+        }
+
+        Object obj = process(preProcess(result));
+        CacheFactory.getInstance().getCache().put(new Element(key, obj));
+
+        return obj;
+    }
+
+
+    protected Object process(AttributedXYColumns columns) {
+
+        // TODO Implement me
+        return null;
+    }
+
+
+    protected AttributedXYColumns preProcess(Collection results) {
+        AttributedXYColumns attColumns = new AttributedXYColumns();
+        Map        map                 = new HashMap();
+        Iterator   iter                = results.iterator();
+
+        int sIdx = -1;
+        int iIdx = -1;
+        int jIdx = -1;
+        int kIdx = -1;
+        int vIdx = -1;
+        int zIdx = -1;
+
+        while (iter.hasNext()) {
+            Result result = (Result) iter.next();
+
+            if (sIdx == -1) {
+                ResultDescriptor rd = result.getResultDescriptor();
+                int columnCount     = rd.getColumnCount();
+
+                sIdx = rd.getColumnIndex("SHAPE");
+                iIdx = rd.getColumnIndex("IPOSITION");
+                jIdx = rd.getColumnIndex("JPOSITION");
+                kIdx = rd.getColumnIndex("KPOSITION");
+                vIdx = rd.getColumnIndex("YORDINATE");
+                zIdx = rd.getColumnIndex("Z");
+
+                for (int i = 0; i < columnCount; i++) {
+                    String colName = rd.getColumnName(i);
+
+                    if (!attributeInList(colName)) {
+                        attColumns.setAttribute(
+                            colName,
+                            result.getObject(colName));
+                    }
+                }
+            }
+
+            try {
+                Point point  = (Point) new WKTReader().read(
+                    result.getString(sIdx));
+                double v     = result.getDouble(vIdx);
+                int i        = result.getInteger(iIdx);
+                int j        = result.getInteger(jIdx);
+                int k        = result.getInteger(kIdx);
+                int z        = result.getInteger(zIdx);
+
+                XYColumn col = new XYColumn(point.getX(), point.getY(), i, j);
+                XYColumn old = (XYColumn)map.get(col);
+
+                if (old == null) {
+                    map.put(old = col, col);
+                }
+
+                old.add(new HeightValue(z, v, k));
+            }
+            catch (ParseException pe) {
+                log.warn("Error while parsing geometry.", pe);
+            }
+        }
+
+        XYColumn[] cols = (XYColumn[])map.values().toArray(
+            new XYColumn[map.size()]);
+        attColumns.setXYColumns(cols);
+
+        return attColumns;
+    }
+
+
+    protected boolean attributeInList(String name) {
+        for (int i = 0; i < ATTRIBUTE_LIST.length; i++) {
+            if (name.equals(ATTRIBUTE_LIST[i]))
+                return true;
+        }
+
+        return false;
+    }
+
+
     @Override
     protected Chart getChart(
         ChartLabels  chartLables,
@@ -61,7 +239,7 @@
         Collection   parameters,
         Collection   measurements,
         Collection   dates,
-        Collection   result,
+        Object       result,
         Locale       locale,
         String       uuid,
         boolean      linesVisible,
@@ -78,7 +256,7 @@
             return chart;
 
         log.info("Chart not in cache yet.");
-        
+
         log.warn("This sort of chart is not implemented yet.");
         /* TODO Implement a special chart for this sort of charts.
         chart = new VerticalProfileChart(
@@ -129,7 +307,7 @@
     protected Statistics getStatisticsGenerator() {
         return new VerticalProfileStatistics();
     }
-    
+
     /**
      * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#createCSV(java.io.OutputStream, java.util.Collection)
      */
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java	Tue Dec 22 13:18:07 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java	Tue Dec 22 17:19:10 2009 +0000
@@ -353,12 +353,14 @@
             }
             else if (outputMode.equalsIgnoreCase("csv")) {
                 log.debug("CSV-File will be generated.");
-                Collection<Result> chartResult = this.getChartResult(uuid);
+                Collection<Result> chartResult =
+                    (Collection<Result>) getChartResult(uuid);
                 this.createCSV(outputStream, chartResult);
             } else if (outputMode.equalsIgnoreCase("statistics")) {
                 log.debug("Statistics will be generated.");
                 Statistics s = getStatisticsGenerator();
-                Collection<Result> chartResult = this.getChartResult(uuid);
+                Collection<Result> chartResult =
+                    (Collection<Result>) getChartResult(uuid);
                 Collection<KeyValueDescibeData> parameters = 
                                                 this.getParameters(uuid);
                 Collection<KeyValueDescibeData> measurements = 
@@ -629,7 +631,7 @@
             parameters,
             measurements,
             dates,
-            getChartResult(uuid),
+            (Collection) getChartResult(uuid),
             locale,
             uuid,
             linesVisible,
@@ -684,7 +686,7 @@
             parameters,
             measurements,
             dates,
-            getChartResult(uuid),
+            (Collection) getChartResult(uuid),
             locale,
             uuid,
             linesVisible,
@@ -713,7 +715,7 @@
         Collection   parameters,
         Collection   measurements,
         Collection   dates,
-        Collection   result,
+        Object       result,
         Locale       locale,
         String       uuid,
         boolean      linesVisible,
@@ -736,7 +738,7 @@
             parameters,
             measurements,
             dates,
-            result,
+            (Collection)result,
             timeGapDefinitions,
             locale,
             linesVisible,
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java	Tue Dec 22 13:18:07 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java	Tue Dec 22 17:19:10 2009 +0000
@@ -60,19 +60,24 @@
      * @see de.intevation.gnv.statistics.Statistics#calculateStatistics(java.util.Collection, java.util.Collection, java.util.Collection, java.util.Collection)
      */
     public Collection<StatisticSet> calculateStatistics(
-                                                     Collection<Result> resultSet,
-                                                     Collection<KeyValueDescibeData> parameters,
-                                                     Collection<KeyValueDescibeData> measurements,
-                                                     Collection<KeyValueDescibeData> dates
-                                                     )
-                                                     throws StatisticsException {
-        this.clearStatistics();
+        Collection<Result>              resultSet,
+        Collection<KeyValueDescibeData> parameters,
+        Collection<KeyValueDescibeData> measurements,
+        Collection<KeyValueDescibeData> dates
+    )
+    throws StatisticsException {
+
+        clearStatistics();
         DescriptiveStatistics lStatistics = null;
         SimpleRegression lRegression = null;
         Collection<StatisticSet> statisticSets = new ArrayList<StatisticSet>();
         String break1, break2, break3;
         int lSeries = 1;
 
+        if (resultSet == null) {
+            return statisticSets;
+        }
+
         int b1Idx = -1;
         int b2Idx = -1;
         int b3Idx = -1;

http://dive4elements.wald.intevation.org