ingo@1115: /*
ingo@1115: * Copyright (c) 2010 by Intevation GmbH
ingo@1115: *
ingo@1115: * This program is free software under the LGPL (>=v2.1)
ingo@1115: * Read the file LGPL.txt coming with the software for details
ingo@1115: * or visit http://www.gnu.org/licenses/ if it does not exist.
ingo@1115: */
ingo@1115:
sascha@436: package de.intevation.gnv.raster;
sascha@436:
sascha@779: import de.intevation.gnv.jfreechart.CompactXYItems;
sascha@779: import de.intevation.gnv.jfreechart.PolygonDataset;
sascha@779: import de.intevation.gnv.jfreechart.PolygonSeries;
sascha@779:
sascha@779: import de.intevation.gnv.raster.Vectorizer.Edge;
sascha@436:
sascha@436: import gnu.trove.TDoubleArrayList;
sascha@436:
sascha@779: import java.util.HashMap;
sascha@779: import java.util.List;
sascha@436:
sascha@437: /**
sascha@801: * Vectorizer backend to produce polygon datasets suitable to being
sascha@801: * displayed with {@link de.intevation.gnv.jfreechart.PolygonPlot}.
sascha@803: *
sascha@780: * @author Sascha L. Teichmann
sascha@437: */
sascha@436: public class PolygonDatasetProducer
sascha@465: extends AbstractProducer
sascha@436: {
sascha@801: /**
sascha@801: * Maps value integers to series of polygons to group
sascha@801: * them by value.
sascha@801: */
sascha@436: protected HashMap polygonSeries;
sascha@436:
sascha@801: /**
sascha@801: * Default constructor.
sascha@801: */
sascha@436: protected PolygonDatasetProducer() {
sascha@436: }
sascha@436:
sascha@801: /**
sascha@801: * Constructor to create a PolygonDatasetProducer with
sascha@801: * a given world bound box.
sascha@801: * @param minX Min x coord of the world.
sascha@801: * @param minY Min y coord of the world.
sascha@801: * @param maxX Max x coord of the world.
sascha@801: * @param maxY Max y coord of the world.
sascha@801: */
sascha@436: public PolygonDatasetProducer(
sascha@436: double minX, double minY,
sascha@436: double maxX, double maxY
sascha@436:
sascha@436: ) {
sascha@465: super(minX, minY, maxX, maxY);
sascha@436: polygonSeries = new HashMap();
sascha@436: }
sascha@436:
sascha@436: public void handleRings(
sascha@778: List rings,
sascha@778: int value,
sascha@436: int width,
sascha@436: int height
sascha@436: ) {
sascha@436: if (value == -1) {
sascha@436: return;
sascha@436: }
sascha@436:
sascha@436: Integer v = Integer.valueOf(value);
sascha@436:
sascha@465: PolygonSeries ps = polygonSeries.get(v);
sascha@436:
sascha@436: if (ps == null) {
sascha@436: polygonSeries.put(v, ps = new PolygonSeries());
sascha@436: ps.setAttribute("fill", v);
sascha@436: }
sascha@436:
sascha@436: TDoubleArrayList vertices = new TDoubleArrayList();
sascha@436:
sascha@436: /* minX = 0*m1 + b1 <=> b1 = minX
sascha@436: * maxX = (width-1)*m1 + b1
sascha@436: * m1 = (maxX - minX)/(width-1)
sascha@436: */
sascha@436:
sascha@437: double b1 = minX;
sascha@437: double m1 = width != 1
sascha@437: ? (maxX - minX)/(width-1)
sascha@437: : 0d;
sascha@436:
sascha@437: double b2 = minY;
sascha@437: double m2 = height != 1
sascha@437: ? (maxY - minY)/(height-1)
sascha@437: : 0d;
sascha@436:
sascha@436: for (Edge head: rings) {
sascha@436: Edge current = head;
sascha@436: do {
sascha@437: vertices.add(m1*(current.a % width) + b1);
sascha@437: vertices.add(m2*(current.a / width) + b2);
sascha@436: }
sascha@437: while ((current = current.next) != head);
sascha@436: ps.addRing(new CompactXYItems(vertices.toNativeArray()));
sascha@521: vertices.reset();
sascha@436: }
sascha@436: }
sascha@436:
sascha@801: /**
sascha@801: * Creates a new PolygonDataset from the polygons build by this
sascha@801: * backend.
sascha@801: * @return the new PolygonDataset
sascha@801: */
sascha@436: public PolygonDataset getPolygonDataset() {
sascha@436: return new PolygonDataset(polygonSeries.values());
sascha@436: }
sascha@436: }
sascha@798: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :