Automating Geoprocessing Workflows in ArcGIS Pro with Python

Hey ArcNesian!

In this post, ArcMin will delve into integrating Python and ArcGIS Pro to automate your Geoprocessing Tools workflows. Boost your productivity by leveraging Python scripting to automate repetitive Geoprocessing tasks in ArcGIS. Here, you’ll learn how to work with ArcPy, Esri’s site package that seamlessly integrates Python scripting within ArcGIS Desktop and Pro.

Python has become a leading programming language in Data Science, boasting a vibrant and active user community continuously expanding its capabilities. This community constantly develops new Libraries, Modules, and Application Programming Interfaces (APIs). With such a vast ecosystem, it can be challenging for analysts to discern which Libraries to learn, rely on, and stay updated with.

Many of you are already familiar with ArcGIS Pro, right? ArcGIS Pro can be considered a powerful “workstation” for Spatial Data Science. By combining Exploratory Data Analysis, Visualization, ArcPy, the ArcGIS API for Python, Machine Learning, Deep Learning, and Notebooks, ArcGIS Pro provides a holistic system for tackling complex Data Science problems. This is particularly exciting as these tools are highly discussed and increasingly applied to solve intricate issues related to spatial information.

So, how can you harness the power of Python integration with ArcGIS Pro?

In this tutorial, we’ll guide you through automating processes like Join, Buffer, and Union using Tutorial Data. This data is compiled and modified from OpenStreetMap Indonesia. First, we’ll create a script to buffer the “Jalan” (Road) Feature Class based on distance values from the “TabelBuffer” (Buffer Table), requiring a join process. Second, we’ll select “MenaraListrik” (Electric Tower) point features located within the created buffer areas to modify their attribute values, marking them as priority areas. Finally, you’ll learn how to transform your Python script into a user-friendly script tool.

Follow the steps below after familiarizing yourself with the data!

Running Analysis in PyCharm

Many GIS analysis and data management tasks in ArcGIS Pro involve using multiple Geoprocessing tools. Scripting these tools offers significant advantages. You can develop and execute ArcPy scripts within the Python window in ArcGIS Pro or using an external Python editor like PyCharm.

1. Configuring a New Project in PyCharm

Create a new project in PyCharm and navigate to File > Settings > Project: [Your Project Name] > Project Interpreter. In the dialog box, click the button to select Add, and then click System Interpreter.

Choose the folder C:Program FilesArcGISProbinPythonenvsarcgispro-py3python.exe to specify the interpreter installed with ArcGIS Pro. This ensures you can utilize ArcPy.

If you don’t find the ArcGIS folder in Program Files, ArcGIS might be installed in the Users folder. Explore this path instead:

C:Users\AppDataLocalProgramsArcGISProbinPythonenvsarcgispro-py3python.exe.

(On some systems, the AppData folder is hidden; to access it, in the toolbar, click the Show Or Hide Hidden Files And Folders button).

2. Setting Geoprocessing Environments in a Python Script

Open the BufferJalan.py script to examine it. Whenever you run a script for analysis with ArcPy, you need to configure the geoprocessing environment as follows (refer to lines 2-4).

First, import the ArcPy module using import arcpy. Then, define whether the script can overwrite output by setting arcpy.env.overwriteOutput = True. Finally, define the workspace by specifying the Geodatabase you’ll be using with arcpy.env.workspace = ".LatihanGIS.gdb".

3. Running the Python Script

From the Run menu, select Run and choose BufferJalan.py in the Dialog Box.

Open ArcGIS Pro to verify if the Buffer feature class has been successfully created in your geodatabase.

Using Geoprocessing Tools with Python in ArcGIS Pro

Now that you can develop Python workflows, the next step is accessing your GIS data within ArcGIS Pro. We’ll modify the “Priority” attribute field for the “MenaraListrik” Feature Class for points located within the BufferJalan area.

1. Opening the Python Window

From the Analysis tab, in the Geoprocessing group, click the Python button . Once opened, define the workspace as in the previous step using arcpy.env.workspace = ".LatihanGIS.gdb".

2. Performing Select Layer by Location

In the Geoprocessing Pane, search for “Select Layer by Location.” Click the icon on the Select Layer tool and drag it into the Python window.

Then, fill in the parameters so the code looks like this: arcpy.management.SelectLayerByLocation('MenaraListrik', 'INTERSECT', 'BufferJalan'). Press Enter to execute it.

3. Modifying Attribute Values in a Feature Class Table

Modification is done using a Cursor Object. Cursor objects allow you to access rows within a table, enabling reading and writing of attribute and geometry values. Type and run the following code:

with arcpy.da.UpdateCursor("MenaraListrik", "priority") as cursor:
    for row in cursor:
        row[0] = "YES"
        cursor.updateRow(row)

Verify that for the MenaraListrik feature class, points within the Buffer area now have “YES” in the priority field.

Creating a Script Tool in ArcGIS Pro from a Python Script

You’ve created a Python script and now want to make it easy to use with different input parameters while providing a user-friendly experience for those unfamiliar with Python. You also want a way to share it with others, either within your workplace or through ArcGIS Online. A great way to create a user interface and facilitate sharing is through a Script Tool.

1. Creating a Python Script for Script Tool

Open the BufferTool.py script to examine it. Instead of explicitly writing input and output paths as before, we now use arcpy.GetParameterAsText() with an index within the parentheses. This index becomes important when creating the script tool to distinguish variables.

2. Creating a Script Tool within a Toolbox in ArcGIS Pro

Script Tools are created inside Toolboxes and can be run like any Geoprocessing tool. In the Catalog pane, right-click on Toolboxes and select New Toolbox. Name the new toolbox “BufferMenaraListrik” and click Save.

Next, right-click the BufferMenaraListrik Toolbox in the Catalog pane, hover over New, and select Script. Enter “BufferMenaraListrik” as the Script Tool name.

Click Parameters under General in the New Script dialog box. Define your input and output variables, along with their data types, corresponding to the indices specified in your Python script, and then click OK. Your Script Tool is now created!

By utilizing Python and integrating it with ArcGIS Pro, your GIS tasks can become significantly more efficient, right? We hope this inspires you to further explore the powerful capabilities of ArcGIS Pro, ArcNesian!

That concludes ArcMin’s explanation in this post.

If you have further questions about this article or Esri products, please contact the Esri Indonesia Support Team via email at [email protected].

(This article was created by Dzulfiqar Naufal Fawwaz from the Esri Indonesia Future Leaders Program)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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