I have 2 layers, one with land plots and one with hazard zones. I want to iterate through the layer with land plots and for every plot I want to add 2 (or 3, depending on some variables) fields. Then I calculate the data for these fields, based on the attribute values of the overlapping hazard zone, and fill it in, in the new fields. I'm trying to do this by using the searchcursor to iterate through the plots and get their geometry (I need the geometry to decide if they overlap, touch, intersect etc. with the hazard zone, which affects the calculated data for the new fields).

Next to all this I try to create an updatecursor and loop through both cursors at the same time using zip(), to update the newly created empty fields immidiately after calculating their proper value.

I'm trying to do this in 1 for loop to save time by only iterating the plots once. But the UpdateCursor doesn't work properly. The 2 new fields are Null for all plots except the last plot, that one has the proper values.

Any clues to what is happening here?

I have printed the variables which should be added and they are not Null.

Start of the loop:

        fields_og = arcpy.ListFields(outLayer)
        fields = ['SHAPE@']
        for field in fields_og:
        with arcpy.da.SearchCursor(outLayer, fields) as features:

            if atype == 'SHORT':
                arcpy.AddField_management(outLayer, attrn, atype, 10, 0)
            elif atype == 'DOUBLE':
                arcpy.AddField_management(outLayer, attrn, atype, 10, 2)
                arcpy.AddField_management(outLayer, attrn, atype)

            attrn_s = attrn + "_s"
            minmax_s = attrn + "_mm"
            arcpy.AddField_management(outLayer, attrn_s, 'TEXT')
            out_fields = [attrn, attrn_s]
            minmax = False
            if len(a_minmaxfield) > 0:
                arcpy.AddField_management(outLayer, minmax_s, 'TEXT')
                minmax = True

            with arcpy.da.UpdateCursor(outLayer, out_fields) as cursor:

                for feature, row in zip(features, cursor):

After some calculations etc. I want to update like this:

                    if minmax:

                    if minmax:
                        row[-3] = val2add
                        row[-2] = b
                        row[-1] = c
                        row[-2] = val2add
                        row[-1] = b

  • 2
    This is not a valid flow of control construct. You can't reprocess the search cursor once it's empty. Instead, cache the values you need from the SearchCursor in a dictionary or list once, then make one pass though the UpdateCursor.
    – Vince
    Commented Aug 14, 2019 at 11:30
  • @Vince What do you mean by "You can't reprocess the search cursor once it's empty."? I already started separating the two cursors because it clearly doesn't work but I don't entirely understand why it doesn't. I don't have much experience yet with ArcPy.
    – Jan-Pieter
    Commented Aug 14, 2019 at 11:38
  • My thinking was just that it would save some time because I only had to iterate the plots once instead of twice. BTW saving them in a list in SearchCursor and accessing the list in UpdateCursor works.
    – Jan-Pieter
    Commented Aug 14, 2019 at 11:46
  • 1
    It is unclear to me what you are trying to do. Please describe the task in better detail. I Think you are missunderstanding what a cursor does. For example you are trying to iterate over each row in the attribute table, trying to add the same field for each row. Why use the SearchCursor to add fields? This cant be what you want.
    – Bera
    Commented Aug 14, 2019 at 12:29
  • 2
    Why are you trying to use the SearchCursor on the same layer as the UpdateCursor and iterate over these at the same time? You should only need the updatercursor. But I would recommend using Spatial Join instead of trying to compare geometries using nested cursors. Then when you have the hazard zone attributes joined to the plots use Field Calculator or UpdateCursor to calculate your fields by some logic.
    – Bera
    Commented Aug 14, 2019 at 13:32


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.