0

I have a hundreds of shapefiles and, based on the filename, I need to load it into one of three feature classes (points, polylines, or polygons). My code works until it tries to load the second file of a particular type. Then, I get an error "FC_NAME already exists". Yes, it already exists, that's exactly what I want. Now load the shapefile into the feature class, please!

How do I load a shapefile into an existing feature class?

Here's the code, trimmed down to exclude logic that gets to this point:

result = arcpy.FeatureClassToFeatureClass_conversion(shp, outputLocation, outputPointsFC)
2

1 Answer 1

3

Here's a script I just happened to have lying around, it merges all lines, points and polygons from a database into a new database feature class; there's no field mapping, at this time we just wanted to see all the geometries together without having to load over 1k layers...

A feature class (shapefile or database) can only store one geometry type so it's important to test that before deciding where to put it.

import arcpy, os, sys

# set your parameters input and output database
InDB = sys.argv[1]
OutDB = sys.argv[2]

if not os.path.exists(OutDB):
    DBpath = os.path.dirname(OutDB)
    DBname = os.path.basename(OutDB)
    name,ext = os.path.splitext(DBname)
    if ext.upper() == ".MDB":
        arcpy.AddMessage("Creating output personal database")
        arcpy.CreatePersonalGDB_management(DBpath,DBname)
    elif ext.upper() == ".GDB":
        arcpy.AddMessage("Creating output file database")
        arcpy.CreateFileGDB_management(DBpath,DBname)
    else:
        arcpy.AddError("Unknown output database format")

# set your workspace for ListDatasets
arcpy.env.workspace = InDB

# create empty lists
LineList = list()
PointList = list()
PolyList = list()

# Standalone feature classes
for FeatClass in arcpy.ListFeatureClasses():
    desc = arcpy.Describe(InDB + "\\" + FC + "\\" + FeatClass)
    if desc.shapeType == "Point":
        PointList.append(InDB + "\\" + FC + "\\" + FeatClass)
    elif desc.shapeType == "Polyline":
        LineList.append(InDB + "\\" + FC + "\\" + FeatClass)
    elif desc.shapeType == "Polygon":
        PolyList.append(InDB + "\\" + FC + "\\" + FeatClass)

# iterate through feature datasets
for FC in arcpy.ListDatasets():
    arcpy.env.workspace = InDB + "\\" + FC
    for FeatClass in arcpy.ListFeatureClasses():
        desc = arcpy.Describe(InDB + "\\" + FC + "\\" + FeatClass)
        if desc.shapeType == "Point":
            PointList.append(InDB + "\\" + FC + "\\" + FeatClass)
        elif desc.shapeType == "Polyline":
            LineList.append(InDB + "\\" + FC + "\\" + FeatClass)
        elif desc.shapeType == "Polygon":
            PolyList.append(InDB + "\\" + FC + "\\" + FeatClass)

arcpy.AddMessage("Performing merge")
if len(PointList) > 0:
    arcpy.Merge_management(PointList,OutDB + "\\Merged_Points")
if len(LineList) > 0:
    arcpy.Merge_management(LineList,OutDB + "\\Merged_Lines")
if len(PolyList) > 0:
    arcpy.Merge_management(PolyList,OutDB + "\\Merged_Polygons")

If you replace arcpy.Merge_management (smash a bunch together to a new feature class) with arcpy.Append_management (insert a bunch of feature classes into an existing feature class) then it should do pretty much what you're after - if you do you will also need to change the output to the full path to the existing datasets to insert into.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.