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

1515
14
Jump to solution
09-07-2018 03:34 PM
BIJOYGAYEN
New Contributor II

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
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

Your indentation is throwing off the loops... try this

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..."
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
14 Replies
RandyBurton
MVP Regular Contributor

Can you share the error message?

0 Kudos
BIJOYGAYEN
New Contributor II

I had a small mistake to fix it. The code just picks up the p2 folder's raster file and composite it, also saves it by first raster file name(of the P2 folder) in the same folder. But The function did not pick up the file from another folder(P1) and no composite is going on. 

 Here is two problem

              1.code does not pick up the file from another folder at the same time

              2.composite  file save by first raster file name of the folder

0 Kudos
BIJOYGAYEN
New Contributor II

But I cannot solves the previous reply problems.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Your indentation is throwing off the loops... try this

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..."
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
BIJOYGAYEN
New Contributor II

Message File Name Line Position
Traceback
<module> <module1> 36
CompositeBands C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\management.py 13824
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (CompositeBands).

My aim is, the code will go to every sub-folder ( P1, P2,...) under ppp folder after that code picked up the raster files individual folder and will do band composite and saved this composite file by unique name in the same folder(P1,P2,...).

PPP 

   P1 -Raster files picked up--- Band composite---save in P1 folder by unique name.

   P2 -Raster files picked up--- Band composite---save in P2 folder by unique name.

                                 .

 

   P-Raster files picked up--- Band composite---save in Pfolder by unique name.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

show some of the print statements

0 Kudos
BIJOYGAYEN
New Contributor II

Processing folder: G:/ANFIS_DATA_2/PRECEPITATION/aug/POINT/ppp
-rasters found: 0
-skipping folder...
Processing folder: G:\ANFIS_DATA_2\PRECEPITATION\aug\POINT\ppp\p1
G:\ANFIS_DATA_2\PRECEPITATION\aug\POINT\ppp\p1\aug1_pre.tif
G:\ANFIS_DATA_2\PRECEPITATION\aug\POINT\ppp\p1\aug22_pre.tif
G:\ANFIS_DATA_2\PRECEPITATION\aug\POINT\ppp\p1\aug25_pre.tif
G:\ANFIS_DATA_2\PRECEPITATION\aug\POINT\ppp\p1\aug26_pre.tif
G:\ANFIS_DATA_2\PRECEPITATION\aug\POINT\ppp\p1\aug27_pre.tif
G:\ANFIS_DATA_2\PRECEPITATION\aug\POINT\ppp\p1\aug30_pre.tif
G:\ANFIS_DATA_2\PRECEPITATION\aug\POINT\ppp\p1\aug31_pre.tif
G:\ANFIS_DATA_2\PRECEPITATION\aug\POINT\ppp\p1\aug6_pre.tif
G:\ANFIS_DATA_2\PRECEPITATION\aug\POINT\ppp\p1\aug7_pre.tif
G:\ANFIS_DATA_2\PRECEPITATION\aug\POINT\ppp\p1\aug8_pre.tif
G:\ANFIS_DATA_2\PRECEPITATION\aug\POINT\ppp\p1\aug9_pre.tif
-rasters found: 11
layerstack
\aug1_pre
Traceback (most recent call last):
File "<module1>", line 36, in <module>
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\management.py", line 13824, in CompositeBands
raise e
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (CompositeBands).

>>>

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor
print tifname
ras_name = os.path.join(dirpath,'L_{0}.tif'.format(tifname))
layr =arcpy.CompositeBands_management(rasters,ras_name)

printing the tifname is not useful... 

print the ras_name to make sure that it is correct and make sure 'rasters' in the CompositeBands is correct.

That is where the error message and I suspect that the name is not correct

BIJOYGAYEN
New Contributor II

Then How to save this composite file by unique name for different folder???

0 Kudos