Select to view content in your preferred language

Geoprocessing SDE Feature Classes

2699
7
12-04-2012 12:11 PM
LukeTaylor1
Emerging Contributor
I'm trying to append two SDE feature classes together.  I've been able to piece together the following code with no luck.  My geoprocessor appears to be failing I believe because my feature classes are not wrapped in a strongly typed RCW, but I've not had any luck figuring out how to accomplish this (or I may be totally out in left field with what I'm trying to do).  Any thoughts would be greatly appreciated.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geodatabase;

namespace T
{
    class Program
    {
        private static LicenseInitializer m_AOLicenseInitializer = new T.LicenseInitializer();
   
        [STAThread()]
        static void Main(string[] args)
        {
            //ESRI License Initializer generated code.
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeArcInfo },
            new esriLicenseExtensionCode[] { });

            //ESRI License Initializer generated code.
            //Do not make any call to ArcObjects after ShutDownApplication()

            // Get geoprocessor
            ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
            gp.AddOutputsToMap = false;

            // Get append tool
            ESRI.ArcGIS.DataManagementTools.Append app = new ESRI.ArcGIS.DataManagementTools.Append();

            IPropertySet propertySet = new PropertySetClass();
            propertySet.SetProperty("SERVER", "xxxxx");
            propertySet.SetProperty("INSTANCE", "xxxxx");
            propertySet.SetProperty("DATABASE", "xxxxx");
            propertySet.SetProperty("USER", "xxxxx");
            propertySet.SetProperty("PASSWORD", "xxxxx");
            propertySet.SetProperty("VERSION", "xxxxx");

            // Use activator
            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
            //workspaceFactory.Open(propertySet, 0);

            //IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();

            IWorkspace workspace = (IWorkspace)workspaceFactory.Open(propertySet, 0);

            IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)workspace;

            //IFeatureWorkspace pFeatureWorkspace = workspaceFactory.Open(propertySet, 0) as IFeatureWorkspace;

            ESRI.ArcGIS.Geodatabase.IFeatureClass inputClass = (IFeatureClass)pFeatureWorkspace.OpenFeatureClass("sde.DBO.Tester2Updater");
            ESRI.ArcGIS.Geodatabase.IFeatureClass targetClass = (IFeatureClass)pFeatureWorkspace.OpenFeatureClass("sde.DBO.Tester2Updater");

            //MessageBox.Show(inputClass.ShapeType.ToString());

            IVariantArray array = new VarArrayClass();
            array.Add(inputClass);
            array.Add(targetClass);
            array.Add("NO_TEST");

            //IVariantArray array = new VarArrayClass();
            //array.Add(@"D:\ProjectData\TestData2\test2.shp");
            //array.Add(@"D:\ProjectData\TestData1\test1.shp");
            //array.Add("NO_TEST");

            gp.Execute("Append_management", array, null);

            m_AOLicenseInitializer.ShutdownApplication();
        }
    }
}
0 Kudos
7 Replies
LukeTaylor1
Emerging Contributor
Had a typo in code displayed, but not the problem...

ESRI.ArcGIS.Geodatabase.IFeatureClass targetClass = (IFeatureClass)pFeatureWorkspace.OpenFeatureClass("sde.DBO.Tester1Main");
0 Kudos
MarcinDruzgala
Frequent Contributor
Ok, first of all use CODE TAGS when you copy/paste your code;)
Next thing I don't see anywhere that you are using this Append process:
// Get append tool
ESRI.ArcGIS.DataManagementTools.Append app = new ESRI.ArcGIS.DataManagementTools.Append();

and nothing more...
Here is the sample of using Append and Geoprocessor:
Append append = new Append();
append.inputs = inputClass; 
append.target  = targetClass ;
append.schema_type = "NO_TEST"

GP.Execute(append , null);

and the documentation you should read: click!

