AnsweredAssumed Answered

Python Toolbox failing to create a table

Question asked by granthay on Feb 16, 2019
Latest reply on Feb 25, 2019 by curtvprice

Hi Everyone,

 

I am working on a toolbox that creates a table of extracted values. I have the output parameter set to DEtable, but whenever I run the tool the table is not created. Any help on this would be appreciated.

# Start script
#-----------------------------------------------------------------------------
import arcpy
import os

# Tool initialization
#-----------------------------------------------------------------------------
class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""

        self.label = "Value_Extraction_Tool"
        self.alias = "Value Extraction Tool"

        # 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 = "Value Extraction Tool"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        DataSource = arcpy.Parameter(
              displayName="DataSource",
              name="DataSource",
              datatype="DEWorkspace",
              parameterType="Required",
              direction="Input")

        InputPoint = arcpy.Parameter(
            displayName="InputPoint",
              name="InputPoint",
              datatype="DEFeatureClass",
              parameterType="Required",
              direction="Input")

        OutputTB = arcpy.Parameter(
            displayName="OutputTB",
              name="OutputTB",
              datatype="DEArcInfoTable",
              parameterType="Required",
              direction="Output")

        params = [DataSource, InputPoint, OutputTB]
        return params

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

        # User input arguments
        DataSource = parameters[0].valueAsText
        InputPoint = parameters[1].valueAsText
        OutputTB = parameters[2].valueAsText


        # Add geometry attributes to inupt point
        arcpy.AddGeometryAttributes_management(InputPoint, "POINT_X_Y_Z_M", "", "", 4326)

        # add fields to output table to hold the temporal data
        arcpy.AddField_management(OutputTB, "POINTID", "SHORT")
        arcpy.AddField_management(OutputTB, "DATAINDEX", "SHORT")
        arcpy.AddField_management(OutputTB, "VALUE", "DOUBLE")

        # Loop through geometry attributes and get the x and y
        with arcpy.da.SearchCursor(InputPoint,"*") as cursor:
            PointIndex = 0
            for row in cursor:
                X = row[3]
                Y = row[4]

                # Get rasters and extract data at an X and Y       
                index = 0
                for (path, dirs, files) in os.walk(DataSource):
                    for ThisFile in files:
                        fName,fExt = os.path.splitext(ThisFile)
                        if fExt.upper() == ".IMG" or fExt.upper() == ".TIF":
                            RasterPath = path + "\\" + ThisFile

                            data = (arcpy.GetCellValue_management(RasterPath, str(X) + " " + str(Y), ""))
                            aquisition_date = arcpy.GetRasterProperties_management(in_raster = RasterPath, property_type = "ACQUISITIONDATE")

                            if str(aquisition_date).upper() == "UNKNOWN":
                                insertcursor = arcpy.InsertCursor(OutputTB)
                                row = insertcursor.newRow()
                                row.setValue("POINTID", PointIndex)
                                row.setValue("DATAINDEX", index)
                                row.setValue("VALUE", data)
                                insertcursor.insertRow(row)
                                del insertcursor
                                index += 1

                PointIndex += 0
            del row
            del cursor
        return

 

 

Outcomes