HOW TO

Use an update cursor on a feature selection in a geoprocessing service

Last Published: April 25, 2020

Summary

Fewer considerations are involved when using a Python script as a script tool in ArcMap, compared to using a Python script as a geoprocessing service published in ArcGIS Server. For example, when using the update cursor.

An update cursor in a geoprocessing script requires a feature class in a database location that must be updated manually. Otherwise, either all the values of an associated feature class in a choice list parameter referenced in the script during publishing are updated, or the service fails to update any feature at all because the input layer is a feature layer or feature set.

Procedure

To update selected features using the update cursor, the script sets the workspace and the feature class name must be set as string data type. The strings are used in the update cursor in_table parameter. The following steps provided describe how to do so.

  1. Import the necessary modules.
import sys, os, arcpy
  1. Define a parameter to receive inputs for the desired workspace and feature name.
workspace = arcpy.GetParameterAsText(0)      

fclassname = arcpy.GetParameterAsText(1)
  1. Combine the received inputs to create a string value to be used in the update cursor.
fclass = r"{}\{}".format(workspace, fclassname)
  1. Define a parameter for the feature layer type. This parameter is used in the search cursor to reference the OBJECTID values selected in the map and filter the cursor in the where_clause parameter.
flayer = arcpy.GetParameterAsText(2)
  1. Specify the values for the update cursor.
fields = ['UpdateField']

list = [ ]
  1. Run a loop for the SearchCursor() class to search for selected OBJECTID values in the layer.
with arcpy.da.SearchCursor(flayer, "OBJECTID") as cursor:
    for row in cursor:
        print (row[0])
        list.append(row[0])
  1. Start an editing session.
edit = arcpy.da.Editor(workspace)

edit.startEditing(True, False)
  1. Get the OBJECTID values from step 6 and create a list for the update cursor SQL statement.
strlist = "("

for num in list:
    strlist += str(num) + ", "

strlist = strlist[:-2]+")"
  1. Run a loop with the UpdateCursor() class using the combined string in step 3 and the SQL statement filtering the features that must be updated based on the OBJECTID values selected in the map.
with arcpy.da.UpdateCursor(fclass, fields, '"OBJECTID" IN {}'.format(strlist)) as cursor:
    for row in cursor:
        row[0] = "test"
        cursor.updateRow(row)
  1. Stop the edit session.
edit.stopEditing(True)

print ("Done")

The following shows the full script:
import sys, os, arcpy

workspace = arcpy.GetParameterAsText(0)      

fclassname = arcpy.GetParameterAsText(1)

fclass = r"{}\{}".format(workspace, fclassname)

flayer = arcpy.GetParameterAsText(2)

fields = ['UpdateField']

list = [ ]

with arcpy.da.SearchCursor(flayer, "OBJECTID") as cursor:
    for row in cursor:
        print (row[0])
        list.append(row[0])

edit = arcpy.da.Editor(workspace)

edit.startEditing(True, False)

strlist = "("

for num in list:
    strlist += str(num) + ", "

strlist = strlist[:-2]+")"

with arcpy.da.UpdateCursor(fclass, fields, '"OBJECTID" IN {}'.format(strlist)) as cursor:
    for row in cursor:
        row[0] = "test"
        cursor.updateRow(row)


edit.stopEditing(True)

print ("Done")

Article ID: 000019070

Software:
  • ArcMap
  • ArcGIS Pro 2 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options