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..."
Solved! Go to Solution.
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..."
Can you share the error message?
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
But I cannot solves the previous reply problems.
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..."
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.
.
Pn -Raster files picked up--- Band composite---save in Pn folder by unique name.
show some of the print statements
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).
>>>
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
Then How to save this composite file by unique name for different folder???