Feature class field update with Geoprocessing service

408
3
05-25-2023 01:35 AM
OlivierLefevre
New Contributor III

Hello !

I've been trying to author a geoprocessing service that updates a field.

Below is a snippet of the Python Toolbox that I execute before publishing.

 

 

import arcpy

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "PythonToolbox"
        self.alias = "mypythontoolbox"

        # List of tool classes associated with this toolbox
        self.tools = [PyToolUpdateFeatureField]


class PyToolUpdateFeatureField(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "PyToolUpdateFeatureField"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        param0 = arcpy.Parameter(
            displayName="Input",
            name="infeature",
            #datatype="GPRecordSet",
            #datatype="GPFeatureLayer",
            #datatype="DEFeatureClass",
            #datatype="GPLayer",
            datatype="GPFeatureRecordSetLayer",
            parameterType="Required",
            direction="Input")
        params = [param0]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return
        
    def execute(self, parameters, messages):
        """The source code of the tool."""
        fc = parameters[0].valueAsText
        #fc = parameters[0].value
        fields = ["OBJECTID", "Name"]

        msg = "test"
        
        #fcWorkspace = arcpy.Describe(fc).path
        #edit = arcpy.da.Editor(fcWorkspace)
        #edit.startEditing(False, False)
        #edit.startOperation()

        with arcpy.da.UpdateCursor(fc, fields) as cursor:
            for row in cursor:
                if row[0] == 1:
                    row[1] = msg
                cursor.updateRow(row)
                
        #edit.stopOperation()
        #edit.stopEditing(True)
        
    def postExecute(self, parameters):
        """This method takes place after outputs are processed and
        added to the display."""
        return

 

 


At publication stage the input I provide the tool with is a either a File GDB Feature class or a Enterprise GDB Feature class or a Feature Service layer.

Whatever the input is before publication, the published task only updates Feature Service layers. When trying to update a Feature class the task does not return any error but does not update the field either.

Are geoprocessing services only intended to work with published data ?
If not what am I missing out in authoring ?

0 Kudos
3 Replies
Jamesmillere
New Contributor

Hi,

 

Update the getParameterInfo method:

Change the datatype parameter for the infeature parameter from GPFeatureRecordSetLayer to Any. This will allow the parameter to accept various types of data.

Here's an updated version of the getParameterInfo method:

def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Input",
name="infeature",
datatype="Any",
parameterType="Required",
direction="Input")
# Uncomment and use the appropriate datatype for the input parameter
# param0.datatype = "GPFeatureLayer"
# param0.datatype = "DEFeatureClass"
# param0.datatype = "GPLayer"
params = [param0]
return params

 

Thanks

0 Kudos
OlivierLefevre
New Contributor III

Hi @Jamesmillere 

Thanks for your answer but the tool cannot run which such a parameter. It returns an error something in French like "Tool has failed to open : ValueError: ParameterObject: non valid input value for DataType property"

 

Maybe it's a matter of ArcGIS versions. Mine are ArcGIS Pro 2.9.6 and ArcGIS Server 10.9.1

0 Kudos
OlivierLefevre
New Contributor III

Adding a message to my code below, I noticed that any Feature Class (from GDB or SDE) used as task input is converted to an in memory feature set.

 

 

    def execute(self, parameters, messages):
        """The source code of the tool."""
        fc = parameters[0].valueAsText
        #fc = parameters[0].value
        fields = ["OBJECTID", "Name"]

        msg = "test"
        
        fcWorkspace = arcpy.Describe(fc).path
        messages.addMessage("Input :" + fc)
        messages.addMessage("Path :" + fcWorkspace)
        
        #edit = arcpy.da.Editor(fcWorkspace)
        #edit.startEditing(False, False)
        #edit.startOperation()

        with arcpy.da.UpdateCursor(fc, fields) as cursor:
            for row in cursor:
                if row[0] == 1:
                    row[1] = msg
                cursor.updateRow(row)
                
        #edit.stopOperation()
        #edit.stopEditing(True)

 

 

Messages are :

Input :in_memory\feature_set

Path :in_memory

 

Is it possible to author a generic geoprocessing service that updates Feature Services as well as Feature Classes ?

 

 

0 Kudos