Showing posts with label GIS4102. Show all posts
Showing posts with label GIS4102. Show all posts

Wednesday, June 25, 2025

M6 - GIS Proramming - Working with Geometries in Python


Screenshot of IDLE window after running this week's lab script


This week brings us to our sixth module in the GIS Programming course, which is also our final assignment for this course.

This week focused on working with geometries within ArcPy and how to effectively read and write them in ArcGIS Pro. We learned to use geometry tokens for quick access to feature properties and the use of search cursors.

In our exercise, we had fun extracting XY coordinates from point and polyline features, which helped us understand how to work with individual vertices. Additionally, we explored single-part and multipart features using the isMultipart property and practiced creating new polyline features from a list of coordinates.

Our lab assignment required us to write a script that uses ArcPy to extract vertex information from a shapefile. The script initializes a SearchCursor to retrieve Object IDs, shapes, and names of river segments, counting and outputting the coordinates of each vertex. It also creates a text file and writes the results to that file. The screenshot above shows the IDLE window after running the script, displaying the output.

The pseudocode for my script is below:

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.



Wednesday, May 28, 2025

M2 - GIS Programming - Python Fundamentals

Screenshot of a script I created and ran using IDLE.

Week two of GIS Programming was a crash course introduction to the fundamentals of Python programming. It was a tremendous amount of information to absorb, and I see that I'll be referring back to this chapter of our textbook frequently for some time.

Module 2's lab student outcomes were:

Work with string variables in Python

Use and import modules

Save Python code as a script

Include comments in scripts

Create loops and conditional statements

Iterate variables within loops to control script workflow


We were tasked with working on a prewritten script as well as writing our own script using functions, methods, lists, loops, and more. I enjoyed this module, though I found it somewhat challenging. As mentioned, I'll be utilizing Chapter 4 in our textbook until I master the fundamentals.

The image above is a screenshot of a script I created and ran using IDLE. I found working in IDLE much easier than working directly in ArcGIS Pro Notebooks. However, I appreciate that in Notebooks, you can easily test individual lines of code.

Our script had to successfully perform two main functions. First, we needed to create a loop that adds 20 random numbers between zero and ten to a list, which we assigned to the variable luckyList. This involved initializing an empty list, setting up a counter variable, and using a while loop to append randomly generated numbers until the list reached twenty integers. An if statement was used to check when the count reached twenty, and a break statement was included to exit the loop. Finally, we printed the list after all numbers had been added.

The next part of the assignment required us to create a loop that removes a chosen integer from the previously generated list. We assigned this integer to a variable and used an if-else statement to check if it was present in the list. If the integer was not found, we printed a message indicating this; if it was present, we used the count() method to determine how many times it appeared and printed that information. We then employed a while loop to remove the integer from the list and printed the updated list afterward.

In my initial attempt at writing this code, I completed the expected tasks. However, upon review, I realized I hadn't followed the instructions precisely and had used a different method to achieve the same goal. This was actually exciting, as I was able to rely on my intuition to complete the task, even though it wasn't the instructed method. It perfectly illustrates the principle from “The Zen of Python” that there is often “one obvious way” or a “better” way to achieve a goal. However, I'm still uncertain which of the two approaches (my initial working version versus the assignment's requested method) would truly be considered the better way in terms of general programming practices.


Below is the flow chart I created for this script. I'm anxiously looking forward to what's to come!






Monday, May 19, 2025

M1 - GIS Programming - Python Environments and Flow Charting

 



Welcome to GIS Programming, my third course in the GIS Certification Program at the University of West Florida! This is my first experience with Python, as well as my first taste of coding since I was about 15 years old. At that age, I bought a "C++ For Dummies" book and wanted to learn computer programming. I managed to code a CD music player for Windows 98, but that was the extent of my pursuit. I've always had an interest in coding, but I never learned anything beyond that.

As I worked through this first module, the excitement was reignited, and I can't wait to learn more.

Module 1 introduces us to Python and its various coding environments, from the basic Python Command Prompt to Integrated Development Environments (IDEs) such as IDLE and PyCharm. IDEs are applications that facilitate the writing, editing, and execution of code. They include features like syntax checking and debugging, which help users test their code in real time and fix any errors.

We were also introduced to Notebooks, specifically ArcGIS Notebooks. ArcGIS Notebooks are contained within ArcGIS Pro and have similar capabilities to an IDE (minus syntax checking and debugging). What Notebooks offer that IDEs do not is the ability to save your scripting file as a .ipynb file type, which allows for the inclusion of media such as images, hyperlinks, and HTML pages—capabilities that the .py file type does not allow.  Because ArcGIS Notebooks is part of the ArcGIS Pro software, it also allows real-time interaction with the current project data you're working with, helping improve workflow.

Flow charting is another aspect of programming we were introduced to. I must admit, I'm not always the best at planning and preparing for a project. I often jump right in, and because of that, I find myself running into issues I wasn't prepared for. I can see how creating a flow chart using pseudocode before writing your code is important and helpful.


We were also tasked with reading "The Zen of Python," a mantra authored by Tim Peters that can be accessed by typing "import this" at the command prompt, and sharing our interpretation.  

 



To me, “The Zen of Python” emphasizes that Python was created to be a more simple and intuitive way of programming and urges the user to remember this as they code.  It is saying “don’t overcomplicate when unnecessary, but find a balance in complexity.”  If a program you are writing is becoming chaotic, maybe there are other, better ways to write the script, and you should strive to be well-read and prepared for what you’re writing. It’s saying write cleanly, beautifully, and in a way that is not only easy for you to read, but also for others. In my opinion, it is a beautifully written set of philosophical guidelines to abide by when coding, and it makes me feel excited.

On to Module 2!

GIS Portfolio

As I wrap up the Graduate GIS Certificate at the University of West Florida, I’ve pulled together a GIS portfolio that showcases my coursewo...