Select to view content in your preferred language

Using Multivalue to add multiple raster layers

2732
4
Jump to solution
01-25-2016 09:31 AM
ShawnKasprick
New Contributor

I am trying to bring in multiple raster layers by using the multivalue parameter properties, however the script fails and states the dataset does not exist or is not supported.  I have attached the error message as well as the script I am using.  The raster layers are normally brought in directly from the TOC.

Error

Messages

Executing: CalcCompRasters Image1;Image2;Image3;Image4 S:\AVWorks\Scratch.gdb\avgtest

Start Time: Mon Jan 25 11:13:38 2016

Running script CalcCompRasters...

Failed script CalcCompRasters...

Traceback (most recent call last):

  File "S:\Data\My Toolboxes\MyScripts\Calculate Composite Rasters.py", line 24, in <module>

    OutAvgTemp = CellStatistics(InRasters, "MEAN", "DATA")

  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\sa\Functions.py", line 2967, in CellStatistics

    ignore_nodata)

  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\sa\Utils.py", line 53, in swapper

    result = wrapper(*args, **kwargs)

  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\sa\Functions.py", line 2963, in Wrapper

    [function] + Utils.flattenLists(in_rasters_or_constants))

RuntimeError: ERROR 000732: Input Raster: Dataset Image1;Image2;Image3;Image4 does not exist or is not supported

Failed to execute (CalcCompRasters).

Failed at Mon Jan 25 11:13:40 2016 (Elapsed Time: 1.97 seconds)

Below is the Script I am using.  This will be the start of a larger script, but until I get past this I won't add more to it.

# Import arcpy module

import arcpy

from arcpy.sa import *

# Script arguments

InRasters = arcpy.GetParameterAsText(0)

OutAvg = arcpy.GetParameterAsText(1)

# Process: Cell Statistics - Avg

OutAvgTemp = CellStatistics(InRasters, "MEAN", "DATA")

OutAvgTemp.save(OutAvg)

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Your multivalue gets read in as a semicolon-delineated string (e.g. 'ras1;ras2;ras3;ras4'). You need to split that into a regular list, which the split() method will do for you (e.g. 'ras1;ras2;ras3;ras4'.split(';') would return ['ras1','ras2','ras3','ras4']).

# Import arcpy module
import arcpy
from arcpy.sa import *

# Script arguments
InRasters = arcpy.GetParameterAsText(0).split(';') # use split method here
OutAvg = arcpy.GetParameterAsText(1)

# Process: Cell Statistics - Avg
OutAvgTemp = CellStatistics(InRasters, "MEAN", "DATA")
OutAvgTemp.save(OutAvg)

View solution in original post

4 Replies
JamesCrandall
MVP Frequent Contributor

First, I don't see where your workspace is getting set.

arcpy.env.workspace = <workspace>

Next would be to make sure the input rasters are actually being treated as a list of rasters.  Which this doesn't look correct.

InRasters = arcpy.GetParameterAsText(0)

Maybe should be more like:

InRasters = arcpy.ListRasters(arcpy.GetParameterAsText(0))

Although I don't know if that input will be treated as a list of values.  You may have to be more verbose in setting this up and iterate through to populate an actual list of rasters:

InRasters = arcpy.ListRasters(["raster1","raster2"])

DanPatterson_Retired
MVP Emeritus

The error... although is hidden... can be found here Setting paths to data in Python—Help | ArcGIS for Desktop  and the most common problem is how people pass on file paths and filenames in scripts... for example

  • "c:\somepath\that is\here"   is wrong on so many levels...
    • there is no r in front of the path name to utilize python's "raw" syntax
    • it contains a \t which is treated as a python tab
    • it contains a space... not a specific problem in this situation, but it will come back to bite you at some time.
  • r"c:\somepath\that_is\here"  is acceptable, "r" in front "_" for spaces
  • "c:\\somepath\\that_is\\here""  use python double slashes to override the escape useage in python
  • "c:/somepath/that_is/here"    use foreward slashes

and if you file path doesn't exist or it is wrong, you will get that error message... if the file isn't supported you will get that error message... and any combination of the above.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Your multivalue gets read in as a semicolon-delineated string (e.g. 'ras1;ras2;ras3;ras4'). You need to split that into a regular list, which the split() method will do for you (e.g. 'ras1;ras2;ras3;ras4'.split(';') would return ['ras1','ras2','ras3','ras4']).

# Import arcpy module
import arcpy
from arcpy.sa import *

# Script arguments
InRasters = arcpy.GetParameterAsText(0).split(';') # use split method here
OutAvg = arcpy.GetParameterAsText(1)

# Process: Cell Statistics - Avg
OutAvgTemp = CellStatistics(InRasters, "MEAN", "DATA")
OutAvgTemp.save(OutAvg)
ShawnKasprick
New Contributor

I added the split method and the raster calculations run as expected!  Thank You!

0 Kudos