6

I am writing an ArcGIS Engine application that can take a shapefile, and merge a set of polygons together.

Here is the code that I am using to merge the polygons:

private static void mergeFeatures(IFeature merger, IFeature mergee)
{
    ITopologicalOperator union = (ITopologicalOperator)mergee.Shape;
    mergee.Shape = union.Union(merger.Shape);
    merger.Delete();
    mergee.Store();            
}

Is this the best solution to merge polygons?

In my application this part of my code takes 60% of the execution time.

Is it slower to use a shapefile than a file geodatabase help to do this?

Just to clarify, I am taking a large set of polygons and am trying to merge ones under a certain number of acreage. From these polygons that are small, I need to merge polygons with similar attributes (for example on a vegetation layer, ones that are similar vegetation.) So this can incur a large number of small polygons getting merged together, and eliminating some of the merges can be difficult. If there is an easier way to bulk merge, then that would be best, but thus far, I have not found one.

3 Answers 3

5

Try using a profiler.

You are making multiple delete and store calls. These are expensive:

Try:

  • Reducing the number of times you call each function (store and delete);
  • Merge all polygons, store only once (or a few times, after 'n' iterations);
  • Delete only once (check ITable.DeleteSearchedRows(IFilter));

To delete only once, you can store a list of integers, those being the objectid of the features you are merging into the main feature. After that, make a single IN filter and delete them all with ITable interface (you can cast it from IFeatureClass).

0
4

Merging or union as it is also often called can be more or less expensive depending on how the polygons are put together. If you start a one side and merge one polygon after another onto the resulting bigger and bigger polygon there will be a lot of work traversing the bigger resulting polygon for each added small polygon. One way to make this more effective is to not build on one resulting polygon but to merge them together two and two, and then merging the resulting polygons and so on.

You can read about it at Fast polygon merging in JTS using Cascaded Union

Since it is implemented in JTS it is also in GEOS and PostGIS.

Since you ask about a faster way to do this I would try a query something like:

CREATE TABLE merged_polygons AS
SELECT ST_Union(the_geom) AS the_geom, attribute1, attribute2
FROM original_table
WHERE ST_Area(the_geom) > min_area
GROUP BY attribute1, attribute2;

Here you will get a new table with polygons merged that share both attribute 1 and attribute 2 and have an area bigger than some min_area.

Probably an overall fast way of doing it.


And if you also want the bigger polygons in your new table it could look like this:

CREATE TABLE merged_polygons AS
SELECT ST_Union(the_geom) AS the_geom, attribute1, attribute2
FROM original_table
WHERE ST_Area(the_geom) > min_area
GROUP BY attribute1, attribute2
UNION ALL
SELECT the_geom, attribute1, attribute2
FROM original_table
WHERE ST_Area(the_geom) >= min_area;
0
2

The right approach to unioning efficiently multiple polygons in ArcObjects is to store all polygons in a GeometryBag, create a new polygon using ITopologicalOperator interface and union the geomtrybag with the ConstructUnion method.

You have a complete example in 'How to create a union of several polygons' from the ArcGIS Resource Center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.