std::auto_ptr<geom::GeometryCollection> 
VoronoiDiagramBuilder::clipGeometryCollection(const geom::GeometryCollection& geom, const geom::Envelope& clipEnv)
{
   geom::Geometry* clipPoly = geom.getFactory()->toGeometry((Envelope*)&clipEnv);
   std::vector<Geometry*> clipped;
   for(std::size_t i=0 ; i < geom.getNumGeometries() ; i++)
   {
	   const Geometry* ge = geom.getGeometryN(i);
	   Geometry* g = ge->clone();
	   Geometry* result=NULL;

	   if(clipEnv.contains(g->getEnvelopeInternal()))
		   result = g;
	   else if(clipEnv.intersects(g->getEnvelopeInternal()))
	   {
		   result = clipPoly->intersection(ge);
		   result->setUserData(g->getUserData());
	   }

	   if(result!=NULL && !result->isEmpty() )
	   {
		   clipped.push_back(result);
	   }
	   delete g;
   }
   GeometryCollection* ret = geom.getFactory()->createGeometryCollection(clipped);
   return std::auto_ptr<GeometryCollection>(ret);
}
std::auto_ptr<geom::GeometryCollection> 
VoronoiDiagramBuilder::clipGeometryCollection(const geom::GeometryCollection& geom, const geom::Envelope& clipEnv)
{
	std::auto_ptr<geom::Geometry> clipPoly ( geom.getFactory()->toGeometry(&clipEnv) );
	std::auto_ptr< std::vector<Geometry*> >clipped(new std::vector<Geometry*>);
	for(std::size_t i=0 ; i < geom.getNumGeometries() ; i++)
	{
		const Geometry* g = geom.getGeometryN(i);
		std::auto_ptr<Geometry> result;
		// don't clip unless necessary
		if(clipEnv.contains(g->getEnvelopeInternal()))
		{
			result.reset( g->clone() );
      // TODO: check if userData is correctly cloned here?
		}
		else if(clipEnv.intersects(g->getEnvelopeInternal()))
		{
			result.reset( clipPoly->intersection(g) );
			result->setUserData(((Geometry*)g)->getUserData()); // TODO: needed ?
		}

		if(result.get() && !result->isEmpty() )
		{
			clipped->push_back(result.release());
		}
	}
	return std::auto_ptr<GeometryCollection>(geom.getFactory()->createGeometryCollection(clipped.release()));
}