changeset 484:823e4f808418

Generate JTS geometries (multi polygons and multi linestrings) from interpolation with external palette indices. gnv-artifacts/trunk@559 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 18 Jan 2010 11:25:52 +0000
parents c8089cd7d777
children 4939236c2dc9
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/math/AttributedPoint2ds.java gnv-artifacts/src/main/java/de/intevation/gnv/raster/ExternalIndexConverter.java gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java
diffstat 4 files changed, 230 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Mon Jan 18 10:56:41 2010 +0000
+++ b/gnv-artifacts/ChangeLog	Mon Jan 18 11:25:52 2010 +0000
@@ -1,3 +1,16 @@
+2010-01-18	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/gnv/raster/ExternalIndexConverter.java:
+	  New. Helper to convert the internal palette indices
+	  to the explicit configured external ones.
+
+	* src/main/java/de/intevation/gnv/math/AttributedPoint2ds.java:
+	  Store the JTS geometries too.
+
+	* src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java:
+	  Generate JTS multi polygons for parameter regions and multi line strings
+	  for iso lines. TODO: Clip against incoming polygon.
+
 2010-01-18	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/gnv/raster/Palette.java:
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/math/AttributedPoint2ds.java	Mon Jan 18 10:56:41 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/math/AttributedPoint2ds.java	Mon Jan 18 11:25:52 2010 +0000
@@ -1,10 +1,15 @@
 package de.intevation.gnv.math;
 
-import java.util.Map;
+import com.vividsolutions.jts.geom.MultiLineString;
+import com.vividsolutions.jts.geom.MultiPolygon;
+
+import de.intevation.gnv.utils.Pair;
+
+import java.io.Serializable;
+
 import java.util.HashMap;
 import java.util.List;
