Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Running ArcGIS 10.1, 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.

I've tried a few times to start this, but I can't seem to figure out the best way to do this. Any help is much appreciated!

share|improve this question
2  
Would you be able to edit your Question to include the GIS software and version that you are using, please? –  PolyGeo Feb 25 at 0:16

3 Answers 3

up vote 5 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)
        arcpy.SelectLayerByAttribute_management(states_lyr,'NEW_SELECTION',where)

        # Now select the polling stations by location using the selected state feature
        #
        arcpy.SelectLayerByLocation_management(polling_stations_lyr,'INTERSECT',states_lyr)

        # 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
        cur.updateRow(row)

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. –  Markus Feb 25 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 at 16:08
1  
That did it - thanks so much Jason! –  Markus Feb 25 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 at 1:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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