I have used the attached toolbox/script in the past to automate Select by Location. In this case, I have points that I want to assign to polygons. The script allows you to enter the polygon layer and an ID field as well as the item to be selected (points layer). It places the polygons into memory and iterates through each polygon, selecting the points that intersect the polygon and writing the ID field to a table of those records and exporting the output shapefile of the points with the polygon ID assignment. After it iterates through all of the polygons, it merges the output shapefiles into a single shapefile called Final_Merge.shp.
(Note, some people may wonder why I'm not using a spatial join for this operation. The reason is that the "point in polygon" method skips any points that are on the boundary line. Yes, you could use the "closest" option of the spatial join, but I find that to take a long time to calculate the distances when you have large files. In addition, I like the flexibility and control of the select by location options to control just what gets selected and assigned.)
I've used the script for other layer combinations, not just points and polygons. I edit the script before running the toolbox to change which Select by Location method works best for the situation --e.g. INTERSECT versus WITHIN_A_DISTANCE, by using the # to comment out the intersect line and "uncommenting" the line that has the selection option that I want.
The script used to work great. With very large processes, it sometimes ran out of memory after about 500 shapefiles and I would simply re-run the script with the records that were remaining.
I ran this once this morning as a test--and it worked perfectly. However, when I tried to run it again, it always gives me a "ERROR 000210: Cannot create output". I tried rebooting, creating a new MXD, using a different ID, using different output folders, and nothing has worked.
The main reason for my using the script now is that I wanted to try to change the input and output types to file geodatabase feature classes instead of shapefiles, but all of my attempts to make that change have failed and until I can get the original script to run consistently with shapefiles, I don't want to try to make further changes.
I am not a Python programmer and the original code was written by someone else. Can anyone help get this working again? I think this tool could help a LOT of people. I have attached a zip file with the toolbox file and the Python script.
Message was edited by: Susan Zwillinger I uploaded the revised toolbox and script that is working consistently for me when using shapefiles.
Message was edited by: Susan Zwillinger Removed the option that deleted the output folder when the script failed in case someone chose an output folder that contained other data that should not be deleted.
Message was edited by: Susan Zwillinger I got it working again. This time it is more consistent. I removed some of the output messages in the script. It will accept either a shapefile or a feature class as an input, but it still only outputs shapefiles. One note about the different inputs--if you use a file geodatabase as an input, depending on whether or not you used the default XY tolerance value when you created the file geodatabase or whether you specified this in the geoprocessing-->environment settings, you can get very different "select by location" results compared to the same data in a shapefile format.