2

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.

  • 1
    Welcome to GIS SE. As a new user, please take the Tour, which explains how our focused Q & A operates. Unfortunately, scripting questions are expected to contain code. You could start by doing one operation from the UI, then copying the ArcPy code from geoprocessing Results, then adding an import arcpy above and placing it in a looping construct. – Vince Nov 6 '17 at 1:12
  • You're mixing your forward (/) back (\) and double (\\) slashes.. arcpy.env.workspace = r'Y:\comm... to sort that one out but the rest are all mixed up! Backslashes need to be doubled in non-raw strings but forward slashes do not need to be doubled. – Michael Stimson Nov 6 '17 at 1:48
1

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.

  • 2
    Band_[N] won't always work. E.g. for imagery, ArcGIS uses the band description so the path becomes something like path/file/Coastal_Aerosol (i.e Landsat 8 data) instead of path/file/Band_1. Easier to use band index directly via the Make Raster Layer tool. – user2856 Nov 6 '17 at 2:41
  • Good point @Luke, much better to set the workspace and use ListRasters to obtain the bands.. I'll fix that up now. I've not used that tool, I'll have a look at it. – Michael Stimson Nov 6 '17 at 3:16
  • 1
    See also gis.stackexchange.com/q/150067/2856 – user2856 Nov 6 '17 at 3:18
  • I can't get this to work using Python 2. The {}.format are equal to %s in Python 2, correct? – CodingGrandma Nov 14 '17 at 21:25
  • I prefer '{}'.format(var) as you don't need to know what the variable is, %s only works for strings. Are you getting error messages? What is the format of the InRaster? This will not work for GRID raster as there is no associated extension and the output GRID would potentially have too many characters. – Michael Stimson Nov 14 '17 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.