how to split a polyline into equal segments with python

9996
5
Jump to solution
07-03-2014 07:11 AM
ruiyang
New Contributor
I am trying to split a road (a line feature) into equal size segment. I know there is a way to do it in ArcGIS (editor-split), but this can not be coded with python script. Is there anyone can help me to code this? Or Any hint could be very helpful.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Here is something I wrote that will build points along the line that you can then feed to the split by point tool.

def points_along_line(line_lyr, pnt_layer, pnt_dist):
    """
    line_lyr (feature layer) - Single part line
    pnt_layer (feature layer) - Path to point feature class
    pnt_dist (integer) - Interval distance in map units to add points
    """

    search_cursor = arcpy.da.SearchCursor(line_lyr, 'SHAPE@')
    insert_cursor = arcpy.da.InsertCursor(pnt_layer, 'SHAPE@')

    for row in search_cursor:
        for dist in range(0, int(row[0].length), pnt_dist):
            point = row[0].positionAlongLine(dist).firstPoint
            insert_cursor.insertRow([point])


It was setup to work on an existing point feature class. You could just as easily add a few lines to create it on the fly.

    if not arcpy.Exists(pnt_layer):
        arcpy.CreateFeatureclass_management(
            os.path.dirname(pnt_layer), os.path.basename(pnt_layer), 'POINT')
...
    return pnt_layer


Then add the split line at point tool where you call the function.
points_along_line(line_features, point_features, pnt_dist)
arcpy.SplitLineAtPoint_management(line_features, point_features, out_feature_class)

View solution in original post

0 Kudos
5 Replies
MathewCoyle
Frequent Contributor
Here is something I wrote that will build points along the line that you can then feed to the split by point tool.

def points_along_line(line_lyr, pnt_layer, pnt_dist):
    """
    line_lyr (feature layer) - Single part line
    pnt_layer (feature layer) - Path to point feature class
    pnt_dist (integer) - Interval distance in map units to add points
    """

    search_cursor = arcpy.da.SearchCursor(line_lyr, 'SHAPE@')
    insert_cursor = arcpy.da.InsertCursor(pnt_layer, 'SHAPE@')

    for row in search_cursor:
        for dist in range(0, int(row[0].length), pnt_dist):
            point = row[0].positionAlongLine(dist).firstPoint
            insert_cursor.insertRow([point])


It was setup to work on an existing point feature class. You could just as easily add a few lines to create it on the fly.

    if not arcpy.Exists(pnt_layer):
        arcpy.CreateFeatureclass_management(
            os.path.dirname(pnt_layer), os.path.basename(pnt_layer), 'POINT')
...
    return pnt_layer


Then add the split line at point tool where you call the function.
points_along_line(line_features, point_features, pnt_dist)
arcpy.SplitLineAtPoint_management(line_features, point_features, out_feature_class)
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Coincidentally, this exact same topic came up in ArcPy Café the other week:  Split into equal length features.  A team member Dave (I don't know him other than his name is Dave) provided a fairly compact and elegant solution.

0 Kudos
jaykapalczynski
Frequent Contributor

All I am trying to do is highlight a line segment and have to user click somewhere on the line and split it into two segments....user define split.

EDIT:  Trying to do this in an ArcGIS online app or a written JavaScript app.

0 Kudos
BlakeTerhune
MVP Regular Contributor

jay kapalczynski

You should really post this question in the ArcGIS API for JavaScript​ area.

jaykapalczynski
Frequent Contributor

Just realized that and just did that…thanks

Thanks

Jay

Jay Kapalczynski

GIS Coordinator - Virginia Department of Game & Inland Fisheries

(new address) 7870 Villa Park Drive Suite 400, Henrico VA 23228

Phone: 804.367.6796 | Fax: 804.367.2628

ü Please consider the environment before printing this email.

0 Kudos