diff gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java @ 423:2402173a1490

Moved some methods back to old place. gnv-artifacts/trunk@471 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 21 Dec 2009 17:03:10 +0000
parents c6a287398379
children 15b8e95fa8da
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java	Mon Dec 21 16:47:45 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java	Mon Dec 21 17:03:10 2009 +0000
@@ -3,7 +3,10 @@
  */
 package de.intevation.gnv.state.profile.horizontal;
 
+import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
 import java.util.Locale;
 
 import org.apache.log4j.Logger;
@@ -15,18 +18,30 @@
 
 import de.intevation.gnv.artifacts.cache.CacheFactory;
 
+import de.intevation.gnv.geobackend.base.DefaultResult;
+import de.intevation.gnv.geobackend.base.DefaultResultDescriptor;
+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.Interpolation2D;
+import de.intevation.gnv.math.LinearMetrics;
+import de.intevation.gnv.math.Point2d;
 import de.intevation.gnv.chart.Chart;
 import de.intevation.gnv.chart.ChartLabels;
 import de.intevation.gnv.chart.HorizontalCrossProfileChart;
 
-import de.intevation.gnv.geobackend.base.Result;
-
-import de.intevation.gnv.geobackend.base.query.exception.QueryException;
-
+import de.intevation.gnv.utils.DistanceCalculator;
 import de.intevation.gnv.utils.WKTUtils;
 
 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>
  *
@@ -122,16 +137,41 @@
             if (value != null) {
                 result = (Collection<Result>) (value.getObjectValue());
             }else{
-                
+
                 if (this.inputData.containsKey("mesh_linestring")){
-                    
+
                     try {
-                        result = WKTUtils.worldCoordinatesToIndex(
-                            result,
-                            inputData,
-                            ijkQueryID,
+                        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 queryExecutor = QueryExecutorFactory
+                            .getInstance()
+                            .getQueryExecutor();
+
+                        result = process(
+                            Arrays.asList(coords),
+                            queryExecutor.executeQuery(
                             queryID,
-                            generateFilterValuesFromInputData()
+                            addedFilterValues)
                         );
                     } catch (ParseException e) {
                         log.error(e,e);
@@ -141,11 +181,11 @@
                 }else{
                     // TODO: definieren was passiert wenn kein linestring vorhanden ist.
                 }
-                
+
                 if (CacheFactory.getInstance().isInitialized()) {
                     CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, result));
                 }
-                
+
             }
         }
         return result;
@@ -159,4 +199,190 @@
 
         return subtitle;
     }
