Split into equal length features

Request came in last week for a way to split a line feature into 10 equal length line features.

The input looked like this

The accepted solution came from Dave on the team who sent this elegant and efficient solution.

in_fc = r'c:\projects\waterway.gdb\stream'
out_fc = r'c:\projects\waterway.gdb\stream10'
out_count = 10 # how many features desired

line = arcpy.da.SearchCursor(in_fc, ("SHAPE@",)).next()[0]
arcpy.CopyFeatures_management([line.segmentAlongLine(i/float(out_count), ((i+1)/float(out_count)), True) for i in range(0, out_count)], out_fc)

Which outpus a new feature class containing 10 line features like so

How does it work? The first 3 lines are self explanatory. So we will skip.

The following line is also fairly simple, what it does is get the geometry (shape@) of the first record (we only ask for next() once). The [0] is needed to get just the first value in the record (remember cursors return lists of values).

polyline = arcpy.da.SearchCursor(in_fc, ("SHAPE@",)).next()[0]

Next is where the magic happens, python list comprehension is used to turn the polyline object into a list of 10 (as per the out_count variable) equal length segments generated by the segmentAlongLine function. This list of polyline is then used as input to CopyFeatures (as per Using geometry objects with geoprocessing tools) which writes out the 10 polyline as individual features into the output feature class (out_fc).

arcpy.CopyFeatures_management([polyline.segmentAlongLine(i/float(out_count), ((i+1)/float(out_count)), True) for i in range(0, out_count)], out_fc)

EDIT: As was pointed out in the comments, the segmentAlongLine is new at 10.3.



14 thoughts on “Split into equal length features

  1. I’m guessing this is a new method exposed in 10.3 as there is no mention of segmentAlongLine in the 10.2.2 desktop help?

  2. That’s pretty cool. Where can we find the documentation for the segmentAlongLine method? I’ve tried googling it, but can’t find anything.

  3. Where did the segment along line method come from. I certainly don’t have access to it at 10.2.2. What version is it available at and what other new methods have been created for use with geometry objects.

  4. I agree the overall approach is elegant and efficient, but I can’t say the code is highly readable. To each his own, but embedding a 100+ character list comprehension as an argument to a function doesn’t read cleanly to me.

    As a postscript, the most elegant and efficient solution isn’t necessarily a helpful solution if it depends on functionality that isn’t supported. Yes, 10.3 will be released in a couple or few weeks, but it could be months before some organizations move their employees to the newest version. It would have been nice to get a helpful solution as well.

    • Yeah using list comps can sometimes be challenging to read, but they’re a great part of the language and worthwhile tool to know about.

  5. What if I want to specify segment length instead of output segment count? In many cases you would have no idea how many segments you want but might know that you need them to be 10′ long, especially if you are running this on a larger set of features.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s