3

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.

Michael Stimson
  • 25,566
  • 2
  • 35
  • 74
CodingGrandma
  • 129
  • 1
  • 7
  • 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 06 '17 at 01: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 06 '17 at 01:48

1 Answers1

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.

Michael Stimson
  • 25,566
  • 2
  • 35
  • 74
  • 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 06 '17 at 02: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 06 '17 at 03:16
  • 1
    See also https://gis.stackexchange.com/q/150067/2856 – user2856 Nov 06 '17 at 03: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
  • The InRaster is a tiff called sentinel-3a.2017147.0527.1522C.L3.NF3.v8103_1_Band_1.tif. I got this error: ERROR 000725: Output Raster Dataset: Dataset Y:\Commons\Kelly_Carly_Aaron_Project\lvl 3\OLCI-Sentinel3\Florida\Copy_of_2017.0527.tif\sentinel-3a.2017147.0527.1522C.L3.NF3.v8103_1_Band_1.tif already exists. Failed to execute (CopyRaster). – CodingGrandma Nov 14 '17 at 21:51
  • It's telling you that the file already exists and it's not going to overwrite it, insert a line arcpy.env.overwriteOutput = True http://resources.arcgis.com/en/help/main/10.2/index.html#//018z0000004s000000 somewhere near the top of the code to ensure overwriting of existing files or delete the existing file. Based on the file name for the InRaster, is it a multi band image? It's got Band_1 already in the name. – Michael Stimson Nov 14 '17 at 22:00