0

I am trying to write a script to select each individual neighborhood using one FC, then use select by location to see the number of lots from another FC that are in the neighborhood and store that number in the neighborhood FC. I am trying to figure out how to write the script of selecting each neighborhood individually using arcpy.da.UpdateCursor.

This is my first time trying to write a real script outside of school.

closed as off-topic by PolyGeo Feb 12 at 21:20

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

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

3

A Python solution: after creating the field to store your counts, include the neighborhood 'SHAPE@' field in your UpdateCursor, then for each row call Select By Location using the individual geometry as the input, rather than the entire feature class.

with arcpy.da.UpdateCursor(neighborhoods_fc, ['SHAPE@', 'COUNT_FIELD']) as cursor:
    arcpy.MakeFeatureLayer_management(lots_fc, 'lots_lyr')
    for row in cursor:
        geom = row[0]
        arcpy.SelectLayerByLocation_management('lots_lyr', 'WITHIN', geom, selection_type='NEW_SELECTION')
        row[1] = int(arcpy.GetCount_management('lots_lyr').getOutput(0))
        cursor.updateRow(row)

The "within" option for the selection might not be exactly what you want/need, so make sure to read about all of the available options (documentation).

1

You don't need to involve arcpy cursors for this. You can do this either in ArcMap GUI by right-clicking the Neighborhoods layer in the TOC and choosing Joins and Relates > Join. Then:

enter image description here

And then you will get a new feature class with a new field Count where the number of schools is supplied:

enter image description here

Since you want to write a Python script, you would use the Spatial Join GP tool in the code.

It will be much fewer lines of code comparing to using cursors. Unless you working with millions of features, I'd suggest just run the Spatial Join GP tool (available in all license levels) which will work well in most analysis cases like yours.

  • Using this method is giving me a higher count, than when selecting the neighborhood then using the select by location, using with the center in parameter. I'm guessing using the spatial join it is including other lots that it is touching. – elagarde Feb 5 '15 at 17:26
  • Could be provided that your schools are located on the edges of the polygons. Check carefully what kind of match_option you use in Spatial Join tool. – Alex Tereshenkov Feb 5 '15 at 17:43

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