Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Running ArcGIS 10.1 for Desktop, I have two feature classes in a geodatabase. One is a point FC for polling stations, the other is a polygon FC for states.

The polling station FC contains over 1000 points and has a states field indicating which state the polling station is located in. The states FC has an attribute for each state and has a blank field for the number of polling stations in each state.

I need a python script that will get the count of how many polling stations are located in each state from the polling station FC, and then write those numbers to the column in the states FC - obviously to their corresponding state.

share|improve this question

closed as off-topic by PolyGeo May 30 at 4:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 6 down vote accepted

If you are using ArcGIS I would think an easy way to do this, without creating any intermediary products, would be to select each state in your states polygon (assuming you have multipart features) and then select by location the polling stations features. You can then get a count of how many features are selected and writing this value to your states polygon. This would look something like this:

import arcpy

# Define some variables
polling_stations = r'c:\path\to\geodatabase.gdb\polling_stations'
states = r'c:\path\to\geodatabase.gdb\states'

# Make feature layers for processing
polling_stations_lyr = arcpy.MakeFeatureLayer_management(polling_stations,r'in_memory\polling_stations_lyr')
states_lyr = arcpy.MakeFeatureLayer_management(states,r'in_memory\states_lyr')

# Create an update cursor to access and update states features
fields = ['field_containing_state_names','field_which_will_be_updated_with_#_of_polls']
with arcpy.da.UpdateCursor(states_lyr,fields) as cur:
    for row in cur:

        # Make a query to select this feature to be used in a selection of polling places
        state = row[0]
        where = '"field_containing_state_names" = \'{}\''.format(state)

        # Now select the polling stations by location using the selected state feature

        # Count the number of polling stations selected
        number_of_polling_stations = int(arcpy.GetCount_management(polling_stations_lyr).getOutput(0))

        # Update the state feature with this value
        row[1] = number_of_polling_stations

print('Operation complete.')

This code requires ArcGIS 10.x, but can be made compatible with previous versions by using the older style cursor in place of the data access module cursor used here.

share|improve this answer
Thank you, this has helped a lot! It seems that something with the 'SelectLayerByAttribute' is not working properly. I did notice that the where clause was in the incorrect location (it should come after 'NEW_SELECTION') but this didn't solve the problem. When I execute the script after switching the location of the where clause, I get an invalid expression error. – Carto Feb 25 '14 at 15:03
Sorry about that, you're right about the order I'll update that in my answer. I think the "invalid expression" is due to some missing quotation marks that I left out of the where statement. I'll fix that up as well...let me know how it works for you. – Jason Feb 25 '14 at 16:08
That did it - thanks so much Jason! – Carto Feb 25 '14 at 16:36

There are a few ways to tackle this, but here's a pretty easy one. I'm going to assume you're using ArcGIS. This can be easily done without python. Have you looked at the Summary Statistics tool? You can run that on your Polling Station layer and join the resulting table back to the State feature class.

Set the Statistics field to State and statistics type to Count. Join the results to the state feature class and update the Count field in that layer.

share|improve this answer

Assuming you are using ArcGIS, you could write a python script to do this, but you could also just do the following:

  1. Intersect the points with the state polygons. This will produce a new point dataset.
  2. Run a the Frequency tool to get a count of the number of points per State field.
  3. Join this to the original state polygon FC and calculate your column.
share|improve this answer
The only possible issue with using the Frequency tool instead of the Summary Statistics tool is that Frequency is (ridiculously) only available with an ArcInfo/Advanced license. – RyanDalton Feb 26 '14 at 1:43

To bring another not Python-coding-based solution: You could use the tool 'Tabular Intersection' to get an Output table by states with the number of intersecting polling stations. This table again, can be joined to the states Shapefile to add the attribute column with the number of polling stations.

share|improve this answer

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