This is my first time here, so I'll try to be as specific as I can.
What I'm trying to do is found out which polygon contains the most points and write the result in a text file.
Here is my script : (tab25Select are the polygons and chefLyr are the points)
chefLyr = arcpy.MakeFeatureLayer_management(cheflieuIN, "chefLyr")
with arcpy.da.SearchCursor(tab25Select, ["Numero", "Name"]) as rows:
for row in rows:
chefTab25Max = 0
chefTab25 = arcpy.GetCount_management(arcpy.SelectLayerByLocation_management(chefLyr, "WITHIN", tab25Select))
arcpy.AddMessage(cheflTab25)
for chf in cheflTab25:
if chf > chefTab25Max:
chefTab25Max = cheflTab25
a = row[0]
b = row[1]
ficTxt.write("\r\n The polygon containing the most point is :{0}".format(chefTab25Max))
ficTxt.close()
If anyone can help :)
Answer
I can crush this down to 3 lines of code, no cursors required!
import arcpy
arcpy.SpatialJoin_analysis("Site", "points","in_memory/points_SpatialJoin", "JOIN_ONE_TO_MANY", "KEEP_ALL", "", "INTERSECT")
arcpy.Statistics_analysis("points_SpatialJoin", "in_memory/stats", "Join_Count SUM","Id")
Then simply sort the table to find the polygon with most points.
No comments:
Post a Comment