ThinkGeo.com    |     Documentation    |     Premium Support

Buffer Performance

Hi,

I get a lot of shape data with infrastructure. It’s complex and a lot of elements in each shapefile. I almost always have to apply some buffer to the shapes that has a union to ensure overlapping polygons are merged. Code like this…

image

But, this code is dreadfully slow and there is no way to monitor it’s progress or cancel it. Is there anyway to improve upon it?

I’m still using v10.

Thanks,
Damian

Damian,

Union() is from NTS(Net Topology Suite) and I’m afraid not too much we can do about its performance. But you can union one shape per time which gives you a chance to cancel the process, something like this:

        var result = new MultipolygonShape();
        for (int i = 0; i < shapesToMerge.Count; i++)
        {
            if (isCanceled)
                result = AreaBaseShape.Union(new[] { result, shapesToMerge[i] });
        }

And put this to another thread or async method to make it more responsive, For example you can use async method, pass in the CancellationToken to mark if the method was being canceled.

I hope that make sense. Let us know if you have more questions.

Thanks,
Ben

Thanks Ben, I will give it a try.

Regards,
Damian

Sure, just let us know if you have more questions.

Hi Ben,

There are some issues with your syntax above. I had to change a few things like the new[].

While testing this though I found that my bottleneck was not always about the union. Seems that there are some performance issues with buffering Multi-type shapes. I found that doing this

MultipolygonShape result = MultipolygonShape.Buffer(X);

took up to 60 times longer than this at least with my test shape.

List PolygonShape result = new List PolygonShape();
foreach(polygon in MultiPolygonShape.Polygons)
result.AddRange(polygon.Buffer(X));

return new MultipolygonShape(result);

Can you confirm this result?

Regards,
Damian

Hi Damian,

Buffer the sub polygons individually and then combine them together is faster than buffering the entire multiPolygon. But their results are a bit different.

  • Buffering the MultiPolygon as a whole

    • Approach : When you buffer a MultiPolygonShape directly, the buffering process considers all the polygons as part of a single complex shape. This means that the buffer algorithm takes into account any interactions between the polygons, such as overlaps or edges that are close to each other.
    • Result : The buffer around the entire shape may merge polygons that are close to each other when the buffer distance is sufficiently large. If polygons are adjacent or their edges are close, the buffered areas might merge into a single larger polygon in areas where they overlap or touch.
  • Buffering each Polygon separately and then combining :

    • Approach : Buffering each polygon individually and then combining them means each polygon is considered independently. The buffer is applied without regard to neighboring polygons. After buffering, you simply collect all the buffered polygons into a MultiPolygonShape .
    • Result : This method results in individual buffered shapes for each polygon. If two polygons are close to each other, their buffered forms will also be close but initially separate. When combined into a MultiPolygonShape , unless a union operation is explicitly performed, they remain distinct entities within the multipolygon structure.

If the result of buffering individually polygon works for you, you can go with that to have better performance. Otherwise, you might need to buffer the combined multipolygons.

Thanks,
Ben