1

I would like to create a custom merge tool that merges 2 polygons within 1 feature class together. I want to have a tool that merges 2 polygons and adds up a specific attribute. Is there any way to use the existing Merge tool in the editor toolbar and add custom code to it or have another way of merging 2 polygons together in the arcpy environment. enter image description here

Working with ArcGIS 10.2.1 and Python 2.7

  • You should be able to create a Python Addin to merge your polygons. Will you delete the input Polygons after the merge? – klewis Apr 20 '16 at 22:04
  • Can you please explain how? What tool (arcpy or other) would I use? – dklassen Apr 20 '16 at 22:06
  • Here is a tutorial, to create an Addin. desktop.arcgis.com/en/arcmap/10.3/guide-books/python-addins/…. Esri has good documentation. – klewis Apr 20 '16 at 22:09
  • Thanks, I have created several addin's in the past, and am familiar with the process. I just don't know how to, within arcpy, merge 2 features in 1 feature class together. The Merge tool in ArcToolbox is designed for 2 feature classes, not 2 features. Thanks – dklassen Apr 20 '16 at 22:18
  • You could run any geoprocessing tool such as Dissolve or Merge to an in-memory FC, then append the original FC. There are different options with Merge, Dissolve to Sum a field. – klewis Apr 20 '16 at 22:31
1

You can do this by working with the arcpy.Geometry objects. It is very easy to merge polygons once they are selected. As @klewis suggested, you could use an Add-In to select some geometries (draw a box to get an extent object, turn extent object into polygon, then select layer by location intersection of box).

Then to merge all the parts into one, just do something like this:

>>> mxd = arcpy.mapping.MapDocument('current')
>>> sr = arcpy.SpatialRefrence(mxd.activeDataFrame.spatialReference)
>>> array = arcpy.Array()
>>> with arcpy.da.SearchCursor("canb_bound", ['SHAPE@']) as rows:
...     for r in rows:
...         for part in r[0]:
...             # add each part of each selected polygon to array
...             array.append(part)
...             
>>> poly = arcpy.Polygon(array, sr)  # this is what you want, do whatever with it
>>> arcpy.management.CopyFeatures(poly, r'in_memory\multipart_poly_merge') 
  • You might want to add the other field you want stats for, then append those values to a list to perform whatever statistics on those values. – crld Apr 20 '16 at 22:38
  • Yep, that wouldn't be hard to work in, the above is just demonstrating a simple geometry merge. If the user is just adding some values, at each iteration in the top loop (not the parts), they could append the values to a list and run a sum(). – crmackey Apr 20 '16 at 22:42
  • Thanks for your code, good idea of perhaps how to start. I only mentioned the Editor>Merge tool, because this would allow the user to select which polygons attributes we would want to maintain. I am not sure if there is a way to harness this code/tool, and use parts of it within the custom merge tool? – dklassen Apr 20 '16 at 22:44

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.