I've been working on a script that searches down through a tree, finds GIS data, clips it by an Area of Interest poly, then writes it out in a new location duplicating the input tree structure. https://community.esri.com/thread/95511?sr=stream
I only have this working for shapefiles and tifs. I need to also get the grids, but how to I detect a grid, a File System Raster, with python and arcpy?
Listrasters has a specification type allowing you to specify grid etc ArcGIS Help (10.2, 10.2.1, and 10.2.2)
Thanks. Looking at that now, but I can't figure out how to use ListRasters with the way I'm walking the subfolders. I threw it in my os.walk for, but the rasterList is empty because the only grids are down in C:\avdata\PythonTest\ClipLidar\TestData\Input\Highest_Hit
.
.
#Find the input feature classes
for root, dirs, files in os.walk(InputFolder):
rasterList = arcpy.ListRasters("*", "GRID")
for raster in rasterList:
print raster
for name in files:
#Handle tifs
if os.path.splitext(name)[1] == ".tif":
FileObj = arcpy.mapping.Layer(os.path.join(root, name))
FileExtent = FileObj.getExtent()
print "Processing: " + name
#if image overlap or touches AOI polygon, then clip image, write to output
Extent_Disjoint = str(FileExtent.disjoint(AOIextent))
if Extent_Disjoint == 'False':
print "Clipping: " + name
OutputPath = root.replace("Input","Output")
if not os.path.exists(OutputPath):
os.makedirs(OutputPath)
Clipname = os.path.splitext(name)[0] + "cp.tif"
print "Writing " + (os.path.join(OutputPath,Clipname))
arcpy.Clip_management((os.path.join(root, name)),"#",(os.path.join(OutputPath,Clipname)),clipshape,"#","ClippingGeometry")
#for shapefiles, use a different clip, write to output.
if os.path.splitext(name)[1] == ".shp":
.
.
I would suggest that you collect all the rasters first before doing any further processing rather than trying to do everything all at once. Create an empty raster list, then append those found to it as you decend the path tree
Dang, I can see that da.walk would make this a lot easier, but I have to get this to work with ArcGIS 10.0.
I've been playing around with ListRasters, but I can't seem to make it do the os.walk. ListRasters seems to ignore InputFolder, line 12, and only works on the env.workspace, set in line 8 (just as the documentation says). Hmm, do you think I need to reset the env.workspace right after the os.walk statement from each root,dirs combination?
# Import arcpy module
import arcpy
import os
#arcpy.CheckOutExtension("spatial")
# Set Geoprocessing environments
arcpy.env.scratchWorkspace = "c:\\avdata\\PythonTest\\ClipLidar\\Scratch"
#arcpy.env.workspace = "c:\\avdata\\PythonTest\\ClipLidar\TestData"
arcpy.env.workspace = "c:\\avdata\\PythonTest\\ClipLidar\\TestData\\Input\\Highest_Hit"
arcpy.env.overwriteOutput = True
clipshape = "c:\\avdata\\PythonTest\\ClipLidar\\AOIPoly_SP.shp"
InputFolder = "c:\\avdata\\PythonTest\\ClipLidar\\TestData\\Input"
#InputFolder = "c:\\avdata\\PythonTest\\ClipLidar\\TestData\\Input\\Highest_Hit"
#OutputFolder = "c:\\avdata\\PythonTest\\ClipLidar\\TestData\\Output"
#Find AOI extent
AOI = arcpy.mapping.Layer(clipshape)
AOIextent=AOI.getExtent()
#Find the input feature classes
for root, dirs, files in os.walk(InputFolder):
rasterList = arcpy.ListRasters("*", "GRID")
for raster in rasterList:
print raster
## for name in files:
## #Handle tifs