Tuesday, June 10, 2025

Module 4 - Geoprocessing

This week’s module covered geoprocessing with Python using the ArcPy package. ArcPy is a collection of modules as well as functions and classes that enhance Python's capabilities. To start working with ArcPy we first need to import the package by using the line of code import arcpy. Once arcpy is imported all geoprocessing tools found in ArcGIS Pro can be run.

The subsequent step involves configuring the workspace to the default location designated for the input and output files that will be utilized in our tasks. This can be accomplished by employing the arcpy.env.workspace function. The syntax to be followed is arcpy.<class>.<property>, where 'env' is classified as a class, and 'workspace' serves as a property of this class.

Module 4 exercise focused on geoprocessing with ArcGIS Pro, we utilized the Clip tool from the analysis toolbox to clip the soils shapefile with the basin shapefile. Subsequently, we performed a batch process using the Clip tool to clip multiple shapefiles, including flood zones, lakes, rivers, and roads. Additionally, this exercise provided us with the opportunity to learn how to configure geoprocessing options and to explore models and the ModelBuilder feature in ArcGIS Pro.

In the lab assignment, we undertook two primary tasks. The first task involved the creation of a model designed to identify all soils within the basin shapefile feature that are currently or potentially suitable for agricultural purposes. The second task required the development of a script to execute three geoprocessing functions utilizing ArcPy.

To initiate the first task, we ensured that the option to "Allow geoprocessing tools to overwrite existing datasets" was enabled. This setting can be configured via the options menu under geoprocessing. We then created a new project and incorporated the shapefiles for soils and basin into a new map. Subsequently, we established a new toolbox and a new model named "SoilErase." Locating the Clip tool within the geoprocessing analysis toolbox and adding it to the model. Double-clicking on the Clip tool, to accessed the tool dialog box, where we designated "soils" as the input features and "basin" as the clip features. Upon executing the model, a new shapefile, "Soils_Clip," was generated and saved in the default directory, and added to the project.

Clip tool

The next phase involved selecting all soils classified as "Not prime farmland" for removal from the shapefile. To achieve this, we incorporated the select tool from the geoprocessing toolbox. After double-clicking the select tool, we designated "Soils_Clip" as the input feature. To eliminate soils classified as "Not prime farmland," we added the expression [FARMLNDCL] <> 'Not prime farmland' to select all entries that do not fall under this classification within the [FARMLNDCL] field of the attribute table. Running the model the new shape file “Soil_Clip_Select is being created and added to the project.
 
Selection tool

Created Clip_Select Model 

Screenshot of the final result

The second task required the development of a script to execute three geoprocessing functions utilizing ArcPy. There are various approaches to complete this task effectively. Initially, I duplicated the existing shapefile from the Data folder to the Result folder. This practice guarantees that the original layer remains untouched and serves as a backup. Subsequently, I incorporated the XY coordinates into the new shapefile. Following this, I proceeded to create a buffer and, ultimately, performed a dissolve operation. Although the Buffer and Dissolve processes can be executed in a single statement, I chose to implement them as two distinct statements. This decision allows for the generation of two separate layers for the buffer: one containing all individual buffers and the other representing the dissolved buffer.

1. First, I Imported arcpy, and from arcpy import env to set the workspace to the Data folder.

2. In order to enable the script to overwrite outputs, it is essential to check the option “Allow
geoprocessing tools to overwrite existing datasets” within the Geoprocessing settings in ArcGIS Pro
options. Additionally, to ensure that outputs can be overwritten during geoprocessing operations in Python IDLE, it is necessary to include the command “arcpy.env.overwriteOutput = True.”

3. Using arcpy.Copy_management to copy the shape file from the Data folder to the Results folder.

4. Using env.workspace to change and reset the workspace to the Results folder where the new shapefile is been saved.

5. Using arcpy.AddXY_management to add XY coordinates to the new copied shapefile.

6. Using the arcpy.Buffer_analysis and consequently arcpy.Dissolve_management to create a 1000 meter around the hospital features and then dissolve the created buffers into a single feature. The dissolve buffer step can be combined with the Buffer statement by using ALL in the buffer statement.
Using ALL for the dissolve_option instead of NONE will dissolve all buffers together into a single feature, removing any overlap.

arcpy.analysis.Buffer(in_features, out_feature_class, buffer_distance_or_field, {line_side}, {line_end_type}, {dissolve_option}, {dissolve_field}, {method})

7. Adding a print statement at the beginning of each task block and GetMassages() function at the end. This modification will ensure that when the script is executed, it displays an explanation of the processes being carried out, along with the respective start and end times.

Running the code from Notebook ArcGIS Pro 


Running the code in Python IDLE 




Flowchart for Module 4 lab


 

 

 

 

 

No comments:

Post a Comment