[GIS] How to batch merge point, polygon and line feature classes into a new feature class

arcgis-10.3arcgis-desktoparcpymodelbuilder

Using ArcGIS 10.3 for desktop.

For our noxious weed surveys, we have some features recorded as polygons, some as lines, and some as points. I would like to convert everything to points, and then merge it all into one feature class for each species. I have minimal python experience, but am willing to try.

It is currently set up so that there are three file geodatabases. NoxiousWeeds_points.gdb, NoxiousWeeds_poly.gdb, and NoxiousWeeds_lines.gdb. Within each geodatabase is a separate feature class for each species. E.g., "HimalayanBlackberry_points" or "ReedCanarygrass_poly," etc. Names are consistent between the three geodatabases, the only thing that varies is the suffix.

What I'm Trying to Do:

I would like to be able to simply point arc to the three geodatabases and have it automatically convert the line and polygon feature classes to points, then merge with the matching point feature class based on the name, and then saved to a fourth, separate geodatabase with a slightly modified name. For example, "Knotweed_poly" and "knotweed_line" would both be converted to points, merged with the existing "knotweed_points," and then saved as "Knotweed_merged" in a fourth, separate geodatabase named "NoxiousWeeds_merged." Ideally I could simply point arc to the geodatabases that contain all these files and it would go through this whole process for each species by itself without me having to manually select each file. Note that I would also like the original source files to remain unaltered.

I suspect this is possible through some combination of python and ModelBuilder. If so, how would you recommend I go about it?

Best Answer

I suggest populating a python dictionary. The key would be your category, and the values would be a python list of your feature classes to merge. Use a for loop to iterate through your geodatabases, and another to iterate through your feature classes. Check each feature class to determine if it contains points. If not, perform a Feature To Point conversion. Something like this (untested):

#-Locals

#List of GDBs
gdbs = [r"c:\test\NoxiousWeeds_points.gdb",
        r"c:\test\NoxiousWeeds_poly.gdb",
        r"c:\test\NoxiousWeeds_lines.gdb"]

#Merge GDB
mergeGdb = r"c:\test\NoxiousWeeds_merged.gdb"

#Workspace
#Use "in_memory" or workspace geodatabase on disk
workspace = "in_memory"

#-End Locals

#Import modules
from arcpy import *
import os

#dictionary to be populated with list of all feature classes
fcsDi = {}

#Garbage for deleting created feature classes
garbage = []

#Iterate through three GDBs
for gdb in gdbs:
    print "Iterating", gdb
    #Set workspace to GDB
    env.workspace = gdb

    #List feature classes in gdb
    fcs = ListFeatureClasses ()

    #Iterate through feature classes
    for fc in fcs:
        #Check if feature class is points
        shape = Describe (fc).shapeType
        if shape != "Point":
            #Feature to point
            outFile = os.path.join (workspace, fc + "_point")
            FeatureToPoint_management (fc, outFile, "INSIDE")
            garbage += [outFile]
        else:
            outFile = os.path.join (gdb, fc)

        #Remove unwanted text to determine merge
        fcCategory = fc.replace ("_poly", "").replace ("_lines", "").replace ("_points", "")

        #Check if category is in dictionary
        if fcCategory in fcsDi:
            fcsDi [fcCategory] += [outFile]
        #If not in dictionary
        else:
            fcsDi [fcCategory] = [outFile]

#Iterate through dictionary keys
for category in fcsDi:
    print "Merging", category
    #Path to merge feature class
    outFc = os.path.join (mergeGdb, category + "_merged")
    #Get list of feature classes from dictionary
    fcs = fcsDi [category]
    #Merge
    print "Created:", outFc
    Merge_management (fcs, outFc)

#Clean up
for trash in garbage:
    Delete_management (trash)
Related Question