Skip to content

Instantly share code, notes, and snippets.

@cindygis
Created March 25, 2015 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cindygis/6716792374a850d5aeb2 to your computer and use it in GitHub Desktop.
Save cindygis/6716792374a850d5aeb2 to your computer and use it in GitHub Desktop.
Generates chainage at a set distance along line features and saves it to an existing, empty point feature class.
#
# @date 27/11/2014
# @author Cindy Williams
#
# Generates chainage at a set distance along line features
# and saves it to an existing, empty point feature class.
# The point feature class should have a predefined schema
# in case other attributes need to be transferred.
#
# For use as a standalone script.
#
import arcpy
arcpy.env.workspace = r"C:\Some\Arb\Folder\work.gdb"
pts = [] # Empty list for chainage points
route = arcpy.management.MakeFeatureLayer("ftr_route") # Feature class containing route features
chain = arcpy.management.MakeFeatureLayer("ftr_chain") # Empty points feature class
i = 1000 # Set the chainage amount
# Remove previous attempts at generating chainage
arcpy.management.TruncateTable(chain)
print("Chainage feature class emptied.")
# Loop over all the features in the route feature class
with arcpy.da.SearchCursor(route,["SHAPE@", "Route_Name"]) as rows:
for row in rows:
print("\tGenerating chainage along " + row[1])
leng = row[0].length # Get the length of the current feature
pts.append((0,(row[0].positionAlongLine(0)), row[1])) # Get the start point
n = i # Start the count for current feature
while i < leng:
# Add a point at every i metres
pts.append((i,(row[0].positionAlongLine(i)), row[1]))
n += i
pts.append((leng,(row[0].positionAlongLine(leng)), row[1])) # Get the end point
# Open a cursor on the points layer
cursor = arcpy.da.InsertCursor(chain,("LEN","SHAPE@XY", "Route_Name"))
# Insert list of points into chainage feature class
for row in pts:
cursor.insertRow(row)
del pts, cursor
print("Script completed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment