<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Slow Count Points in Polygons Script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/slow-count-points-in-polygons-script/m-p/363765#M28780</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Spatial join might work, but does it work for overlapping polygons? It is critical that the points are accurately counted in my case. I adjusted a few parts of my script and it has sped it up considerably. It's still not fast enough to process 300,000 records in under a few hours, but I thought I'd post this improved version in case it was helpful to someone else out there. I still don't understand why this simple procedure has to take so long! :confused:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#-------------------------------------------------------------------------------
# Name:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Count Points in Polygon
# Purpose:
#
# Author:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hcopeland
#
# Created:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 22/08/2011
# Copyright:&amp;nbsp;&amp;nbsp; (c) hcopeland 2011
# Licence:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;your licence&amp;gt;
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import arcpy
from arcpy import env

#getcontext().prec = 8

ws = r'D:\Work\Projects\SageGrouseEasementScenarios2011\Data\FinalModelData.gdb'
arcpy.env.workspace = ws

# set overwrite outputs to true
arcpy.env.overwriteOutput = 1

POINT = 'D:\Work\Projects\SageGrouseEasementScenarios2011\Data\Final_Development_Buildouts\Cumulative_buildouts.gdb\Cumulative_Baseline'
POLYGON = 'LekParcelFC'

# Converts unselectable feature class into a selectable feature layer
arcpy.MakeFeatureLayer_management(POINT,"point")

rows = arcpy.SearchCursor(POLYGON)

print 'Calculating Points in Polygons...'

### Loop through each row and count the points in each polygon record
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression2 = "\"OBJECTID\" = " + str(row.OBJECTID) + ""
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #expression = "\"OBJECTID\" = " + str(row.OBJECTID) + ""' AND "PARCELNB" &amp;lt;&amp;gt; ' + "'""'"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print expression2
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select each record inside of the polygon feature class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(POLYGON,"polygon2",expression2)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select all the points that are inside of the polygon record
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("point", "intersect", "polygon2", 0, "NEW_SELECTION")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Count the points that are in each polygon
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetCount = arcpy.GetCount_management("point")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Calculate the ASSOC_PTS field with the counted points
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management("polygon2", "CEFeaturesPreventedBaseline", GetCount, "VB", "")

