I have a feature class that can have many features within it i would like to merge all the features in this feature class into just one based on the "BUFF_DIST" like you would in ArcMap when you start editing, select all the features and go to edit, merge and you get the popup "Chose the feature with which other features will be merged:" i am trying to avoid creating a new feature class. Anyone done this or have an example of some python code?
Solved! Go to Solution.
Hi 2CQuiker ,
Can you change the function "DissolveGeoms" for this one below and try again (on a copy of your data)?:
def DissolveGeoms(geoms):
cnt = 0
for geom in geoms:
cnt += 1
if cnt == 1:
diss_geom = geom
else:
diss_geom = diss_geom.union(geom)
return diss_geom
Hi 2CQuiker ,
During an editing session in Pro, you can select multiple polygon features and merge them:
...resulting in a single feature:
Is this what you are looking for?
yes i can do it on pro, i am trying to do it in a python script.
I have the following to start with but i am not quite here but my approach may not be the correct one.
BD = str('BUFF_DIST')
array = arcpy.Array()
with arcpy.da.SearchCursor(lyr, BD) as rows:
for r in rows:
for part in r[0]:
array.append(part)
poly = arcpy.Polygon(array, sr)
arcpy.management.CopyFeatures(poly, r'in_memory\multipart_poly_merge')
Hi 2 Quiker ,
Do you just want to merge the geometries or do you need merge rules on the attributes too? Doing this on the same featureclass could lead to loss of data if something in the script goes wrong. It is possible though.
When I come to think of it a little more... you have a field BUFF_DIST, so you buffered on an attribute and you could dissolve the result during buffering directly and no need to do any additional scripting.
The lyr just happened to be one that I was testing try to see how I can accomplish the task. The real layer i have is a different layer.
I tried the following with SHAPE@ and get the following. The other issue with this code is that none of the attributes get transferred, there is only OID, Shape in the new temp layer.
array = arcpy.Array()
with arcpy.da.SearchCursor(lyr, ['SHAPE@']) as rows:
for r in rows:
for part in r[0]:
array.append(part)
poly = arcpy.Polygon(array, sr)
arcpy.management.CopyFeatures(poly, r'in_memory\multipart_poly_merge')
If the shape (eg. row) has multiple parts, you will effectively just add all the parts back into the array and end up with the same polygon.
If you truncate the appending, you will end up with less than the whole. So to demonstrate... let's cut your 'cursor' at the first part skipping the rest.
type(p_0)
arcpy.arcobjects.geometries.Polygon
p_0.partCount
2
type(p_0[0])
arcpy.arcobjects.arcobjects.Array
sorry but i am not sure how i can incorporate what you posted into my code to do what i need it to do.
What I was pointing out that your approach of merging/appending/unioning a multipart shape is just going to leave you with a multipart shape and won't give you the desired result. Notice, you only search for the geometry then cycled through the geometry bits and appended the parts before copying all the parts back together to recreate the polygon. My demo was to show you what happens during the process when you terminate the append bit component.
arcpy.da.SearchCursor(lyr, ['SHAPE@']) as rows:
If you want to merge on some other field, you should be using that field in the search cursor, then you can append the polygons.
But... using the appropriate tool in arctoolbox is a lot better since it will check the geometries and perform the necessary intersections, and cleaning up the geometry as it goes. The reluctance to create a new file is a bit confusing... you can always add Delete_management to any script you want after you have done a Union_analysis
two layers. union geometry
inFeatures = your features
arcpy.Union_analysis(inFeatures, outFeatures,.... blah blah)
arcpy.Delete_management(inFeatures) # delete one or both
Delete—Data Management toolbox | ArcGIS Desktop
one layer, union geometry
arcpy.Dissolve_management(tempLayer, outFeatureClass, dissolveFields, "", "SINGLE_PART", "DISSOLVE_LINES")
How Dissolve (Data Management) works—Data Management toolbox | ArcGIS Desktop
Then delete the original if needed
I appreciate the info but I was trying to find a way to do this with out so many process.
Hi 2 Quiker ,
So, if I understand it correctly, your featureclass is not necessarily the result of buffering (so you cannot apply dissolve during buffering) and you don't want to create a new output (it should overwrite the featureclass) and you want to apply merge rules on the attributes. Sounds to me that you are in for a bit of coding. Do you consider that it is worth it? How large is your featureclass (how many features, how complex are they and how many features can be merged together)? Doing this in memory on a large and complex featureclass may run into limitations.