Is there an ArcPy method to merge polylines within the same layer?

4752
13
07-03-2018 03:44 PM
GuyBeels
New Contributor II

Is there a way using ArcPy to merge features selected from the same layer into a new feature, exactly as the Edit > Merge tool does it?  I want to script this, because several dozen merges need to be done on a series of feature selections (iterating through a list of saved SQL expressions).  I looked, but could not find. 

I am working in ArcGIS Pro 2.0, but I also have ArcGIS Desktop 10.6 available.  Thanks for your help.

Tags (2)
0 Kudos
13 Replies
DarrenWiens2
MVP Honored Contributor

I'll try to steer you toward creating a new field and populating it by an ID that you can use to either merge together or leave alone, then use Dissolve to make your new feature class. Selections are very slow through Arcpy. An alternative would be to loop through your features using an UpdateCursor and use the Polyline union method to make your new geometries (one feature at a time), but this would be quite a bit more complicated.

0 Kudos
GuyBeels
New Contributor II

Due to the attribution scheme I used, that would be really complicated.  (But thanks - your reply was much appreciated, I'm likely to use it at some point.)  My workaround, to be scripted, is to iterate this:  1) select by attributes (over a list of stored expressions), 2) make layer from selected features, 3) export the layer to a feature class, 4) select all from the feature class, and 5) dissolve.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Sharing with Python‌ and Analysis

JoshuaBixby
MVP Esteemed Contributor

This is possible, but how do you want to handle the merge?  For example, if you have multiple features selected, do you want the lowest OID feature to become the new merged feature and the other features gets deleted, or do you want a new merged feature created (new OID) and all the old features removed?

0 Kudos
GuyBeels
New Contributor II

Joshua, the Dissolve will create a very simple output feature class (single feature with just 3 or 4 attributes, one of them being an initially <Null> name field to be populated as the last step in the loop).  Nothing from the source dataset will be disturbed.  After the iterations, all of the identically structured one-feature output feature classes will be Merged into a single one.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

In your original post you mention merge, now you are talking dissolve.  Do you want to modify the original data set like Edit > Merge does or create a new data set?

0 Kudos
GuyBeels
New Contributor II

Create a new dataset.  I've given up on trying to find the kind of Merge I want in ArcPy.  I can make my flow work with Dissolve - with a few more steps than I think should be necessary, but that's life.  Darren's earlier response put Dissolve in my head, and I made that flow work in a testbed project.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I am fairly certain it can be done in-place, like Edit > Merge, but how the code is structured depends on the answers to the questions I originally asked about how you want to handle merged features in the original data set.

0 Kudos
GuyBeels
New Contributor II

Edit > Merge does work fine for what I want to do, using New Feature option to leave existing features alone - but only manually.  I can find no ArcPy function (the original question) for this.  And doing this many times manually is no way to spend an evening.  (Mine, anyway.)

0 Kudos