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:


START

Configure environment:

SET arcpy.env.workspace to "S:/GISProgramming/Module6/Data"
SET arcpy.env.overwriteOutput to TRUE

Define feature class:

SET fc = "rivers.shp"

Open output text file:

PRINT "Opening output text file" message
OPEN output_txt FOR WRITING "S:/GISProgramming/Module6/Results/rivers_akj39.txt"

Create search cursor for rivers layer:

SET cursor = call SearchCursor(fc, ["OID@", "SHAPE@", "NAME"])

Initialize vertex number:

SET vertex_num = 0

Iterate through rows in cursor:

CREATE FOR loop for each row in cursor
    SET oid = row[0]
    SET shape = row[1]
    SET name = row[2]

Iterate through parts of the shape:

            CREATE FOR loop for each part in shape.getPart()
            Iterate through points in the part
                    CREATE FOR loop for each point in part
                            INCREMENT vertex_num
                            SET output_line = STRING(oid) + " " + STRING(vertex_num) + " " +                                                          STRING(point.X) + " " + STRING(point.Y) + " " + name + "\n"
                            WRITE output_line TO output_tx
                             PRINT output_line

END FOR loop
           END FOR loop
END FOR loop

Clean Up

del cursor
CLOSE output_txt

PRINT "Processing complete"

END


I've enjoyed this course, but I definitely found it to be more challenging than the other courses I've taken in the program thus far. I realize that programming is a skill that requires practice, and I'm excited to see what I can accomplish with Python in the future.





No comments:

Post a Comment

Applications in GIS - Orientation

Hello all! I'm Austin. Pleased to meet you!  I’m currently enrolled in the GIS Certification Program at UWF, having just completed my th...