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

I am trying to make a script that will go through buildings in a shapefile row-by-row and do a count of points within .25, .5,...1 mile of it. The other points are restaurants.

import arcpy  
arcpy.env.workspace = "C:\Users\zafard\Desktop\Building.gpd"  

#need to do 4 iterations for the distance radii
buffDist = [0.25,0.5,0.75,1]                              
for Dist in buffDist:
    print ("FOR A %s MILE RADIUS: " % (Dist))
    inTable = "C:\Users\zafard\Desktop\Building\XYBuild.shp"

    #set up cursor in table
    rows = arcpy.SearchCursor(inTable)
    row =

    #set up loop to analyze every row of the table
    While row:
        print row.OBJECTID  

        #use select by distance tool    
        arcpy.SelectLayerByLocation_management("FRest","WITHIN_A_DISTANCE", row, Dist, "NEW_SELECTION")

        #count the hopefully selected points for the layer.
        count = int(arcpy.GetCount_management("FRest"))

        print count
        row =

I am now getting an error with running the tool in the while loop. "Object: Error in executing tool" Please help! This is mindwrakking!

share|improve this question

If I understand correctly your two feature layers are FRest and XYBuild, right? At first glance, I think the problem might be the paths to your feature layers. Is Building a geodatabase? If so the extension seems wrong (shouldn't be .gdb?). And is XYbuild is in the Building geodatabase? If that is the case then is not a shapefile and your should assign only "XYbuild" to your inTable variable.

share|improve this answer
Not certain but I think the shapefiles are being stored in a folder called C:\Users\zafard\Desktop\Building.gpd rather than the latter being a misnamed file geodatabase – PolyGeo Aug 5 '12 at 0:42

Now that we have the syntax issues sorted out, there might be a better approach performance-wise. You can use PointDistance to get an output table of all of the restaurant points within the search radius of each building. Next, use SummaryStatistics to get the count for each building. The statistics_field parameter would be [['near_fid', 'count']] and the case field would be input_fid.

share|improve this answer
I got rid of the period (THANKS!) and still have an error on the print count. – Dan Aug 3 '12 at 17:50
count = int(arcpy.GetCount_management("FRest")) – twospeeder Aug 3 '12 at 17:52
great find! yeah it runs now, but only does one iteration of the while loop before saying "Runtime error <type 'exceptions.RuntimeError'>: Object: Error in executing tool" – Dan Aug 3 '12 at 17:54
sorry, it just does the OID and cannot run the the tools at all. My first day at arcpy scripting. Sorry.................. – Dan Aug 3 '12 at 18:01
I think my main problem is that when I say the row it might not use the coordinates in that row?? – Dan Aug 3 '12 at 19:15

I think this line:

arcpy.env.workspace = "C:\Users\zafard\Desktop\Building.gpd"

needs to be changed to one of the following:

arcpy.env.workspace = r"C:\Users\zafard\Desktop\Building.gpd"   
arcpy.env.workspace = "C:\\Users\\zafard\\Desktop\\Building.gpd"   
arcpy.env.workspace = "C:/Users/zafard/Desktop/Building.gpd"

Similarly for:

inTable = "C:\Users\zafard\Desktop\Building\XYBuild.shp"

Also to use the geometry stored for each row this line:

arcpy.SelectLayerByLocation_management("FRest","WITHIN_A_DISTANCE", row, Dist, "NEW_SELECTION")

should be:

arcpy.SelectLayerByLocation_management("FRest","WITHIN_A_DISTANCE", row.Shape, Dist, "NEW_SELECTION")
share|improve this answer

Your Answer


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.