4

I am trying to trim each polyline in a shapefile down to the first 15 feet of each line. The segmentAlongLine method was suggested to me, but this code is getting the AttributeError: 'list' object has no attribute 'segmentAlongLine'.

SHAPE@ in this case is a polyline object... Why is it calling it a list and why isn't segmentAlongLine working? I have found questions about this error but none of the answers seem to apply to my issue.

with arcpy.da.UpdateCursor(fc,"SHAPE@") as cursor:  
    for row in cursor:   
        row.segmentAlongLine(0,15)
1
  • print(row) outputs (<Polyline object at 0x161d5c90[0x161d5d60]>,) for each of my 260,000 rows
    – ElizaC
    Commented May 12, 2021 at 16:05

2 Answers 2

5

row is a list containing the shape of the feature. Use row[0].segmentAlongLine(0,15).

3
  • Thank you. I tried this and got "Attribute Error: 'NoneType' object has no attribute 'segmentAlongLine' ". Any ideas?
    – ElizaC
    Commented May 12, 2021 at 15:29
  • Your list is empty
    – Ian Turton
    Commented May 12, 2021 at 15:38
  • @ElizaC it means that the feature has an empty geometry. Because row[0] returns None. Commented May 13, 2021 at 4:37
4

What if you try:

errors = []
with arcpy.da.UpdateCursor(fc,["SHAPE@", "OID@"]) as cursor:  
    for row in cursor:
        try:
            row[0].segmentAlongLine(0,15)
        except AttributeError:
            errors.append(row)

What is in errors list?

And you are not changing anything with your code. You need to assign row[0] a new value and cursor.updateRow(row)

3
  • print (errors) outputs [[None, 11035]]
    – ElizaC
    Commented May 12, 2021 at 16:42
  • Then that rows geometry is broken/empty
    – Bera
    Commented May 12, 2021 at 16:43
  • Thank you so much. I deleted that record and the script seemed to work successfully (no errors anyway) however... none of the lines actually shortened. The shapefile looks exactly the same. I can't imagine what the problem could be now.
    – ElizaC
    Commented May 12, 2021 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.