Anyway, back to my problem at hand. I believe I understand what is causing
my problem but I do not understand why it is a problem since it seems to me
like it should be a valid use case. If I take a union of two polygons and
place the result in a multi_polygon, that succeeds. However, if I take a
multi_polygon and then repeatedly union the polygons into that multi_polygon
it fails.
Below is an adaptation of the code example for union
(http://www.boost.org/doc/libs/1%5f55%5f0/libs/geometry/doc/html/geometry/reference/models/model_multi_polygon.html)
that demonstrates my problem:
-----------------------
// Display some info about a multi_polygon
void check(const multi_polygon& mp)
{
std::cout << " Size: " << mp.size() << std::endl;
for(int i = 0; i < mp.size(); ++i)
{
std::cout << " Num points: " << boost::geometry::num_points(mp[i])
<< std::endl;
}
std::cout << " Intersects: " << boost::geometry::intersects(mp) <<
std::endl;
}
int main()
{
polygon green, blue;
// Using polygons from example - details are unimportant, any overlapping
polygons would do
boost::geometry::read_wkt(
"POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3
2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
"(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", green);
boost::geometry::read_wkt(
"POLYGON((4.0 -0.5 , 3.5 1.0 , 2.0 1.5 , 3.5 2.0 , 4.0 3.5 , 4.5 2.0 ,
6.0 1.5 , 4.5 1.0 , 4.0 -0.5))", blue);
// Try 1 [WORKS]: Take union of two polygons and put result into
multi_polygon
multi_polygon output1;
boost::geometry::union_(green, blue, output1);
std::cout << "output1 = union(green,blue)" << std::endl;
check(output1);
std::cout << std::endl;
// Try 2 [FAILS]: Build up multi_polygon by repeatedly unioning in each
polygon
multi_polygon output2;
boost::geometry::union_(output2, green, output2);
std::cout << "output2 = union(output2,green)" << std::endl;
check(output2);
boost::geometry::union_(output2, blue, output2);
std::cout << "output2 = union(output2,blue)" << std::endl;
check(output2);
return 1;
}
-----------------------
Result (Using Boost 1.55):
output1 = union(green,blue)
Size: 1
Num points: 27
Intersects: 0
output2 = union(output2,green)
Size: 1
Num points: 17
Intersects: 0
output2 = union(output2,blue)
Size: 2
Num points: 17
Num points: 27
Intersects: 1
-----------------------
It appears that when I repeatedly union the polygons in one at a time, then
it does not do what I expect. I would expect the above two cases to be
equivalent, but when I add polygons one at a time, then the resulting
multi-polygon has multiple parts and keeps the first polygon in addition to
the union. Therefore it self-intersects and cannot be used for further
calls.