How do I get my script to only populate fields on selected objects while editing on the map?
# Importing arcpy
import arcpy
# Set the workspace environment
arcpy.env.workspace = r'C:\Users\aa2zz6\Desktop\TrackTraceDownload\UPDM2016.gdb'
rows = arcpy.UpdateCursor('P_ExcessFlowValve')
for row in rows:
if row.ItemNumber == '100':
row.Status = 'In Service'
row.Owner = ''
row.Diameter = '1'
row.Capacity = ''
rows.updateRow(row)
del row
del rows
print "Updated Selected EFV Features on the map"
2 Answers 2
Use the data access module cursors instead, they are faster. If you are only looking to update selected records in the map then you need to use the feature layer as input (=do not point the updatecursor to the feature class in the geodatabase) and execute the code in the python window of ArcGIS. To use the feature layer as input just use the same name as the feature layer in the table of contents. ArcMap will autocomplete the name for you:
with arcpy.da.UpdateCursor("Name_of_feature_layer",['ItemNumber','Status','Owner','Diameter','Capacity']) as cursor:
for row in cursor:
if row[0]=='100':
row[1],row[2],row[3],row[4]=['In Service','','1','']
cursor.updateRow(row)
Example of selecting records and returning them using the SearchCursor:
-
Do you know if it's possible to select 2 out of 4 points with the same Item number and change the attributes in the 2 points without changing all 4?Jordan– Jordan2017年11月04日 20:12:05 +00:00Commented Nov 4, 2017 at 20:12
-
Yes you can select manually and execute the code. You should remove the env.workspace and fc line to make sure the code is being executed on the feature layer in the map Which you have selectedBera– Bera2017年11月04日 20:24:19 +00:00Commented Nov 4, 2017 at 20:24
-
I can't figure out how to get the script to only populate info in selected features highlighted on the map. The If statement will edit and update everything that matches the == value but I'm trying to update only selected features on the map so that when I add new point data I can add a unique code and run the script without the script rewriting fields for 15000 features each time.Jordan– Jordan2017年11月05日 03:06:19 +00:00Commented Nov 5, 2017 at 3:06
-
3If you execute the code in the python window of arcgis with a selection and you make sure you are using the feature layer as input to the cursor (do not point at the feature class in the geodatabase!) it will only update selected records.Bera– Bera2017年11月05日 08:17:15 +00:00Commented Nov 5, 2017 at 8:17
You can add the query into the actual UpdateCursor call. Also, regular UpdateCursor (as opposed to arcpy.da.UpdateCursor()) doesn't set fields as callable by the row. So the following would work for you I think:
rows = arcpy.UpdateCursor('P_ExcessFlowValve', '*', "ItemNumber ='100'")
for row in rows:
row.setValue('Status','In Service' )
row.setValue('Owner', '')
row.setValue('Diameter, '1')
row.setValue('Capacity', '')
rows.updateRow(row)
del rows
print "Updated Selected EFV Features on the map"
Important note, make sure the field types match the values you are assigning. Make sure ItemNumber, Diameter etc. are Text (which you are using here) and not Short or Long integer, as this will cause the script to fail. If they are integers, simply remove the quote ' marks for those values and this should work fine.