Deleting tables from the in_memory workspace

1339
6
05-21-2012 08:29 AM
BruceNielsen
Occasional Contributor III
Is it possible to delete tables from the in_memory workspace (ArcGIS 9.2, Visual Studio 2005) that are created as output from geoprocessing tools? They are causing problems if the code is run more than once. The Delete_management tool doesn't seem to work.

Optionally, can the entire workspace be deleted?
[ATTACH=CONFIG]14485[/ATTACH]
0 Kudos
6 Replies
DennisGeasan
Occasional Contributor II
Try setting the object to null. 

In C#:
object = null;

DG
0 Kudos
BruceNielsen
Occasional Contributor III
Try setting the object to null. 

In C#:
object = null;


How do I do that? The geoprocessing tools are creating the workspace automatically when I specify "in_memory" as the output location. I tried
Dim pWS2 As IWorkspaceFactory2 = New InMemoryWorkspaceFactory
Dim pWS As IWorkspace = pWS2.OpenFromString("GPInMemoryWorkspace", 0)
pWS = Nothing

but it failed to execute.
0 Kudos
KenBuja
MVP Esteemed Contributor
What are the Geoprocesses that you're using to create these in memory tables? You mentioned that you have a problem when you run the process more than one time. Can you set the OverwriteOutput property to true?

You can get the reference from an object created by a geoprocessing tool. Here's an example of creating a table and returning a reference for that table. This is from code I've written for ArcGIS 10, but hopefully the interfaces would be applicable for ArcGIS 9.2 (although you'd probably have to use previous versions, like IGeoProcessorResult instead of IGeoProcessorResult2)

   Friend Function CreateNewTable(ByVal TableLocation As String, ByVal TableName As String) As ESRI.ArcGIS.Geodatabase.ITable

        Dim NewTable As New ESRI.ArcGIS.DataManagementTools.CreateTable
        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
                releaser.ManageLifetime(NewTable)

                NewTable.out_path = TableLocation
                NewTable.out_name = TableName

                Result = RunTool(NewTable, Nothing)
                If Result Is Nothing Then Return Nothing

                Return ReturnObjectfromResult(Result, "Table")

            End Using
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace.ToString, "Create Table")
            Return Nothing
        End Try

    End Function

    Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel2, Optional ByVal AddOutput As Boolean = False) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor
            Dim GP As New ESRI.ArcGIS.Geoprocessor.Geoprocessor

            GP.AddOutputsToMap = AddOutput

            Result = CType(GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2)
            If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then ReturnMessages(Result, "Geoprocessing Error")
            GP.ClearMessages()
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Run Geoprocessor")
        End Try

        Return Result

    End Function

    Friend Function ReturnObjectfromResult(ByVal result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Type As String) As Object

        Dim GPVal As ESRI.ArcGIS.Geodatabase.IGPValue
        Dim InMemFC As String
        Dim GPUtil As ESRI.ArcGIS.Geoprocessing.IGPUtilities3 = New ESRI.ArcGIS.Geoprocessing.GPUtilities

        Try
            GPVal = result.GetOutput(0)
            InMemFC = GPVal.GetAsText()

            Select Case Type
                Case "Feature Class"
                    Return GPUtil.OpenFeatureClassFromString(InMemFC)
                Case "Table"
                    Return GPUtil.OpenTableFromString(InMemFC)
                Case "Feature Layer"
                    Return GPUtil.OpenFeatureLayerFromString(InMemFC)
                Case Else
                    Return Nothing
            End Select

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "ReturnObjectfromResult error")
            Return Nothing
        End Try

    End Function
0 Kudos
BruceNielsen
Occasional Contributor III
Here is the code that creates the in_memory workspace:
Dim pGP As IGeoProcessor = New GeoProcessor
Dim pParams As IVariantArray = New VarArrayClass
pGP.OverwriteOutput = True

Dim strMemSpace As String = "in_memory\"
Dim strTmpFields As String = "HELfields"
pParams.Add(pSelLayer) 'This is the selected polygon layer from the TOC that has been run through some validation code.
pParams.Add(strMemSpace + strTmpFields)
pGP.Execute("CopyFeatures_management", pParams, Nothing)
Debug.Print("Copied the land units")

After this I have about 3 pages of Clip, Dissolve, Statistics, TableToTable, MakeTableView, etc. The feature layers are deleted with IMap::\DeleteLayer. I figured out how to delete the TableViews with IGPUtilities::\Delete. That leaves three tables hanging around that I can't figure out how to remove.
0 Kudos
DuncanHornby
MVP Notable Contributor
Bruce,

Don't know if I am giving you a red herring here but the IGPUtilities Interface has some methods like ReleaseInternalTable and ReleaseInternals try calling these after you have deleted the in_memory workspace. Below is some code I just knocked together in VBA.

Duncan

Public Sub DeleteTable()
    ' Create Utils object
  Dim pGPUtils As IGPUtilities
    Set pGPUtils = New GPUtilities
    
   ' Create GPValue object and set path
  Dim pGPValue As IGPValue
    Set pGPValue = New DETable
    pGPValue.SetAsText ("in_memory\abc")
    
   ' Delete table
  pGPUtils.Delete pGPValue
    pGPUtils.ReleaseInternals
End Sub
0 Kudos
BruceNielsen
Occasional Contributor III
Thanks for the info Duncan. I will try it if the issue comes up again.

I ended up re-writing the code to eliminate creating the in_memory tables.
0 Kudos