cpicciri

Geoprocessor affecting subsequent scripts

Discussion created by cpicciri on Oct 11, 2012
Hi everyone,

The code below accepts a list of layers as input, appends them into a new layer and puts the output into a file geodatabase for further processing. The script that runs immediately after this script does a spatial query between the new, appended layer and another layer. All of this works fine but my problem is a performance issue. If I run the two scripts back to back as normally would be the case, it takes the query script 3 minutes to process 100 records. If I run the append script by itself, stop the code and then run the query script by itself, it takes less than 30 seconds to process the same 100 records which leads me to believe something is not right with the append script. I am using 100 records for ease of testing but in reality, the code will cycle through over 300,000+ records so performance is an issue.

I suspect there is a memory problem like something is not being released but I cannot figure it out. I've been trying different things from other forum post for almost 2 days now and am out of ideas. Can someone shed some light on what is going on or have a better way to append layers together?

This is a stand alone application written in C# using Visual Studio 2008, .NET 3.5 and ArcGIS 9.3.1.

public static void AppendAoiLayers(List<string> layerArray)
{
 IPropertySet pPropertySet = new PropertySetClass();
 IWorkspaceFactory pWorkspaceFactory = new SdeWorkspaceFactoryClass();
 IWorkspaceFactory pFileGDBWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
 IFields pFields = new FieldsClass();
 IFeatureWorkspace pFeatureWorkspace = null;
 IFeatureClass pOutFeatureClass = null;
 IFeatureLayer pFeatureLayer = new FeatureLayerClass();
 Append pAppend = new Append();
 ITrackCancel pTrackCancel = new CancelTrackerClass();
 Geoprocessor pGeoprocessor = new Geoprocessor();
 IWorkspaceFactory pGetFieldsWorkspaceFactory = new SdeWorkspaceFactoryClass();
 IFeatureWorkspace pGetFieldsFeatureWorkspace = null;
 IFeatureClass pGetFieldsFeatureClass = null;
 string AppendLayers = string.Empty;
 int arrayIndex = 0;
 string fieldsCollectionLayer = string.Empty;

 try
 {
  //Delete connection file if it exists.
  if (File.Exists(ClsENS.PATH_Scratch_Local + "grespsde.sde"))
   File.Delete(ClsENS.PATH_Scratch_Local + "grespsde.sde");

  //Create connection file.
  pPropertySet = ClsENS.GetSdePropertySet("grespsde", null);
  pWorkspaceFactory.Create(ClsENS.PATH_Scratch_Local, "grespsde.sde", pPropertySet, 0);

  //Set inputs.
  foreach (String layerName in layerArray)
  {
   if (arrayIndex == layerArray.Count - 1)
   {
    AppendLayers += ClsENS.PATH_Scratch_Local + "grespsde.sde\\" + layerName;

    //Use last layer to get fields collection from.
    fieldsCollectionLayer = layerName;
   }
   else
   {
    AppendLayers += ClsENS.PATH_Scratch_Local + "grespsde.sde\\" + layerName + ";";
   }

   arrayIndex++;
  }

  pAppend.inputs = AppendLayers;

  //Set output.
  pAppend.target = ClsENS.PATH_Scratch_Local + "temp.gdb" + "\\" + "App_Merge";
  pAppend.schema_type = "NO_TEST";

  //Get fields collection from an input layer so it can be applied to the destination layer.
  pGetFieldsFeatureWorkspace = pGetFieldsWorkspaceFactory.OpenFromFile(ClsENS.PATH_Scratch_Local + "\\grespsde.sde", 0) as IFeatureWorkspace;
  pGetFieldsFeatureClass = pGetFieldsFeatureWorkspace.OpenFeatureClass(fieldsCollectionLayer);
  pFields = pGetFieldsFeatureClass.Fields;

  //Create destination layer.
  pFeatureWorkspace = pFileGDBWorkspaceFactory.OpenFromFile(ClsENS.PATH_Scratch_Local + "temp.gdb", 0) as IFeatureWorkspace;
  pOutFeatureClass = ClsENS.CreateFeatureClass(pFeatureWorkspace, "App_Merge", esriFeatureType.esriFTSimple, esriGeometryType.esriGeometryPolygon, pFields, null, null, null);
  pFeatureLayer.FeatureClass = pOutFeatureClass;

  pTrackCancel.CancelOnClick = false;
  pTrackCancel.CancelOnKeyPress = true;

  //Do Append.
  pGeoprocessor.SetEnvironmentValue("workspace", ClsENS.PATH_Scratch_Local + "temp.gdb");
  pGeoprocessor.Execute(pAppend, pTrackCancel);
 }
 catch (Exception ex)
 {
  //ClsENS.LogError(ex.StackTrace, ex.Message, "AppendFeatureClasses", "myApplications");
  MessageBox.Show(ex.StackTrace + "\r\n" + ex.Message);
 }
 finally
 {
  if (pWorkspaceFactory != null) { Marshal.ReleaseComObject(pWorkspaceFactory); pWorkspaceFactory = null; }
  if (pFileGDBWorkspaceFactory != null) { Marshal.ReleaseComObject(pFileGDBWorkspaceFactory); pFileGDBWorkspaceFactory = null; }
  if (pPropertySet != null) { Marshal.ReleaseComObject(pPropertySet); pPropertySet = null; }
  if (pFields != null) { Marshal.ReleaseComObject(pFields); pFields = null; }
  if (pFeatureWorkspace != null) { Marshal.ReleaseComObject(pFeatureWorkspace); pFeatureWorkspace = null; }
  if (pOutFeatureClass != null) { Marshal.ReleaseComObject(pOutFeatureClass); pOutFeatureClass = null; }
  if (pFeatureLayer != null) { Marshal.ReleaseComObject(pFeatureLayer); pFeatureLayer = null; }
  if (pTrackCancel != null) { Marshal.ReleaseComObject(pTrackCancel); pTrackCancel = null; }
  if (pGetFieldsWorkspaceFactory != null) { Marshal.ReleaseComObject(pGetFieldsWorkspaceFactory); pGetFieldsWorkspaceFactory = null; }
  if (pGetFieldsFeatureWorkspace != null) { Marshal.ReleaseComObject(pGetFieldsFeatureWorkspace); pGetFieldsFeatureWorkspace = null; }
  if (pGetFieldsFeatureClass != null) { Marshal.ReleaseComObject(pGetFieldsFeatureClass); pGetFieldsFeatureClass = null; }
  if (pGeoprocessor != null) { pGeoprocessor = null; }
  if (pAppend != null) { pAppend = null; }
 }
}


Thanks for your time and assistance!
Carlos

Outcomes