1

I have one raster data of a state and also I have 14 shapefile in same area. Now I want to clip 14 shapefile in arcpy using for loop. The issue is when I run the script its showing TypeError:'NoneType' object is not iterable. Here is the code

import arcpy

rasterlayer = (r"L:\\Arcpy\\Data\\Mosaic")   
output = (r"L:\\Arcpy\\Data\\clip")    
vector_layer = arcpy.ListFeatureClasses(r"L:\\Arcpy\\Data\\vector_data\\selected_district")


for vectordata in vector_layer:        
  arcpy.CalculateValue_management("\"%Name%\",.split(\"_\")[0]", "","Variant")
  arcpy.Clip_management(rasterlayer, "", output, vectordata, "0", "ClippingGeometry", "NO_MAINTAIN_EXTENT")

1 Answer 1

2

The reason you're getting this error is because ListFeatureClasses does not accept a path as an argument, the wildcard parameter is used to filter the feature classes returned to only those matching a particular file name pattern, for example 'Edit*' will return only the feature classes that start with edit.

You need to set your arcpy.env.workspace first, this will be the location arcpy searches:

import arcpy, os

rasterlayer = ("L:\\Arcpy\\Data\\Mosaic")  # file name needed here 
outputDir = ("L:\\Arcpy\\Data\\clip")    
arcpy.env.workspace = "L:\\Arcpy\\Data\\vector_data\\selected_district"
arcpy.env.overwriteOutput = True
vector_layer = arcpy.ListFeatureClasses()

for vectordata in vector_layer:
    # the first part of the file name before extension (if present) then add .tif
    output = os.path.join(outputDir,os.path.splitext(vectordata)[0].split('_')[0] + '.tif')
    arcpy.Clip_management(rasterlayer, "", output, vectordata, "0", "ClippingGeometry", "NO_MAINTAIN_EXTENT")

Using the first part of the name of the input vector data spit by underscores and adding .tif to the end; ArcGIS doesn't require in many tools to stipulate the format, instead it takes the extension and loaction and works out the appropriate output driver.. if none is given the result is an Esri GRID which can cause issues due to strict requirements.

5
  • I fixed the workspace as you told. Here I am using the Calculate value tool for specifies the name of output. Suppose I have 14 district each district has a name, so I want each output has each name as input file. Aug 4, 2020 at 6:17
  • Thank you for reply, but this time error showing in clip. It is showing Execute Error: ERROR 000582: Error occurred during execution. Aug 6, 2020 at 4:01
  • Is your rasterlayer valid, the path suggests an Esri GRID? 582 is a fairly generic error with invalid parameters and not very helpful, there are a few possibilities, the most likely is that your rasterlayer is a folder and not a raster. The next most likely is that one of your feature classes isn't a polygon or that the coordinate reference system is undefined. Output will occur in your workspace and as that is listed we can be sure that exists. Aug 6, 2020 at 4:53
  • In above code you declare outputDir variable and in the for loop you mention output in both clip and os path. Please explain these. Aug 6, 2020 at 7:32
  • Thank you now code is running perfectly Aug 6, 2020 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.