changeset 440:eb2ac62e853a

Cleaned up messy code a bit in "Horizontales Schnittprofil" and "Profilschnitt". gnv-artifacts/trunk@488 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 28 Dec 2009 17:13:12 +0000
parents 8975de9d7483
children 31a12ff53f99
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/math/XYColumn.java gnv-artifacts/src/main/java/de/intevation/gnv/raster/IsoPolygonSeriesProducer.java gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.java
diffstat 6 files changed, 128 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Mon Dec 28 16:24:05 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Mon Dec 28 17:13:12 2009 +0000
@@ -1,3 +1,15 @@
+2009-12-28	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java,
+	  src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java,
+	  src/main/java/de/intevation/gnv/utils/WKTUtils.java: Refactored most 
+	  silly code like inefficent access to input data and redundant 
+	  WKT line string parsing.
+
+	* src/main/java/de/intevation/gnv/raster/IsoPolygonSeriesProducer.java,
+	  src/main/java/de/intevation/gnv/math/XYColumn.java:
+	  Removed needless imports.
+
 2009-12-28	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/gnv/state/OutputStateBase.java,
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/math/XYColumn.java	Mon Dec 28 16:24:05 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/math/XYColumn.java	Mon Dec 28 17:13:12 2009 +0000
@@ -7,8 +7,6 @@
 import org.apache.commons.math.analysis.interpolation.SplineInterpolator;
 import org.apache.commons.math.analysis.interpolation.UnivariateRealInterpolator;
 
-import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
-
 import org.apache.commons.math.analysis.UnivariateRealFunction;
 
 import org.apache.commons.math.MathException;
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/raster/IsoPolygonSeriesProducer.java	Mon Dec 28 16:24:05 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/raster/IsoPolygonSeriesProducer.java	Mon Dec 28 17:13:12 2009 +0000
@@ -1,7 +1,6 @@
 package de.intevation.gnv.raster;
 
 import java.util.List;
-import java.util.Map;
 import java.util.HashSet;
 import java.util.HashMap;
 import java.util.ArrayList;
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java	Mon Dec 28 16:24:05 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java	Mon Dec 28 17:13:12 2009 +0000
@@ -12,8 +12,6 @@
 import org.apache.log4j.Logger;
 import org.w3c.dom.Node;
 
-import com.vividsolutions.jts.io.ParseException;
-
 import de.intevation.artifactdatabase.Config;
 
 import de.intevation.gnv.artifacts.cache.CacheFactory;
@@ -32,6 +30,8 @@
 import de.intevation.gnv.chart.ChartLabels;
 import de.intevation.gnv.chart.HorizontalCrossProfileChart;
 
+import de.intevation.gnv.state.InputData;
+
 import de.intevation.gnv.utils.DistanceCalculator;
 import de.intevation.gnv.utils.WKTUtils;
 import de.intevation.gnv.utils.StringUtils;
@@ -41,8 +41,6 @@
 import org.jfree.chart.ChartTheme;
 
 import com.vividsolutions.jts.geom.Coordinate;
