Table of Contents
Introduction
If you head over to the this 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.
Leverage ArcPy for geospatial data management workflows within ArcGIS Pro. Learn the fundamentals of utilising ArcGIS Pro geoprocessing tools with ArcPy for data management, conversions, and analysis. This course is packed with amazing content that will help you discover the power of automating tasks within the ArcGIS Pro environment. Take your ArcPy skills from beginner to snake charmer. A little code goes a long way, a little ArcPy provides you with an in-demand skill. Sign up now for our highly rated course.
Toolbox Alias
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.
Import the ArcPy module
ArcPy is a Python site package that enables automation, analysis, and management of geographic data within the ArcGIS software environment.
import arcpy
ImportToolbox
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")
Test Data
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.
Define Parameters
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"
Call the Custom Tool
Now, we call our custom 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.
On-the-fly alias
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)
################################################################################
ArcGIS Pro Definition Query Masterclass with ArcPy & Python
Unlock the power of Definition Queries in ArcGIS Pro through this comprehensive course that dives deep into the intricacies of managing and utilizing definition queries with spatial data using ArcPy and Python scripting. Definition Queries provide a dynamic way to filter and display specific subsets of your data, enabling you to create more insightful maps and analyses. In this course, we will focus exclusively on Definition Queries, covering their creation, modification, and application through hands-on exercises and real-world scenarios. Accredited by the Association for Geographic Information (AGI) for 1 CPD point.