Separate raster bands in python toolbox

2491
5
Jump to solution
03-03-2016 03:00 AM
BenLeslie1
Occasional Contributor III

I have a python script that, as part of the code, separates raster bands and then uses Con to identify black pixels; like this:

MyRaster = r"MyRaster.tif"
redband = MyRaster + "\Band_1"
greenband = MyRaster + "\Band_2"
blueband = MyRaster + "\Band_3"
BlackPixels = Con (((Raster(redband)==0 & (Raster(greenband)==0..................

However, I'm now trying to make this script into a python toolbox but the bands are not being recognised; like this:

def getParameterInfo(self):
     inRaster = arcpy.Parameter(
          displayName="input raster",
          name="inraster",
          datatype=["GPRasterLayer","DERasterDataset"],
          parameterType="Required",
          direction="Input")
.......

     return [inRaster]

     def execute (self,parameters,messages):
          MyRaster = parameters[0].valueAsText
          redband = MyRaster + "/Band_1"
          greenband = MyRaster + "/Band_2"
          blueband = MyRaster + "/Band_3"
          BlackPixels = Con (((Raster(redband)==0 & (Raster(greenband)==0..................

I get following message

RuntimeError: ERROR 000732: Input Raster: Dataset MyRaster.tif/Band_1 does not exist.

What am I doing wrong?

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

print the path to see what is returned

View solution in original post

0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

print the path to see what is returned

0 Kudos
BenLeslie1
Occasional Contributor III

messages.addMessage("MyRaster = " + MyRaster)

--> MyRaster = MyRaster.tif

this is what I expect.  maybe my expectations are wrong.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Since it is a multiband raster, you will have to split it by band or access it by band, if you need to perform the con statement in your execute section, confirm that you can do it manually first, then use the results window.

0 Kudos
BenLeslie1
Occasional Contributor III

got ya.

path = arcpy.Describe(MyRaster).path

redband = path + "\\" + MyRaster + "\\Band_1"

0 Kudos
Luke_Pinner
MVP Regular Contributor

Couple of ways to do this easily in the answers to the following GIS Stack Exchange question:

python - Access individual bands and use them in map algebra - Geographic Information Systems Stack ...

I'll throw this in for future reference for other users as the band names are not always guaranteed to be "Band_[1-N]"