com.vividsolutions.jts.operation.polygonize.Polygonizer Maven / Gradle / Ivy
The newest version!
/*
* The JTS Topology Suite is a collection of Java classes that
* implement the fundamental operations required to validate a given
* geo-spatial data set to a known topological specification.
*
* Copyright (C) 2001 Vivid Solutions
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, contact:
*
* Vivid Solutions
* Suite #1A
* 2328 Government Street
* Victoria BC V8T 5G5
* Canada
*
* (250)385-6040
* www.vividsolutions.com
*/
package com.vividsolutions.jts.operation.polygonize;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryComponentFilter;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Polygon;
/**
* Polygonizes a set of {@link Geometry}s which contain linework that
* represents the edges of a planar graph.
* All types of Geometry are accepted as input;
* the constituent linework is extracted as the edges to be polygonized.
* The processed edges must be correctly noded; that is, they must only meet
* at their endpoints. Polygonization will accept incorrectly noded input
* but will not form polygons from non-noded edges,
* and reports them as errors.
*
* The Polygonizer reports the follow kinds of errors:
*
* - Dangles - edges which have one or both ends which are not incident on another edge endpoint
*
- Cut Edges - edges which are connected at both ends but which do not form part of polygon
*
- Invalid Ring Lines - edges which form rings which are invalid
* (e.g. the component lines contain a self-intersection)
*
* Polygonization supports extracting only polygons which form a valid polygonal geometry.
* The set of extracted polygons is guaranteed to be edge-disjoint.
* This is useful for situations where it is known that the input lines form a
* valid polygonal geometry.
*
* @version 1.7
*/
public class Polygonizer
{
/**
* Adds every linear element in a {@link Geometry} into the polygonizer graph.
*/
private class LineStringAdder
implements GeometryComponentFilter
{
public void filter(Geometry g) {
if (g instanceof LineString)
add((LineString) g);
}
}
// default factory
private LineStringAdder lineStringAdder = new LineStringAdder();
protected PolygonizeGraph graph;
// initialize with empty collections, in case nothing is computed
protected Collection dangles = new ArrayList();
protected List cutEdges = new ArrayList();
protected List invalidRingLines = new ArrayList();
protected List holeList = null;
protected List shellList = null;
protected List polyList = null;
private boolean isCheckingRingsValid = true;
private boolean extractOnlyPolygonal;
private GeometryFactory geomFactory = null;
/**
* Creates a polygonizer with the same {@link GeometryFactory}
* as the input {@link Geometry}s.
* The output mask is {@link #ALL_POLYS}.
*/
public Polygonizer()
{
this(false);
}
/**
* Creates a polygonizer and allow specifyng if only polygons which form a valid polygonal geometry are to be extracted.
*
* @param extractOnlyPolygonal true if only polygons which form a valid polygonal geometry are to be extracted
*/
public Polygonizer(boolean extractOnlyPolygonal)
{
this.extractOnlyPolygonal = extractOnlyPolygonal;
}
/**
* Adds a collection of geometries to the edges to be polygonized.
* May be called multiple times.
* Any dimension of Geometry may be added;
* the constituent linework will be extracted and used.
*
* @param geomList a list of {@link Geometry}s with linework to be polygonized
*/
public void add(Collection geomList)
{
for (Iterator i = geomList.iterator(); i.hasNext(); ) {
Geometry geometry = (Geometry) i.next();
add(geometry);
}
}
/**
* Add a {@link Geometry} to the edges to be polygonized.
* May be called multiple times.
* Any dimension of Geometry may be added;
* the constituent linework will be extracted and used
*
* @param g a {@link Geometry} with linework to be polygonized
*/
public void add(Geometry g)
{
g.apply(lineStringAdder);
}
/**
* Adds a linestring to the graph of polygon edges.
*
* @param line the {@link LineString} to add
*/
private void add(LineString line)
{
// record the geometry factory for later use
geomFactory = line.getFactory();
// create a new graph using the factory from the input Geometry
if (graph == null)
graph = new PolygonizeGraph(geomFactory);
graph.addEdge(line);
}
/**
* Allows disabling the valid ring checking,
* to optimize situations where invalid rings are not expected.
*
* The default is true