diff gnv-artifacts/src/main/java/de/intevation/gnv/raster/PolygonDatasetProducer.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents f953c9a559d8
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/raster/PolygonDatasetProducer.java	Fri Sep 28 12:14:00 2012 +0200
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2010 by Intevation GmbH
+ *
+ * This program is free software under the LGPL (>=v2.1)
+ * Read the file LGPL.txt coming with the software for details
+ * or visit http://www.gnu.org/licenses/ if it does not exist.
+ */
+
+package de.intevation.gnv.raster;
+
+import de.intevation.gnv.jfreechart.CompactXYItems;
+import de.intevation.gnv.jfreechart.PolygonDataset;
+import de.intevation.gnv.jfreechart.PolygonSeries;
+
+import de.intevation.gnv.raster.Vectorizer.Edge;
+
+import gnu.trove.TDoubleArrayList;
+
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ * Vectorizer backend to produce polygon datasets suitable to being
+ * displayed with {@link de.intevation.gnv.jfreechart.PolygonPlot}.
+ *
+ * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
+ */
+public class PolygonDatasetProducer
+extends      AbstractProducer
+{
+    /**
+     * Maps value integers to series of polygons to group
+     * them by value.
+     */
+    protected HashMap<Integer, PolygonSeries> polygonSeries;
+
+    /**
+     * Default constructor.
+     */
+    protected PolygonDatasetProducer() {
+    }
+
+    /**
+     * Constructor to create a PolygonDatasetProducer with
+     * a given world bound box.
+     * @param minX Min x coord of the world.
+     * @param minY Min y coord of the world.
+     * @param maxX Max x coord of the world.
+     * @param maxY Max y coord of the world.
+     */
+    public PolygonDatasetProducer(
+        double minX, double minY,
+        double maxX, double maxY
+
+    ) {
+        super(minX, minY, maxX, maxY);
+        polygonSeries = new HashMap<Integer, PolygonSeries>();
+    }
+
+    public void handleRings(
+        List<Edge> rings,
+        int        value,
+        int        width,
+        int        height
+    ) {
+        if (value == -1) {
+            return;
+        }
+
+        Integer v = Integer.valueOf(value);
+
+        PolygonSeries ps = polygonSeries.get(v);
+
+        if (ps == null) {
+            polygonSeries.put(v, ps = new PolygonSeries());
+            ps.setAttribute("fill", v);
+        }
+
+        TDoubleArrayList vertices = new TDoubleArrayList();
+
+        /* minX = 0*m1 + b1 <=> b1 = minX
+         * maxX = (width-1)*m1 + b1
+         * m1 = (maxX - minX)/(width-1)
+         */
+
+        double b1 = minX;
+        double m1 = width != 1
+           ? (maxX - minX)/(width-1)
+           : 0d;
+
+        double b2 = minY;
+        double m2 = height != 1
+           ? (maxY - minY)/(height-1)
+           : 0d;
+
+        for (Edge head: rings) {
+            Edge current = head;
+            do {
+                vertices.add(m1*(current.a % width) + b1);
+                vertices.add(m2*(current.a / width) + b2);
+            }
+            while ((current = current.next) != head);
+            ps.addRing(new CompactXYItems(vertices.toNativeArray()));
+            vertices.reset();
+        }
+    }
+
+    /**
+     * Creates a new PolygonDataset from the polygons build by this
+     * backend.
+     * @return the new PolygonDataset
+     */
+    public PolygonDataset getPolygonDataset() {
+        return new PolygonDataset(polygonSeries.values());
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org