diff gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java @ 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 22229249e9fc
children 422275fc9927
line wrap: on
line diff
--- 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)
      */

http://dive4elements.wald.intevation.org