Friday, 14 September 2018

arcgis desktop - Alternative to 'Editors Merge' feature using ArcPy


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




Answer



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


>>> g1, g2 = [f[0] for f in arcpy.da.SearchCursor("BufferedPoints","SHAPE@")]
>>> g1

>>> g2

>>> g3 = g1.union(g2)
>>> arcpy.CopyFeatures_management(g3,"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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...