6

I'm looking for a work around that has had me in a pickle for sometime. It involves combining a multi-part feature polygon into a single feature. Obviously if you were editing a shape you would select the features in question, start editing then "Editor" > "Merge.

Seems simple enough. However, I'm looking to do this via QGIS/ArcPy/Python (any method that will work).

Is there a way to append to a new shapefile and have it move into one feature?

See the end result example below:

Before: before

After: After

9

You could use the arcpy.Geometry() object's .union() method:

>>> g1, g2 = [f[0] for f in arcpy.da.SearchCursor("BufferedPoints","SHAPE@")]
>>> g1
<Polygon object at 0x1929f830[0x1929f920]>
>>> g2
<Polygon object at 0x1929f730[0x1929f440]>
>>> g3 = g1.union(g2)
>>> arcpy.CopyFeatures_management(g3,"in_memory\unionres")
<Result 'in_memory\\unionres'>

Here I have a feature class with two polygons overlapping and after union there is just one.

enter image description here

Now you can use the arcpy.da.UpdateCursor() to delete one of the features (.deleteRow() method) you want to discard and then preserve another one (that you want to keep the attributes of) and update its SHAPE@ with the newly created arcpy.Geometry object.

  • Will look into this, Thanks for the code snippet Alex – Slevy May 20 '16 at 7:15
  • This worked for me, although I did not need the arcpy.CopyFeature_management line. I simply used the Update Cursor to replace the original geometry with the g3 geometry. – spaine Feb 24 '17 at 16:53
4

if the feature that need to be merged have a common attribute value (or if they all need to be merged), then you can use the dissolve tool (exists both in ArcGIS Data management -> Generalization -> Dissolve) and QGIS vector ->geoprocessing tools --> dissolve). In ArcGIS, there is an option to create (or not) multipart polygons. In QGIS you get multipart polygons by default. Note that there is a QGIS plugin if you want more control on your attribute table.

MULTI_PART —Specifies multipart features are allowed. This is the default.

SINGLE_PART —Specifies multipart features are not allowed. Instead of creating multipart features, individual features will be created for each part.

  • I didn't see the Multipart polygons part - this is a great help. Thanks Radouxju – Slevy May 20 '16 at 7:14
  • @Slevy, just fyi, you won't be able to use the Dissolve GP tool if the features don't share a common attribute value. From your sample, it doesn't look as they have. – Alex Tereshenkov May 20 '16 at 13:23
  • @AlexTereshenkov as mentioned in my answer, you can also use it for merging all features in the same feature class (non need for an common attribute then). Note that this is similar with your solution: either will you union all features or do you need some value to filter the features to be merge with your searchCursor. The main difference between our two method is that yours can work within the layer, while dissolve will create a new one. – radouxju May 20 '16 at 18:38
  • @radouxju, ah cool, thanks for pointing this out, I've never thought to use the Dissolve GP without specifying the dissolve field (as it does indeed merges all the features into one). Having it set to multipart will also merge overlapping and keep other features as a part of multipart features. Great! – Alex Tereshenkov May 21 '16 at 5:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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