1

What tool does:

Simply creates tbf file from raster extent.

What is the problem:

I ran toolbox in ArcMap 10.5.1 and ArcGIS Pro 2.2.4.

When I run it as standalone script it is working just fine.

I am not new to Python nor Python Toolboxes, but today:

1) I received an error which I never encountered before (error shown below)

2) when I declare rasters = arcpy.ListRasters('tif'), list of rasters is empty.

Error code:

Running script Tool...

Traceback (most recent call last): File "", line 54, in execute File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing_base.py", line 527, in set_ self[env] = val File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing_base.py", line 587, in setitem ret_ = setattr(self._gp, item, value) enter code here

RuntimeError: Object: Error in accessing environment Failed to execute (Tool).

Tool code below:

import arcpy
import os

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "tfb file generator"
        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 = "Generating tbf files"
        self.description = ""
        self.canRunInBackground = True

    def getParameterInfo(self):
        """Define parameter definitions"""
        # Pierwszy parametr
        inside = arcpy.Parameter(
            displayName="In workspace",
            name="in_gdb",
            datatype="DEWorkspace",
            parameterType="Required",
            direction="Input")

        p = [inside]
        return p

    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."""
        arcpy.env.overwriteOutput = True

        arcpy.env.workspace = parameters[0].valueAsText
        output = parameters[0].valueAsText
        rasters = arcpy.ListRasters('*.tif')


        i=1
        for ras in rasters:

            arcpy.AddMessage('Working on raster nr:  {0} of {1} '.format(i, len(rasters)))
            f = arcpy.Raster(ras).extent

            north = f.YMax
            west = f.XMin
            south = f.YMin
            east = f.XMax

            i+= 1
            ras_split=ras.split('.')[0]

            with open(os.path.join(output,'{0}.tfb'.format(ras_split)), "w+") as text_file:
                text_file.write('north={0}\n'.format(north))
                text_file.write('west={0}\n'.format(west))
                text_file.write('south={0}\n'.format(south))
                text_file.write('east={0}'.format(east))

        return

Fixed all mistakes - tool fully working.

2

arcpy.env.workspace = parameters[0] should be arcpy.env.workspace = parameters[0].valueAsText.

You are setting the workspace to a Parameter object, not a string, see Accessing parameters within a Python toolbox

Your rasters = arcpy.ListRasters('tif') will find only rasters called exactly "tif". If you want to find rasters with a .tif file extension, use rasters = arcpy.ListRasters('*.tif')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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