-
-import java.io.Serializable;
+import java.util.Map;
 
 /**
  *  @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
@@ -12,8 +17,11 @@
 public class AttributedPoint2ds
 implements   Serializable
 {
-    protected List<? extends Point2d> points;
-    protected Map                     attributes;
+    protected List<? extends Point2d>             points;
+    protected Map                                 attributes;
+    protected AreaInterpolation                   interpolation;
+    protected List<Pair<Object, MultiLineString>> lineStrings;
+    protected Map<Integer, MultiPolygon>          polygons;
 
     public AttributedPoint2ds() {
     }
@@ -42,5 +50,31 @@
     public void setPoints(List<? extends Point2d> points) {
         this.points = points;
     }
+
+    public void setInterpolation(AreaInterpolation interpolation) {
+        this.interpolation = interpolation;
+    }
+
+    public AreaInterpolation getInterpolation() {
+        return interpolation;
+    }
+
+    public void setPolygons(Map<Integer, MultiPolygon> polygons) {
+        this.polygons = polygons;
+    }
+
+    public Map<Integer, MultiPolygon> getPolygons() {
+        return polygons;
+    }
+
+    public void setLineStrings(
+        List<Pair<Object, MultiLineString>> lineStrings
+    ) {
+        this.lineStrings = lineStrings;
+    }
+
+    public List<Pair<Object, MultiLineString>> getLineStrings() {
+        return lineStrings;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/raster/ExternalIndexConverter.java	Mon Jan 18 11:25:52 2010 +0000
@@ -0,0 +1,44 @@
+package de.intevation.gnv.raster;
+
+import org.apache.log4j.Logger;
+
+/**
+ * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
+ */
+public class ExternalIndexConverter
+implements   JTSMultiPolygonProducer.ValueConverter
+{
+    private static Logger log = Logger.getLogger(
+        ExternalIndexConverter.class);
+
+    protected Palette palette;
+
+    public ExternalIndexConverter() {
+    }
+
+    public ExternalIndexConverter(Palette palette) {
+        this.palette = palette;
+    }
+
+    public Integer convert(Integer value) {
+        if (value == null) {
+            return value;
+        }
+
+        int v = value.intValue();
+
+        if (v == -1) {
+            return value;
+        }
+
+        Palette.Entry entry = palette.getEntryByIndex(v);
+
+        if (entry == null) {
+            log.warn("cannot find palette entry for index: " + v);
+            return value;
+        }
+
+        return Integer.valueOf(entry.getExternalIndex());
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java	Mon Jan 18 10:56:41 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java	Mon Jan 18 11:25:52 2010 +0000
@@ -2,6 +2,8 @@
 
 import com.vividsolutions.jts.geom.Coordinate;
 import com.vividsolutions.jts.geom.Envelope;
+import com.vividsolutions.jts.geom.MultiLineString;
+import com.vividsolutions.jts.geom.MultiPolygon;
 import com.vividsolutions.jts.geom.Polygon;
 
 import de.intevation.artifactdatabase.Config;
@@ -29,6 +31,15 @@
 import de.intevation.gnv.math.Point2d;
 import de.intevation.gnv.math.QueriedXYDepth;
 
+import de.intevation.gnv.raster.ExternalIndexConverter;
+import de.intevation.gnv.raster.IsoAttributeGenerator;
+import de.intevation.gnv.raster.JTSMultiLineStringProducer;
+import de.intevation.gnv.raster.JTSMultiPolygonProducer;
+import de.intevation.gnv.raster.Palette;
+import de.intevation.gnv.raster.PaletteManager;
+import de.intevation.gnv.raster.Raster;
+import de.intevation.gnv.raster.Vectorizer;
+
 import de.intevation.gnv.state.InputData;
 import de.intevation.gnv.state.OutputStateBase;
 
@@ -37,6 +48,7 @@
 import de.intevation.gnv.state.timeseries.TimeSeriesOutputState;
 
 import de.intevation.gnv.utils.FileUtils;
+import de.intevation.gnv.utils.Pair;
 import de.intevation.gnv.utils.StringUtils;
 import de.intevation.gnv.utils.WKTUtils;
 
@@ -48,7 +60,9 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.log4j.Logger;
 
@@ -327,12 +341,11 @@
             return process(
                 env,
                 p,
-                numSamples(callContext),
-                preprocess(
+                callContext,
+                preProcess(
                     queryExecutor.executeQuery(
                         queryID,
-                        addedFilterValues)),
-                getGroundInterpolation(callContext));
+                        addedFilterValues)));
         }
         catch (QueryException e) {
             log.error(e,e);
@@ -341,12 +354,12 @@
         throw new StateException("no result produced");
     }
 
