view gnv-artifacts/src/main/java/de/intevation/gnv/raster/JTSMultiLineStringProducer.java @ 497:75306d856004

Fixed import error. gnv-artifacts/trunk@577 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 19 Jan 2010 17:55:08 +0000
parents f7038820df2e
children 4080b57dcb52
line wrap: on
line source
package de.intevation.gnv.raster;

import java.util.List;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Coordinate;

import gnu.trove.TIntObjectHashMap;

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

import de.intevation.gnv.utils.Pair;

import de.intevation.gnv.math.IJKey;

/**
 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
 */
public class JTSMultiLineStringProducer
extends      IsoProducer
{
    private static Logger log = Logger.getLogger(
        JTSMultiLineStringProducer.class);

    protected GeometryFactory geometryFactory;

    public JTSMultiLineStringProducer(
        double minX, double minY,
        double maxX, double maxY
    ) {
        this(
            JTSMultiPolygonProducer.createDefaultGeometryFactory(),
            minX, minY,
            maxX, maxY);
    }

    public JTSMultiLineStringProducer(
        GeometryFactory geometryFactory,
        double minX, double minY,
        double maxX, double maxY
    ) {
        super(minX, minY, maxX, maxY);
        this.geometryFactory = geometryFactory;
    }

    public List<Pair<Object, MultiLineString>> getMultiLineStrings(
        AttributeGenerator attributeGenerator
    ) {
        ArrayList<Pair<Object, MultiLineString>> multiLineStrings =
            new ArrayList<Pair<Object, MultiLineString>>();

        double b1 = minX;
        double m1 = width != 1
            ? (maxX - minX)/(width-1)
            : 0d;

         double b2 = minY;
         double m2 = height != 1
            ? (maxY - minY)/(height-1)
            : 0d;

        ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();

        for (IJKey key: joinPairs()) {
            ArrayList<LineString> lineStrings = new ArrayList<LineString>();

            // process complete
            ArrayList<Edge> completeList = complete.get(key);
            if (completeList != null) {
                for (Edge head: completeList) {
                    Edge current = head;
                    do {
                        coordinates.add(new Coordinate(
                            m1*(current.a % width) + b1,
                            m2*(current.a / width) + b2));
                    }
                    while ((current = current.next) != head);
                    // add head again to close shape
                    coordinates.add(new Coordinate(
                        m1*(head.a % width) + b1,
                        m2*(head.a / width) + b2));
                    lineStrings.add(
                        geometryFactory.createLineString(
                            coordinates.toArray(
                                new Coordinate[coordinates.size()])));
                    coordinates.clear();
                }
            }

            // process open
            TIntObjectHashMap map = commonOpen.get(key);

            if (map != null) {
                for (Edge head: headList(map)) {

                    head = Vectorizer.simplify(head, width);
                    Edge current = head, last = head;
                    do {
                        coordinates.add(new Coordinate(
                            m1*(current.a % width) + b1,
                            m2*(current.a / width) + b2));
                        last = current;
                    }
                    while ((current = current.next) != null);
                    // add b from tail
                    coordinates.add(new Coordinate(
                        m1*(last.b % width) + b1,
                        m2*(last.b / width) + b2));
                    lineStrings.add(
                        geometryFactory.createLineString(
                            coordinates.toArray(
                                new Coordinate[coordinates.size()])));
                    coordinates.clear();
                } // for all in common open
            } // if map defined for key

			if (!lineStrings.isEmpty()) {
                MultiLineString multiLineString =
                    geometryFactory.createMultiLineString(
                        lineStrings.toArray(
                            new LineString[lineStrings.size()]));
                lineStrings.clear();
                Object attribute = attributeGenerator != null
                    ? attributeGenerator.generateAttribute(key.i, key.j)
                    : key;
                multiLineStrings.add(new Pair<Object, MultiLineString>(
                    attribute,
                    multiLineString));
			}
        } // for all pairs

        return multiLineStrings;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org