How to Use Custom Tools in a Python Script with ArcPy

If you head over to the ArcPy page you will find a couple of custom toolboxes that we have been creating. These are great, you can access via ArcGIS Pro and use to perform their intended tasks. But what if you want to add these to chained workflows in a Python script? There’s a very simple way to do that.

Interested in learning ArcPy? there’s a course for that here.

Accessing tools with ArcPy requires a toolbox alias. You can manually add one or you can use one on the fly when scripting. Let’s first look adding manually. We will use the Advanced Tool with a Basic License toolbox from the ArcPy page. Navigate to the toolbox in the Catalog pane and right-click on the toolbox and select Properties. The Toolbox Properties box will appear. You can rename the Label and Alias. We have set the Alias below to basic. Click OK.

If you expand the Toolbox you can see several tools in the Data Management toolset. We are going to access the Split Line at Point tool with Python and also use it in a chained workflow.

Open up your favourite Python editor and import arcpy.

import arcpy

We need to import our Toolbox. Right-click on the Toolbox in ArcGIS Pro and select Copy Path, you can paste the path into the code below.

arcpy.ImportToolbox(r"C:\path\to\your\toolbox\Advanced Tools with a Basic License.atbx")

The image below represents a polyline and point feature class that we will use for the Split Line at Point tool. You can download here.

We’ll set up the variables to feed into the parameters of our tool.

## setting workspace where both feature classes reside and where output will go
arcpy.env.workspace = r"C:\path\to\your\geodatabase\My_Geodatabase.gdb"

## input feature classes required
line_fc = "line_fc"
pt_fc = "point_fc"

## linear unit required
distance = "50 METERS"

## output feature class name
out_fc = "splitlines_fc"

Now, we call out tool. Notice the syntax is arcpy.toolbox_alias.tool_name()

arcpy.basic.splitLineAtPoint(line_fc, pt_fc, out_fc, distance)

When we run this script, the 5 lines shown earlier are split and the output feature class contain 21 linear features. Excellent, we have just shown how we can use a tool from a custom toolbox using ArcPy.

Let’s take it a little further. This time we will use an on-the-fly alias (see fdm used the in ImportFunction in the code below), and we will use the result object from Split Line at Point tool (which we will store in the memory workspace) and feed that into the Create Random Points tool to generate 3 random points along each line.

import arcpy

################################################################################
## Esri Documentation
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/importtoolbox.htm
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/setparameter.htm
##
## ArcGIS Pro Version: 3.1.0
##
################################################################################

################################################################################
## IMPORT YOUR TOOLBOX

## notice this time we have used an extra parameter in the ImportToolbox function to define the alias for our toolbox
arcpy.ImportToolbox(r"C:\path\to\your\toolbox\Advanced Tools with a Basic License.atbx", "fdm")

################################################################################

################################################################################
## USER INPUTS

## setting workspace where both feature classes reside and where output will go
arcpy.env.workspace = r"C:\path\to\your\geodatabase\My_Geodatabase.gdb"

## input feature classes required
line_fc = "line_fc" # split line at point tool
pt_fc = "point_fc" # split line at point tool

## linear unit required
distance = "50 METERS" # split line at point tool

## output feature class names
out_fc = "random_fc" # create random points tool

## number of random points per feature
num_points = 3 # create random points tool

################################################################################

################################################################################
## RUN THE TOOLS

splitlines = arcpy.fdm.splitLineAtPoint(line_fc, pt_fc, "memory\\splitlines_fc", distance)

## FEED INTO CREATE RANDOM POINTS TOOL

arcpy.fdm.createRandomPoints(splitlines, out_fc, num_points)

################################################################################

################################################################################
## CLEAN UP THE MEMORY WORKSPACE

arcpy.management.Delete(splitlines)

################################################################################

And there we have it, we have used ArcPy to call our custom tools in a Python script.

Leave a Comment

Your email address will not be published. Required fields are marked *