0

I am trying to create and update a field with a count of line features (tLayer) within a distance of point features (sLayer). I am attempting to use a combination of AddField_management, arcpy.da.SearchCursor, SelectLayerByLocation_management, arcpy.GetCount_management, and arcpy.da.UpdateCursor.

The code I have for this is currently updating all records for the Line_Count field with the count of the point features (i.e. 2) for only the last record. Though, a print statement following the GetCount line will return the line count for all of the point features (with a few unessential iterations). I believe it is only printing the last row in 'result' for the Line_Count update statement. Is there an efficient way for result (i.e. result=int(arcpy.GetCount_management("tLayer_lyr").getOutput(0))) to be saved as a list or an array that can be written to the Line_Count field? ...Or would it be best to use a dictionary?

What do I need to do to appropriately update the Line_Count field for all of the records? Also, this process will be applied to a large dataset and will be extended to include 'where clauses'. Are there any suggestions as to how to make this as efficient as possible?

import arcpy
from arcpy import env
arcpy.env.OverwriteOutput = True
defaultGdbPath = 'C:\Topo_Check_Tess_V5.gdb'
sLayer='C:\Topo_Check_Tess_V5.gdb\Stations'
tLayer='C:\Topo_Check_Tess_V5.gdb\Lines'
#ppLayer='C:\Topo_Check_Tess_V5.gdb\Plants'
arcpy.AddField_management(sLayer, "Line_Count", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
TLineCountField = "Line_Count"
arcpy.MakeFeatureLayer_management(tLayer, "tLayer_lyr")
with arcpy.da.UpdateCursor (sLayer, TLineCountField) as LineCountList:
 for s_row in LineCountList:
 with arcpy.da.SearchCursor(sLayer,["OBJECTID", "SHAPE@"]) as s_list:
 for station in s_list:
 arcpy.SelectLayerByLocation_management("tLayer_lyr", 'WITHIN_A_DISTANCE', station[1],.000002, "NEW_SELECTION")
 result=int(arcpy.GetCount_management("tLayer_lyr").getOutput(0))
 print result
 LineCountList.updateRow(s_row)
 del station 
 del s_row
print "Done"

Updated Line_Count Field Print readout

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Feb 14, 2017 at 23:29
7
  • Add s_row[0] = result before LineCountList.updateRow(s_row) Commented Feb 14, 2017 at 23:51
  • You might be able to use the Generate Near Table tool to accomplish the same thing. Commented Feb 14, 2017 at 23:52
  • for each row in the update cursor, it is running through every row in the search cursor, so you're only ever getting the result from of the final row of the search cursor. make sense? Commented Feb 14, 2017 at 23:59
  • Why the needless scripting? Create a frequency table, then join and calculate pro.arcgis.com/en/pro-app/tool-reference/analysis/frequency.htm Commented Feb 15, 2017 at 0:13
  • 1
    Nesting cursors and expensive GP calls for spatial operations is terrible for performance. Unravel all those for loops and see where it can be optimized. Commented Feb 15, 2017 at 1:17

1 Answer 1

1

It is generally not an efficient approach (as several have commented) to use cursor processing in this way, but if you accept the performance hit (maybe you're not working with large feature classes), then you should at least eliminate the nested cursor. The update cursor can also be used as your 'read' cursor, so something like the following rewrite of your outer with block should work - this is untested:

with arcpy.da.UpdateCursor (sLayer, [TLineCountField, "SHAPE@"]) as LineCountList:
 for s_row in LineCountList:
 arcpy.SelectLayerByLocation_management("tLayer_lyr", 'WITHIN_A_DISTANCE', s_row[1], .000002, "NEW_SELECTION")
 result=int(arcpy.GetCount_management("tLayer_lyr").getOutput(0))
 print result
 s_row[0] = result
 LineCountList.updateRow(s_row)
answered Feb 15, 2017 at 8:09
1
  • Thank you soooo much. This works perfectly! I will be working with large feature classes, so I will need to look into ways to optimize efficiency. I'll look into some of the suggestions for this. Thanks again! Commented Feb 15, 2017 at 15:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.