Update Cursor Help. To Iterate through line segments and get a count of points surrounding each segment.

236
5
Jump to solution
05-30-2019 08:51 AM
ConnorMcivor
New Contributor III

I have a series of line segments that I would like to use to find the count of points within 200 meters of each segment. Im having issues with the loop i have generated and I feel stumped on this one...

ClassLoc_Events_Lyr = arcpy.MakeFeatureLayer_management(ClassLoc_Events_Sort, "ClassLoc_Events_Lyr")
Point_ClassLoc_FC_Lyr = arcpy.MakeFeatureLayer_management(Poly_ClassLoc_FC, "Point_ClassLoc_FC_Lyr")

with arcpy.da.UpdateCursor(ClassLoc_Events_Lyr, ["OID@", "DWELLINGS_COUNT"]) as ClassLoc_Segments:
     for Segment in ClassLoc_Segments:
          Seg = arcpy.SelectLayerByAttribute_management(ClassLoc_Events_Lyr, "NEW_SELECTION", "OBJECTID = {}".format(Segment[0]))
          Sel = arcpy.SelectLayerByLocation_management(Point_ClassLoc_FC_Lyr, "WITHIN_A_DISTANCE", Seg, "200 Meters", "NEW_SELECTION")
          Out = arcpy.MakeFeatureLayer_management(Sel, "ID_Points_Lyr")
          Point_Count = arcpy.GetCount_management(Out)
          Segment.setValue("DWELLINGS_COUNT", Point_Count)
          ClassLoc_Segments.updateRow(Segment)
          arcpy.Delete_management("ID_Points_Lyr")
 

So the loop is supposed to update the line segments. First supposed to select the first row. Next it should then search within 200 meters of the selected row and make a new selection including only the points within the proximity. Then it makes a a seperate feature layer out of the selection in order to do the next step. Next step is to get a count of the selected points. Then it is supposed to update the row "DWELLINGS_COUNT" with the counted points. Finally the loop goes to the next row and the process repeats.

Getting an error on line 10:

AttributeError: 'list' object has no attribute 'setValue'

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Even if your current approach will technically work, it will be slow/inefficient.  ArcGIS geoprocessing tools are designed to work on sets of data, and since they are compiled code, they will generally outperform Python loops.  I suggest looking at Generate Near Table—Help | ArcGIS Desktop  and consider having it create the table for you, and then you can iterate through it to extract the results.

View solution in original post

5 Replies
ConnorMcivor
New Contributor III

So i have got the loop to start to function however the results are incorrect. The "DWELLINGS_COUNT" field is now populated with "3" in each row. The following is my revised code:

ClassLoc_Events_Lyr = arcpy.MakeFeatureLayer_management(ClassLoc_Events_Sort, "ClassLoc_Events_Lyr")
Point_ClassLoc_FC_Lyr = arcpy.MakeFeatureLayer_management(Poly_ClassLoc_FC, "Point_ClassLoc_FC_Lyr")
row = 1

with arcpy.da.UpdateCursor(ClassLoc_Events_Lyr, ["OID@", "DWELLINGS_COUNT"]) as ClassLoc_Segments:
	for Segment in ClassLoc_Segments:
		Seg = arcpy.MakeFeatureLayer_management(ClassLoc_Events_Lyr, "Segment_Lyr", "OBJECTID = " + str(row))
		Sel = arcpy.SelectLayerByLocation_management(Point_ClassLoc_FC_Lyr, "WITHIN_A_DISTANCE", ClassLoc_Events_Lyr, "200 Meters", "NEW_SELECTION")
		Out = arcpy.MakeFeatureLayer_management(Sel, "ID_Points_Lyr")
		Point_Count = arcpy.GetCount_management(Out)
		Segment[1] = str(Point_Count)
		ClassLoc_Segments.updateRow(Segment)
		arcpy.Delete_management("Segment_Lyr")
		arcpy.Delete_management("ID_Points_Lyr")
		row = row + 1
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Even if your current approach will technically work, it will be slow/inefficient.  ArcGIS geoprocessing tools are designed to work on sets of data, and since they are compiled code, they will generally outperform Python loops.  I suggest looking at Generate Near Table—Help | ArcGIS Desktop  and consider having it create the table for you, and then you can iterate through it to extract the results.

ConnorMcivor
New Contributor III

So this process is way faster than my loop however the results are showing only the closest point to the line segment. Im not sure how i can manipulate this process to give me the answers I need but I will keep trying... Attached are some screenshots. 

Note:

Line segments are overlapping features 100m meters apart (See in image 1, and 2, how there is slight overlap)

Image 3 shows the result table called "DWELLINGS_COUNT" and shows the corresponding features selected in the row. (One point seen selected to the right side near the line)

I need all points within my search radius listed (to be counted after the fact)

Image 4 shows what should be identified and counted

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Looking at the summary for Generate Near Table—Help | ArcGIS Desktop 

Summary

Calculates distances and other proximity information between features in one or more feature class or layer. Unlike the Neartool, which modifies the input, Generate Near Table writes results to a new stand-alone table and supports finding more than one near feature.

The tool is designed specially to give "more than one near feature."  Looking at the syntax for the tool, the default is for closest parameter to be CLOSEST, you should be passing ALL with a search radius.

0 Kudos
ConnorMcivor
New Contributor III

Hey Joshua thanks for the suggestion, I went through it and found that you were indeed correct! Thanks for the help!

0 Kudos