TransWikia.com

Buffering multiple files in subfolders using ArcPy with ArcMap

Geographic Information Systems Asked on July 6, 2021

I have 153 shapefiles with one point in each file. Each shapefile has 10 GRID raster files associated with it. I need to buffer each group of 10 GRID files around each shapefile. I have the data in folders with a few levels of subfolders (folder/subfolder_1/subfolder_2/shapefile and 10 GRID files). Each subfolder_2 has one shapefile with one point and 10 GRID files. I want to buffer those 10 files around the one point, save the resulting 10 GRID files in a new subfolder, and then repeat for each of the 153 shapefiles. Is there a way to do this in Python without coding the individual paths for each of the 153 bottom subfolders? Basically something that says "look at all the subfolders in this folder," "buffer all the rasters in each subfolder with the shapefile in that subfolder," "save the files in a new subfolder. I’ve attached an image of how the data is structured. I want the data saved to subfolders called "t_00_out," t_12_out," t_24_out," and t_48_out." So all of the buffered GRID files from all the "t_00" subfolders will go into one main "t_00_out" subfolder, etc.

folder structure

Here is what I’ve come up with so far:

import arcpy
import os
from arcpy import env
from arcpy.sa import *

env.workspace = r'C:ArcGISGR 8573storms'
arcpy.env.overwriteOutput = True

workspace = r'C:ArcGISGR 8573bc_batch_test_in'

walk = arcpy.da.Walk(workspace, datatype="FeatureClass")

rasters = arcpy.ListRasters("*", "GRID")

shpfiles = arcpy.ListFeatureClasses()

OutputFolder = r'C:ArcGISGR 8573storms'

for dirpath, dirnames, filenames in walk:
    set arcpy.env.workspace = dirpath
    ras = os.path.join(dirpath, rasters)
    shp = os.path.join(dirpath, shpfiles)
    buff_shp = "temp_shp"
    arcpy.Buffer_analysis(shp, buff_shp, "3 DecimalDegrees")
    clip_ras = "temp_ras"
    arcpy.Clip_management(ras, "#",clip_ras, buff_shp, "0", "ClippingGeometry", "MAINTAIN_EXTENT")
    outRaster = OutputFolder + "/out" + rasters
    arcpy.CopyRaster_management(clip_ras, outRaster, format="GRID")

print "Done"

I’m getting this error:

Traceback (most recent call last):
File "C:ArcGISGR 8573walk_test_01.py", line 21, in
ras = os.path.join(dirpath, rasters)
File "C:Python27ArcGIS10.7libntpath.py", line 85, in join
result_path = result_path + p_path
TypeError: coercing to Unicode: need string or buffer, list found

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP