/********************************************************************** * $Id: Polygonizer.cpp,v 1.6.2.1.2.2 2005/12/09 10:32:49 strk Exp $ * * GEOS - Geometry Engine Open Source * http://geos.refractions.net * * Copyright (C) 2001-2002 Vivid Solutions Inc. * Copyright (C) 2005 Refractions Research Inc. * * This is free software; you can redistribute and/or modify it under * the terms of the GNU Lesser General Public Licence as published * by the Free Software Foundation. * See the COPYING file for more information. * **********************************************************************/ #include #include //#define DEBUG 1 namespace geos { Polygonizer::LineStringAdder::LineStringAdder(Polygonizer *p): pol(p) { } void Polygonizer::LineStringAdder::filter_rw(Geometry *g) { LineString *ls = dynamic_cast(g); if ( ls ) pol->add(ls); } /* * Create a polygonizer with the same GeometryFactory * as the input Geometry */ Polygonizer::Polygonizer(): lineStringAdder(new Polygonizer::LineStringAdder(this)), graph(NULL), dangles(NULL), cutEdges(NULL), invalidRingLines(NULL), holeList(NULL), shellList(NULL), polyList(NULL) { } Polygonizer::~Polygonizer() { delete lineStringAdder; delete dangles; delete cutEdges; delete graph; delete holeList; delete shellList; if ( invalidRingLines ) { for (unsigned int i=0, n=invalidRingLines->size(); isize(); i *geomList) { for(unsigned int i=0, n=geomList->size(); iapply_rw(lineStringAdder); } /* * Add a linestring to the graph of polygon edges. * * @param line the LineString to add */ void Polygonizer::add(LineString *line) { // create a new graph using the factory from the input Geometry if (graph==NULL) graph=new PolygonizeGraph(line->getFactory()); graph->addEdge(line); } /* * Gets the list of polygons formed by the polygonization. * @return a collection of Polygons */ vector* Polygonizer::getPolygons() { polygonize(); vector *ret = polyList; polyList = NULL; return ret; } /* * Get the list of dangling lines found during polygonization. * @return a collection of dangles LineStrings from input. */ vector* Polygonizer::getDangles() { polygonize(); return dangles; } /* * Get the list of cut edges found during polygonization. * @return a collection of the input {@LineStrings} which are cut edges */ vector* Polygonizer::getCutEdges() { polygonize(); return cutEdges; } /* * Get the list of lines forming invalid rings found during polygonization. * @return a collection of the input {@LineStrings} which form invalid rings */ vector* Polygonizer::getInvalidRingLines() { polygonize(); vector *ret = invalidRingLines; invalidRingLines = NULL; return ret; } /* * Perform the polygonization, if it has not already been carried out. */ void Polygonizer::polygonize() { // check if already computed if (polyList!=NULL) return; polyList=new vector(); if (graph==NULL) // No valid geometries added { return; } dangles=graph->deleteDangles(); cutEdges=graph->deleteCutEdges(); vector *edgeRingList=graph->getEdgeRings(); #if DEBUG cerr<<"Polygonizer::polygonize(): "<size()<<" edgeRings in graph"< *validEdgeRingList=new vector(); invalidRingLines=new vector(); findValidRings(edgeRingList, validEdgeRingList, invalidRingLines); #if DEBUG cerr<<" "<size()<<" valid"<size()<<" invalid"<size()<<" holes"<size()<<" shells"<size(); ipush_back(er->getPolygon()); } delete validEdgeRingList; } void Polygonizer::findValidRings(vector *edgeRingList, vector *validEdgeRingList, vector *invalidRingList) { for (unsigned int i=0, n=edgeRingList->size(); iisValid()) validEdgeRingList->push_back(er); else { invalidRingList->push_back(er->getLineString()); } } } void Polygonizer::findShellsAndHoles(vector *edgeRingList) { holeList=new vector(); shellList=new vector(); for (unsigned int i=0, n=edgeRingList->size(); iisHole()) holeList->push_back(er); else shellList->push_back(er); } } void Polygonizer::assignHolesToShells(vector *holeList,vector *shellList) { for (unsigned int i=0, n=holeList->size(); i *shellList) { polygonizeEdgeRing *shell=polygonizeEdgeRing::findEdgeRingContaining(holeER, shellList); if (shell!=NULL) shell->addHole(holeER->getRingOwnership()); } } // namespace geos /********************************************************************** * $Log: Polygonizer.cpp,v $ * Revision 1.6.2.1.2.2 2005/12/09 10:32:49 strk * Cleaned up debugging line left over from previous commit * * Revision 1.6.2.1.2.1 2005/12/09 10:04:52 strk * Fixed a bug making PolygonizeGraph choking on invalid LineStrings. * Minor optimizations in Polygonizer loops. * * Revision 1.6.2.1 2005/06/17 15:07:48 strk * Polygonizer segfault fix * * Revision 1.6 2004/12/08 13:54:44 strk * gcc warnings checked and fixed, general cleanups. * * Revision 1.5 2004/10/27 13:57:07 strk * Added some debugging lines (disabled by default) * * Revision 1.4 2004/10/26 16:09:21 strk * Some more intentation and envelope equality check fix. * * Revision 1.3 2004/10/19 19:51:14 strk * Fixed many leaks and bugs in Polygonizer. * Output still bogus. * * Revision 1.2 2004/07/02 13:28:29 strk * Fixed all #include lines to reflect headers layout change. * Added client application build tips in README. * * Revision 1.1 2004/04/08 04:53:56 ybychkov * "operation/polygonize" ported from JTS 1.4 * * **********************************************************************/