-    public AttributedPoint2ds preprocess(Collection<Result> results) {
+    public AttributedPoint2ds preProcess(Collection<Result> results) {
 
         boolean debug = log.isDebugEnabled();
 
         if (debug) {
-            log.debug("--- preprocess: " + results.size() + " results");
+            log.debug("--- preProcess: " + results.size() + " results");
         }
 
         AttributedPoint2ds ap2ds = new AttributedPoint2ds();
@@ -409,28 +422,46 @@
     public AttributedPoint2ds process(
         Envelope           boundingBox,
         Polygon            polygon,
-        int                numSamples,
-        AttributedPoint2ds input,
-        int                groundInterpolation
+        CallContext        callContext,
+        AttributedPoint2ds input
     ) {
         if (input == null) {
             log.error("no data to interpolate");
             return null;
         }
 
+        Integer parameterId =
+            (Integer)input.getAttribute("PARAMETERID"); // XXX: hardcoded
+
+        if (parameterId == null) {
+            log.error("missing parameter id");
+            return null;
+        }
+
+        Map<Integer, PaletteManager> paletteManagers =
+            getPalettes(callContext);
+
+        PaletteManager paletteManager = paletteManagers.get(parameterId);
+
+        if (paletteManager == null) {
+            log.error("no palette found for parameter id " + parameterId);
+            return null;
+        }
+
         boolean debug = log.isDebugEnabled();
 
         if (debug) {
-            log.debug("before interpolation");
+            log.debug("interpolation");
         }
 
         AreaInterpolation interpolation =
             new AreaInterpolation();
 
-        List<? extends Point2d> points = input.getPoints();
+        int numSamples          = numSamples(callContext);
+        int groundInterpolation = getGroundInterpolation(callContext);
 
         if (!interpolation.interpolate(
-            points, 
+            input.getPoints(), 
             boundingBox,
             new Dimension(numSamples, numSamples),
             new QueriedXYDepth(groundInterpolation)
@@ -439,17 +470,90 @@
             return null;
         }
 
+        // Do the post processing
+        Raster raster = new Raster(
+            interpolation.getRaster(),
+            numSamples);
+
+        // TODO: Filter operations.
+
         if (debug) {
-            log.debug("after interpolation");
+            log.debug("to indexed raster");
         }
 
-        return null;
+        // scan for regions with base palette
+        Palette basePalette = paletteManager.getBase();
+
+        int [] intRaster = raster.toIndexed(basePalette);
+
+        // produce JFreeChart compatible polygons
+
+        if (debug) {
+            log.debug("vectorize indexed raster");
+        }
+
+        // produce JTS compatible polygons
+
+        JTSMultiPolygonProducer jtsmpp = new JTSMultiPolygonProducer(
+            boundingBox.getMinX(), boundingBox.getMinY(),
+            boundingBox.getMaxX(), boundingBox.getMaxY());
+            
+        int numRegions = new Vectorizer(intRaster, numSamples)
+            .process(jtsmpp);
+
+        Map<Integer, MultiPolygon> polygons = jtsmpp.getMultiPolygons(
+            new ExternalIndexConverter(basePalette));
+
+        jtsmpp.clear(); jtsmpp = null; // help gc
+
+        int numColors = polygons.size();
+
+        if (debug) {
+            log.debug("number of regions: " + numRegions);
+            log.debug("number of colors:  " + numColors);
+        }
+        // generate iso lines
+
+        int numIso;
+
+             if (numColors <  5) { numIso = 5; }
+        else if (numColors < 10) { numIso = 2; }
+        else                     { numIso = 0; }
+
+        Palette isoPalette;
+
+        if (numIso == 0) { // same palette
+            isoPalette = basePalette;
+            /* intRaster = intRaster; */
+        }
+        else {
+            isoPalette = paletteManager.getLevel(numIso);
+            intRaster  = raster.toIndexed(isoPalette);
+        }
+
+        JTSMultiLineStringProducer jtslsp = new JTSMultiLineStringProducer(
+            boundingBox.getMinX(), boundingBox.getMinY(),
+            boundingBox.getMaxX(), boundingBox.getMaxY());
+
+        numRegions = new Vectorizer(false, intRaster, numSamples)
+            .process(jtslsp);
+
+        IsoAttributeGenerator iag = new IsoAttributeGenerator(isoPalette);
+
+        List<Pair<Object, MultiLineString>> lineStrings =
+            jtslsp.getMultiLineStrings(iag);
+
+        jtslsp.clear(); jtslsp = null; // help gc
+
+        input.setInterpolation(interpolation);
+
+        input.setPolygons(polygons);
+        input.setLineStrings(lineStrings);
+
+        return input;
     }
 
     
-    /**
-     * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#setup(org.w3c.dom.Node)
-     */
     @Override
     public void setup(Node configuration) {
         super.setup(configuration);
@@ -486,5 +590,18 @@
 
         return RasterObject.getInterpolationType(interpolation);
     }
+
+    private static Map<Integer, PaletteManager> getPalettes(
+        CallContext callContext
+    ) {
+        GNVArtifactContext context = 
+            (GNVArtifactContext)callContext.globalContext();
+        Map<Integer, PaletteManager> palettes =
+            (Map<Integer, PaletteManager>)context.get(
+                GNVArtifactContext.PALETTES_KEY);
+        return palettes != null
+            ? palettes
+            : new HashMap<Integer, PaletteManager>();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org