AnsweredAssumed Answered

Help with SearchCursor

Question asked by cbruce on Jul 22, 2016
Latest reply on Jul 22, 2016 by cbruce

I have a line feature class and I want to split (or extract) each line at a specific length. This length value is contained in an attribute. I've found a couple of code examples that do similar things but I'm a Python novice and I'm having a hard time putting it all together.


On the ArcPy Cafe site I found this example that splits a line into 10 equal parts using the segmentAlongLine method:


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)


This is similar to what I want to do but rather than split a single line into multiple parts I want to iterate through each line in the feature class and extract a segment of the line length from 0 to the number that's contained in an attribute.


I also found this example code in the ArcGIS help, which iterates through features in a feature class and prints attributes and geometry values:


fc = 'c:/data/base.gdb/well'

fields = ['WELL_ID', 'WELL_TYPE', 'SHAPE@XY']


# For each row print the WELL_ID and WELL_TYPE fields, and the feature's x,y coordinates

with arcpy.da.SearchCursor(fc, fields) as cursor:

    for row in cursor:

        print('{0}, {1}, {2}'.format(row[0], row[1], row[2]))


I can get code similar to the above to work with my data but I'm having a hard time combining this with the first example to accomplish what I need. I think what I'm having a hard time with is just accessing that length attribute within the cursor. I've tried the following (and several variations) but it crashes:


import arcpy

in_fc = r'F:\scratch\transects_processing.gdb\transects_test'

out_fc = r'F:\scratch\transects_processing.gdb\transects_split'

fields = ['SplitLength', 'SHAPE@']


with arcpy.da.SearchCursor(in_fc, fields) as cursor:

    for row in cursor:

        len = row[0]

        arcpy.CopyFeatures_management(cursor.segmentAlongLine(0, len, False), out_fc)


If anyone can help with this I would greatly appreciate it!