-import com.vividsolutions.jts.geom.LineString;
-import com.vividsolutions.jts.io.WKTReader;
 
 /**
  * @author Tim Englich <tim.englich@intevation.de>
@@ -141,48 +139,50 @@
                 result = (Collection<Result>) (value.getObjectValue());
             }else{
 
-                if (this.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
-                        );
+                InputData meshLine = inputData.get("mesh_linestring");
+                InputData meshId   = inputData.get("meshid");
 
-                        String[] filterValues =
-                            generateFilterValuesFromInputData();
-                        String[] addedFilterValues =
-                            new String[filterValues.length+1];
-                        System.arraycopy(
-                            filterValues, 0,
-                            addedFilterValues, 0,
-                            filterValues.length);
-                        addedFilterValues[filterValues.length] = additionWhere;
+                if (meshLine == null) {
+                    log.error("mesh_linestring is not defined");
+                    throw new IllegalStateException("missing mesh_linestring");
+                }
 
-                        QueryExecutor queryExecutor = QueryExecutorFactory
-                            .getInstance()
-                            .getQueryExecutor();
+                if (meshId == null) {
+                    log.error("meshid is not defined");
+                    throw new IllegalStateException("missing meshid");
+                }
 
-                        result = process(
-                            Arrays.asList(coords),
-                            queryExecutor.executeQuery(
-                            queryID,
-                            addedFilterValues)
-                        );
-                    } catch (ParseException e) {
-                        log.error(e,e);
-                    }catch (QueryException e) {
-                        log.error(e,e);
-                    }
-                }else{
-                    // TODO: definieren was passiert wenn kein linestring vorhanden ist.
+                Coordinate [] coords = WKTUtils.toCoordinates(
+                    meshLine.getValue());
+
+                if (coords == null) {
+                    throw new IllegalStateException("cannot read coordinates");
+                }
+
+                try {
+                    String additionWhere =
+                        WKTUtils.worldCoordinatesToIndex(
+                            coords,
+                            result,
+                            meshId.getValue(),
+                            ijkQueryID);
+
+                    String[] addedFilterValues = StringUtils.append(
+                        generateFilterValuesFromInputData(),
+                        additionWhere);
+
+                    QueryExecutor queryExecutor = QueryExecutorFactory
+                        .getInstance()
+                        .getQueryExecutor();
+
+                    result = process(
+                        Arrays.asList(coords),
+                        queryExecutor.executeQuery(
+                        queryID,
+                        addedFilterValues));
+                }
+                catch (QueryException e) {
+                    log.error(e,e);
                 }
 
                 if (CacheFactory.getInstance().isInitialized()) {
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java	Mon Dec 28 16:24:05 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java	Mon Dec 28 17:13:12 2009 +0000
@@ -14,6 +14,8 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 
 import org.apache.log4j.Logger;
 
@@ -25,9 +27,6 @@
 
 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.artifacts.CallContext;
 
@@ -51,6 +50,8 @@
 import de.intevation.gnv.math.XYColumn;
 import de.intevation.gnv.math.IJKey;
 
+import de.intevation.gnv.state.InputData;
+
 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
 import de.intevation.gnv.state.exception.StateException;
 import de.intevation.gnv.state.timeseries.TimeSeriesOutputState;
@@ -106,59 +107,64 @@
             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();
+        InputData meshLine = inputData.get("mesh_linestring");
+        InputData meshId   = inputData.get("meshid");
 
-                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.");
+        if (meshLine == null) {
+            log.error("mesh_linestring is not defined");
+            throw new IllegalStateException("missing mesh_linestring");
         }
 
-        Object obj = process(preProcess(result));
+        if (meshId == null) {
+            log.error("meshid is not defined");
+            throw new IllegalStateException("missing meshid");
+        }
+
+        Coordinate [] coords = WKTUtils.toCoordinates(
+            meshLine.getValue());
+
+        if (coords == null) {
+            throw new IllegalStateException("cannot read coordinates");
+        }
+
+        try {
+            String additionWhere = WKTUtils.worldCoordinatesToIndex(
+                coords,
+                result,
+                meshId.getValue(),
+                ijkQueryID);
+
+            String[] addedFilterValues = StringUtils.append(
+                generateFilterValuesFromInputData(),
+                additionWhere);
+
+            QueryExecutor exec = QueryExecutorFactory
+                .getInstance()
+                .getQueryExecutor();
+
+            result = exec.executeQuery(queryID, addedFilterValues);
+        }
+        catch (QueryException qe) {
+            log.error(qe, qe);
+        }
+
+        Object obj = process(
+            Arrays.asList(coords),
+            preProcess(result),
+            callContext);
+
         CacheFactory.getInstance().getCache().put(new Element(key, obj));
 
         return obj;
     }
 
 
-    protected Object process(AttributedXYColumns columns) {
+    protected Object process(
+        List<Coordinate>    path,
+        AttributedXYColumns columns,
+        CallContext         callContext
+    ) {
 
         // TODO Implement me
         return null;
@@ -230,16 +236,6 @@
     }
 
 
-    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,
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.java	Mon Dec 28 16:24:05 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.java	Mon Dec 28 17:13:12 2009 +0000
@@ -10,13 +10,12 @@
 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.LinearFunction;
-import de.intevation.gnv.state.InputData;
 
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
-import java.util.Map;
 
 import org.apache.commons.math.optimization.OptimizationException;
 import org.apache.commons.math.optimization.fitting.CurveFitter;
@@ -84,20 +83,17 @@
 
 
     public static String worldCoordinatesToIndex(
-        Collection<Result>     result,
-        Map<String, InputData> inputData,
-        String                 ijkQueryID
-    ) throws ParseException, QueryException
+		Coordinate []       coords,
+        Collection<Result>  result,
+		String              meshid,
+        String              ijkQueryID
+    )
+	throws QueryException
     {
         // 1. IJK Anfragen für Stuetzpunkte im Netz ausführen.
-        LineString ls = (LineString)new WKTReader().read(
-            inputData.get("mesh_linestring").getValue());
-
-        Coordinate[] coords = ls.getCoordinates();
 
         List<java.awt.Point> points = new ArrayList<java.awt.Point>(coords.length);
 
-        String meshid = inputData.get("meshid").getValue();
         QueryExecutor queryExecutor = QueryExecutorFactory
                                         .getInstance()
                                         .getQueryExecutor();
@@ -189,4 +185,18 @@
 
         return additionWhere;
     }
+
+	public static Coordinate [] toCoordinates(String wkt) {
+		try {
+			LineString ls = (LineString)new WKTReader().read(wkt);
+			return ls.getCoordinates();
+		}
+		catch (ParseException pe) {
+			log.error("cannot read WKT line string", pe);
+		}
+		catch (ClassCastException cce) {
+			log.error("cannot read WKT line string", cce);
+		}
+		return null;
+	}
 }

http://dive4elements.wald.intevation.org