except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.GetMessages(2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Script Bombed")

print 'Count Points in Polygon Script Finished!'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 16:57:11 GMT</pubDate>
    <dc:creator>HollyCopeland</dc:creator>
    <dc:date>2021-12-11T16:57:11Z</dc:date>
    <item>
      <title>Slow Count Points in Polygons Script</title>
      <link>https://community.esri.com/t5/python-questions/slow-count-points-in-polygons-script/m-p/363763#M28778</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I wrote this script to iterate through a polygon fc and count the number of points in each polygon and put that information in a new field. The script works, but it is REALLY slow. If I use Hawth's Tools to do the same thing, it takes 20-30 minutes. With this script, it was set to take 4 days. Any ideas what I've done wrong--I have a feeling it's related to the search cursor. Should I not be using the select by location/select by attribute ESRI functions? Thanks, Holly&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#-------------------------------------------------------------------------------
# Name:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Count Points in Polygon
# Purpose:
#
# Author:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hcopeland
#
# Created:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 22/08/2011
# Copyright:&amp;nbsp;&amp;nbsp; (c) hcopeland 2011
# Licence:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;your licence&amp;gt;
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import arcpy
from arcpy import env

#getcontext().prec = 8

ws = r'D:\Work\Projects\SageGrouseEasementScenarios2011\Data\FinalModelData.gdb'
arcpy.env.workspace = ws

# set overwrite outputs to true
arcpy.env.overwriteOutput = 1

POINT = 'D:\Work\Projects\SageGrouseEasementScenarios2011\Data\Final_Development_Buildouts\Cumulative_buildouts.gdb\Cumulative_Unconstrained'
POLYGON = 'LekParcelFC'

print 'Making Multipart into Single Part....'
#Convert multipart to singlepart
#arcpy.MultipartToSinglepart_management(POLYGON,"LekParcelFCSingle")

# Converts unselectable feature class into a selectable feature layer
arcpy.MakeFeatureLayer_management(POINT,"point")
arcpy.MakeFeatureLayer_management(POLYGON,"polygon")

rows = arcpy.SearchCursor("polygon")

print 'Calculating Points in Polygons...'

# Loop through each row and count the points in each polygon record
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select each record inside of the polygon feature class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #expression = "\"OBJECTID\" =" + str(row.OBJECTID) + ' AND "PARCELNB" &amp;lt;&amp;gt; ' + "''"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = "\"OBJECTID\" = " + str(row.OBJECTID) + ""
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print expression
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SelPoly = arcpy.SelectLayerByAttribute_management("polygon", "NEW_SELECTION",expression)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select all the point that are inside of the polygon record
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SelPts = arcpy.SelectLayerByLocation_management("point", "WITHIN", SelPoly, 0, "NEW_SELECTION")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Count the points that are in each polygon
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetCount = arcpy.GetCount_management(SelPts)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Calculate the ASSOC_PTS field with the counted points
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management("polygon", "CEFeaturesPreventedHigh", GetCount, "VB", "")

except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.GetMessages(2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Script Bombed")

print 'Count Points in Polygon Script Finished!'&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Oct 2011 21:44:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-count-points-in-polygons-script/m-p/363763#M28778</guid>
      <dc:creator>HollyCopeland</dc:creator>
      <dc:date>2011-10-06T21:44:25Z</dc:date>
    </item>
    <item>
      <title>Re: Slow Count Points in Polygons Script</title>
      <link>https://community.esri.com/t5/python-questions/slow-count-points-in-polygons-script/m-p/363764#M28779</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, that would indeed be a slow boat to China... A faster method: Use the SpatialJoin or Identity tool to assign the polygons OBJECTID onto the points. Then use the Frequency tool to count the occurances (points per polygon OBJECTID). Then use a simple tabular join and field calc to put the count (aka "FREQUENCY") values into a new field in the polygon FC.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Oct 2011 22:20:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-count-points-in-polygons-script/m-p/363764#M28779</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2011-10-06T22:20:47Z</dc:date>
    </item>
    <item>
      <title>Re: Slow Count Points in Polygons Script</title>
      <link>https://community.esri.com/t5/python-questions/slow-count-points-in-polygons-script/m-p/363765#M28780</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Spatial join might work, but does it work for overlapping polygons? It is critical that the points are accurately counted in my case. I adjusted a few parts of my script and it has sped it up considerably. It's still not fast enough to process 300,000 records in under a few hours, but I thought I'd post this improved version in case it was helpful to someone else out there. I still don't understand why this simple procedure has to take so long! :confused:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#-------------------------------------------------------------------------------
# Name:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Count Points in Polygon
# Purpose:
#
# Author:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hcopeland
#
# Created:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 22/08/2011
# Copyright:&amp;nbsp;&amp;nbsp; (c) hcopeland 2011
# Licence:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;your licence&amp;gt;
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import arcpy
from arcpy import env

#getcontext().prec = 8

ws = r'D:\Work\Projects\SageGrouseEasementScenarios2011\Data\FinalModelData.gdb'
arcpy.env.workspace = ws

# set overwrite outputs to true
arcpy.env.overwriteOutput = 1

POINT = 'D:\Work\Projects\SageGrouseEasementScenarios2011\Data\Final_Development_Buildouts\Cumulative_buildouts.gdb\Cumulative_Baseline'
POLYGON = 'LekParcelFC'

# Converts unselectable feature class into a selectable feature layer
arcpy.MakeFeatureLayer_management(POINT,"point")

rows = arcpy.SearchCursor(POLYGON)

print 'Calculating Points in Polygons...'

### Loop through each row and count the points in each polygon record
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression2 = "\"OBJECTID\" = " + str(row.OBJECTID) + ""
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #expression = "\"OBJECTID\" = " + str(row.OBJECTID) + ""' AND "PARCELNB" &amp;lt;&amp;gt; ' + "'""'"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print expression2
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select each record inside of the polygon feature class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(POLYGON,"polygon2",expression2)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select all the points that are inside of the polygon record
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("point", "intersect", "polygon2", 0, "NEW_SELECTION")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Count the points that are in each polygon
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetCount = arcpy.GetCount_management("point")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Calculate the ASSOC_PTS field with the counted points
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management("polygon2", "CEFeaturesPreventedBaseline", GetCount, "VB", "")

except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.GetMessages(2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Script Bombed")

print 'Count Points in Polygon Script Finished!'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:57:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-count-points-in-polygons-script/m-p/363765#M28780</guid>
      <dc:creator>HollyCopeland</dc:creator>
      <dc:date>2021-12-11T16:57:11Z</dc:date>
    </item>
    <item>
      <title>Re: Slow Count Points in Polygons Script</title>
      <link>https://community.esri.com/t5/python-questions/slow-count-points-in-polygons-script/m-p/363766#M28781</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;but does it work for overlapping polygons?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There's only one way to find out...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Using the Spatial Join tool, you will need to specify a Join operation of "ONE_TO_MANY" to deal with the overlapping polys. In addition to the afore mentioned Identity tool, the Intersect tool will also work. Both the Identity and Intersect correctly handle the one-to-many point/poly relationship by default.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Don't reinvent the wheel... There are (at least three) out of the box tools that are designed to do exactly what you want!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Oct 2011 15:57:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-count-points-in-polygons-script/m-p/363766#M28781</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2011-10-11T15:57:46Z</dc:date>
    </item>
  </channel>
</rss>

