arcpy.da.Editor setup for tool

2737
12
02-16-2017 03:29 PM
DevinUnderwood2
Occasional Contributor

I am not sure how I should have my syntax to use this with a tool ?

# Set Editing
workspace = r'C:\xyz'
edit = arcpy.da.Editor(workspace) 
edit.startEditing(False, True) 
edit.startOperation()
edit = arcpy.GetParameterAsText(3)

Tags (2)
0 Kudos
12 Replies
JoshuaBixby
MVP Esteemed Contributor

The last line definitely looks odd.  What is arcpy.GetParameterAsText(3) returning?  Better yet, can you elaborate a bit more on what you are trying to do?

MitchHolley1
MVP Regular Contributor

What is the goal of this script?

0 Kudos
DevinUnderwood2
Occasional Contributor

I want a user with the option to check the Start Editing after typing in the string FacilityID.

I am having trouble passing the selected feature to arcpy.GetParameterAsText() tool parameter.

Also, I have used editing arcpy syntax.

#Set to current mxd and dataframe
mxd = arcpy.mapping.MapDocument ("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
df.scale = 350
# Set Editing
workspace = r'C:\Users\abc.gdb'
edit = arcpy.da.Editor(workspace)  
edit.startEditing(False, True)  
edit.startOperation()
edit = arcpy.GetParameterAsText(3)
       
# Set the tool parameters
InputFeatureClass = arcpy.GetParameterAsText(0)
InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)

        
# SQL expression used in selecting a feature
# The equation needs to be enclosed with double quote marks, in addition, since the input FacilityID is string, it needs to be enclosed with #single quote marks. 
whereclause = """{} = '{}'""".format(arcpy.AddFieldDelimiters(InputFeatureClass,InputField),InputValue)
#Select feature by facilityid (InputValue)
arcpy.SelectLayerByAttribute_management(InputFeatureClass, "NEW_SELECTION", whereclause)

df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
0 Kudos
IanMurray
Frequent Contributor

You need to make the edit session starting dependent on whether or not the Starting Editting box is checked, right now it runs regardless.  I set the result of GetParameterAsText(3) to a variable(InputEdit) then created an if statement that would run if that variable was True.

workspace = r'C:\Users\abc.gdb'
mxd = arcpy.mapping.MapDocument ("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
df.scale = 350

# Set the tool parameters
InputFeatureClass = arcpy.GetParameterAsText(0)
InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)
InputEdit = arcpy.GetParameterAsText(3)

if InputEdit:
#Starting Editing Session
  edit = arcpy.da.Editor(workspace)  
  edit.startEditing(False, True)  
  edit.startOperation()

       

        
# SQL expression used in selecting a feature
# The equation needs to be enclosed with double quote marks, in addition, since the input FacilityID is string, it needs to be enclosed with #single quote marks. 
whereclause = """{} = '{}'""".format(arcpy.AddFieldDelimiters(InputFeatureClass,InputField),InputValue)
#Select feature by facilityid (InputValue)
arcpy.SelectLayerByAttribute_management(InputFeatureClass, "NEW_SELECTION", whereclause)

df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Not sure how you would logic the ending of the edit session into your code, since it seems like what you want your code to do is zoom to the select feature ID and edit if they choose.  You might have the user close the editting session within the map document itself when they are done.  What sort of edits would someone be doing to this feature(geometry, attributes, etc.)?

DevinUnderwood2
Occasional Contributor

Thanks for the feedback. You are exactly correct that I want the user to use the map document default tool to stop and save edits.

Most edits will be attributes but some point feature creation and lines.

I will work on this.

0 Kudos
DevinUnderwood2
Occasional Contributor

Not working yet based on your suggestions. So it just starts an edit session ? How does it know to edit the selected feature, I am not seeing it in the code.

0 Kudos
IanMurray
Frequent Contributor

Are the feature class you putting into the tool in the same workspace you are setting for the editting workspace?  That would be necessary for you to be able to edit the features, since you are setting the workspace to be editted, not the feature itself.  Any feature class in that workspace would be editable at that point.  Any edits you want to make would have to be within the map document itself, unless you change some code to do it within the program.  So right now all the code I wrote should do is use the current map document, subset a feature class based on an input field and value, zoom to it and start an editting session to allow edits on that feature(if it is in the workspace we set for editting).  The user would then have to make the changes to the geometry and/or attributes, save and close the edit session within the map document.

I'm guessing you were setting this up for basic GIS users to easily view and possibly edit a specific feature?

0 Kudos
DevinUnderwood2
Occasional Contributor

Yes, the feature class is in the same workspace .gdb. I am creating this for basis GIS users, it would be very convenient.

0 Kudos
DevinUnderwood2
Occasional Contributor

There is something missing, as it is still not working. Just not sure what.

0 Kudos