Merge multiple features

4712
6
Jump to solution
02-03-2015 10:33 AM
PeterVersteeg
Occasional Contributor II

Good day,

I have a GDB with features with almost the same name only the last postfix is a underscore and a number (Test_area,  Test_area_1)

I am working an a script that will merge the features with the same name. This is what i have so far:

It works but the feature list is about 50 features long and more important I would like to learn how I can improve this script.

import arcpy
from arcpy import env
import os

# Set the workspace for the ListFeatureClass function
  1. arcpy.env.workspace = r"D:\ GIS \Test.gdb"
outputA = r" D:\ GIS \Output.gdb \ TestA_area " outputB = r" D:\ GIS \ Output.gdb \ TestB_area " outputC = r" D:\ GIS \ Output.gdb \ TestC_area " outputD = r" D:\ GIS \ Output.gdb \ TestD_area " # Use the ListFeatureClasses function to return a list of all FC. fclistA = arcpy.ListFeatureClasses("TestA_area*","ALL") fclistB = arcpy.ListFeatureClasses("TestB_area*","ALL") fclistC = arcpy.ListFeatureClasses("TestC_area*","ALL") fclistD = arcpy.ListFeatureClasses("TestD_area*","ALL")
  1. arcpy.Merge_management(fclistA, outputA)
  2. arcpy.Merge_management(fclistB, outputB)
  3. arcpy.Merge_management(fclistC, outputC)
  4. arcpy.Merge_management(fclistD, outputD)

greeting peter

0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

Does something like this get you what you need?

def main():
    import arcpy
    import os

    inputgdb = r"C:\temp\input.gdb"
    outputgdb = r"C:\temp\output.gdb"
    arcpy.env.workspace = inputgdb

    for i in ["A", "B", "C", "D"]:
        wild_card = "Test{}_area*".format(i)
        fclist = arcpy.ListFeatureClasses(wild_card)
        outputfc = os.path.join(outputgdb, "Test{}_area".format(i))
        arcpy.Merge_management(fclist, outputfc)

    arcpy.ClearWorkspaceCache_management()  ## Cleanup


if __name__ == '__main__':
    main()

This assumes you do not have any feature classes in a feature dataset. If you do, the code will be slightly different.

View solution in original post

6 Replies
DanPatterson_Retired
MVP Emeritus

Why dont' you get a list of featureclasses first specifying an appropriate wildcard, then perform the merge within a loop (probably)

0 Kudos
BlakeTerhune
MVP Regular Contributor

In your example you only have four feature classes with different prefixe variations (A, B, C, and D). Are those four prefixes always fixed or could they change? If there are more than just those four or if they change, please describe the formatting and/or range of the possible prefixes.

Also, I noticed some inconsistencies with your imports:

  • If you import arcpy (like you do on the first line), it includes env. It is redundant to have from arcpy import env if you've already imported everything in arcpy; especially if you're still calling workspace with the full arcpy.env.workspace
  • You import os but I don't see that you're using it anywhere. Might save a little time to just not import it if you're not using it.
0 Kudos
PeterVersteeg
Occasional Contributor II

thank you for your response and your remark on my inconsistencies.

the feature class list is about 100 lines long and the TestA_area* etc. the names are always the same.

what I was thinking is it possible to make the script look at the first 10 characters and put those features in a list without setting the wildcard (TestA_area*). So all features in a list who's first 10 characters are the same.

greetings peter

0 Kudos
BlakeTerhune
MVP Regular Contributor

Does something like this get you what you need?

def main():
    import arcpy
    import os

    inputgdb = r"C:\temp\input.gdb"
    outputgdb = r"C:\temp\output.gdb"
    arcpy.env.workspace = inputgdb

    for i in ["A", "B", "C", "D"]:
        wild_card = "Test{}_area*".format(i)
        fclist = arcpy.ListFeatureClasses(wild_card)
        outputfc = os.path.join(outputgdb, "Test{}_area".format(i))
        arcpy.Merge_management(fclist, outputfc)

    arcpy.ClearWorkspaceCache_management()  ## Cleanup


if __name__ == '__main__':
    main()

This assumes you do not have any feature classes in a feature dataset. If you do, the code will be slightly different.

PeterVersteeg
Occasional Contributor II

Because the names are a bit different then in my example I had to do same minor adjustments. I had same trouble getting the right outputname but it works now. I think one could get the outputname in a cleaner way but I am still learning and am very happy it works.

Thanks Blake for the help

def main():  
    import arcpy  
    import os  
  
    inputgdb = r"Y:\Duitsland\GUTZ\Duitsland_GUTZ_N52_OLD2.gdb"  
    outputgdb = r"Y:\Duitsland\GUTZ\outputTest.gdb"  
    arcpy.env.workspace = inputgdb  
  
    for i in ["A_AA010", "A_AA514", "A_AB000", "A_AB030"]:  
        wild_card = "{}_*".format(i)  
        fclist = arcpy.ListFeatureClasses(wild_card)
        fcname = fclist[0]
        fcOutputName = "{}".format(i) + fcname[7:]
        outputfc = os.path.join(outputgdb, fcOutputName)  
        arcpy.Merge_management(fclist, outputfc)


arcpy.ClearWorkspaceCache_management()  ## Cleanup  
  
  
if __name__ == '__main__':  
    main()
0 Kudos
BlakeTerhune
MVP Regular Contributor

I should have clarified that you only need the arcpy.ClearWorkspaceCache_management() at the end if you're making an SDE connection. so if you're in a personal or file geodatabase you can get rid of that line.