-1

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:\ArcGIS\GR 8573\storms' arcpy.env.overwriteOutput = True workspace = r'C:\ArcGIS\GR 8573\bc_batch_test_in' walk = arcpy.da.Walk(workspace, datatype="FeatureClass") rasters = arcpy.ListRasters("*", "GRID") shpfiles = arcpy.ListFeatureClasses() OutputFolder = r'C:\ArcGIS\GR 8573\storms' 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:\ArcGIS\GR 8573\walk_test_01.py", line 21, in ras = os.path.join(dirpath, rasters) File "C:\Python27\ArcGIS10.7\lib\ntpath.py", line 85, in join result_path = result_path + p_path TypeError: coercing to Unicode: need string or buffer, list found

Edit - I've updated the code as suggested, stringifying both the raster and shapefile lists:

import arcpy import os from arcpy import env from arcpy.sa import * env.workspace = r'C:\ArcGIS\GR 8573\bc_batch_test_in' arcpy.env.overwriteOutput = True workspace = r'C:\ArcGIS\GR 8573\bc_batch_test_in' walk = arcpy.da.Walk(workspace, datatype="FeatureClass") rasters = arcpy.ListRasters("*", "GRID") ras_string = ''.join(rasters) shpfiles = arcpy.ListFeatureClasses() ras = ''.join(shpfiles) OutputFolder = r'C:\ArcGIS\GR 8573\bc_batch_test_out' for dirpath, dirnames, filenames in walk: env.workspace = dirpath ras = os.path.join(dirpath, ras_string) 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 get the following error:

Runtime error Traceback (most recent call last): File "", line 26, in File "C:\Python27\ArcGIS10.7\Lib\ntpath.py", line 85, in join result_path = result_path + p_path TypeError: coercing to Unicode: need string or buffer, list found

It's a similar error but not exactly the same. Am I not creating the strings correctly?

0

1 Answer 1

1

You are getting the error:

TypeError: coercing to Unicode: need string or buffer, list found

from this line of code:

ras = os.path.join(dirpath, rasters) 

You have previously set variable rasters to be a list of rasters. The variable dirpath is previously set to a string by the generator that arcpy.da.walk returns. Consequently, you are trying to join a string to a list of rasters (instead of a string to another string), and you get a TypeError accordingly.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.