[GIS] How to extract ALL bands from multiband raster in ArcGIS

arcgis-desktoparcpymulti-bandpython

I have seen many questions about how to extract a singe band from a multiband raster in ArcGIS, but how do you extract ALL of the bands and save them as separate tifs? I want to automate this process using Arcpy because I have many bands in one multiband raster. I also want to be able to do a batch import to separate bands from multiple multiband rasters.

I'm guessing I can use CopyRaster, but I'm not sure how to use it when I need to make copies of everything!

import arcpy
import os
from arcpy.sa import Raster

#setup workspace
arcpy.env.workspace = "Y:\Commons\Kelly_Carly_Aaron_Project\lvl 3\OLCI-Sentinel3\Florida"
arcpy.env.overwriteOutput= True

#setup output directory
out_dir= "Y://Personal//crobbins//Final_Project//Separate_bands_results//"

#list all rasters in workspace
RasterList = arcpy.ListRasters()

#get list of bands from a multiband raster
def list_bands(raster_workspace):

    raster_workspace = arcpy.env.workspace
    RasterList = arcpy.ListRasters()
    bands = [Raster(os.path.join(raster_workspace, b)) for b in RasterList]

    print bands
list_bands("Y:\\Commons\\Kelly_Carly_Aaron_Project\\lvl 3\\OLCI-Sentinel3//Florida\\Copy_of_2017.0527.tif\\")

This doesn't work. It just lists the names of all the multiband tiffs I have in Y:\Commons\Kelly_Carly_Aaron_Project\lvl 3\OLCI-Sentinel3\Florida.

Best Answer

Bands are similar to raster datasets and can be used in CopyRaster as single, one band, rasters by supplying the path to the raster followed by \Band_X. This simple script should get you started:

import os, sys, arcpy

InRaster = sys.argv[1] # like c:\\some\\path\\raster.ext

# get a list of the bands that make up the raster
arcpy.env.workspace = InRaster
bRng = arcpy.ListRasters()

for ThisBnd in bRng:
    # loop through the bands and export each one with CopyRaster
    InBand  = '{}\\{}'.format(InRaster,ThisBnd)
    bndDesc = arcpy.Describe(InBand)
    NoData  = bndDesc.noDataValue 
    InSplit = os.path.splitext(InRaster) # split the image name and extension
    # output file name is c:\\some\\path\\raster_Band_X.ext
    OutRas  = '{}_{}{}'.format(InSplit[0],ThisBnd,InSplit[1])
    arcpy.CopyRaster_management(InBand,OutRas,nodata_value = NoData)

You can get all the raster properties you need, and some other handy info, with the describe statement, like band count and no data value; note the difference in properties between a raster dataset and a band of a raster dataset.

Related Question