1

I am inserting new rows into a POINT FeatureClass within a geodatabase. The way I do it is by creating an InsertCursor over an empty FeatureClass and inserting new rows while loopinf through the values of a table using a SearchCursor. At the beginning of each loop, I need to be able to create a search cursor on the FeatureClass, which, at the bigninning of the second loop for example, should contain one row. However, it is always empty. I guess it might be a problem of the InsertCursor not refreshing itself at each loop, making the creation of the SearchCursor to the FeatureClass to be always empty.

Below is my code. Basically, at line 6, search_fc seems to be always empty causing the next line (for elem in search_fc) to be False and the for loop to be skipped (the debug goes directly to line 14 (the if statement)). This should ONLY happen at the beginning, when output_fc is empty.

    with arcpy.da.InsertCursor(output_fc, fields) as fc_cursor:
        table_field = [IDfield, ADDRESSfield]
        table_cursor = arcpy.da.SearchCursor(address_table,table_field)
        for row in table_cursor:
        try:
            search_fc = arcpy.da.SearchCursor(output_fc,fields)
            for elem in search_fc:
                if row[1] != elem[1]:
                    continue
                arcpy.AddMessage("Address {0} already geocoded".format(row[1]))
                field = [row[0],row[1]] + elem[2:]
                fc_cursor.insertRow(field)
                break
            if not row[1] in [elem[1] for elem in search_fc]:
                arcpy.AddMessage("Geocoding {0}".format(row[1]))
                print 'Geocoding ' + row[1]
                address_google, latitude, longitude, precisione = geocode(row[1])
                xy = (longitude,latitude)
                field = [row[0],row[1],address_google,precisione,xy]
                fc_cursor.insertRow(field)
                count = scriviglobal(today)
                sleep(WAIT_INTERVAL)
        except Exception as e:
            print e.message
            sleep(WAIT_INTERVAL)
        finally:
            print 'something'
5
  • 4
    I would recommend removing the try/except statement for debugging and also properly format the with statement. It is difficult to determine what you are trying to do--perhaps you can add comments or a brief outline.
    – Aaron
    Mar 1, 2016 at 12:34
  • 1
    In addition to Aaron's recommendation of removing the try/except clause you should also correct some of the indentation problems with your sample code (line 2 should start an indent block). Also, try creating all of your cursors using the with statement like your InsertCursor.
    – Evan
    Mar 1, 2016 at 14:54
  • @Evan Thanks, it was a mistake of copy and paste however.
    – umbe1987
    Mar 1, 2016 at 15:03
  • @Aaron I removed the try/except and tested it again w/o success. What do you mean with "properly format the with statement"? My aim is to geocode some addresses in a table through the Google API, and output a feature class. In the loop, the first round search_fc cursor should tell there are no rows in the featureclass, and the geocoding is performed. In the other rounds, the aim is to avoid redoing the geocoding if the featureclass already contains the address to be geocoded (that's why I amcreating a search cursor each time).
    – umbe1987
    Mar 1, 2016 at 15:03
  • I see you corrected the indentation issue in the with statement--thanks.
    – Aaron
    Mar 1, 2016 at 19:23

1 Answer 1

1

Finally I got it working.

The problem is that I cannot create a cursor on a feature class while another cursor has already been created on the same feature class. This seems to cause a conflict. In my code, I create an InsertCursor on the output_fc and in the loop I am trying to read in the row of the same feature class with the SearchCursor. If I simply delete one cursor before creating the other one, the whole process works fine.

Obviously, I had to adjust my code (first of all, I removed the with statement at the beginning). Now my code looks like this (insert_addr is just a function which creates an insert cursor and deletes it at the end):

table_field = [IDfield, ADDRESSfield]
table_cursor = arcpy.da.SearchCursor(address_table,table_field)
for row in table_cursor:
    codificato = False
    search_fc = arcpy.da.SearchCursor(output_fc,fieldList)
    for elem in search_fc:
        if row[1] != elem[1]:
            continue
        arcpy.AddMessage("Address {0} already geocoded".format(row[1]))
        print 'indirizzo ' + row[1] + ' gia geocodificato'
        field = []
        field.append(row[0])
        field.append(row[1])
        field.append(elem[2])
        field.append(elem[3])
        field.append(elem[4])
        del search_fc
        insert_addr(output_fc,fieldList,field)
        codificato = True
        break

    if codificato == False:
        if not row[1] in [elem[1] for elem in search_fc]:
            arcpy.AddMessage("Geocoding {0}".format(row[1]))
            print 'Geocoding ' + row[1]
            address_google, latitude, longitude, precisione = geocode(row[1])
            xy = (longitude,latitude)
            field = [row[0],row[1],address_google,precisione,xy]
            del search_fc
            insert_addr(output_fc,fieldList,field)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.