0

I have multiple Sentinel 2a raster images in separated folders at same location, which means every image is in separate folder. Now I want to create composite band of each raster. I have written a code and its given an error, its says

File "C:\Python27\ArcGIS10.3\lib\genericpath.py", line 91, in _splitext sepIndex = p.rfind(sep) AttributeError: 'list' object has no attribute 'rfind'

How do I specify arguments in composite band?

Here is the code

import arcpy, os
from arcpy.sa import *

arcpy.env.workspace = r"L:\Arcpy\Data\Satellite_image\T44QPJ_20200405"
arcpy.env.overwriteOutput = True

input_raster = arcpy.ListRasters()
outputDir = r"L:\Arcpy\Data\Layer_stack"
print(input_raster)

blue = r"L:\Arcpy\Data\Satellite_image\T44QPJ_20200405_B02_10m.jp2"
green = r"L:\Arcpy\Data\Satellite_image\T44QPJ_20200405T045651_B03_10m.jp2"
red = r"L:\Arcpy\Data\Satellite_image\T44QPJ_20200405T045651_B04_10m.jp2"
nir = r"L:\Arcpy\Data\Satellite_image\T44QPJ_20200405T045651_B08_10m.jp2"
swir_1 = r"L:\Arcpy\Data\Satellite_image\T44QPJ_20200405T045651_B011_20m.jp2"
swir_2 = r"L:\Arcpy\Data\Satellite_image\T44QPJ_20200405T045651_B012_20m.jp2"

# the first part of the file name before extension (if present) then add *.img
output = os.path.join(outputDir,os.path.splitext(input_raster)[0].split('_')[0] + '.img')
arcpy.CompositeBands_management(";".join([blue,green,red,nir,swir_1,swir_2]), output)

I am stuck in CompositeBands_managemnet tool. How can I solve this problem?

0
1

You were passing raster objects to a function that does not want them. Possible inputs to Composite bands are:

Mosaic Dataset ; Mosaic Layer ; Raster Dataset ; Raster Layer

Try something like:

import arcpy

b02 = r"D:\Sentinel2\202006\S2A_MSIL2A_20200613T103031_N0214_R108_T33VVJ_20200613T120604.SAFE\GRANULE\L2A_T33VVJ_A025988_20200613T103028\IMG_DATA\R10m\T33VVJ_20200613T103031_B02_10m.jp2"
b11 = r"D:\Sentinel2\202006\S2A_MSIL2A_20200613T103031_N0214_R108_T33VVJ_20200613T120604.SAFE\GRANULE\L2A_T33VVJ_A025988_20200613T103028\IMG_DATA\R20m\T33VVJ_20200613T103031_B11_20m.jp2"
b12 = r"D:\Sentinel2\202006\S2A_MSIL2A_20200613T103031_N0214_R108_T33VVJ_20200613T120604.SAFE\GRANULE\L2A_T33VVJ_A025988_20200613T103028\IMG_DATA\R20m\T33VVJ_20200613T103031_B12_20m.jp2"

arcpy.CompositeBands_management(in_rasters=";".join([b02,b11,b12]), out_raster=r"C:\GIS\data\testdata\123out.tif")
9
  • 1
    I have done changes in the code as you say, but this time I get this error raise e ExecuteError: ERROR 000271: Cannot open the input datasets Failed to execute (CompositeBands). On above code I made the changes also – Siddhartha Dhara Aug 17 '20 at 15:21
  • Your rasters are directly in the Satellite_image folder (not in a subfolder to it)? The for loop doesnt do anything relevant as of now. In fact, if input_raster is an empty list then I belive composite bands will fail because blue green red etc will then be nothing. Try removing the foor loop (only this line: for data in input_raster:) and see if composite bands work then. – BERA Aug 17 '20 at 15:47
  • I made changes as you say this time error occurred in split text. File "C:\Python27\ArcGIS10.3\lib\genericpath.py", line 91, in _splitext sepIndex = p.rfind(sep) AttributeError: 'list' object has no attribute 'rfind' – Siddhartha Dhara Aug 17 '20 at 16:24
  • What is the actual name you want as output? It might be easier to extract it from any of the bands path – BERA Aug 17 '20 at 16:45
  • I want the Tile name means like T44QPJ. – Siddhartha Dhara Aug 17 '20 at 16:47
0

The code should be like this

import arcpy, os

#Define the workspace that contains all of the folders with the Sentinel Imagery
arcpy.env.workspace = "L:\\Arcpy\\Data\\Satellite_image"

#Specify where you would like the output to go
output = "L:\\Arcpy\\Data\\Layer_stack"

# List of all workspace perviously defind the workspace.
folders = arcpy.ListWorkspaces()

for folder in folders:
    arcpy.env.workspace = folder
    images = arcpy.ListRasters("*.jp2")# Filter all bands in the folder.
    print "images: "+str(images)
    name = os.path.join(output, images[1].split("_")[0] + ".img")
    arcpy.CompositeBands_management(images, name)
print(images)
print "Finished"

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.