0

I have a file geodatabase containing line and polygon feature classes of the same name. Below is an example:

  • NATGAS_6IN_Line
  • NATGAS_6IN_Polygon
  • CAD_ARD_6IN_Line
  • CAD_ARD_6IN_Polygon

I have 20 line and polygons pairs I want to intersect. The line must only be intersected with the polygon of the matching name, e.g., the NATGAS_6IN line must only be intersected with the NATGAS_6IN polygon. Because I have 20 sets, I want to write a loop that iterates through the geodatabase so I don't have to write 20 individual arcpy.analysis.Intersect lines of code. The for loop I've come up with looks like this:

arcpy.env.workspace = buffer_cl_gdb
for file in arcpy.ListFeatureClasses(feature_type='ALL'):
    Line_List = ['NATGAS_6IN','CAD_ARD_8IN']
    for name in Line_List:
        buffer =  '{}_buffers'.format(name)
        centerline = '{}_CL'.format(name)
        output = class_segments_gdb +"\\" + os.path.basename(name)
        print(buffer)
        arcpy.analysis.Intersect([buffer,centerline],output)

This code does work, however, it iterates through the Line_List 40 times for some reason. I would like it to only iterate through the list once.

Any thoughts on how to improve the code?

1
  • For code review there is the Code Review Stack Exchange.
    – PolyGeo
    Feb 14 at 19:07

1 Answer 1

2

It because you have a redundant loop that is causing it to loop over all datasets. The offending line, which you don't actually use in any way, is:

for file in arcpy.ListFeatureClasses(feature_type='ALL'):

Your code should be:

arcpy.env.workspace = buffer_cl_gdb
Line_List = ['NATGAS_6IN','CAD_ARD_8IN']
for name in Line_List:
    buffer =  '{}_buffers'.format(name)
    centerline = '{}_CL'.format(name)
    output = class_segments_gdb +"\\" + os.path.basename(name)
    print(buffer)
    arcpy.analysis.Intersect([buffer,centerline],output)
1
  • That did the trick, thank you very much!
    – BNew
    Feb 14 at 19:20

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.