Select to view content in your preferred language

Recognise whether Python Toolbox tool is used in batch processing mode

484
3
Jump to solution
01-22-2023 06:21 AM
Manu
by
Regular Contributor

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?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

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")

 

 

JohannesLindner_1-1674458221425.png

 

 

 

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.


Have a great day!
Johannes

View solution in original post

3 Replies
DanPatterson
MVP Esteemed Contributor

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


... sort of retired...
0 Kudos
JohannesLindner
MVP Frequent Contributor

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")

 

 

JohannesLindner_1-1674458221425.png

 

 

 

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.


Have a great day!
Johannes
Manu
by
Regular Contributor

Thanks, I didn't know about the multiValue option. This is exactly what I needed!

0 Kudos