Union of arbitrary number of overlapping polygons. #947
|
@barendgehrels is there an algorithm allowing to calculate union of any number of potentially overlapping (multi-)polygons? In buffer maybe? If there is does it work for all coordinate systems? |
Replies: 4 comments 10 replies
|
No, we don't have that out of the box. All our algorithms work on either one or two. |
|
I was thinking about buffer because a union of multiple overlapping rings of buffer parts is calculated there. The purpose is not to calculate valid multipolygon from an invalid one (like in the alternative for dissolve) but to calculate a union of some number of polygons like AFAIU it's done in buffer for parts. |
|
This works well: and you can call it by for example |
|
The following implementation seems to be quite fast too. Faster than the recursive one in my case (spatially ordered polygons) template <typename MutiPolygonType>
void cascadeUnion(std::vector<MutiPolygonType>& to_unify)
{
size_t step = 1;
size_t half_step;
// the outer loop doubles the distance between two
// polygons to be merged at every iteration
do
{
half_step = step;
step *= 2;
size_t i = 0;
// the inner loop merges polygons at i and i+half_step storing the result at i
do
{
MutiPolygonType unified;
bg::union_(to_unify.at(i), to_unify.at(i + half_step), unified);
to_unify.at(i) = std::move(unified);
i += step;
} while (i + half_step < to_unify.size());
} while (step < to_unify.size());
} |
This works well: