I need some help with some nested Search/Insert Cursors adding data to a table and to a point feature class.
- What I have:
- I have one big table with stations 1-n with for example 10,000 rows for each station.
- A point feature class in a file geodatabase. - What I want:
- I want to copy the rows for each station into a newly created table (all attributes).
- AND I want to copy the first row from this new table into the point feature class but only with the relevant fields (5 attributes) adding its point to the geometry of course. - What I get:
I managed to create one new table and copy the table content into the new table. The script runs without giving an error but in the end I don't have a new point in my feature class.
When I try to let it run for more than one station I get the RuntimeError: workspace already in transaction mode (below in Row 14).
When I remove the part with creating the point I can create as many tables as I want.
Printing the values that are supposed to be inserted into the point feature class (fillValues) they look good to me. - What I also already tried:
I just tried also with removing the table relevant part (since they are all created now). Now I get an IndexError: list index out of range pointing to my FirstObject. But it first runs through the whole script - but again without adding my point 😞
I also tried without adding the OBJECTID but with the same result.
I would be very happy if someone could push me in the right direction to make this work.
Here is simplified version of my module:
table=input table
fieldnames = [field names from input table except OBJECTID]
pfields=arcpy.ListFields(points)
fillnames=[field names for point feature class, manually created, not OBJECTID, not SHAPE]
# Create cursors and insert new rows
for profile in profile_nrs:
expression="Station="+str(profile)
with arcpy.da.SearchCursor(table,fieldnames,expression) as sCursor:
for sRow in sCursor:
table_name=str(sRow[0])+"_"+str(sRow[1])+"_data1"
with arcpy.da.InsertCursor(table_name,fieldnames) as iCursor:
for sRow in sCursor: -> This is where I get the RuntimeError
iCursor.insertRow(sRow)
del iCursor
del sRow
del sCursor
print("Table "+table_name+" filled")
# From here starts the point feature creation
#Identify first OBJECTID used in new table
oid_nrs=unique_values(table_name, "OBJECTID")
FirstObject=oid_nrs[0]
#Define expression to limit the extracted rows to the first row
pexpression="OBJECTID="+str(oid_nrs[0])
#Define field names used for point table
pfieldnames=['Cruise','Station', 'Type', 'Date', 'Longitude', 'Latitude']
print(pfieldnames)
pfieldsvalues=[FirstObject]
for pname in pfieldnames:
with arcpy.da.SearchCursor(table_name, pname, pexpression) as pCursor:
for pRow in pCursor:
fillvalue=pRow[0]
print(fillvalue)
pfieldsvalues.append(fillvalue)
ShapeVal=[pfieldsvalues[5],pfieldsvalues[6]]
print(fillnames)
#take the values extracted from the first row in the new table and put
#them into a new array adding OBJECTID and Shape (Geometry)
fillValues=[pfieldsvalues[0], ShapeVal, pfieldsvalues[1], pfieldsvalues[2], pfieldsvalues[3], pfieldsvalues[4], pfieldsvalues[5], pfieldsvalues[6]]
print(fillValues)
newPoint=arcpy.da.InsertCursor(points, fillnames)
newPoint.insertRow(fillValues)
print("point added to All_points")
del pRow
del pCursor