Cheers
MDruzgala
0 Kudos
LukeTaylor1
Emerging Contributor
Thanks for the reply and the help with posting protocol.  I've tried this a thousand different ways, so sorry if I posted some obviously messy code.  I was trying to use an IVariantArray in this situation as I used it to append shapefiles that were on my desktop successfully.  However, I really believe the problem lies in the sde feature classes I'm utilizing not being .NET wrapped (just a generic System._ComObject).  I know my sde connection is fine as I've used it for other matters.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geodatabase;

using ESRI.ArcGIS.Carto;

namespace T
{
    class Program
    {
        private static LicenseInitializer m_AOLicenseInitializer = new T.LicenseInitializer();
    
        [STAThread()]
        static void Main(string[] args)
        {
            //ESRI License Initializer generated code.
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeArcInfo },
            new esriLicenseExtensionCode[] { });

            //ESRI License Initializer generated code.
            //Do not make any call to ArcObjects after ShutDownApplication()

            // Get geoprocessor
            ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
            gp.AddOutputsToMap = false;

            // Get append tool
            ESRI.ArcGIS.DataManagementTools.Append app = new ESRI.ArcGIS.DataManagementTools.Append();

            IPropertySet propertySet = new PropertySetClass();
            propertySet.SetProperty("SERVER", "xxxxx");
            propertySet.SetProperty("INSTANCE", "xxxxx");
            propertySet.SetProperty("DATABASE", "xxxxx");
            propertySet.SetProperty("USER", "xxxxx");
            propertySet.SetProperty("PASSWORD", "xxxxx");
            propertySet.SetProperty("VERSION", "xxxxx");

            // Use activator
            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);

            IWorkspace workspace = (IWorkspace)workspaceFactory.Open(propertySet, 0);

            IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)workspace;

            ESRI.ArcGIS.Geodatabase.IFeatureClass inputClass = (IFeatureClass)pFeatureWorkspace.OpenFeatureClass("sde.DBO.Tester2Updater");
            ESRI.ArcGIS.Geodatabase.IFeatureClass targetClass = (IFeatureClass)pFeatureWorkspace.OpenFeatureClass("sde.DBO.Tester1Main");

            //IVariantArray array = new VarArrayClass();
            //array.Add(inputClass);
            //array.Add(targetClass);
            //array.Add("NO_TEST");

            app.inputs = inputClass;
            app.target = targetClass;
            app.schema_type = "NO_TEST";

            //This works for non-sde input (not worried about creating sde connection)
            //IVariantArray array = new VarArrayClass();
            //array.Add(@"D:\ProjectData\TestData2\test2.shp");
            //array.Add(@"D:\ProjectData\TestData1\test1.shp");
            //array.Add("NO_TEST");
            //gp.Execute("Append_managament", array, null);

            //gp.Execute("Append_management", array, null);
            gp.Execute(app, null);

            m_AOLicenseInitializer.ShutdownApplication();
        }
    }
}
0 Kudos
NeilClemmons
Honored Contributor
I would guess the problem is your program is ending before the geoprocessor has completed.  The call to Execute is asynchronous.  Your code will not stop at that point and wait for the process to finish.  Instead, it will continue on and in this case that's the end of the program.  You should be subscribing to the geoprocessor's events and waiting for it to finish before exiting your program.
0 Kudos
LukeTaylor1
Emerging Contributor
Thanks Neil for the suggestion.  I'll give it a shot.
0 Kudos
MarcinDruzgala
Frequent Contributor
Can you do something like this:

try
{
      gp.Execute(app, null);
}
catch(Exception exc)
{
      MessageBox.Show(exc.ToString());
}


And if there is no error try this:
gp.ToolExecuted += new EventHandler<ToolExecutedEventArgs>(geoprocessor_ToolExecuted);


private void geoprocessor_ToolExecuted(object sender, ToolExecutedEventArgs e)
{
       m_AOLicenseInitializer.ShutdownApplication();
}
0 Kudos
LukeTaylor1
Emerging Contributor
Thanks for all the help guys.  Installing Service Pack 3 fixed the problem.
0 Kudos