Help with SearchCursor

1572
5
Jump to solution
07-22-2016 07:54 AM
ChrisBruce
New Contributor III

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!

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

In your example, the variable 'line' is actually a polyline geometry object (line.segmentAlongLine()). You are trying to apply segmentAlongLine to a cursor object. Try something like:

arcpy.CopyFeatures_management(row[1].segmentAlongLine(0, len, False), out_fc)

View solution in original post

5 Replies
DarrenWiens2
MVP Honored Contributor

In your example, the variable 'line' is actually a polyline geometry object (line.segmentAlongLine()). You are trying to apply segmentAlongLine to a cursor object. Try something like:

arcpy.CopyFeatures_management(row[1].segmentAlongLine(0, len, False), out_fc)

View solution in original post

ChrisBruce
New Contributor III

Thanks Darren. That makes sense. I realized I was having a more fundamental problem with PyScripter because it was crashing on the variable assignment. Not sure what's going on there but when I ran the same thing in a different IDE that part worked. And it's writing out the first line correctly. But now the problem is it's crashing after the first iteration because it says the output feature class already exists. I'm getting closer anyway!

0 Kudos
DarrenWiens2
MVP Honored Contributor

One sticking point may be that len is a built-in function, returning length of an object, so it may not like that.

Also, you probably don't want to run CopyFeatures on each iteration, but build a list of geometries inside the loop and run CopyFeatures afterwards, or use an InsertCursor rather than SearchCursor to write new geometries inside the cursor.

0 Kudos
ChrisBruce
New Contributor III

Good point though that variable name seems to be working. I read up on CopyFeatures and found that it will indeed crash if the feature class already exists (hence it working on the first iteration and crashing on the second). Now I'm trying Append and getting an error about the schema not matching.

0 Kudos
ChrisBruce
New Contributor III

OK, I'm not sure why the initial error but I got it to work with append. So the code looks like this:

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.Append_management(row[1].segmentAlongLine(0, len, False), out_fc)

There are still a few things I need to figure out but I'm much further along than when I started so thanks again!

0 Kudos