Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am writing a arc 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 slow because I am using a shapefile? Does a file geodatabase help with this? Or is this even just the totally wrong approach for merging polygons.

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.

share|improve this question
add comment

3 Answers 3

Hallo

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 here

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.

EDIT: 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;

Cheers Nicklas

share|improve this answer
    
+1 for cascaded union technique, however, I'm not sure if Esri-folk have something similar to this –  Mike T Jul 13 '11 at 23:58
    
Great comment! Altougth this is an ESRI question, it still applies. I'll try to whip something up here and see what are the speed differences and I'll post it here. Very informative +1. –  George Jul 14 '11 at 11:07
add comment

Are you 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).

Cheers

share|improve this answer
    
The bulk deletes may help, I will try that out. I can't eliminate stores sadly, but maybe I can find a way to bulk store to save on disk calls... (clarified in my question of what the process is doing). –  baens Jul 13 '11 at 21:03
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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