Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

why is python toolbox script not working standalone in PyScripter?

$
0
0

I am learning Python Toolboxes with PyScripter.

I have a test python toolbox that outputs a raster file. It works when run from ArcCatalog.

As I understand it, it should also run standalone within Pyscripter but when I run it, no output appears in the Python Interpreter, and no file is created in the target geodatabase. I also can’t locate advice on how to make the messages appear within the Pyscripter window, as they do in ArcCatalog, eg to indicate the analysis has finished within PyScripter without having to check in ArcCatalog.

I tried from the “Run” command on the drop down menu and from the “external run”

I am using ArcGIS Desktop 10.2.2

import arcpy
import os
import sys
from arcpy.sa import *

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 = "CarbonToolbox"

        # List of tool classes associated with this toolbox
        self.tools = [CapacityTool, DemandTool]


class CapacityTool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Capacity"
        self.description = "Calculates capacity score "
        self.canRunInBackground = False

    def getParameterInfo(self):

        p0 = arcpy.Parameter(
            displayName="Cell size and extent",
            name="SAcell",
            datatype="DERasterDataset",
            parameterType="Required",
            category = "Default cell size and extent" ,
            direction="Input")
        p0.value =  os.path.join(os.path.dirname(os.path.dirname(__file__)),'ModelOutputsES1BaseMapOutputs.gdb', 'SA010')

        p1 = arcpy.Parameter(
            displayName="Outputs",
            name="Outputs",
            datatype="DEWorkspace",
            parameterType="Required",
            category = "Default data location",
            direction="Input")
        p1.value =  os.path.join(os.path.dirname(os.path.dirname(__file__)),'ModelOutputsES2Carbon_storage_regulationOutputs.gdb')

        p2 = arcpy.Parameter(
            displayName="Scratch",
            name="Scratch",
            datatype="DEWorkspace",
            parameterType="Required",
            category = "Default data location",
            direction="Input")
        p2.value =  os.path.join(os.path.dirname(os.path.dirname(__file__)),'ModelOutputsES2Carbon_storage_regulationScratch.gdb')

        # parameter list

        return [p0, p1, p2]


    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."""

        SAcell             = parameters[0].valueAsText
        Outputs            = parameters[1].valueAsText
        Scratch            = parameters[2].valueAsText


        # set analysis environment e.g. extent, cell size, snap raster

        ext2                 = arcpy.Describe(SAcell).extent
        arcpy.env.extent     = ext2
        cell1                = parameters[0].valueAsText
        arcpy.env.cellSize   = cell1
        arcpy.env.snapRaster = parameters[0].valueAsText


        # set all local variables used in this model analysis

        test                 = "test"
        out2               = os.path.join(Outputs, test)


        # set overwrite and scratch workspace

        arcpy.env.overwriteOutput = True
        arcpy.env.workspace = Scratch
        arcpy.CheckOutExtension('Spatial')

        messages.addMessage("Starting analysis")


        arcpy.CopyRaster_management(cell1, out2)

        del test, cell1

        messages.addMessage("Analysis finished")

        return

class DemandTool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Demand"
        self.description = "Calculates demand score "
        self.canRunInBackground = False

Viewing all articles
Browse latest Browse all 767

Trending Articles