I know I can do it with Dissolve, but the issue with Dissolve is that it creates a separate layer and I know I can do this a script but I need to in Model Builder. I need the function to merge feature class based on two attributes. Can this be down with Calculate value in model builder? I have tried the following, but it never seems to finish the process. The model function does seem to work, I check the feature class and the feature class features are merged. I have to manually hit stop.
Expression:
f(fc)
Code block:
import sys, arcpy, os
from arcpy import env
from arcpy import da
from itertools import groupby
from operator import itemgetter
from functools import reduce
fc = r"C:/TEMP/FEMA.gdb/FEMA_TempLyrs"
def f(fc):
case_fields = ["FLD_ZONE",'ZONE_SUBTY'] # field(s) to group records for merging
sort_field, sort_order = "OBJECTID", "ASC"
shape_field = "SHAPE@"
fields = case_fields + [sort_field, shape_field]
sql_orderby = "ORDER BY {}, {} {}".format(", ".join(case_fields), sort_field, sort_order)
with da.UpdateCursor(fc, fields, sql_clause=(None, sql_orderby)) as cur:
case_func = itemgetter(*range(len(case_fields)))
merged_polys = {
key:reduce(arcpy.Geometry.union, (row[-1] for row in group))
for key, group
in groupby(cur, case_func)
}
cur.reset()
for key, group in groupby(cur, case_func):
row = next(group)
cur.updateRow(row[:-1] + [merged_polys[key]])
for row in group:
cur.deleteRow()
del cur
return fc