view gnv-artifacts/src/main/java/de/intevation/gnv/raster/IsoProducer.java @ 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 f7038820df2e
children 9a828e5a2390
line wrap: on
line source
package de.intevation.gnv.raster;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import gnu.trove.TIntObjectHashMap;
import gnu.trove.TIntHashSet;
import gnu.trove.TObjectProcedure;

import de.intevation.gnv.math.IJKey;

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

/**
 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
 */
public class IsoProducer
extends      AbstractProducer
{
    public interface AttributeGenerator {

        Object generateAttribute(int neighbor1, int neighbor2);

    } // interface AttributeGenerator

    protected HashMap<Edge, Integer>            open;
    protected HashMap<IJKey, TIntObjectHashMap> commonOpen;
    protected HashMap<IJKey, ArrayList<Edge>>   complete;

    protected int    width;
    protected int    height;

    public IsoProducer(
        double minX, double minY,
        double maxX, double maxY
    ) {
        super(minX, minY, maxX, maxY);

        open       = new HashMap<Edge, Integer>();
        commonOpen = new HashMap<IJKey, TIntObjectHashMap>();
        complete   = new HashMap<IJKey, ArrayList<Edge>>();
    }

    public void handleRings(
        List<Edge> rings, 
        int        value, 
        int        width,
        int        height
    ) {
        if (value == -1) {
            return;
        }
        this.width  = width;
        this.height = height;

        Integer v = Integer.valueOf(value);

        for (Edge head: rings) {
            Edge current = head;
            do {
                Integer neighbor = open.remove(current);

                if (neighbor != null) {
                    IJKey pair = new IJKey(value, neighbor.intValue());
                    pair.sort();

                    TIntObjectHashMap co = commonOpen.get(pair);

                    if (co == null) {
                        commonOpen.put(pair, co = new TIntObjectHashMap());
                    }

                    Edge edge = new Edge(current);

                    Edge otherA = (Edge)co.remove(edge.a);
                    if (otherA != null) {
                        otherA.chain(edge, edge.a);
                    }

                    Edge otherB = (Edge)co.remove(edge.b);
                    if (otherB != null) {
                        otherB.chain(edge, edge.b);
                    }

                    if (edge.isComplete()) {
                        ArrayList list = complete.get(pair);
                        if (list == null) {
                            complete.put(pair, list = new ArrayList());
                        }
                        list.add(Vectorizer.simplify(edge, width));
                    }
                    else {
                        if (otherA == null) {
                            co.put(edge.a, edge);
                        }
                        if (otherB == null) {
                            co.put(edge.b, edge);
                        }
                    }
                }
                else {
                    Edge edge = new Edge(current.b, current.a);
                    open.put(edge, v);
                }

                current = current.next;
            }
            while (current != head);
        } // for all rings

    } // handleRings

    protected HashSet<IJKey> joinPairs() {
        HashSet<IJKey> pairs = new HashSet<IJKey>();
        pairs.addAll(complete  .keySet());
        pairs.addAll(commonOpen.keySet());
        return pairs;
    }

    protected static ArrayList<Edge> headList(TIntObjectHashMap map) {
        final ArrayList<Edge> headList = new ArrayList<Edge>();
        map.forEachValue(new TObjectProcedure() {
            TIntHashSet headSet = new TIntHashSet();
            public boolean execute(Object value) {
                Edge head = ((Edge)value).head();
                if (headSet.add(head.a)) {
                    headList.add(head);
                }
                return true;
            }
        });
        return headList;
    }

    public void clear() {
        open.clear();
        complete.clear();
        commonOpen.clear();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org