TransWikia.com

Clip raster data by multiple shapefile in ArcPy using Clip data management tool

Geographic Information Systems Asked by Siddhartha Dhara on October 26, 2020

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:ArcpyDataMosaic")   
output = (r"L:ArcpyDataclip")    
vector_layer = arcpy.ListFeatureClasses(r"L:ArcpyDatavector_dataselected_district")


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

One Answer

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:ArcpyDataMosaic")  # file name needed here 
outputDir = ("L:ArcpyDataclip")    
arcpy.env.workspace = "L:ArcpyDatavector_dataselected_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.

Answered by Michael Stimson on October 26, 2020

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