Question

I'm using the GEOS library, and I'm trying to create a Polygon with a hole in it. As per the documentation, I have to pass in a LinearRing, which represents the outer "shell", and a std::vector<Geometry*>, which represents the holes in the shell. The first parameter is easy, but the second one is giving me trouble. Polygon wants the elements in the second parameter to be LineStrings (LineString is a subclass of Geometry); otherwise, it throws an exception saying that LineStrings are needed for the holes. If I just cast the LineStrings to Geometry, then it throws the exception. If I don't cast it, I get a compile error saying that pointers of one type can't be cast into pointers of another type. I can't figure out what to do here.

Here's a short code sample that demonstrates the error:

geos::geom::CoordinateSequence* temp = factory->getCoordinateSequenceFactory()->create((std::size_t) 0, 0);

temp->add(geos::geom::Coordinate(0, 0));
temp->add(geos::geom::Coordinate(100, 0));
temp->add(geos::geom::Coordinate(100, 100));
temp->add(geos::geom::Coordinate(0, 100));
temp->add(geos::geom::Coordinate(0, 0));

geos::geom::LinearRing *shell=factory->createLinearRing(temp);

temp = factory->getCoordinateSequenceFactory()->create((std::size_t) 0, 0);

temp->add(geos::geom::Coordinate(1, 1));
temp->add(geos::geom::Coordinate(10, 1));
temp->add(geos::geom::Coordinate(10, 10));
temp->add(geos::geom::Coordinate(1, 10));
temp->add(geos::geom::Coordinate(1, 1));

geos::geom::LinearRing *hole=factory->createLinearRing(temp);
holes->push_back((geos::geom::Geometry*) hole);

factory->createPolygon(shell,holes);

Any suggestions?

Was it helpful?

Solution

I solved it.

I had an include line that included geos/geom/GeometryFactory.h. In that file, there was a forward-declaration to geos::geom::LinearRing, but it didn't say that that class was a subclass of geos::geom::Geometry. Therefore, the compiler treated it as two different classes. Having #include <geos/geom/LinearRing.h> fixed it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top