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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 4, 2019 at 7:33
2
  • What is a tfb (or is it tbf) file? Commented Jan 4, 2019 at 7:58
  • Tbh, I know only my client need to generate hundreds of tfb files for his tiffs. He is using them for this purpose I guess: rolands.com/jtls/j_designs/JTLS-2015-12485.pdf Commented Jan 4, 2019 at 8:08

1 Answer 1

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')

answered Jan 4, 2019 at 7:42
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.