0

I have 30 shapefiles, each of which I want to be clipped with another 3850 shapefiles (contain only one polygon). I know I need a loop in Python and use ArcPy. But I'm very new to Python. Here's my code but I got error:

ERROR 000732: Clip Features: Dataset C:\Users\Farzane\Desktop\Master97\GNOMEmodeling\Postprocessing\Ropme does not exist or is not supported

Failed to execute (Clip).

import os
import arcpy
from arcpy import env

# setting my source and target geodatabase 
SourceWorkspace = r"C:\Users\Farzane\Desktop\Master97\GNOMEmodeling\Postprocessing\Results"
TargetWorkspace = r"C:\Users\Farzane\Desktop\Master97\GNOMEmodeling\Postprocessing"

#setting my clip feature (which is a polygon)
clipfeature = r"C:\Users\Farzane\Desktop\Master97\GNOMEmodeling\Postprocessing\Ropme\*.shp"

env.workspace = SourceWorkspace

fclist = arcpy.ListFeatureClasses()

for fc in fclist:
    outClipFeatureClass = os.path.join(TargetWorkspace, "clip area_" + fc)
    arcpy.Clip_analysis(fc,clipfeature,outClipFeatureClass)
5
  • So you have two groups of shapes. Do you mean each of 30 shapefiles need to be clipped by all other 3850 ones?
    – ArashMad
    Dec 27, 2020 at 9:29
  • clipfeature parameter that you've passed to Clip_analysis should be a shapefile ends with .shp but it is a url I guess.
    – ArashMad
    Dec 27, 2020 at 9:52
  • yes, I need another list for Clip feature because I have 3850 shapefiles. but I don't Know how can do this? Dec 27, 2020 at 9:59
  • Second argument passed to Clip_analysis should be a shapefile ends with .shp but clipfeature is a url.
    – ArashMad
    Dec 27, 2020 at 11:16
  • Wildcards are not legal in nearly every function within ArcPy. You always need to use a list function and iterate (wildcards are only valid in list functions).
    – Vince
    Dec 27, 2020 at 20:36

2 Answers 2

1

This does not look like the path to a polygon feature class:

clipfeature = r"C:\Users\Farzane\Desktop\Master97\GNOMEmodeling\Postprocessing\Ropme"

It should maybe a feature class in a file geodatabase:

clipfeature = r"C:\Users\Farzane\Desktop\Master97\GNOMEmodeling\Postprocessing\Ropme.gdb\polygons123"

or a shapefile:

clipfeature = r"C:\Users\Farzane\Desktop\Master97\GNOMEmodeling\Postprocessing\Ropme.shp
1
  • In fact, this is a path to the folder containing the shapefiles needed for the clip feature classes. Dec 27, 2020 at 11:38
0

If you want to execute clip analysis multiple time for each of 30 shapefiles, do something as follow:

import os
import glob
import arcpy
    
# setting my source and target geodatabase 
src_dir= r"\GNOMEmodeling\Postprocessing\source"
des_dir = r"\GNOMEmodeling\Postprocessing\target"
    
#setting my clip feature (which is a polygon)
clippers_dir = r"\GNOMEmodeling\Postprocessing\Ropme"
    
arcpy.env.workspace = src_dir
fclist = arcpy.ListFeatureClasses()
clippers = [glob.glob(f) for f in [clippers_dir + '/*.shp']]

for fc in fclist:
    src_file_dir, src_file_name = os.path.split(clipper)
    for clipper in clippers:
        clipper_dir, clipper_name = os.path.split(clipper)
        clipper_name = src_file_name.split('.')[0] + "_" + clipper_name 
        output_dir = os.path.join(des_dir , clipper_name)
        arcpy.Clip_analysis(fc,clipper,output_dir)

Please remember Clip_analysis is executed once and accepts two shapefiles. Therefore you will have 30*3850 individual clipped shape files when it finishes.

6
  • I have the merged shapefile which contains 3850 polygons but I need to clip them individually with 30 other shape files and then perform another operation. However, thanks for your first code, it is very helpful. Dec 27, 2020 at 11:46
  • Let us continue this discussion in chat.
    – ArashMad
    Dec 27, 2020 at 13:27
  • I ran the last code you sent and got an error. I think I should update fclist with the replace command. But I do not know how? AttributeError: 'list' object has no attribute 'replace' Dec 28, 2020 at 18:51
  • No, because I haven't 20 reputation on The Stack Exchange Network. how can I fix it? Dec 28, 2020 at 19:04
  • thanks, my problem is solved. Dec 28, 2020 at 20:16

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.