4

Trying to write a script to merge feature classes based off their name. Steps are as follows -

  1. In the original geodatabase, find polygon feature classes that end with a particular suffix ( ie *15_AM)
  2. In a new geodatabase, merge them together based on their names (So Peaks_15_AM, Peaks_14_AM and Peaks_13_AM to Peaks_AM)

I'm a novice and my code is very crude, it works for a single feature class but the loop does not accept a list for the 'Keyword' variable. I know I need to somehow iterate over a list but have been stuck how to approach it.

Ideally if I could use the feature class names without the suffix as the 'Keyword' that would be great, but could also just use some raw strings.

import arcpy
import os
#variables
arcpy.env.workspace = r"D:\Old.gdb"
OutputGDB = "D:\New.gdb"
FCS = set(arcpy.ListFeatureClasses("*15_AM", "polygon") + arcpy.ListFeatureClasses("*14_AM", "polygon") + arcpy.ListFeatureClasses("*13_AM", "polygon"))
Keyword = "Peaks"
fc = []
#merge
for i in FCS:
 if Keyword in i:
 fc.append(i)
 arcpy.Merge_management(fc,OutputGDB+"/"+Keyword+"_AM")
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 22, 2019 at 6:39
2
  • How do you identify the suffix? Anything after the final underscore? Commented Mar 24, 2019 at 18:02
  • Suffix is always *13/14/15_AM - they are annotation masks of different scales Commented Mar 24, 2019 at 23:24

2 Answers 2

2

I recommend collections.defaultdict when grouping of some kind is involved:

import arcpy, os
from collections import defaultdict
arcpy.env.workspace = r'C:\scratch.gdb' #Change
output_gdb = r'C:\scratch2.gdb' #Change
lookfor = ['15_AM','14_AM'] #Change
d = defaultdict(list)
[d[l] for l in lookfor] #Add the keys
for feature in arcpy.ListFeatureClasses():
 for k,v in d.items(): #d.iteritems() py2/ArcMap
 if feature.endswith(k):
 d[k].append(feature)
#d is now:
#defaultdict(<class 'list'>, {'15_AM': ['ressers_15_AM', 'fhgg_15_AM'], '14_AM': ['sdfsdfs_14_AM', 'bvbnvbn_14_AM']})
for k,v in d.items():
 arcpy.Merge_management(inputs=v, output=os.path.join(output_gdb,'merged'+k))

enter image description here

answered Mar 22, 2019 at 7:41
0
1

Extending the @BERA's answer, I had to make some changes as I needed to merge based on the class (i.e. "ConservationArea") rather than the suffixes.

I've included a screenshot of the GDB, so you can see why I required startswith and endswith.

import arcpy, os
from collections import defaultdict
arcpy.env.workspace = r'D:\Data.gdb' #Change
output_gdb = r'D:\MASK_Merge_Combined_Test.gdb' #Change
lookfor = ['ConservationArea','Cape_Mtn'] #Change
Suffix = ('_15_AM','_14_AM','_13_AM')
d = defaultdict(list)
[d[l] for l in lookfor] #Add the keys
for feature in arcpy.ListFeatureClasses():
 for k,v in d.items(): #d.iteritems() py2/ArcMap
 if feature.startswith(k) and feature.endswith(Suffix):
 d[k].append(feature)
for k,v in d.items():
 arcpy.Merge_management(inputs=v, output=os.path.join(output_gdb,k+'_AM'))

enter image description here

Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Mar 25, 2019 at 5:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.