Created
October 6, 2015 13:39
-
-
Save cindygis/38b3099b2a7dafb460e1 to your computer and use it in GitHub Desktop.
Uses an arcpy FeatureSet to allow the user to interactively create points on a line which will be used to split that line into parts, while retaining all attributes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
@date 06/10/2015 | |
@author Cindy Williams | |
@version 1.0 | |
Uses an arcpy FeatureSet to allow the user to interactively create | |
points on a line which will be used to split that line into parts, | |
while retaining all attributes. | |
Requirements: | |
- Works as a script tool. Add to a toolbox. | |
- Can only work on a single line for now. Select the line, right | |
click the layer and "Create layer from selected features". Use | |
this selection layer as input to the tool. | |
- Digitise points snapped to the selected line. Tool will fail if not | |
snapped | |
- Digitise points left to right along the line. | |
Future versions will include: | |
- split multiple lines at same time | |
- snap points automatically/error that points should be snapped | |
- points will be sorted after digitising to ensure correct order | |
Built on an idea here http://gis.stackexchange.com/questions/154708/standalone-python-script-to-split-a-polyline-with-a-point-layer | |
''' | |
import arcpy | |
inset = arcpy.GetParameter(0) # FeatureSet of interactive points from ArcMap | |
inline = arcpy.GetParameterAsText(1) # Input polyline feature layer | |
# Get all field names excluding dynamic fields | |
fields = ["SHAPE@"] | |
fields.extend([field.name for field in arcpy.ListFields(inline) if field.name not in ["OBJECTID", "Shape_Length", "Shape"]]) | |
# Open an insert cursor for inserting the cut lines | |
cursor_ins = arcpy.da.InsertCursor(inline, fields) | |
# Get the spatial reference from the polyline | |
sr = arcpy.Describe(inline).spatialReference | |
# Get the polyline and delete the original record | |
with arcpy.da.UpdateCursor(inline, fields) as cursor: | |
for row in cursor: | |
polyline = row | |
cursor.deleteRow() | |
# Iterate over the points | |
with arcpy.da.SearchCursor(inset, "SHAPE@") as cursor: | |
for row in cursor: | |
point = row[0].firstPoint | |
# Build a temporary diagonal line from the point | |
diag = arcpy.Polyline(arcpy.Array([arcpy.Point(point.X + 10.0, point.Y + 10.0), arcpy.Point(point.X-10.0, point.Y-10.0)]), sr) | |
# Cut the original polyline | |
geom = polyline[0].cut(diag) | |
# Create a new line with the right side of the geom i.e. finished with this side | |
newline = [geom[1]] | |
newline.extend(polyline[1:]) | |
# Insert the line into the feature layer | |
cursor_ins.insertRow(newline) | |
# Set the left side of the cut line as the new line for cutting | |
polyline = [geom[0]] | |
polyline.extend(newline[1:]) | |
# Insert the last portion of the line | |
cursor_ins.insertRow(polyline) | |
del cursor_ins | |
del polyline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment