4

I have been working on an ArcGIS Python script tool which takes the co-ordinate system from an input raster and reprojects a vector to the same co-ordinate system.

The script works fine when i hard code the addresses for the workspace and input datasets. But as soon as I try to pass those as parameters in a toolbox it no longer works.

Can anyone here point out what I am doing wrong?

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 = "Toolbox"
        self.alias = ""

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

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

    def getParameterInfo(self):
        """Define parameter definitions"""
        param0 = arcpy.Parameter(
            displayName="Input workspace",
            name="workspace",
            datatype="DEWorkspace",
            parameterType="Required",
            direction="Input")
        param1 = arcpy.Parameter(
            displayName="Input classified raster",
            name="input_raster",
            datatype="GPRasterLayer",
            parameterType="Required",
            direction="Input")
        param2 = arcpy.Parameter(
            displayName="Input features",
            name="input_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")

        params = [param0, param1, param2]

        return params

    def execute(self, parameters, messages):
        """The source code of the tool."""

        # Define some paths/variables
        outWorkspace = arcpy.GetParameterAsText(0)
        arcpy.env.workspace = outWorkspace
        input_raster = arcpy.GetParameterAsText(1)
        input_features = arcpy.GetParameterAsText(2)

        output_features = outWorkspace + "\\projected.shp"

        out_coordinate_system = arcpy.Describe(input_raster).spatialReference
        proj_grid = arcpy.Project_management(input_features, output_features, out_coordinate_system)

        return

When I run the tool it provides the following error:

Traceback (most recent call last):
  File "<string>", line 75, in execute
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\__init__.py", line 1245, in Describe
    return gp.describe(value, data_type)
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 370, in describe
    self._gp.Describe(*gp_fixargs(args, True)))
OSError: "" does not exist
 Failed to execute (Tool).

it seems to me that the input variables of the parameters are not being passed correctly.

2
  • Try arcpy.GetParameter() instead of arcpy.GetParameterAsText() May 29, 2019 at 8:07
  • 1
    arcpy.GetParameter() did not work. What did work was: parameters[].valueAsText Thanks for the assistance tho. May 29, 2019 at 8:49

2 Answers 2

6

The code that you have presented is for a Python Toolbox (*.pyt) for which you need to use the Parameter class.

GetParameter() and GetParameterAsText() are only used with Python Script tools in Standard Toolboxes (*.tbx).

0
5

Using the answer from @PolyGeo my corrected code is as follows:

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 = "Toolbox"
        self.alias = ""

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


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

    def getParameterInfo(self):
        """Define parameter definitions"""
        param0 = arcpy.Parameter(
            displayName="Input workspace",
            name="workspace",
            datatype="DEWorkspace",
            parameterType="Required",
            direction="Input")
        param1 = arcpy.Parameter(
            displayName="Input classified raster",
            name="input_raster",
            datatype="GPRasterLayer",
            parameterType="Required",
            direction="Input")
        param2 = arcpy.Parameter(
            displayName="Input features",
            name="input_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")


        params = [param0, param1, param2]

        return params

    def execute(self, parameters, messages):
        """The source code of the tool."""

        # Define some paths/variables
        outWorkspace = parameters[0].valueAsText
        arcpy.env.workspace = outWorkspace
        output_location = parameters[0].valueAsText
        input_raster = parameters[1].valueAsText
        input_features = parameters[2].valueAsText

        output_features = output_location + "\\projected.shp"

        out_coordinate_system = arcpy.Describe(input_raster).spatialReference
        proj_grid = arcpy.Project_management(input_features, output_features, out_coordinate_system)

        return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.