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))
        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))


If anyone can help :)

  • I am sure you can script something like this...support.esri.com/cn/knowledgebase/techarticles/detail/30779
    – Branco
    Jan 28, 2015 at 16:33
  • 2
    Every time the "for row in rows" loop is run, the counter will be set to 0, and from what I understand, you want to get the max out of your entire loop, so you may want to initialize it outside. Also, arcpy.addMessage takes in a string, so if you could clarify from where "cheflTab25" comes from, that would be great.
    – Martin
    Jan 28, 2015 at 16:38
  • 1
    This seems like an elaborate way to rewrite the Spatial Join tool, which counts points within polygons (Join_Count field).
    – phloem
    Jan 28, 2015 at 17:44

2 Answers 2


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.

  • 1
    I've never been able to find a satisfactory answer for a system tool for saving out as a CSV file. So I tend to work with geodatabase tables/dbase. If a text file is required I would probably do it your way. Sort the table then read the first row and at that point use a cursor. But for this task I would almost certainly have approached it using modelbuilder writing to dBase file. Horses for courses! :)
    – Hornbydd
    Jan 28, 2015 at 20:20

The following approach uses a Search Cursor and Python dictionary to perform the following workflow:

  1. Select points within each polygon feature
  2. Update dictionary with key (OID) and value (point count) for each iteration
  3. Find max point value and corresponding OID and write to a text file

import arcpy, os

points = r'C:\temp\mytest\points.shp'
polys = r'C:\temp\mytest\polys.shp'

arcpy.MakeFeatureLayer_management(points, "pointsLyr")

data = {}

with arcpy.da.SearchCursor(polys, ["OID@", "SHAPE@"]) as cursor:
    for row in cursor:
        arcpy.SelectLayerByLocation_management("pointsLyr", select_features = row[1])
        count = int(arcpy.GetCount_management("pointsLyr").getOutput(0))
        data[row[0]] = count

# Write the output to a text file
text_file = open(r"C:\temp\Output.txt", "w")
text_file.write("OID = %s; count = %s" % (max(data, key = data.get), data[max(data, key = data.get)]))

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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