I am working on creating a addon button to update some address points and copy that updated feature. The first part of the code does what it is suppose to do (only update the selected feature) but the second part of the code doesn't do what it is suppose to do and that is copy "only" the selected feature to the file geodatbase feature class. The code runs fine i don't get any errors but the second part of the codes doesn't copy the selected feature.
I would also like to an an "if" statement. if there is no feature selected i want it to do nothing or tell me there is nothing selected. if there is something selected i want it to only updates the selected feature and only copy the selected feature. Any ideas?
import arcpy, time import pythonaddins import os #populate selected feature APT = "TEST" arcpy.env.overwriteOutput = True mxd = arcpy.mapping.MapDocument("CURRENT") if int(arcpy.GetCount_management(APT).getOutput(0)) > 0: rows = arcpy.UpdateCursor(APT) for row in rows: row.FacltyType = ("Single Family Home") row.StructType = ("Primary, Private") row.Verified = ("Yes, GRM, TA") row.Status = ("Active") row.StructCat = ("Residential") row.APA_CODE = ("1110") rows.updateRow(row) del row, rows #Copies address point to backup address points in filegeodatabase fc1 = "CCAP" #Database workspace = r"C:\GIS\CCAP\CCAP.mdb" arcpy.env.overwriteOutput = True # Start an edit session. Must provide the worksapce. edit = arcpy.da.Editor(workspace) # Edit session is started without an undo/redo stack for versioned data # (for second argument, use False for unversioned data) edit.startEditing(True) # Start an edit operation edit.startOperation() if int(arcpy.GetCount_management(APT).getOutput(0)) > 0: arcpy.Append_management(APT, fc1, "NO_TEST") # Stop the edit operation. edit.stopOperation() # Stop the edit session and save the changes edit.stopEditing(True) arcpy.RefreshActiveView()
Solved! Go to Solution.
after making the change to "incidentFCfield.editable() == True. i still get the error.
Runtime error
Traceback (most recent call last):
File "<string>", line 28, in <module>
TypeError: 'bool' object is not callable
thanks.
The error was due to the fact that I had added parentheses after the editable property. It was supposed to be identFCfield.editable not identFCfield.editable(). I have successfully run the code with your test data and it only copies the selected points and all of the matching fields had their data transferred when each backup point was created..
Richard the codes runs fine but if there is nothing selected i get
Traceback (most recent call last):
File "<string>", line 55, in <module>
NameError: name 'row' is not defined
If i remove the del row, del rows, del rowInserter the codes runs with points selected and with out points selected. Without points i do get the message "There are no features selected". I thought the "if" statment would handle it if nothing was selected?
Thank you for all your help Richard. I am trying to understand the list part in your code. could you or do you mind explaining what is happening in this line (if FCfield.name.upper() == incidentFCfield.name.upper() and FCfield.type == incidentFCfield.type and incidentFCfield.editable == True and not (FCfield.name.upper() in matchedFields): )
Do not remove the del statements, correct their indentation. For some reason the indentation of the del statements changed between post 19 and post 20 in this thread, and my code had the incorrect indentation. So the del statements were falling outside of the if block and happening no matter what the selection was. I have indented the del statements one level and now everything should work exactly as you want. The cursors will be cleaned up and the code will not throw any error.
The print of "There are no features selected" is what is supposed to happen if no points are selected, but now no error will occur.
So the code should now completely satisfy all of your requirements as a Correct Answer.
(if FCfield.name.upper() == incidentFCfield.name.upper() and FCfield.type == incidentFCfield.type and incidentFCfield.editable == True and not (FCfield.name.upper() in matchedFields):
This line checks to make sure that both the FC and incidentFC have the same field in them, then it makes sure both fields are the same type, then it makes sure the field is editable so that data can be written to it, and finally it makes sure the field is not already in the matchedFields list. If all these conditions are satisfied the field is appended to the matchedFields list.
Awesome, thank you.