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


No comments:

Post a Comment

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...