// g++ -g test6.cpp -I/usr/local/include/geos -lgeos; ./a.out
// g++ -g test6.cpp -I/usr/local/include/geos -lgeos ; valgrind --leak-check=yes --num-callers=10 ./a.out

#include "stdio.h"
#include "io.h"
#include "geom.h"

using namespace geos;


//create a simple multi-polygon
Geometry *a()
{

	GeometryFactory *geomFactory;
	Geometry *g1, *g2 ;
    Coordinate *c;
    CoordinateList *cl;
    LinearRing *outerRing;
    vector<Geometry *> *innerRings=new vector<Geometry *>;
	PrecisionModel *pm= new PrecisionModel();
	geomFactory= new GeometryFactory( pm, -1);
	LinearRing *innerRing;
    vector<Geometry *> *polys=new vector<Geometry *>;

	cl = new BasicCoordinateList(5);


	c = new Coordinate(0, 0);
	cl->setAt( *c ,0);
	delete c;
	c = new Coordinate(10, 0);
	cl->setAt( *c ,1);
	delete c;
	c = new Coordinate(10, 10);
	cl->setAt( *c ,2);
	delete c;
	c = new Coordinate(0, 10);
	cl->setAt( *c ,3);
	delete c;
	c = new Coordinate(0, 0);
	cl->setAt( *c ,4);
	delete c;

	outerRing = (LinearRing*) geomFactory->createLinearRing(cl);


	cl = new BasicCoordinateList(5);


	c = new Coordinate(2, 2);
	cl->setAt( *c ,0);
	delete c;
	c = new Coordinate(4, 2);
	cl->setAt( *c ,1);
	delete c;
	c = new Coordinate(4, 4);
	cl->setAt( *c ,2);
	delete c;
	c = new Coordinate(2, 4);
	cl->setAt( *c ,3);
	delete c;
	c = new Coordinate(2, 2);
	cl->setAt( *c ,4);
	delete c;

			innerRing = (LinearRing *) geomFactory->createLinearRing(cl);
			innerRings->push_back(innerRing);


	g1 = geomFactory->createPolygon(outerRing, innerRings);

	polys->push_back(g1);

	g2 = geomFactory->createMultiPolygon(polys);

	string s= g2->toString();
	cout << "geom1 == "<<s << endl;



	//delete g1;
	//delete cl;

	delete geomFactory;
	delete pm;

	return g2;
}

/// create geometry, union it, then delete it

void op()
{
	Geometry *aa, *bb, *cc;

		aa = a();
		bb = a();
		cc = aa->Union(bb);
		delete aa;
		delete bb;
		delete cc;
}

int main(int argC, char* argV[])
{

		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();
		op();

}


