Select to view content in your preferred language

Loop through multiple folder and composite-band of raster of Same folder

3295
14
Jump to solution
09-07-2018 03:34 PM
BIJOYGAYEN
Emerging Contributor

Hi 

I am trying to make a loop function.  loop function picks up the rasters file from every subfolder, composite(layer stack#) the raster file and keeps it in that subfolders. I am getting an error. please help anyone. Under ppp folder have two subfolder P1 and P2 both folders contain the raster file. Thanks in advance.

import arcpy ,os ,sys
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
env.workspace = "G:/ANFIS_DATA_2/PRECEPITATION/aug/POINT/ppp"

ws = r"G:/ANFIS_DATA_2/PRECEPITATION/aug/POINT/ppp"
walk = arcpy.da.Walk(ws, topdown=True, datatype="RasterDataset")
for dirpath, dirnames, filenames in walk:
        print "Processing folder:", dirpath

        rasters=[]
        for filename in filenames:
            raster= os.path.join(dirpath,filename)
            print raster
            if filename.endswith(".tif"):
                    rasters.append(raster)
print "-rasters found:",len(rasters)

if len(rasters) != 0:
            print " layerstack"
            file_name_only = os.path.splitext(rasters[0]) [0]
            tifname = file_name_only[-9:]
            print tifname
            ras_name = os.path.join(dirpath,'L_{0}.tif'.format(tifname))
            layr =arcpy.CompositeBands_management(rasters,ras_name)


else :
    print" -skipping folder..."

print "Finished..."
Tags (1)
0 Kudos
14 Replies
DanPatterson_Retired
MVP Emeritus

before the composite band rasters call, what is ras_name?

you could add something to make it unique

extra = 0
ras_name = os.path.join(dirpath,'L_{0}.tif'.format(tifname)) + str(extra)
layr =arcpy.CompositeBands_management(rasters,ras_name)
extra += 1
BIJOYGAYEN
Emerging Contributor

ras_name saves root + file name.

I am just adding your code for the unique name but code give me the error

import arcpy ,os ,sys
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
env.workspace = "G:/ANFIS_DATA_2/PRECEPITATION/aug/POINT/ppp"

ws = r"G:/ANFIS_DATA_2/PRECEPITATION/aug/POINT/ppp"
walk = arcpy.da.Walk(ws, topdown=True, datatype="RasterDataset")
for dirpath, dirnames, filenames in walk:
print "Processing folder:", dirpath

rasters=[]
for filename in filenames:
raster= os.path.join(dirpath,filename)
print raster
if filename.endswith(".tif"):
rasters.append(raster)
print "-rasters found:",len(rasters)

if len(rasters) != 0:
print " layerstack"
file_name_only = os.path.splitext(rasters[0]) [0]
tifname = file_name_only[-5:]
# print tifname
extra = 0
ras_name = os.path.join(dirpath,'L_{0}.tif'.format(tifname)) + str(extra)
layr =arcpy.CompositeBands_management(rasters,ras_name)
extra += 1
else:
print" -skipping folder..."

print "Finished..."

Message File Name Line Position
Traceback
<module> <module1> 37
CompositeBands C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\management.py 13824
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000445: Extension is invalid for the output raster format.
Failed to execute (CompositeBands).

0 Kudos
BIJOYGAYEN
Emerging Contributor

Thank you so much. It is working.

I have another question suppose P1, P2.. , folder containing different name file as like idwaugpre.tif file i want to avoid this file picking time. Have any way?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Bijoy  You should mark my answer correct then, so that people that an answer was obtained.

As for your second part, For tasks like this, it is preferable to make a tool in a toolbox and make parameters in your script than enables users to make selections to limit or expand upon the criteria.  It is far easier this way than coding for all possibilities of potential entries.

BIJOYGAYEN
Emerging Contributor

Thank you so much. I solved this issue.

0 Kudos