2

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

8
  • On the tool itself you can specify a list of strings, see resources.arcgis.com/en/help/main/10.1/index.html#//… and look at Filter, Filter Type = Value List. Commented Apr 21, 2015 at 0:45
  • @Thanks Michael. I have been viewing that, but what I'm unclear of is how the script is within arcpy.MosaicToNewRaster_management. For example: pixel_type = "32_BIT_UNSIGNED". Do I leave it as that or change it to read something like: pixel_type = "" Commented Apr 21, 2015 at 0:54
  • the mosaic to new raster tool creates a raster first and then mosaics it. If you are unsure copy one (it doesn't matter which) to the output name and use Mosaic instead - the mosaic tool inserts rasters into an existing raster, that way the band count, cell size, pixel type, spatial reference are all set. Have you used Mosaic Dataset yet? I haven't done a mosaic in Esri since I discovered them. IMO a much better way of mosaicing rasters. Commented Apr 21, 2015 at 1:14
  • @Michael I updated what I'm actually working with. I think I'm closer now. I was trying to create a script that gave the same parameters the MosaicToNewRaster gives in ArcMap, but I wanted the script to go through every root and sub directory. Since I want to make it useable by others, and not just myself, I'm trying to create those parameters the tool itself provides. Commented Apr 21, 2015 at 1:27
  • Number of bands is a long (don't need to quote) but an Int would do (2,147,483,647 is a lot of bands). The pixel type is a string exactly as described in the tool resources.arcgis.com/en/help/main/10.1/index.html#//… so a value list in the filter would do that. Commented Apr 21, 2015 at 1:33

1 Answer 1

4

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!

4
  • Wow. I am impressed. I understand now that I'm able to see it. Often I feel like I'm putting a puzzle together without knowing what the outcome should look like. I will definitely build upon what you've taught me tonight. I noticed one thing though, in_workspace should be replaced by BaseFolder. I like how much more simple the script looks too. Thank you! @Michael Commented Apr 21, 2015 at 4:26
  • Well spotted. Damn that copy-and-paste. Commented Apr 21, 2015 at 4:44
  • One last question. Why leave out Cell Size? Is it like pixel_type in that parameters generally not used should be left out? Commented Apr 21, 2015 at 17:35
  • Put it in if you like, it's in the list so why not have it as an option? I skipped it to show you (and others) how to jump over an optional parameter, well, one way to do that, I think it's neater and a lot more obvious what you're trying to achieve for other readers of the script (and the author when it needs to be revised/verified for the next release). Commented Apr 21, 2015 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.