Working with Geometries in ArcPy
Today We Are Going To Talk About Geometries!
In Arc GIS, we can use Python scripting through ArcPy to read geometry data and write the data to text files, csv's, or other file types. This is extremely helpful when trying to draw geometries in applications and mapping systems.
The Python script that I created in this coding example takes a shapefile containing rivers (polylines of rivers) over an area and copies the rivers from the shapefile to a text file. This is extremely useful because we can share this text file with another GIS analyst, geocode these XY points to another map, run queries to analyze the data, and copy the data to other formats for web applications, dashboards, etc...
Sounds Simple Right? I promise you its not...
Let's go over the following things we need to do to make this happen:
- import our modules and set up our workspace
- create a text file called rivers text file
- create a search cursor to iterate through our rivers shapefile
- write some of the metadata from our shapefile to the text file
- We want the rivers name's, X and Y coordinates, Object ID's, and a vector ID
- Each river from the shapefile needs to have its own line in the text file
Rivers are shown in purple |
Pseudocode...
# Import all modules
# Enable the
overwrite Output setting
# Set the workspace
# Prepare to write
.txt file. Create riversTxtName with file path
# (open) riversTxtName.txt
file for writing ("w")
# Define river
shapefile to file path as a variable, set to infc
# Create search
cursor for infc(rivers.shp). The cursor should call on the OID, SHAPE, and NAME
fields.
# Set for loop: for every row in the cursor
# Create the vertex id at 0
# For every point in the rows shape
attribute at the first element" aka using get part method
#
Add 1 to your vertex ID number --- In other words increment the vertex id by
1"
# Now we will write our rivers, including the
id, vertex id, coordinates and the name
#
add 1 to vertex ID
#
Print point out to check and make sure we have the right coord’s
#
Add a line to your .txt file using the .write() method. Need to str() aka stringify
#
write to txt file from rivers object id
# write to txt file from rivers vertex
id
# write to txt file
from rivers x point
# write to txt file
from rivers point Y
# write to txt file
from rivers Name – index field of name
# Put break “\n”
# del cursor
# close rivers txt
file
Comments
Post a Comment