0

I'm trying to create a toolbox that utilizes a SearchCursor (with ArcGis Pro) to filter a layer with another's attributes. This is my first attempt and I've ran into some errors I don't know how to fix.

The script works when I put it in the python terminal. But when I paste it into the script tool I get an error:

Error 000601: Cannot delete \\Location\Test.gdb may be locked by another application.

Except I'm not trying to delete a gdb, I'm trying to save a layer file (output) into the gdb.

lyr = "Heatmap_Filtered"
events = arcpy.da.SearchCursor(lyr, ['Event'])
unique_events = list(set([row[0] for row in events]))

sql = "Event IN(" + ','.join(map(str,unique_events)) + ")"
arcpy.management.SelectLayerByAttribute("RTL", "NEW_SELECTION", sql, None)

arcpy.management.CopyFeatures("RTL", fr"{DB2}\RTL_Filtered", '', None, None, None) 

arcpy.management.SelectLayerByAttribute("RTL", "CLEAR_SELECTION", '', None)

When I put it in the toolbox it breaks and I'm not experienced enough with toolboxes to understand why.

Paramaters

Heres my script

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

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


class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "SearchCursor"
        self.description = "SearchCursor is a read only tool that will loop through and return data"
        
    def getParameterInfo(self):
        
        # Points Filtered Field parameter
        points = arcpy.Parameter(
            displayName="Points Filtered",
            name="Points_Filtered",
            datatype="Feature Layer",
            parameterType="Required",
            direction="Input")
        
        points.value = "Points"
        
        # RTL Unfiltered Field parameter
        rtl = arcpy.Parameter(
            displayName="RTL Unfiltered",
            name="RTL_Unfiltered",
            datatype="Feature Layer",
            parameterType="Required",
            direction="Input")
        
        rtl.value = "RTL"

        # Derived Output Features parameter
        outfeatures = arcpy.Parameter(
            displayName="Save Location",
            name="Save_Location",
            datatype="Workspace",
            parameterType="Required",
            direction="Output")
        
        outfeatures.parameterDependencies = [points.name]
        outfeatures.schema.clone = True

        parameters = [points, rtl, outfeatures]
        
        return parameters


    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."""
        if parameters[0].altered:
            parameters[1].value = arcpy.ValidateFieldName(parameters[1].value,
                                                          parameters[0].value)
        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."""
        
        points  = parameters[0].valueAsText
        rtl  = parameters[1].valueAsText
        database  = parameters[2].valueAsText

        
        events = arcpy.da.SearchCursor(points, ['Event'])
        unique_events = list(set([row[0] for row in events]))

        sql = "Event IN(" + ','.join(map(str,unique_events)) + ")"
        arcpy.management.SelectLayerByAttribute("rtl", "NEW_SELECTION", sql, None)

        arcpy.management.CopyFeatures(rtl, fr"{database}\RTL_Filtered", '', None, None, None) 

        arcpy.management.SelectLayerByAttribute(rtl, "CLEAR_SELECTION", '', None)


        return

    def postExecute(self, parameters):
        """This method takes place after outputs are outputs are processed and
        added to the display."""
        return
4
  • 1
    Are you overwriting the same feature class with copy features?
    – BERA
    Jan 11, 2023 at 17:21
  • I don't think so. When I do it in the terminal or through the geo tools it creates a new different named layer.
    – Davec433
    Jan 12, 2023 at 11:29
  • You could try naming the output to something unique, for example current time: out_feature_class = f"RTL_Filtered_{datetime.datetime.now().strftime('%H-%M-%S')}" or set arcpy.env.overwriteOutput=True to rule out that problem
    – BERA
    Jan 13, 2023 at 12:54
  • import os import arcpy Filter_Layer = arcpy.GetParameterAsText(0) # Feature Layer Search_Layer = arcpy.GetParameterAsText(1) # Feature Layer events = arcpy.da.SearchCursor(Filter_Layer, ['Event']) unique_events = list(set([row[0] for row in events])) sql = "Event IN(" + ','.join(map(str, unique_events)) + ")" arcpy.management.SelectLayerByAttribute(Search_Layer, "NEW_SELECTION", sql, None) Can't figure out how to edit the comment section but this works for selecting the layer when I attacth it within the model.
    – Davec433
    Jan 18, 2023 at 15:21

0

Your Answer

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