diff gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java @ 468:7ba4c7222265

Added ij-Index determination for horizontal-cross-sections. gnv-artifacts/trunk@531 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 12 Jan 2010 11:34:25 +0000
parents 3ddc22aab764
children 06887e2e3f7a
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java	Tue Jan 12 08:49:51 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java	Tue Jan 12 11:34:25 2010 +0000
@@ -7,23 +7,40 @@
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Locale;
 
 import org.apache.log4j.Logger;
 
 import org.jfree.chart.ChartTheme;
+import org.w3c.dom.Node;
+
+import com.vividsolutions.jts.geom.Coordinate;
+import com.vividsolutions.jts.geom.Envelope;
+import com.vividsolutions.jts.geom.Polygon;
+import com.vividsolutions.jts.io.ParseException;
+import com.vividsolutions.jts.io.WKTReader;
 
 import au.com.bytecode.opencsv.CSVWriter;
+import de.intevation.gnv.artifacts.cache.CacheFactory;
+import de.intevation.gnv.artifacts.context.GNVArtifactContext;
 import de.intevation.gnv.chart.Chart;
 import de.intevation.gnv.chart.ChartLabels;
 import de.intevation.gnv.geobackend.base.Result;
+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.state.InputData;
 import de.intevation.gnv.state.exception.StateException;
 import de.intevation.gnv.state.timeseries.TimeSeriesOutputState;
 import de.intevation.gnv.statistics.Statistics;
+import de.intevation.gnv.utils.StringUtils;
+import de.intevation.gnv.utils.WKTUtils;
 
+import de.intevation.artifactdatabase.Config;
 import de.intevation.artifacts.CallContext;
 
 /**
@@ -40,6 +57,8 @@
      * The UID of this Class
      */
     private static final long serialVersionUID = 3233620652465061860L;
+    
+    private String ijkQueryID = null;
 
     /**
      * Constructor
@@ -104,7 +123,92 @@
 
         return chart;
     }
+    
+    /**
+     * @see de.intevation.gnv.state.OutputStateBase#getChartResult(java.lang.String, de.intevation.artifacts.CallContext)
+     */
+    @Override
+    protected Object getChartResult(String uuid, CallContext callContext) {
+        log.debug("HorizontalProfileMeshCrossOutputState.getChartResult");
+        Collection<Result> result = null;
+        if (CacheFactory.getInstance().isInitialized()) {
+            String key = uuid + super.getID();
+            log.debug("Hash for Queryelements: " + key);
+            net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key);
+            if (value != null) {
+                result = (Collection<Result>) (value.getObjectValue());
+            }else{
 
+                InputData meshPolygon = inputData.get("mesh_polygon");
+                InputData meshId   = inputData.get("meshid");
+
+                if (meshPolygon == null) {
+                    log.error("mesh_polygon is not defined");
+                    throw new IllegalStateException("missing mesh_linestring");
+                }
+
+                if (meshId == null) {
+                    log.error("meshid is not defined");
+                    throw new IllegalStateException("missing meshid");
+                }
+                try {
+                    // TODO: Refactore in own Method to WKTUtils?
+                    Polygon p = (Polygon)new WKTReader().read(meshPolygon.getValue());
+                    Envelope env  = p.getEnvelopeInternal();
+                    Coordinate[] coords = new Coordinate[4];
+                    coords[0] = new Coordinate(env.getMinX(), env.getMinY());
+                    coords[1] = new Coordinate(env.getMinX(), env.getMaxY());
+                    coords[2] = new Coordinate(env.getMaxX(), env.getMaxY());
+                    coords[2] = new Coordinate(env.getMaxX(), env.getMinY());
+
+                    String additionWhere =
+                        WKTUtils.worldEnvelopeCoordinatesToIndex(
+                            coords,
+                            result,
+                            meshId.getValue(),
+                            ijkQueryID);
+
+                    String[] addedFilterValues = StringUtils.append(
+                        generateFilterValuesFromInputData(),
+                        additionWhere);
+
+                    QueryExecutor queryExecutor = QueryExecutorFactory
+                        .getInstance()
+                        .getQueryExecutor();
+
+                    result = process(
+                        Arrays.asList(coords),
+                        numSamples(callContext),
+                        queryExecutor.executeQuery(
+                        queryID,
+                        addedFilterValues),
+                        p);
+                } catch (QueryException e) {
+                    log.error(e,e);
+                } catch (ParseException e) {
+                    log.error(e,e);
+                }
+
+                if (CacheFactory.getInstance().isInitialized()) {
+                    CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, result));
+                }
+
+            }
+        }
+        return result;
+        
+        
+    }
+    
+    public static Collection<Result> process(
+            List<Coordinate>   path,
+            int                numSamples,
+            Collection<Result> input,
+            Polygon polygon
+        ) {
+        // TODO: IMPLEMENT ME INTEGRATE POLYGONCLIPPING
+        return null;
+    }
 
     /**
      * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#getStatisticsGenerator()
@@ -154,5 +258,25 @@
                     "No Data given for generating an CSV-File.");
         }
     }
+    
+    /**
+     * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#setup(org.w3c.dom.Node)
+     */
+    @Override
+    public void setup(Node configuration) {
+        super.setup(configuration);
+        this.ijkQueryID = Config.getStringXPath(configuration,"queryID-ijk");
+        
+    }
+    
+    private static int numSamples(CallContext callContext) {
+        GNVArtifactContext context =
+            (GNVArtifactContext)callContext.globalContext();
+        Integer samples = (Integer)context.get(
+            GNVArtifactContext.HORIZONTAL_CROSS_SECTION_SAMPLES_KEY);
+        return samples != null
+            ? samples.intValue()
+            : GNVArtifactContext.DEFAULT_HORIZONTAL_CROSS_SECTION_SAMPLES;
+    }
 
 }

http://dive4elements.wald.intevation.org