Select to view content in your preferred language

merge or append features classes based on a part of their names

2778
7
Jump to solution
01-22-2014 03:25 AM
PaoloPensalfini
Occasional Contributor
Hi everybody!
I've got ONE file geodatabase with about 400 feature classes (FC). Here attached is a part of that .gdb.
I need to merge that features (or better append one feature - 00 - to the other)  that has got the same name except for the "00".
For example:
"_01P11_line" must be merged with "_01P11_line00"
or better
"_01P11_line00" (input) appended to "_01P11_line" (target)
and, after the merging/appending, delete the "00" feature class.
I'm really a newbie with python...could you help me, please?
Thanks in advance
Paolo
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoshuaChisholm
Frequent Contributor
Hello Paolo,

I think the issue is caused because your target feature exists (example "_01P11_line"), but the input does not exist (example "_01P11_line00").

If you modify the script, it will can skip the feature classes that don't have a corresponding "00" feature class:

import arcpy  gdbPath=r"C:\Users\Path\To\Data.gdb"  arcpy.env.workspace = gdbPath  fcList=arcpy.ListFeatureClasses()  for fc in fcList:     if fc.endswith("A"):         tgtFC=fc         inFC=fc+"00" #this line requires there always to be a identical FC ending in '00'         if not arcpy.Exists(inFC):             print inFC+" not found. "+fc+" was skipped."             arcpy.AddMessage(inFC+" not found. "+fc+" was skipped.")             continue         arcpy.Append_management(inFC,tgtFC) #append FC '00' to the FC without '00'. inFC and tgtFC must have the same schema         arcpy.Delete_management(inFC)


As before, you can replace if fc.endswith("line"): with if not fc.endswith("00"):, and then the script will work on points, polygons, lines and annotations at the same time (without the need to duplicate the script).

Let me know if it works or if you run into any other problems.
Good luck!

View solution in original post

0 Kudos
7 Replies
JoshuaChisholm
Frequent Contributor
Good day Paolo,

This should work:
import arcpy

gdbPath=r"C:\Users\Path\To\Data.gdb"

arcpy.env.workspace = gdbPath

fcList=arcpy.ListFeatureClasses()

for fc in fcList:
    if fc.endswith("line"):
        tgtFC=fc
        inFC=fc+"00" #this line requires there always to be a identical FC ending in '00'
        arcpy.Append_management(inFC,tgtFC) #append FC '00' to the FC without '00'. inFC and tgtFC must have the same schema
        arcpy.Delete_management(inFC)


If you want to run it on all FCs without '00', just replace if fc.endswith("line"): with if not fc.endswith("00"):.

Let me know if it works for you. Good luck!
0 Kudos
PaoloPensalfini
Occasional Contributor
WOW!!
Thank you very much Joshua!!
Great job!!
It works perfectly!!
Since I could also find _polygon00 and _point00 and beacuse I don't know python to use only one script for all the three situations (line, point, polygon), I triplicated your line00 script and adapted it and put the three scripts in a modelbuilder flow...
It works!!
Really thank you very very much again!!
Paolo
0 Kudos
PaoloPensalfini
Occasional Contributor
I'm sorry!!
I pushed the wrong button!! Of course I wanted to vote "helpful" !!!
Now I can't correct it!!
I'm really very sorry....
Paolo
0 Kudos
JoshuaChisholm
Frequent Contributor
My pleasure to help! Thank you for the up vote and green check! I'm really happy it worked out for you.
0 Kudos
PaoloPensalfini
Occasional Contributor
Hi Joshua !
I've got a little problem.
I used the script in a .gdb containing points, polygons, lines and annotations.
I get this error (also see attached):
"ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000338: Inputs must be either all Feature Classes, Tables or Rasters; not mixed.
Failed to execute (Append)."
Could it depend because I've got many FC ending with "_line" but without identical FC ending with "_line00"?
Or could it depend because I'm not completlly sure Schemas between FC_line and FC_line00 are identical (but they would have to be!!).
Any suggestion?
Thanks in advance!
paolo
0 Kudos
JoshuaChisholm
Frequent Contributor
Hello Paolo,

I think the issue is caused because your target feature exists (example "_01P11_line"), but the input does not exist (example "_01P11_line00").

If you modify the script, it will can skip the feature classes that don't have a corresponding "00" feature class:

import arcpy  gdbPath=r"C:\Users\Path\To\Data.gdb"  arcpy.env.workspace = gdbPath  fcList=arcpy.ListFeatureClasses()  for fc in fcList:     if fc.endswith("A"):         tgtFC=fc         inFC=fc+"00" #this line requires there always to be a identical FC ending in '00'         if not arcpy.Exists(inFC):             print inFC+" not found. "+fc+" was skipped."             arcpy.AddMessage(inFC+" not found. "+fc+" was skipped.")             continue         arcpy.Append_management(inFC,tgtFC) #append FC '00' to the FC without '00'. inFC and tgtFC must have the same schema         arcpy.Delete_management(inFC)


As before, you can replace if fc.endswith("line"): with if not fc.endswith("00"):, and then the script will work on points, polygons, lines and annotations at the same time (without the need to duplicate the script).

Let me know if it works or if you run into any other problems.
Good luck!
0 Kudos
PaoloPensalfini
Occasional Contributor
WOW! WOW! and WOW again!!
Thank you sooo much Joshua!
This is really "THE FINAL SOLUTION"!
Paolo
0 Kudos