Counting points inside a polygon

7528
20
Jump to solution
01-26-2016 07:32 AM
AdrianWelsh
MVP Honored Contributor

This is a fairly "simple" GIS operation but I wanted to ask, is Spatial Join the only, or best, method to calculate this? As described in this article:

30779 - Count the number of point features within a polygon

What I am basically wondering is how to have a continually updated polygon feature that would keep a count of points within it. Does that make sense?

Thanks!

0 Kudos
20 Replies
forestknutsen1
MVP Regular Contributor

You could script the solution with python and then use Windows Task Scheduler to have the operation run every night or more often if needed (every hour, every 30 minutes, or whatever is appropriate for your situation). If python is not your thing one could make it in model builder and the export it to python.

AdrianWelsh
MVP Honored Contributor

Yeah, this might be something that I might have to do, unfortunately. But, it's still sort of a solution...

0 Kudos
VinceAngelo
Esri Esteemed Contributor

If the geometry is being continually updated, then the time to make the PCOUNT edit is when the shape is updated (together, in one transaction).  Updating the counts in batch would only make sense if all the polygons are edited at the same time, in which case you should make all the edits to an intermediate copy, then update the master after the long-running job is complete.

- V

0 Kudos
DarrenWiens2
MVP Honored Contributor

You could look into creating a Python Add-in Application Extension. See a list of available triggers here. It depends how often you want to run your script, but I expect you would probably want something that triggers fairly regularly, like: onEditorSelectionChanged() or onCurrentTaskChanged() or onSaveEdits(). Your script could be as simple as one line within the add-in event method calling Spatial Join.

AdrianWelsh
MVP Honored Contributor

Thanks Darren, I'll look into that.

0 Kudos
JimmyKroon
Frequent Contributor

I have a simple python script that will count points within polygons and add the count to a field in the polygon feature class. It's pretty simple and you could run it automatically using a batch file and windows scheduler - or just run the script as needed from explorer. If that sounds helpful I'll find and post it.

AdrianWelsh
MVP Honored Contributor

Jimmy, that sounds awesome. I would love to see it.

0 Kudos
JimmyKroon
Frequent Contributor

It is attached. You'll need to edit these variables to make it work and the polygon feature class needs an integer field to store the results.

Points = "D:\\Data\\Geodatabase.gdb\\PointFC"
Zones = "D:\\Data\\Geodatabase.gdb\\PolygonFC"

zoneNameField = "NAME"
zoneIncidentsField = "INCIDENTS"

Once your paths and field names are stored you can double-click from windows explorer to run anytime you want.

AdrianWelsh
MVP Honored Contributor

Wow thanks! Great script. Those does exactly what I needed it to do. One question though, what is the point of the "zoneNameField" in this? Doesn't the script just cycle through all of the features within that feature class? If some features had the same zone name, would those get counted the same?

Thanks again.

0 Kudos
JimmyKroon
Frequent Contributor

zoneNameField is used to build a query which is required to create a feature layer containing one polygon. You could use the objectID field for this value too.

queryString = '"' + zoneNameField + '" = ' + "'" + zoneName + "'"
arcpy.MakeFeatureLayer_management(Zones, "CurrentZoneLayer", queryString)

And yes, if you had multiple polygons with the same name, they would get grouped together for the counts because the query would select them all.