view gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/PolygonSeries.java @ 799:feeaf5aec552

ISSUE213: Wrong Geometrytype used for the generation of an Layer with Multipolygon-Geometries gnv-artifacts/trunk@881 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 06 Apr 2010 11:56:53 +0000
parents cdade5005cba
children 22c18083225e
line wrap: on
line source
package de.intevation.gnv.jfreechart;

import java.util.HashMap;
import java.util.Map;

import org.jfree.data.Range;

import org.jfree.data.general.Series;

/**
 * This class represents a series of polygon items.
 *
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha Teichmann</a>
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class PolygonSeries
extends      Series
{
    /**
     * Polygons.
     */
    protected CompactXYItems []  rings;

    /**
     * A map containing attribues.
     */
    protected Map                attributes;

    /**
     * The unique key of this series.
     */
    private static long uniqueKey;

    /**
     *
     * @return a unique key.
     */
    protected synchronized static Long createUniqueKey() {
        return new Long(uniqueKey++);
    }

    /**
     * Constructor to create an empty PolygonSeries with a unique key.
     */
    public PolygonSeries() {
        this(createUniqueKey(), null);
    }

    /**
     *
     * @param key The key used for  this series.
     * @param rings Polygons.
     */
    public PolygonSeries(Comparable key, CompactXYItems [] rings) {
        this(key, null, rings, new HashMap());
    }

    /**
     *
     * @param key The key used for this series.
     * @param description A description of this series.
     * @param rings Polygons.
     */
    public PolygonSeries(
        Comparable       key,
        String           description,
        CompactXYItems[] rings
    ) {
        this(key, description, rings, new HashMap());
    }

    /**
     *
     * @param key The key used for this series.
     * @param description A description of this series.
     * @param rings Polygons.
     * @param attributes Some attribues.
     */
    public PolygonSeries(
        Comparable        key,
        String            description,
        CompactXYItems [] rings,
        Map               attributes
    ) {
        super(key, description);
        this.rings      = rings;
        this.attributes = attributes;
    }


    /**
     *
     * @param rings
     */
    public void setRings(CompactXYItems [] rings) {
        this.rings = rings;
    }

    /**
     *
     * @return
     */
    public CompactXYItems [] getRings() {
        return rings;
    }

    /**
     *
     * @param newRing
     */
    public void addRing(CompactXYItems newRing) {
        if (rings == null) {
            rings = new CompactXYItems [] { newRing };
        }
        else {
            CompactXYItems [] nRings = new CompactXYItems[rings.length + 1];
            System.arraycopy(rings, 0, nRings, 0, rings.length);
            nRings[rings.length] = newRing;
            rings = nRings;
        }
    }

    /**
     *
     * @param newRings
     */
    public void addRings(CompactXYItems [] newRings) {
        if (newRings == null || newRings.length == 0) {
            return;
        }
        if (rings == null || rings.length == 0) {
            rings = newRings;
        }
        else {
            CompactXYItems [] both =
                new CompactXYItems[rings.length + newRings.length];
            System.arraycopy(rings, 0, both, 0, rings.length);
            System.arraycopy(newRings, 0, both, rings.length, newRings.length);
            rings = both;
        }
    }

    /**
     *
     * @param key
     * @return
     */
    public Object getAttribute(Object key) {
        return attributes.get(key);
    }


    /**
     *
     * @param key
     * @param value
     * @return
     */
    public Object setAttribute(Object key, Object value) {
        return attributes.put(key, value);
    }


    /**
     *
     * @return
     */
    public int getItemCount() {
        return rings != null ? rings.length : 0;
    }


    /**
     *
     * @param idx
     * @return
     */
    public CompactXYItems getItem(int idx) {
        return rings[idx];
    }


    /**
     *
     * @param key
     * @return
     */
    public Object removeAttribute(Object key) {
        return attributes.remove(key);
    }


    /**
     *
     * @param key
     * @return
     */
    public boolean hasAttribute(Object key) {
        return attributes.containsKey(key);
    }


    /**
     *
     * @return the range of the x axis.
     */
    public Range getDomainBounds() {
        double upper = Double.NEGATIVE_INFINITY;
        double lower = Double.POSITIVE_INFINITY;
        int    count = getItemCount();

        for (int i = 0; i < count; i++) {
            CompactXYItems  items = getItem(i);
            double          minX  = items.getMinX();
            double          maxX  = items.getMaxX();

            if (!Double.isNaN(minX)) {
                lower = Math.min(lower, minX);
            }

            if (!Double.isNaN(maxX)) {
                upper = Math.max(upper, maxX);
            }
        }

        if (lower > upper) {
            return null;
        }

        return new Range(lower, upper);
    }


    /**
     *
     * @return the range of the y axis.
     */
    public Range getRangeBounds() {
        double upper = Double.NEGATIVE_INFINITY;
        double lower = Double.POSITIVE_INFINITY;
        int    count = getItemCount();

        for (int i = 0; i < count; i++) {
            CompactXYItems  items = getItem(i);
            double          minY  = items.getMinY();
            double          maxY  = items.getMaxY();

            if (!Double.isNaN(minY)) {
                lower = Math.min(lower, minY);
            }

            if (!Double.isNaN(maxY)) {
                upper = Math.max(upper, maxY);
            }
        }

        if (lower > upper) {
            return null;
        }

        return new Range(lower, upper);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org