All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.filosganga.geogson.jts.JtsLineStringIterable Maven / Gradle / Ivy

There is a newer version: 1.4.31
Show newest version
package com.github.filosganga.geogson.jts;

import com.google.common.collect.UnmodifiableIterator;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.Polygon;

/**
 * @author Filippo De Luca - [email protected]
 */
class JtsLineStringIterable implements Iterable {

    private final LineStringProvider src;

    private JtsLineStringIterable(LineStringProvider src) {
        this.src = src;
    }

    public static JtsLineStringIterable of(final MultiLineString src) {
        return new JtsLineStringIterable(new LineStringProvider() {
            @Override
            public int getNumLineStrings() {
                return src.getNumGeometries();
            }

            @Override
            public LineString getLineStringN(int n) {
                return (LineString) src.getGeometryN(n);
            }
        });
    }

    public static JtsLineStringIterable forHolesOf(final Polygon src) {
        return new JtsLineStringIterable(new LineStringProvider() {
            @Override
            public int getNumLineStrings() {
                return src.getNumInteriorRing();
            }

            @Override
            public LineString getLineStringN(int n) {
                return src.getInteriorRingN(n);
            }
        });
    }


    @Override
    public UnmodifiableIterator iterator() {
        return new Iterator(src);
    }

    private static class Iterator extends UnmodifiableIterator {

        private final LineStringProvider src;

        private int index = 0;

        private Iterator(LineStringProvider src) {
            this.src = src;
        }

        @Override
        public boolean hasNext() {
            return src.getNumLineStrings() > index;
        }

        @Override
        public LineString next() {
            return src.getLineStringN(index++);

        }

    }

    private interface LineStringProvider {

        int getNumLineStrings();

        LineString getLineStringN(int n);

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy