0

I would like to to iterate through polygon features in a shapefile and spatially join them with points. I am able to do this with model builder, as well as when the polygons are in separate shapefiles (using ListFeatureClasses) but I would like to know how to do this with many polygons in a single shapefile using Search Cursor.

import os
import sys
import arcpy
from arcpy import env

points = r'C:\Temp2\points.shp' 
polygons = r'C:\Temp2\polygons.shp' 

output = r'C:\Temp2\output'

bCursor = arcpy.da.SearchCursor(polygons, '*') 

for row in bCursor:
   print row                  
   arcpy.SpatialJoin_analysis(row, points,output+'%i%.shp',"JOIN_ONE_TO_ONE","KEEP_ALL","", 'INTERSECT', '', '')


print 'Done'

Error (when run in WinPython Spyder):

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\WinPython-32bit-2.7.10.3\python-2.7.10\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 790, in runfile
    execfile(filename, namespace)
  File "C:\WinPython-32bit-2.7.10.3\python-2.7.10\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 77, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/GISDATA/python/batchSpatialJoin.py", line 26, in <module>
    arcpy.SpatialJoin_analysis(row, points,output+'%i%.shp',"JOIN_ONE_TO_ONE","KEEP_ALL","", 'INTERSECT', '', '')
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\analysis.py", line 481, in SpatialJoin
    raise e
RuntimeError: Object: Error in executing tool

Error (when run in ArcMap Python Window):

Runtime error 
Traceback (most recent call last):
  File "<string>", line 15, in <module>
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\analysis.py", line 481, in SpatialJoin
    raise e
RuntimeError: Object: Error in executing tool

I have tried this on different computers, ArcMap 10.1 and 10.4, with the same errors

  • You seem to be getting that error from the first line i.e. import os and it looks like there is an unexpected path for Python. Are you running something other than the Python installed by ArcGIS for Desktop? – PolyGeo Aug 4 '16 at 8:28
  • I am using WinPython spyder, but it is pointing to the C:\python27 that Arcmap is using. Other scripts work and I have tried in the python window with the same result – woot Aug 4 '16 at 9:12
  • I suspect that you could cut this code snippet down to just the first line only and still see this error. I think it is related to your Python install rather than ArcPy. – PolyGeo Aug 4 '16 at 9:27
  • The spatial join works if I get rid of the search cursor. And the search cursor prints all the rows if I # out the line arcpy.SpatialJoin_analysis – woot Aug 4 '16 at 9:36
  • I'm still confused by why your script name is not showing up with a line number at the beginning of the error message. – PolyGeo Aug 4 '16 at 10:10
5

Python is case sensitive, you have a lower case "p" in both the points and polygon variable names, but reference those two variables in your code with a capital "P". The code fails b/c it is trying to reference variables that do not exist within the cursor and join statements.

With you update an additional error msg I see couple more issues:

  1. The first two parameters of the spatial join method needs to be a feature layer, you are providing a row object and a shp.
  2. If you want to run a spatial join on each feature one by one you should convert the shp to a feature layer, then use select layer by attribute method to select the current row/feature, and finally run the spatial join on the selected feature layer with the selection.
  • Ah, very true. I did a bad thing by skipping straight to the meat of the problem rather than worrying about the syntax (and in the end it probably will be about the syntax). Meaning, in the original post I replaced the real filenames with simplified ones. Now they are real filenames and the problem still stands – woot Aug 4 '16 at 7:10
0

OK: Spatial Join (or Intersect or Tabulate Intersection etc...) does not take a search cursor row as an input. It needs a feature layer. The code below works by first making the polygons a feature layer, looping through them, selecting by FID, and using the layer in spatial join. There is probably a more elegant way of doing this.

import os
import sys
import arcpy
from arcpy import env

points = r'C:\Temp2\points.shp' 
polygons = r'C:\Temp2\polygons.shp' 

output = r'C:\Temp2\output'

arcpy.MakeFeatureLayer_management(polygons, "Poly_lyr1")

bCursor = arcpy.da.SearchCursor(polygons, '*') 
count = 0

for row in bCursor:
   print row                  
   arcpy.SelectLayerByAttribute_management("Poly_lyr1", "NEW_SELECTION",  """ "FID" = """+str(count))
   arcpy.SpatialJoin_analysis("Poly_lyr1", points,output+str(count)+'.shp',"JOIN_ONE_TO_ONE","KEEP_ALL","", 'INTERSECT', '', '')
   count = count + 1

print 'Done'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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