Select to view content in your preferred language

If feature class has the same name as feature class in another database

1037
6
12-06-2012 05:24 AM
LouisEarly
Emerging Contributor
I have two databases (Staging and Prod) each containing the same feature classes.  I want to append the data from each individual feature class in the staging.gdb to its matching feature class in the Prod.gdb.  What is the most efficient way of writing this in python.

Currently i am using the append tool for each individual feature class. this is making the script kind of long and a lot to maintain if something changes as I have 20 appends to account for the 20 different feature classes.


Example

Staging.gdb  ...........   Prod.gdb
Wetlands  --append--> Wetlands
Streams   --append-->  Streams
Tags (2)
0 Kudos
6 Replies
AndrewChapkowski
Esri Regular Contributor
There are many ways to do this, but have you just tried deleting the old feature class and copying the new one in?

You can also use insert cursors to insert new rows from your source db to your destination db.


Hope this helps.
0 Kudos
LouisEarly
Emerging Contributor
See the attachment.  This is how I am currently doing it.  It works fine, I just see it as inefficient.  Shouldn't there be a way to say something like:

If feature class name from database "A" matches feature class name from database "B" then append those features from "A" to "B".  This way each append function doesn't have to be written out.

Keep in mind I'm still learning python, I've only been involved with it for about a month now.  Thanks, I appreciate the help.
0 Kudos
LouisEarly
Emerging Contributor
So no responses on this?  I'll assume i'm doing it correctly then?
0 Kudos
MathewCoyle
Honored Contributor
So no responses on this?  I'll assume i'm doing it correctly then?


I would probably do something like this.

import os
import sys
import traceback
import arcpy

sde_prod = r"Database Connections\GENERAL_DATA-PROD.sde"
sde_stage = r"Database Connections\STAGING-PROD.sde"

# I find this useful to add
if not arcpy.Exists(sde_prod) or not arcpy.Exists(sde_stage):
    # Add contingency sde connection file on network
    sde_prod = "somwhere1"
    sde_stage = "somewhere2"

arcpy.env.workspace = sde_prod

fcList = arcpy.ListFeatureClasses("*PERM_ABA*")
try:
    for fc in fcList:
        arcpy.DeleteFeatures_management(fc)
        print "Delete {0}".format(fc)
        arcpy.Append_management(
            os.path.join(sde_stage, fc), os.path.join(sde_prod, fc), "TEST")
        print "Append {0}".format(fc)

except arcpy.ExecuteError:
    # Get the tool error messages
    msgs = arcpy.GetMessages(2)
    # Return tool error messages for use with a script tool
    arcpy.AddError(msgs)
    # Print tool error messages for use in Python/PythonWin
    print msgs
except:
    # Get the traceback object
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    # Concatenate information together concerning the error into a message
    pymsg = "PYTHON ERRORS:\nTraceback info:\n{0}\nError Info:\n{1}".format(
        tbinfo, sys.exc_info()[1])

    msgs = "ArcPy ERRORS:\n{0}\n".format(arcpy.GetMessages(2))
    # Return python error messages for use in script tool or Python Window
    arcpy.AddError(pymsg)
    arcpy.AddError(msgs)
    # Print Python error messages for use in Python / Python Window
    print "{0}\n".format(pymsg)
    print msgs
0 Kudos
LouisEarly
Emerging Contributor
Thanks for the response, that works.  Well it did until our IT group decided to change somethings.  Now the feature classes in the staging database are all prefixed.  So an example is in staging MGISSP.GISONEAST.PERM_ABA_Roads and the same feature class in Prod is MGISP.GISONEAST.PERM_ABA_Roads.  Notice the difference at the beginning, staging is MGISSP and prod is MGISP.  As a result your solution doesnt work any longer.  Any other ideas? 

Again thanks for the help!
0 Kudos
MathewCoyle
Honored Contributor
Thanks for the response, that works.  Well it did until our IT group decided to change somethings.  Now the feature classes in the staging database are all prefixed.  So an example is in staging MGISSP.GISONEAST.PERM_ABA_Roads and the same feature class in Prod is MGISP.GISONEAST.PERM_ABA_Roads.  Notice the difference at the beginning, staging is MGISSP and prod is MGISP.  As a result your solution doesnt work any longer.  Any other ideas? 

Again thanks for the help!


The solution for such an issue is very simple using python and string replacement. If you are going to be using python scripts and tools in a production setting I'd recommend you learn the basics of python so you understand what your tools are doing.

Here's a good place to start.
http://docs.python.org/release/2.6.5/library/stdtypes.html?highlight=.replace#str.replace
0 Kudos