Iterate clip of polygon fcs by boundary polygon fcs of same name?

252
3
Jump to solution
01-20-2021 06:57 PM
KathleenHoenke
New Contributor III

I have a gdb of 50 polygon fcs named based on state abbreviation - ie: AR_HUC12, AL_HUC12, etc. 

I have another gdb of polygon fcs also based on the same state abbreviation - ie:  AR_wetlands, AL_wetlands.

I want to create a short scripts that loops through the second gdb and clips each of these fcs by the fc in the first gdb that has the same name. For example, I want to take the fc named AR_wetlands and clip it by the fc named AR_HUC12. 

 

Is there a quick script that will do this? I am very much a beginner!

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

it's not going to win any beauty contests, but it'll work (underlying premise is that everything before the _ in your fc names matches in each fgdb).

 

import arcpy
import os

#not a fan of stating the obvious, but change the paths below to your fgdbs
#also keep the r then surround the path in quotes

#fgdb of features being clipped
source_fgdb = r'D:\a.gdb'
#fgdb of features doing the clipping
clipping_fgdb = r'D:\b.gdb'
#preferably empy fgdb to put outputs
output_fgdb = r'D:\c.gdb'


#list all the feature classes

arcpy.env.workspace = source_fgdb

source_fcs = arcpy.ListFeatureClasses()

arcpy.env.workspace = clipping_fgdb

clipping_fcs = arcpy.ListFeatureClasses()

#make a dictionary of source fc path
#to clipping fc and source name
#NB relies on names before the '_' being equal
source_clip_fc_dict = {}

for source_fc in source_fcs:
    for clipping_fc in clipping_fcs:
        if source_fc.split("_")[0] == clipping_fc.split("_")[0]:
            source_fc_path = os.path.join(source_fgdb, source_fc)
            clipping_fc_path = os.path.join(clipping_fgdb, clipping_fc)
            source_clip_fc_dict[source_fc_path] = [clipping_fc_path, source_fc]

#clip each source feature and output to output_fgdb

for source_fc_path in source_clip_fc_dict:
    clipping_fc_path = source_clip_fc_dict[source_fc_path][0]
    source_fc_name = source_clip_fc_dict[source_fc_path][1]
    out_fc_name = source_fc_name + "_Clip"
    out_fc_path = os.path.join(output_fgdb, out_fc_name)


    arcpy.Clip_analysis(source_fc_path, clipping_fc_path, out_fc_path)
            

 

View solution in original post

0 Kudos
3 Replies
DavidPike
MVP Frequent Contributor

it's not going to win any beauty contests, but it'll work (underlying premise is that everything before the _ in your fc names matches in each fgdb).

 

import arcpy
import os

#not a fan of stating the obvious, but change the paths below to your fgdbs
#also keep the r then surround the path in quotes

#fgdb of features being clipped
source_fgdb = r'D:\a.gdb'
#fgdb of features doing the clipping
clipping_fgdb = r'D:\b.gdb'
#preferably empy fgdb to put outputs
output_fgdb = r'D:\c.gdb'


#list all the feature classes

arcpy.env.workspace = source_fgdb

source_fcs = arcpy.ListFeatureClasses()

arcpy.env.workspace = clipping_fgdb

clipping_fcs = arcpy.ListFeatureClasses()

#make a dictionary of source fc path
#to clipping fc and source name
#NB relies on names before the '_' being equal
source_clip_fc_dict = {}

for source_fc in source_fcs:
    for clipping_fc in clipping_fcs:
        if source_fc.split("_")[0] == clipping_fc.split("_")[0]:
            source_fc_path = os.path.join(source_fgdb, source_fc)
            clipping_fc_path = os.path.join(clipping_fgdb, clipping_fc)
            source_clip_fc_dict[source_fc_path] = [clipping_fc_path, source_fc]

#clip each source feature and output to output_fgdb

for source_fc_path in source_clip_fc_dict:
    clipping_fc_path = source_clip_fc_dict[source_fc_path][0]
    source_fc_name = source_clip_fc_dict[source_fc_path][1]
    out_fc_name = source_fc_name + "_Clip"
    out_fc_path = os.path.join(output_fgdb, out_fc_name)


    arcpy.Clip_analysis(source_fc_path, clipping_fc_path, out_fc_path)
            

 

View solution in original post

0 Kudos
KathleenHoenke
New Contributor III

Thank you so much, I sincerely appreciate it!

I'm wondering what I'm doing wrong, if I were to use and alter this same script to do an extract by mask with rasters?  See below. I'm  good in model builder, but just learning arcpy!

 

0 Kudos
DavidPike
MVP Frequent Contributor

No problem, also if you think that's the correct answer please mark it so.

I'd recommend stating this as a new problem in the forum, stating what you intend the script to do exactly, and then actually post the code as text (properly formatted) as a screenshot is maybe ok for capturing what an error message looks like, but not blocks of code someone would have to type out to correct/modify.

0 Kudos