Marc_Alx's portfolio

Marc_Alx's personal page

View on GitHub

published first as an answer on comunity.esri.com

Making an ArcGIS Enterprise Geoprocessing that returns a file.

Here’s a tutorial on how to make an ArcGIS Enterprise python Geoprocessing that returns a file.

Important notes

The code

# -*- coding: utf-8 -*-

import arcpy
from os.path import join

class Toolbox(object):
    def __init__(self):
        """Sample file toolbox"""
        self.label = "Sample file toolbox"
        self.alias = "File toolbox"

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

class GetFileTool(object):

    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Get file tool"
        self.description = "A tool to get a file"
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        return [arcpy.Parameter(displayName="Output_File",
                                name='Output_File',
                                datatype=['DEFile'],
                                parameterType='Derived',
                                direction='Output')]

    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 exitWithError(self,msg):
        """
            exit geoprocessing with a given error msg
            under the hood it raise an exception, msg is logged as error
        """
        raise ValueError(msg)

    def execute(self, parameters, messages):
        """The source code of the tool."""
        p = join(arcpy.env.scratchFolder, "test.txt")
        with open(p, 'w') as f:
            f.write('hello')
        #set result with SetParameterAsText, setting result via 'value' parameter of objects inside parameters does nothing
        arcpy.SetParameterAsText(0,p)