Sunday, 9 October 2016

Setting parameters in ArcGIS desktop script


How would I create the parameters pixel_type and number_of bands (as found in MosaicToNewRaster) as user inputs at the start of my own script?


ERROR 000714: Error in script BatchMosaic. Error in executing: cmd.exe /C C:\Erosion\Scripts\BATCHM~1.PY "C:\finalproject\Mosaic" "C:\finalproject\Mosiac_out" "mosaicme" "32_BIT_SIGNED" "1" Failed to execute (BatchMosaic).


import arcpy, os, sys

#User parameters.
in_workspace = sys.argv[1]
out_workspace = sys.argv[2]
output_name = sys.argv[3]


def getParameterInfo(self):
param4 = arcpy.Parameter(
pixel_type="Input value")

param4.filter.type = "ValueList"
param4.filter.list = [""]


def getParameterInfo(self):
param5 = arcpy.Parameter(

number_of_bands="Input value")

param5.filter.type = "ValueList"
param5.filter.list = []

#Attempts to mosaic all raster files in root and sub directories.
rasters = []
for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, topdown = True, datatype="RasterDataset", type = "TIF"):
for filename in filenames:
rasters.append(os.path.join(dirpath, filename))


#Pixel Type and Number of Bands must be manualy inserted to script.
arcpy.MosaicToNewRaster_management(';'.join(rasters), out_workspace, output_name, pixel_type = "32_BIT_UNSIGNED", number_of_bands = 1)

enter image description here



Answer



Your code is quite nice, thanks for the arcpy.da.walk example, there is a bit of confusion about feeding a tool the correct values and sys.argv, arcpy.GetParameter and arcpy.GetParameterAsText... I always use sys.argv, mostly because it's less typing, it also takes the confusion out of supplying the correct type to the tool.


import os, sys, arcpy

BaseFolder = sys.argv[1]

OutFolder = sys.argv[2]
OutName = sys.argv[3]

SpatRel = sys.argv[4]
PixelType = sys.argv[5]
NumBands = sys.argv[6]

rasters = []

for dirpath, dirnames, filenames in arcpy.da.Walk(BaseFolder , topdown = True, datatype="RasterDataset", type = "TIF"):

# thanks for that, I used to use os.walk() but this is much handier
for filename in filenames:
rasters.append(os.path.join(dirpath, filename))

arcpy.MosaicToNewRaster_management(';'.join(rasters),OutFolder,OutName,SpatRel,PixelType,number_of_bands = NumBands) # note, skipping cell size

This is your script with a few brief modifications. Note that I've skipped the CellSize parameter on the tool by using the parameter by name number_of_bands = NumBands, this can be done for any optional parameter.


To add this to a toolbox you need to specify one script parameter per sys.argv[] (or GetParameterAsText):


enter image description here


The data type in the second column has nothing to do with what is passed to the tool, it is only used by the tool to help you find the correct items.. for example if you specify 'feature class' the tool will only let you browse for a feature class, but the script is not passed a feature class - it is passed a string with the path to the feature class.



If you want to control the pixel types to a list of values you do that in the tool dialog: enter image description here


And this cuts down the options you are given to pick from... which is also handy on a monday morning - when you're struggling to remember was that Int32, Int_32, 32bit or 32_bit_int?? by giving only a few options (all valid) there's no chance of giving the wrong value to the tool... be sensible though, most rasters are 8 bit unsigned, 32 bit float or some integer value. If you don't deal with 1 bit images or 64 bit Int for example then don't put it in the list!


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...