Generally, I am trying to find the # of features in one shapefile within 1000 feet of EACH feature in a second shapefile - and then populate a field in the first shapefile with that #.

More specifically, I trying to get a count of features in one shapefile (call it Practice_Stops.shp) that lie within a certain proximity (1000 feet) of EACH feature in a second shapefile (called Practice_Stops1.shp). I also want to restrict the count of features to only include those that also occurred within a certain time period, determined by the date attached to the feature in Practice_Sales1.shp.

I think this script is pretty close, but I have messed up some syntax and I don't know where:

Import arcpy module

Practice_Sales1 = "Practice_Sales1"
Practice_Stops = "Practice_Stops"
I_Practice_Sales1_FID = "I_Practice_Sales1_FID"

cursor = arcpy.SearchCursor("Y:\\mfriedma\\Practice_Sales1.shp", "")

#Iterate through the rows in the cursor for row in cursor:

#Select all features in Practice_Stops within 1000 feet of the current row in Practice_Sales1

arcpy.SelectLayerByLocation_management(Practice_Stops, "WITHIN_A_DISTANCE", row.POINT_X, row.POINT_Y, "1000 Feet", "NEW_SELECTION", "NOT_INVERT")

#Within selected features, further select only those features that occurred within >365 days of the current row in Practice_Sales1

arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", ' "stopdateindex" >=< row.getValue(saledateinndex) AND "stopdateindex" > (row.getValue(saledateinndex)- >365) ')

#Returns the # of selected features
    count = int(arcpy.GetCount_management(Practice_Stops).getOutput(0))

#Replace row value in COUNT field of Practice_Sales1 with 
    row.setValue("COUNT", count)

    row = rows.next()

Using model builder I have come up with the following:

enter image description here

The idea is that I go row by row in the Practice_Sales1.shp file, where the Iterate Row Selection tool conditions on date and location so as to select every row in the table. For each row, I use the Select Layer By Location tool to compare the Sale location to features in the Practice_Stops.shp file and select those within 1000 feet. This selection is then input for the Select Layer By Attribute tool, which refines the selection to only include features from the Practice_Stops file that occurred within a certain time proximity from "Value (2)" - the date field in each row of the Practice_Sales(1).shp file. Finally, I want to count all the features and populate a field in the row currently being iterated from the Practice.Sales.shp file. The problem I have is that the Get Count tool doesn't seem to give me output that I can populate a table field with - how can this be done.

  • Do you have an advanced license? The tool Near will give you the distance (in spatial reference units) and the ID of the nearest feature, then attribute join if you want to copy attributes from the nearest feature. This tool appends the values of NEAR_FID, NEAR_DIST to the existing points, if you don't want them to remain in the table remove field when you're done. Jun 4, 2015 at 21:03
  • Michael, thanks for replying. I am not looking for the nearest feature (stop) to each sale, just a count of all features within 1000ft and are dated within one year of the sale. Perhaps I am reading your comment wrong, if so, please clarify how a near table will satisfy these needs. Jun 4, 2015 at 21:09
  • I think this is what you're looking for gis.stackexchange.com/questions/147875 There are other approaches to solving the problem though, including using Generate Near Table (not Near) or a Spatial Join. Because of the time window though, I don't see a way around iterative selection by attributes at some point.
    – Chris W
    Jun 4, 2015 at 21:36
  • Generate Near table can do this, coupled with Summary Statistics.. you will need to use 'all' and remove entries with unsuitable distances then summarize with the case field of the stops FID and statistics field of the other FID type Count. Then join the summarized table to the stops by FID to copy the count value. but you must have an advanced license otherwise your iterative select layer by location is the best non-advanced approach, sometimes it helps to remake the layer for each selection, especially if you're working with a selection of a selection. Jun 4, 2015 at 21:45
  • Michael, thanks for clarifying. I see what you mean now, however I still have the issue of only wanting to include features in the stops file dated in close proximity to sale feature. I think that doing it the way you are suggesting I would still need to first create a layer from the stops file where I select on attributes (date), then run the Generate Near Table against that layer - is that correct? I would be very interested in working around the iteration process if it is possible, I just don't see how at the moment. I have all of the spatial analyst tools. Jun 4, 2015 at 22:19

1 Answer 1


Use the Spatial Join tool with the One to Many option and a Search Radius value of 1000 (assuming your geometry units are already in feet). Your Practice Sales1 will be the Target features and the Practice Stops will be the Join Features. This will duplicate the Practice Sales1 features to match the number of Practice Stops points within 1000 feet of each sale and combine the attributes of the original two features into a single feature of the Spatial Join output.

You should then be able to write one SQL expression to select just the Practice Stops dates that are within a range of the Practice Sales1 date field. For a file geodatabase it should be something like:

StopDate - SalesDate <= -365 and StopDate - SalesDate >= 365

This particular SQL expression gets Stop Dates that are in a range that is between approximately 1 year before to approximately 1 year after the Sales Date.

Then you can use the Summary Statistics tool to get a count for each TargetID value as a Case Field and a Summary of Count on the TargetID field. Join the Summary output to your original Practice Sales1 features on the ObjectID and TargetID fields. Then calculate the Frequency or Count_TargetID value of the Summary into the Practice Sales1 Count field you added. No iteration code or model is required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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