Python Toolbox Issues: using ArcPy Select by Location

819
3
Jump to solution
05-18-2022 02:42 PM
DPG
by
New Contributor II

Hi there. I am fairly new to Python and I am struggling to get my first Python Toolbox to work.

 

The purpose of the tool is to create a fishnet over a set area, before selecting cells within the fishnet that intersect a boundary shapefile. Cells that do not intersect the boundary are deleted. See this example below which uses the UK:

 

DPG_0-1652909592119.png

DPG_1-1652909838762.png

 

The boundary shapefile has been set up to be a selectable parameter inside the tool. My current script looks like this:

 

 

 

# -*- coding: utf-8 -*-

import arcpy


class Toolbox(object):
    def __init__(self):

        self.label = "Toolbox"
        self.alias = "toolbox"

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


class Tool(object):
    def __init__(self):

        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):

        # Param 0 = UK Boundary
        param0 = arcpy.Parameter(
        displayName="Input Feature Class",
        name="Parameter0",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Input")
        param0.filter.list=["POLYGON"]

        params = [param0]
        
        return params

    def isLicensed(self):

        return True

    def updateParameters(self, parameters):

        return

    def updateMessages(self, parameters):

        return

    def execute(self, parameters, messages):

        # Overwrite existing file of the same name
        arcpy.env.overwriteOutput = True

        # Reference Parameters
        input1 = parameters[0].valueAsText

        arcpy.management.CreateFishnet(
            "UKFishNet",
            "-13.6913900375366 49.8654174804687",
            "-13.6913900375366 59.8654174804687",
            1, 1, None, None,
            "1.76416802406319 61.527084350586",
            "NO_LABELS",
            "-13.6913900375366 49.8654174804687 1.76416802406319 61.527084350586",
            "POLYGON")


        arcpy.management.SelectLayerByLocation(
        "UKFishNet",
        "INTERSECT",
        input1,
        None,
        "NEW_SELECTION",
        "INVERT")

        arcpy.management.DeleteRows("UKFishNet")


        return

 

However, when I run the script - although I get no errors - I simply get an empty shapefile ("UKFishNet") sent to my geodatabase. Can anybody provide some guidance on what I'm doing wrong? Any pointers would be very much appreciated! 

0 Kudos
1 Solution

Accepted Solutions
JohnStreeb
Occasional Contributor

I'm not in a position to test out your specific code at the moment and I'm not personally that familiar with the CreateFishnet tool, but just from a brief review, it looks like CreateFishnet outputs a feature class.  You are then trying to use that feature class as the input to SelectLayerByLocation.  However, SelectLayerByLocation does not actually allow feature classes as an input, only layers (of feature classes).  So, while there may or may not be other problems causing the issue, at the very least you should consider adding Make Feature Layer tool (https://pro.arcgis.com/en/pro-app/2.8/tool-reference/data-management/make-feature-layer.htm) before SelectLayerByLocation.  And use that newly created layer as the input to SelectLayerByLocation and DeleteRows.  Then, to keep from having a bunch of temporary layers in memory (which can cause issues if you have multiple layers with the same exact name), I would add Delete tool (https://pro.arcgis.com/en/pro-app/2.8/tool-reference/data-management/delete.htm) after DeleteRows to delete the temporarily created layer.

View solution in original post

3 Replies
AlfredBaldenweck
MVP Regular Contributor

Oh weird.

My guess is that it's not honoring the selection by Location when you run Delete Rows.

Maybe try setting the select by Location as a new variable, and passing that variable into DeleteRows?

FishSelect= arcpy.management.SelectLayerByLocation("yadda yadda yadda")

arcpy.management.DeleteRows(FishSelect)
0 Kudos
JohnStreeb
Occasional Contributor

I'm not in a position to test out your specific code at the moment and I'm not personally that familiar with the CreateFishnet tool, but just from a brief review, it looks like CreateFishnet outputs a feature class.  You are then trying to use that feature class as the input to SelectLayerByLocation.  However, SelectLayerByLocation does not actually allow feature classes as an input, only layers (of feature classes).  So, while there may or may not be other problems causing the issue, at the very least you should consider adding Make Feature Layer tool (https://pro.arcgis.com/en/pro-app/2.8/tool-reference/data-management/make-feature-layer.htm) before SelectLayerByLocation.  And use that newly created layer as the input to SelectLayerByLocation and DeleteRows.  Then, to keep from having a bunch of temporary layers in memory (which can cause issues if you have multiple layers with the same exact name), I would add Delete tool (https://pro.arcgis.com/en/pro-app/2.8/tool-reference/data-management/delete.htm) after DeleteRows to delete the temporarily created layer.

DPG
by
New Contributor II

Thank you! I added this right after creating the fishnet and it worked like a treat:

        arcpy.management.MakeFeatureLayer("UKFishNet", "UKFishNet_lyr")


        arcpy.management.SelectLayerByLocation(
        "UKFishNet_lyr",
        "INTERSECT",
        input1,
        None,
        "NEW_SELECTION",
        "INVERT")

        arcpy.management.DeleteRows("UKFishNet_lyr")


        return