I have a Python Toolbox tool that extracts values from a raster to a .csv table.
Now I want to enable the user to apply the tool to multiple rasters in a batch process and to combine the results in a single .csv table.
Is there a way to find out whether the tool is run in batch mode from within that tool?
(I thought about checking whether the tool is in batch mode and, if true, to append the existing output table if it was initially created during an earlier run of the tool during that batch process.)
Is this even possible and, if it is, how?
Solved! Go to Solution.
Hmm, the way I see it, there are at least 2 options.
Option 1: Don't use batching, use multivalue parameters.
class SomeTool:
label = "SomeTool"
def getParameterInfo(self):
return [
arcpy.Parameter(name="raster", displayName="Raster", datatype="DERasterDataset", parameterType="Required", direction="Input", multiValue=True),
arcpy.Parameter(name="csv", displayName="Output CSV", datatype="DEFile", parameterType="Required", direction="Output"),
]
def execute(self, parameters, messages):
par = {p.name: p for p in parameters}
rasters = str(par["raster"].value).split(";")
with open(str(par["csv"].value), "w") as csv_file:
csv_file.write("Header line\n")
for raster in rasters:
# extract and write for each raster
csv_file.write(str(raster) + "\n")
Option 2, if you really want to batch: Check if the output csv exists. If not, create it and write the header line. Then use append mode to write the extracted raster values.
Python tool box batch process not working. - Esri Community
has some discussion and the limitations are mentioned in the help topic
Batch geoprocessing—ArcGIS Pro | Documentation
Hmm, the way I see it, there are at least 2 options.
Option 1: Don't use batching, use multivalue parameters.
class SomeTool:
label = "SomeTool"
def getParameterInfo(self):
return [
arcpy.Parameter(name="raster", displayName="Raster", datatype="DERasterDataset", parameterType="Required", direction="Input", multiValue=True),
arcpy.Parameter(name="csv", displayName="Output CSV", datatype="DEFile", parameterType="Required", direction="Output"),
]
def execute(self, parameters, messages):
par = {p.name: p for p in parameters}
rasters = str(par["raster"].value).split(";")
with open(str(par["csv"].value), "w") as csv_file:
csv_file.write("Header line\n")
for raster in rasters:
# extract and write for each raster
csv_file.write(str(raster) + "\n")
Option 2, if you really want to batch: Check if the output csv exists. If not, create it and write the header line. Then use append mode to write the extracted raster values.
Thanks, I didn't know about the multiValue option. This is exactly what I needed!