1

I have a series of buffer polygons that I need to perform a spatial join on to get overall counts within the buffers. I can do this by copy and pasting the spatial join script over and over and just swapping out the buffer layer/output file name.

However, I'd like to figure out a simple loop that will go through all the buffers at once. I attempted a code myself but I'm not versed enough to figure out the solution to the error I'm getting. Any tips?

Buffer_5mi = "G:\\Admissions\\GBD\\GBD\\Admissions.gdb\\Buffer_5mi" Buffer_10mi = "G:\\Admissions\\GBD\\GBD\\Admissions.gdb\\Buffer_10mi" Targets = [Buffer_5mi, Buffer_10mi] Freshmen_Admitted = "Freshmen_Admitted" for Target in Targets: output = "G:\\Admissions\\GBD\\GBD\\Admissions.gdb\\admitted%s" % Target arcpy.SpatialJoin_analysis(Target, Freshmen_Admitted, output, "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT", "", "") 

This is the error I'm getting. I understand why the error is popping up, what I dont know is the solution to it.

Runtime error Traceback (most recent call last): File "<string>", line 9, in <module> File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\analysis.py", line 471, in SpatialJoin raise e ExecuteError: ERROR 000210: Cannot create output G:\Admissions\GBD\GBD\Admissions.gdb\admittedG:\Admissions\GBD\GBD\Admissions.gdb\Buffer_5mi Failed to execute (SpatialJoin). 
2
  • Your output is a double path: G:\Admissions\GBD\GBD\Admissions.gdb\admittedG:\Admissions\GBD\GBD\Admissions.gdb\Buffer_5mi Commented Dec 2, 2015 at 16:02
  • Hélène, Yes I saw that it was a double path. What I'm hoping to get some help on is how to get the output path to save as G:\\Admissions\\GBD\\GBD\\Admissions.gdb\\admitted_ + Variable Name Commented Dec 2, 2015 at 16:10

1 Answer 1

2

You're currently passing a variable containing a full path (Target) when you only want the file name itself. Pay attention to the "output =" line:

import os, arcpy Buffer_5mi = "G:\\Admissions\\GBD\\GBD\\Admissions.gdb\\Buffer_5mi" Buffer_10mi = "G:\\Admissions\\GBD\\GBD\\Admissions.gdb\\Buffer_10mi" Targets = [Buffer_5mi, Buffer_10mi] Freshmen_Admitted = "Freshmen_Admitted" for Target in Targets: output = os.path.join("G:\\Admissions\\GBD\\GBD\\Admissions.gdb", 'admitted_{}'.format(os.path.basename(Target))) arcpy.SpatialJoin_analysis(Target, Freshmen_Admitted, output, "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT", "", "") 
4
  • This is fantastic! Thank you Tom. I'm making note of this for future use. One follow up question as I try to run this, I'm getting a syntax error on line 10. I'm not seeing the error in syntax on that line. 'Parsing error SyntaxError: invalid syntax (line 10)' Commented Dec 2, 2015 at 16:30
  • Glad I could help, @HowardSmith. The syntax error is actually because I had neglected to close the parentheses on line 9. There should be three right-parentheses at the end of the line. I fixed it in my answer. Commented Dec 2, 2015 at 16:39
  • Really appreciate the help! I see where that extra ) in line nine fixed the syntax. Trying to learn arcpy as best I can with a combo of python basics and applying it on current projects. Stackexchange is really been helping pointing out errors that a beginner like me run in to. Commented Dec 2, 2015 at 16:51
  • 1
    @HowardSmith, I wish you luck in your journey to learn coding. It's incredibly helpful when working with GIS, and it's about the only way to get a job in GIS anymore. If my answer fixed your issue, feel free to mark it is the correct answer. Commented Dec 2, 2015 at 16:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.