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'
try/except
statement for debugging and also properly format thewith
statement. It is difficult to determine what you are trying to do--perhaps you can add comments or a brief outline.with
statement like yourInsertCursor
.with
statement--thanks.