+
+
+    public static Collection<Result> process(
+        List<Coordinate>   path,
+        Collection<Result> input
+    ) {
+        log.debug("------  number of points before processing: " + input.size());
+        ArrayList<Result> output = new ArrayList<Result>();
+
+        Result last = null;
+
+        int [] diffColums = null;
+
+        SectionHandler sectionHandler = null;
+
+        for (Result result: input) {
+
+            if (sectionHandler == null) {
+
+                ResultDescriptor rd = result.getResultDescriptor();
+                diffColums = rd.getColumnIndices(DIFF_COLUMS);
+                int columns = rd.getColumnCount();
+
+                DefaultResultDescriptor resultDescriptor =
+                    new DefaultResultDescriptor();
+
+                for (int j = 0; j < columns; ++j) {
+                    String columnName = rd.getColumnName(j);
+                    if (!blacklisted(columnName)) {
+                        resultDescriptor.addColumn(
+                            columnName,
+                            rd.getColumnClassName(j));
+                    }
+                }
+
+                sectionHandler = new SectionHandler(
+                    path,
+                    output,
+                    resultDescriptor);
+
+                sectionHandler.setPrototyp(result);
+            }
+
+            if (last != null && WKTUtils.different(last, result, diffColums)) {
+                sectionHandler.finish();
+                sectionHandler.setPrototyp(result);
+            }
+
+            sectionHandler.handle(result);
+
+            last = result;
+        }
+
+        if (sectionHandler != null) {
+            sectionHandler.finish();
+        }
+
+        log.debug("------  number of points after processing: " + output.size());
+
+        return output;
+    }
+
+
+    private static final boolean blacklisted(String column) {
+        for (int i = 0; i < COLUMN_BLACKLIST.length; ++i) {
+            if (COLUMN_BLACKLIST.equals(column)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private static final String [] DIFF_COLUMS = {
+        "GROUP1",
+        "GROUP2",
+        "GROUP3"
+    };
+
+    private static final String [] COLUMN_BLACKLIST = {
+        "MEDIAN.MESHPOINT.JPOSITION",
+        "MEDIAN.MESHPOINT.IPOSITION"
+    };
+
+    public static final double EPSILON = 1e-5d;
+
+    public static final int    INTERPOLATION_STEPS =
+        Integer.getInteger("interpolation.steps", 500).intValue();
+
+    public static final class SectionHandler
+    implements                Interpolation2D.Consumer
+    {
+        private ArrayList<Point2d> points;
+        private List<Coordinate>   path;
+        private Collection<Result> output;
+        private Result             prototyp;
+        private ResultDescriptor   descriptor;
+        private boolean            lastWasSuccess;
+
+        public SectionHandler() {
+        }
+
+        public SectionHandler(
+            List<Coordinate>   path,
+            Collection<Result> output,
+            ResultDescriptor   descriptor
+        ) {
+            this.path       = path;
+            this.output     = output;
+            this.descriptor = descriptor;
+            points          = new ArrayList<Point2d>();
+            lastWasSuccess  = true;
+        }
+
+        public void finish() {
+            if (!points.isEmpty()) {
+                double distance = WKTUtils.toKM(
+                    DistanceCalculator.calculateDistance(path));
+
+                if (distance > EPSILON) {
+
+                    Interpolation2D.interpolate(
+                        path,
+                        points,
+                        0d,
+                        distance,
+                        INTERPOLATION_STEPS,
+                        LinearMetrics.INSTANCE,
+                        this);
+                }
+
+                points.clear();
+            }
+            lastWasSuccess = true;
+        }
+
+        public void setPrototyp(Result prototyp) {
+            this.prototyp = prototyp;
+        }
+
+        public void handle(Result result) {
+            Coordinate coordinate =
+                WKTUtils.toCoordinate(result.getString("SHAPE"));
+            double value = result.getDouble("YORDINATE");
+            int iPos     = result.getInteger("MEDIAN.MESHPOINT.JPOSITION");
+            int jPos     = result.getInteger("MEDIAN.MESHPOINT.JPOSITION");
+            Point2d p = new Point2d(
+                coordinate.x,
+                coordinate.y,
+                value,
+                iPos, jPos);
+            points.add(p);
+        }
+
+        public void interpolated(Coordinate coordinate, boolean success) {
+            DefaultResult result = new DefaultResult(descriptor);
+            ResultDescriptor pd = prototyp.getResultDescriptor();
+
+            int pcolums = pd.getColumnCount();
+            for (int i = 0, j = 0; i < pcolums; ++i) {
+                String colname = pd.getColumnName(i);
+                if (blacklisted(colname)) {
+                    continue;
+                }
+                if (colname.equals("SHAPE")) {
+                    result.addColumnValue(j, WKTUtils.toWKT(coordinate));
+                }
+                else if (colname.equals("YORDINATE")) {
+                    if (success) {
+                        result.addColumnValue(j, Double.valueOf(coordinate.z));
+                    }
+                    else if (lastWasSuccess) {
+                        // only insert null if last was valid.
+                        // This prevents flooding the result set with nulls
+                        // if interpolating over a large gap.
+                        result.addColumnValue(j, null);
+                    }
+                }
+                else {
+                    result.addColumnValue(j, prototyp.getObject(i));
+                }
+                ++j;
+            }
+            output.add(result);
+            lastWasSuccess = success;
+        }
+    }
 }

http://dive4elements.wald.intevation.org