view gnv-artifacts/src/main/java/de/intevation/gnv/raster/PolygonDatasetProducer.java @ 442:52e031261eaa

Create a new 'gnv' section in config file. gnv-artifacts/trunk@490 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 29 Dec 2009 10:09:26 +0000
parents b624879d2902
children f7038820df2e
line wrap: on
line source
package de.intevation.gnv.raster;

import java.util.List;
import java.util.HashMap;

import gnu.trove.TDoubleArrayList;

import de.intevation.gnv.raster.Vectorizer.RingsHandler;
import de.intevation.gnv.raster.Vectorizer.Edge;

import de.intevation.gnv.jfreechart.PolygonSeries;
import de.intevation.gnv.jfreechart.PolygonDataset;
import de.intevation.gnv.jfreechart.CompactXYItems;

/**
 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
 */
public class PolygonDatasetProducer
implements   RingsHandler
{
    protected double minX;
    protected double minY;
    protected double maxX;
    protected double maxY;

    protected HashMap<Integer, PolygonSeries> polygonSeries;

    protected PolygonDatasetProducer() {
    }

    public PolygonDatasetProducer(
        double minX, double minY,
        double maxX, double maxY

    ) {
        this.minX = minX;
        this.minY = minY;
        this.maxX = maxX;
        this.maxY = 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)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.clear();
        }
    }

    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