Wednesday, June 18, 2025

M5 - GIS Programming - Explore and Manipulate Data using Python


This week allowed us to dive deeper into ArcPy and explore data manipulation within ArcGIS Pro. We focused on using Python scripts to interact with spatial data, enhancing our ability to automate geoprocessing tasks.

We learned how to set up our workspace effectively and utilize various ArcPy functions, including the Describe function, to gather essential properties of our datasets. Additionally, we explored listing functions to efficiently manage feature classes and fields.

Cursors were a major highlight of the week, particularly:

  • Search Cursors: Used to retrieve and query records, allowing us to filter data based on specific criteria.
  • Update Cursors: Enabled us to modify existing records, ensuring data integrity.
  • Insert Cursors: Allowed us to add new entries to our datasets.
For our lab this week,  we used the new methods and procedures we learned to build a script that accomplishes the following tasks:
  • Creates a new file geodatabase (fGDB).
  • Copies feature classes from a data folder to the fGDB.
  • Extracts names and populations of 'County Seat' cities.
  • Stores data in a dictionary.
This was a fun but challenging lab.  It was fulfilling to compile a more complex script this week, and it reminded me of my teenage years when I was obsessed with The Matrix and aspired to be a hacker or coder. ðŸ˜†

The above images included in this blog post are screenshots of the IDLE window showing the results of running the script.  I wasn't confident enough in my flowchart skills to create a flowchart for this week's lab.  If I'm being honest, making the flowcharts has been the biggest challenge for me in this course. Below is the pseudocode for my script.
Import necessary modules:
import acrpy
import env from arcpy

Configure environment:
SET arcpy.env.workspace to working folder
SET arcpy.env.overwriteOutput to TRUE

Create new fGDB
  PRINT "Creating" message
SET output env for new fGDB
call CreateFileGDB_management 
PRINT "Complete" message

List all feature classes and SET variable 
SET fclist = ListFeatureClasses()

Copy all feature classes to new fGDB
create FOR loop for each feature class in fclist
SET base_name variable = Describe(feature_class).baseName
PRINT "Copying" message including the basename for each iteration 
call CopyFeatureClasses_Management 
PRINT "Copying complete" message 
End FOR loop
PRINT "Feature classes successfully copied" message

SET workspace to new fGDB
SET arcpy.env.workspace to new fGDB
PRINT "Creating search cursor" message

Create a search cursor for cities layer
SET fc = cities
SET cursor = call SearchCursor(fc)

Create empty dictionary
SET county_seats = {}
PRINT "Designing and populating dictionary" message

Populate dictionary with !NAMES! and !POP_2000!
Create FOR loop for each row in cursor
IF row.getValue("FEATURE") == "County Seat"
SET city_name = row.getValue("NAME")
SET population = row.getValue("POP_2000")
SET county_seats[city_name] = population
PRINT "City Name: " + city_name + ", Population: " + population
END IF
END FOR

Clean Up
del cursor
del row

PRINT "Dictionary complete"
PRINT county_seats

END


Tuesday, June 10, 2025

M4 - GIS Programming - Geoprocessing

 

The above image displays screenshots of the results from a geoprocessing script executed in IDLE. The map screenshots display the different output layers created by the script, including XY coordinates added to the "hospitals" shapefile, a 1000-meter buffer around each hospital location, and the 1000-meter buffer being dissolved into a single feature.

Week 4 allowed us to dig in and get our hands dirty with ArcGIS Pro ModelBuilder, a visual programming interface that enables users to create, manage, and automate geoprocessing workflows. In addition to exploring ModelBuilder, we also began working with writing and executing geoprocessing scripts in Python.

This week, we learned more about setting up our workspace within our Python script and automating the execution of ArcGIS Pro geoprocessing tools using scripting.

The above image shows screenshots of my IDLE Python prompt window after executing a script I wrote that adds XY coordinates to, buffers, and dissolves a hospital shapefile within our ArcGIS Pro project.

To create my full script, I started by setting up my workspace. While it wasn’t strictly necessary to include workspace setup steps within my Notebook (since I was working with an open project in ArcGIS Pro), I added these steps in case I may want to use the script as a standalone in the future. I also set the script’s ability to overwrite files to True.

Once my workspace was set, I opened each individual tool within the geoprocessing pane and configured all the parameters using the tool’s graphical user interface (GUI). After setting all the parameters, I clicked the drop-down menu attached to the “Run” button and selected "Copy Python Command." I then pasted the copied command into a new cell in my ArcGIS Pro Notebook. I added “processing” messages directly before each step so users would know where the program is in its process, as well as the resulting tool messages following each step.

At the end of my script, I included a “Processing complete” message to indicate that the script had run and completed its steps successfully.

This was a valuable learning experience, and I thoroughly enjoyed returning to working directly with ArcGIS Pro.

Wednesday, June 4, 2025

M3 - GIS Programming - Debugging and Error Handling


Module 3 of Computer Programming for GIS introduced us to the various types of errors that can occur during coding, ranging from basic syntax errors to more complex runtime errors.

A syntax error is a mistake in the script, such as missing punctuation, misspellings, or incorrect letter casing. This type of error prevents the program from running altogether because the interpreter cannot understand the code. In contrast, a runtime error occurs during script execution and can cause the program to terminate unexpectedly unless error handling is implemented. Examples of runtime errors include division by zero, attempting to access an undefined variable, or referencing an incorrect file path.

We explored the debugging process for our scripts, learning about various methods and tools to assist us. Additionally, we discussed error handling, which involves writing code to define how the program should respond when an error occurs, particularly through the use of try-except statements. This allows the program to manage errors without crashing, enabling the execution of the subsequent code.

Our Module 3 lab assignment included three scripts for us to work on.

In Script 1, we focused on identifying and correcting simple syntax errors, primarily involving misspellings and inconsistencies in letter casing (since Python is case-sensitive).

Script 2 presented a greater challenge with several errors to identify and fix. This script contained both syntax and runtime errors, including an incorrect file path. I was pleased to recognize and correct most of the errors before my initial attempt to run the script. The final error I encountered stemmed from having the project file open in ArcGIS Pro, which prevented the program from modifying the .aprx file. Once I closed ArcGIS Pro, the script executed as intended.

Script 3 was the most advanced part of this module, requiring us to implement error handling (try-except) for a known error in the code.  Pictured below is the flowchart I created illustrating the process. 

Overall, this was a fun and informative module, and I genuinely enjoyed it. I can see how debugging a large program could be tedious and time-consuming, but I'm optimistic about refining my skills to navigate the process efficiently and effectively.



M5 - GIS Programming - Explore and Manipulate Data using Python

This week allowed us to dive deeper into ArcPy and explore data manipulation within ArcGIS Pro. We focused on using Python scripts to intera...