Hello,
I am trying to write a script that calls a batch tool but cannot get it to work. Any feedback is much appreciated. Code snippet is below (I marked out the file path for privacy purposes):
Thanks,
Nicole
Hi Nicole,
Could you post the code of BatchSelectLayerByLocation which appears to reside in Default.tbx? It's citing two lines in there that I'm guessing are not reading parameters correctly.
Okay, does this help (please disregard shortened file paths)?
# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2023-01-05 15:35:27
"""
import arcpy
from sys import argv
def # NOT IMPLEMENTED# Function Body not implemented
def BatchSelectLayerByLocation(Input_Features, Relationship="INTERSECT", Batch_Selecting_Features, Search_Distance, Selection_type="NEW_SELECTION", Invert_spatial_relationship=False): # Batch Select Layer By Location
# To allow overwriting outputs change overwriteOutput option to True.
arcpy.env.overwriteOutput = False
for Selecting_Features in # NOT IMPLEMENTED(Batch_Selecting_Features):
# Process: Parse Path (Parse Path) (mb)
Path, Name, Extension, Workspace_Name = arcpy.mb.ParsePathExt(in_data_element=Selecting_Features, format="FORMAT")
# Process: Select Layer By Location (Select Layer By Location) (management)
if Name:
Layer_With_Selection, Output_Layer_Names, Count = arcpy.management.SelectLayerByLocation(in_layer=Input_Features, overlap_type=Relationship, select_features=Selecting_Features, search_distance=Search_Distance, selection_type=Selection_type, invert_spatial_relationship=Invert_spatial_relationship)
# Process: Collect_Values (Collect Values)
# Collect Values Utility is not implemented
return Layer_With_Selection, Output_Layer_Names, Count, Output_Values
if __name__ == '__main__':
# Global Environment settings
with arcpy.EnvManager(scratchWorkspace=r"P:\...\Default.gdb", workspace=r"P:\...\Default.gdb"):
BatchSelectLayerByLocation(*argv[1:])
Oh, yikes I didn't realize this was something from modelbuilder. I am an infant in MB. I would probably go with writing a python toolbox (*.pyt) which gives you more control over the workflow and parameters. What exactly is this supposed to do? I could write up a quick example for you; it would be easier to read and maintain.
Eric, thanks. It's not a model that I built in model builder--I just right-clicked the select by location geoprocessing tool to create/save a batch tool to my toolbox. ESRI automatically converts batch tools created in that way to model tools. I right-clicked to edit the batch tool and exported the code from the model builder interface since there is no option to pull the code from the tool's properties menu, like with custom script tools. The tool works when I run it by itself. It allows you to select by location on one feature class, based on it's spatial relationship to many feature classes. e.g. I can select all parcels that intersect features within multiple other layers.
If there's no easy way to call a batch tool in Python, I may exclude that tool from my script and try to write some code to do the same thing. I just thought that calling batch tools looked quick/simple, so I wanted to figure out what I was doing wrong.
When posting code please format it, click on the 3 dots icon to do that then the </> icon. YOU can always go back an edit your question.
If that code is directly from the Model, I think the model is broke. Your layers you want to intersect are assigned to the Batch_Selecting_Features argument, but in the method, the code is not using that variable and is says:
for Selecting_Features in # NOT IMPLEMENTED(Batch_Selecting_Features):
It probably should be:
for Selecting_Features in Batch_Selecting_Features:
Output_Values is not assigned in the return.
return Layer_With_Selection, Output_Layer_Names, Count, Output_Values
So my guess is the model needs to be looked at and fixed. I know export to python sometimes creates bad code, but it shouldn't look like what you pasted unless there are issues.