# [geos-devel] Problems creating polygons with holes

Darren Carman darren.carman at infoterra-global.com
Wed Jun 30 11:13:58 EDT 2004

```The way we deal with them holes can contain polygons, which in turn can contain holes, etc. and we still treat holes as polygons -- hence my confusion.

If you would like to use my code as another one of your examples in the distribution - for polygons with holes - then feel free (if you think it may help someone else).

Here is the working code:

#include <geos/io.h>
#include <geos/geom.h>

using namespace std;
using namespace geos;

typedef struct boundaryPt {
double x;
double y;
};
typedef vector<boundaryPt>    boundary;
typedef vector<boundary>      boundaryList;
typedef vector<boundaryList>  polygonList;

void createPolys(polygonList &polys) {
int i;

boundaryPt poly1[5];
boundaryPt hole1[5];
boundary tmpBoundary;
boundaryList tmpBoundaryList;
polygonList ptPolygons;

poly1[0].x = 339252; poly1[0].y = 1019302;
poly1[1].x = 339252; poly1[1].y = 1029077;
poly1[2].x = 348752; poly1[2].y = 1029077;
poly1[3].x = 346102; poly1[3].y = 1019653;
poly1[4].x = 339252; poly1[4].y = 1019302;

hole1[0].x = 340000; hole1[0].y = 1020000;
hole1[1].x = 340000; hole1[1].y = 1021000;
hole1[2].x = 341000; hole1[2].y = 1021000;
hole1[3].x = 341000; hole1[3].y = 1020000;
hole1[4].x = 340000; hole1[4].y = 1020000;

for (i=0; i<5; i++) {
tmpBoundary.push_back(poly1[i]);
}
tmpBoundaryList.push_back(tmpBoundary);
tmpBoundary.clear();

for (i=0; i<5; i++) {
tmpBoundary.push_back(hole1[i]);
}
tmpBoundaryList.push_back(tmpBoundary);
tmpBoundary.clear();

polys.push_back(tmpBoundaryList);
for(int i=0; i< static_cast<int>(tmpBoundaryList.size()); i++) {
tmpBoundaryList[i].clear();
}
tmpBoundaryList.clear();
}

int main(int argc, char *argv[]) {
vector<Geometry *> *polygons;
vector<LinearRing *> *holes;
int i,j,k;
string resStr = "";
polygonList ptPolygons;

createPolys(ptPolygons);
try{
GeometryFactory *gf=new GeometryFactory(new PrecisionModel(),0);

// for each outer ring create a CoordinateList and add all the points to it
polygons = new vector<Geometry *>();
for(i=0; i< static_cast<int>(ptPolygons.size()); i++) {
CoordinateList *cl1=CoordinateListFactory::internalFactory->createCoordinateList();
boundaryList bl = ptPolygons[i];
boundary b = bl[0];
cout << "Polygon " << i << endl;
for(k=0; k< static_cast<int>(b.size()); k++) {
cout << "Added point " << b[k].x << ',' << b[k].y << endl;
}

// for each hole create a coordinate list like above, create a polygon and add to a Geometry vector
holes = new vector<LinearRing *>();
for(j=1; j< static_cast<int>(bl.size()); j++) {
cout << "Hole " << j << endl;
boundary b = bl[j];
CoordinateList *cl2=CoordinateListFactory::internalFactory->createCoordinateList();
for(k=0; k< static_cast<int>(b.size()); k++) {
cout << "Added point " << b[k].x << ',' << b[k].y << endl;
}
cout << "Creating polygon for hole " << j << ": " << cl2->toString() << endl;
//LinearRing *lin1=(LinearRing *)gf->createPolygon(gf->createLinearRing(cl2),NULL);
LinearRing *lin1=gf->createLinearRing(cl2);
cout << "Geometry " << j << " : " << lin1->toString() << endl;
holes->push_back(lin1);
delete cl2;
}
// create a polygon from the 1st CoordinateList and the holes
cout << "Creating polygon " << i << ": " << cl1->toString() << endl;
Geometry *geom1=(Geometry *)gf->createPolygon(gf->createLinearRing(cl1),(vector<Geometry *> *)holes);
cout << "Geometry " << i << " : " << geom1->toString() << endl;

// save this polygon in another vector
cout << "Adding polygon " << i << endl;
polygons->push_back(geom1);
delete cl1;
}
cout << "Creating multi-polygon" << endl;
// when all done create a MultiPolygon object
Geometry *geom2=gf->createMultiPolygon((vector<Geometry *> *)polygons);
cout << "Creating string" << endl;
resStr = geom2->toString();

//clean up
delete geom2;
delete gf;
}
catch (GEOSException *ge) {
cout << "ERROR: " << ge->toString() << endl;
}
cout << resStr << endl;
}

Cheers,
Darren

-----Original Message-----
From: chodgson at refractions.net [mailto:chodgson at refractions.net]
Sent: 30 June 2004 15:55
To: GEOS Development List
Subject: RE: [geos-devel] Problems creating polygons with holes

> I had assumed that the holes were treated as polygons as well :(

If polygon holes were defined by polygons, then your holes could have holes (which in turn could have holes...)!

:)
Chris

_______________________________________________
geos-devel mailing list
geos-devel at geos.refractions.net
http://geos.refractions.net/mailman/listinfo/geos-devel

"The information in this e-mail and any attachment is confidential and may be privileged. If you have received this e-mail in error, please delete it immediately and destroy any copies on your system. You should not retain, copy or use this e-mail for any purpose, nor disclose all or any part of its content to any other person.
Opinions expressed in this e-mail may not be endorsed by the company and unless explicitly indicated, this e-mail shall not form part of any binding agreement".

```