Union all shapefiles in a geodatabase

1104
4
08-02-2012 11:04 AM
SuyunMa
New Contributor
Hi,
I have 2806 polygon shapefiles stored in the same geodatabase and I want to union all of them together. So I was wondering how can I do that by using Python script. Thanks a lot.

SYM
Tags (2)
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
Hi SYM,

Here's an example:

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"

lstFCs = arcpy.ListFeatureClasses("*", "POLYGON")
arcpy.Union_analysis(lstFCs, "Union_FC")


This script is creating a list of all the polygon feature classes, and then passing this list as the input for the Union GP tool.
0 Kudos
SuyunMa
New Contributor
Thanks a lot.
Hi SYM,

Here's an example:

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"

lstFCs = arcpy.ListFeatureClasses("*", "POLYGON")
arcpy.Union_analysis(lstFCs, "Union_FC")


This script is creating a list of all the polygon feature classes, and then passing this list as the input for the Union GP tool.
0 Kudos
ChristopherThompson
Occasional Contributor III
Does ArcView allow more than two inputs to a union?  I think that AV only accepts two inputs for intersects and think the same limitation applies to other overlay processes like union.
0 Kudos
ChrisSnyder
Regular Contributor III
ArcView only allows two FCs to be unioned at a time. Here is some ***untested*** Python code that could serve as a work-around:

#Be carefull to delete any "tmp_*" or "final_union" FCs before you run the code... otherwise those'll get unioned too
import arcpy
arcpy.env.workspace = r"C:\temp\python\test.gdb"
fcList = arcpy.ListFeatureClasses("*", "POLYGON")
i = 0
for fc in fcList:
   i = i + 1
   if i == 1:
      unionFcList = [fcList[i-1],fcList]
   else:
      inputTmpFC = "tmp_" + str(i-1)
      unionFcList = [fcList,inputTmpFC]
   outTmpFC = "tmp_" + str(i)
   arcpy.Union_analysis(unionFcList, outTmpFC)
   arcpy.Delete_managment(inputTmpFC)
arcpy.Rename_managment(outTmpFC, "final_union")
0 Kudos