GIS5103 | Module 6
Module 6 | Working with Geometries
In module 6, we explored using Python to work with the geometry's aspect of a feature. Oftentimes we rely on the software we are using to correctly draw our features, but there are times when Python can dive deeper into the information. Polylines and polygons are made up of sometimes hundreds of individual vertices, and using the geometry aspects of Python can help us understand our data more intricately.
The follow pseudo code is an example of a script I wrote that extracts the x and y coordinates from a polyline layer, and writes that information to a text file, along with the name of the feature being extracted. Below the code is a screenshot of the results.
Script Flow
-
START Script
-
Import required modules (
arcpy,os) -
Set environment to allow overwrite of outputs
-
Set workspace to the Module 6 data folder
-
Define the output
.txtfile path
Main Logic Inside TRY block)
-
Open the text file in write mode
-
Create a search cursor on
rivers.shp-
Fields:
OBJECTID,SHAPE,NAME
-
-
Loop through each row in the cursor:
-
Extract
OID,SHAPE, andNAME -
Set
vertexID = 0 -
For each point in the geometry using
.getPart():-
Increment
vertexIDby 1 -
If the point is not None:
-
Write the following to the text file:
-
OID,vertexID,X,Y, andName
-
-
Print the line to the console
-
-
-
-
Delete the cursor
-
Print a confirmation message
Error Handling
-
If an error occurs, print the error message
END Script
Comments
Post a Comment