<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculate value function that reads feature class path in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/calculate-value-function-that-reads-feature-class/m-p/1208247#M65444</link>
    <description>&lt;P&gt;Thanks for the reply, I guess I am not understand how to put the code/model together to work.&lt;/P&gt;&lt;P&gt;Is there another way that this can be accomplished?&lt;/P&gt;</description>
    <pubDate>Wed, 31 Aug 2022 15:46:50 GMT</pubDate>
    <dc:creator>2Quiker</dc:creator>
    <dc:date>2022-08-31T15:46:50Z</dc:date>
    <item>
      <title>Calculate value function that reads feature class path</title>
      <link>https://community.esri.com/t5/python-questions/calculate-value-function-that-reads-feature-class/m-p/1207367#M65426</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Expression: &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;f(fc)&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Code block:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2022 19:47:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-value-function-that-reads-feature-class/m-p/1207367#M65426</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2022-08-29T19:47:00Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate value function that reads feature class path</title>
      <link>https://community.esri.com/t5/python-questions/calculate-value-function-that-reads-feature-class/m-p/1207836#M65439</link>
      <description>&lt;P&gt;I would look at separating your operations.&amp;nbsp; If you are building a dictionary in the first part, do that in a SearchCursor.&amp;nbsp; Drop the cur.reset().&amp;nbsp; Then with an UpdateCursor, iterate over the key, group dictionary and do your update.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2022 18:08:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-value-function-that-reads-feature-class/m-p/1207836#M65439</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-08-30T18:08:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate value function that reads feature class path</title>
      <link>https://community.esri.com/t5/python-questions/calculate-value-function-that-reads-feature-class/m-p/1208247#M65444</link>
      <description>&lt;P&gt;Thanks for the reply, I guess I am not understand how to put the code/model together to work.&lt;/P&gt;&lt;P&gt;Is there another way that this can be accomplished?&lt;/P&gt;</description>
      <pubDate>Wed, 31 Aug 2022 15:46:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-value-function-that-reads-feature-class/m-p/1208247#M65444</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2022-08-31T15:46:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate value function that reads feature class path</title>
      <link>https://community.esri.com/t5/python-questions/calculate-value-function-that-reads-feature-class/m-p/1208342#M65445</link>
      <description>&lt;P&gt;This is untested and to just show you the splitting of two operations that you are doing that I think would replace the reset().&amp;nbsp; Probably need an iterator in the each cursor somewhere? That's something you'll have to test and see.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;merged_polys = None
with da.SearchCursor(fc, fields, sql_clause=(None, sql_orderby)) as cur:
        case_func = itemgetter(*range(len(case_fields)))
        merged_polys = {
            key:reduce(arcpy.Geometry.union, (r[-1] for r in group))
            for key, group
            in groupby(row, case_func)
        }

with da.UpdateCursor(fc, fields, sql_clause=(None, sql_orderby)) as uCur:
    for key, group in groupby(uCur, case_func):
        row = next(group)
        uCur.updateRow(row[:-1] + [merged_polys[key]])
        for row in group:
            uCur.deleteRow()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alternative is add print statements to see where it is getting stuck in the loop and go from there.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Aug 2022 17:50:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-value-function-that-reads-feature-class/m-p/1208342#M65445</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-08-31T17:50:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate value function that reads feature class path</title>
      <link>https://community.esri.com/t5/python-questions/calculate-value-function-that-reads-feature-class/m-p/1209441#M65480</link>
      <description>&lt;P&gt;Sorry for the late response. I wasn't sure if you meat separate the two into two separate calculate values, how I was suppose to call the first calculate value to the second calculate value. So I figured why the model didn't stop, FLD_ZONE had some empty attributes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the following it runs but nothing is merged, no errors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;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:
        for row in cur:
            print (row)
            if row[0] == None:
                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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also tried separating the operations like you stated but nothing gets merged, with error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Error:&lt;/P&gt;&lt;P&gt;ERROR 000539: Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;expression&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 38, in fc1&lt;BR /&gt;TypeError: 'NoneType' object is not subscriptable&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;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)

    merged_polys = None
    with da.SearchCursor(fc, fields, sql_clause=(None, sql_orderby)) as cur:
        case_func = itemgetter(*range(len(case_fields)))
        for row in cur:
            print (row)
            if row[0] == None:
                merged_polys = {
                    key:reduce(arcpy.Geometry.union, (row[-1] for row in group))
                    for key, group
                    in groupby(cur, case_func)
                    }
            else:
                pass

    del cur

    with da.UpdateCursor(fc, fields, sql_clause=(None, sql_orderby)) as uCur:
        for key, group in groupby(uCur, case_func):
            row = next(group)
            uCur.updateRow(row[:-1] + [merged_polys[key]])
            for row in group:
                uCur.deleteRow()
    return fc&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Sep 2022 22:26:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-value-function-that-reads-feature-class/m-p/1209441#M65480</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2022-09-02T22:26:56Z</dc:date>
    </item>
  </channel>
</rss>

