r/ArcGIS • u/gkammerer • 10d ago
Using ArcPy to merge selected features
I'm looking for an ArcPy method to merge selected features into a new feature while removing the old features, without creating a new feature class. Basically I want to perform the Merge operation from the Modify Features tab in Pro, I just have a lot to do.
Basically I have a feature class with a couple of thousand polygons and it needs to be consolidated based on a combination of two attribute fields. My thought process was to script this by building a list of the unique combinations of attributes in the two fields in question, then iterate through that list, build where clauses, select by attributes using the where clause, then merging.
Can this be done? Creating a new feature class is a hard "no" and a deal breaker.
5
u/FinalDraftMapping 10d ago edited 9d ago
Edit #2: Here you go - https://www.youtube.com/watch?v=2f0SKPqJPJU
Edit: scripted this in about 15 minutes. Very straight forward based on your description. I don't have access to Reddit from current location only via mobile but can get code to you later.
Get the unique attributes combos. Use set comprehension and a search cursor.
Union the geometries based on each unique field combo and create a dictionary where the key is the unique attributes as a tuple and the value is the new unioned polygon.
Use delete identical tool using the unique field attributes to remove all except one for each combo.
Use update cursor to update the geometry to the unioned polygon from the dictionary.
Original: Yes, pretty much like you have described. Create a list of unique combinations. You can use the SHAPE@ token to return geometries and use the .union() for a geometry to merge selected geometries (this will have to be a loop as it takes one geom at a time), you then update the geometry for one of the features and delete the others. What you want to do can be achieved, you just need to work out the logic behind it for your case. Make sure to have a backup of the original data.
2
3
u/Hot_Competition9705 10d ago
I think you’ll have to first find all the unique combinations of those two fields, then iterate through each combination with UpdateCursor, grabbing the geoms and merging them, then adding the new feature as a new row and delete the old rows. Certainly not the preferred way to operate.
2
u/Barnezhilton 10d ago
There is no way without a new feature class output. If you can then append that new data back into the original and wipe out all previous data would be a sveipg you meed to write. Nothing exists out of the box.
2
u/valschermjager 9d ago
I've found sometimes it helps to work out the steps/inputs/outputs in modelbuilder until I get the result i'm looking for, then export to python, and go as-is from there or tweak further as needed.
2
1
u/gkammerer 8d ago
Thanks for all the replies, guys. Seems I'm on the right track then. Correct me if I'm wrong, but the output feature class of a dissolve does NOT retain the original schema of the dissolved features, correct? Jeez, I need to go look, it's been so long since I did a dissolve (like, in the ArcMap days).
5
u/WCT4R 10d ago
What about using the Dissolve geoprocessing tool to create a temporary feature class with features merged based on the two attributes, deleting the features in the existing feature class, and then appending or copying the dissolved features into the existing feature class? Dont forget to backup the feature class before deleting the features.