You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects General forum

deleting a raster and reusing its name   Valentina Boycheva Feb 07, 2006
Re: deleting a raster and reusing its name   David Gobbett Feb 15, 2006
Re: deleting a raster and reusing its name   Valentina Boycheva May 24, 2006
Re: deleting a raster and reusing its name   Jay Simpson Jun 12, 2006
Re: deleting a raster and reusing its name   Valentina Boycheva Jun 12, 2006
Re: deleting a raster and reusing its name   Asma Ali Jun 19, 2006
Re: deleting a raster and reusing its name   Vishesh Maskey Jun 20, 2006
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject deleting a raster and reusing its name 
Author Valentina Boycheva 
Date Feb 07, 2006 
Message Hi listers,

It appears that there is no reliable way to create with ArcObjects a raster, delete it and reuse its name. I have browsed the forums and came up with 3 different ways, none of which works. In all 3 cases the raster is created through esriGeoprocessing.GpDispatch.

Here's a detailed report - in the hopes that someone from Raster Team can jump in and help.

1. Delete the raster through esriGeoprocessing.GpDispatch.

The program crashes with an automation error. I suspect the error is due to the existence of the .aux file. If I end the program and attempt to delete the raster through ArcCatalog, I get another error "Failed to delete selected object(s)". If I try to delete through ArcToolbox, I see a yellow exclamation point which states that there are multiple objects in the same path. Then ArcToolbox finishes without crashing and apparently deletes the grid. However, if I reuse the grid name, I get an automation error when I try to create the raster. I have also noticed that the arc.dir file in the info folder still has an entry for the raster bnd file:

RASTER3.BND ARC0000

Deleting the arc.dir file does't have any effect and I get again the automation error when I try to create the raster.

2. Delete the raster as a dataset.

The Dataset.CanDelete property is always false and therefore the raster is not deleted.

3. Delete the raster through Catalog objects. The results are same as with ArcToolbox. Raster gets deleted but name can't be reused.

By the way, I tried to delete the .aux file in Windows Explorer but Explorer says "Cannot delete raster3: It is being used by another person or program."

All the files are local to my drive and all the folders have read and write permissions. ArcCatalog is not open while the program runs. If I exit from ArcMap and rerun my program, the lock has disappeared and the raster is deleted successfully, but only this one time.

I hate to create rasters with random names and clutter the disk. Can anyone help?
 
 
Option Explicit
Sub test()

Dim sRasterPath As String, sRasterName As String
    sRasterPath = "F:\temp"
    sRasterName = "raster3"
Dim pDoc As IMxDocument
    Set pDoc = ThisDocument
Dim pMap As IMap
    Set pMap = pDoc.FocusMap
Dim pFL As IFeatureLayer
    Set pFL = pDoc.SelectedLayer
If pFL Is Nothing Then
    MsgBox "No selected layer"
    Exit Sub
ElseIf pFL.FeatureClass.ShapeType <> esriGeometryPolygon Then
    MsgBox "Select a polygon layer"
    Exit Sub
End If

    
Create_raster pFL, sRasterPath, sRasterName

Dim i As Integer, pLayer As ILayer, pRLayer As IRasterLayer
    For i = 0 To pMap.LayerCount - 1
        Set pLayer = pMap.Layer(i)
        If TypeOf pLayer Is IRasterLayer And pLayer.name = sRasterName Then
            pMap.DeleteLayer pLayer
            Exit For
        End If
    Next
    
' attempt 1: delete with geoprocessor
 Delete_raster_GP sRasterPath, sRasterName

' attempt 2: delete as dataset
'Delete_raster_DS sRasterPath, sRasterName

' attempt 3: delete through Catalog
'Delete_raster_Catalog sRasterPath, sRasterName

Set pDoc = Nothing: Set pMap = Nothing: Set pFL = Nothing
Set pLayer = Nothing: Set pRLayer = Nothing

End Sub

Sub Create_raster(pFLayer As IFeatureLayer, sPath As String, sName As String)

On Error GoTo ErrH

Dim GP As Object
    Set GP = CreateObject("esriGeoprocessing.GpDispatch.1")
    GP.OverwriteOutput = 1
    Dim sTBoxPath As String
' get the installation path for ArcGIS
    sTBoxPath = GetArcGIS_InstalationPath & "ArcToolbox\Toolboxes\Conversion Tools.tbx"
    GP.Toolbox = sTBoxPath
    If GP Is Nothing Then
        MsgBox "Toolbox " & sTBoxPath & " not found!" & sTBoxPath
        Exit Sub
    End If

Dim sOutRasterName As String
    sOutRasterName = sPath & "\" & sName
    sOutRasterName = Replace(sOutRasterName, "\", "\\", 1, , vbTextCompare)
  ' if using variables, make sure the backslashes are replaced by double backslashes or single slashes
  
  ' the grid field is taken from the layer's field "SHEET"
    GP.FeatureToRaster pFLayer.Name, "SHEET", sOutRasterName
    MsgBox "Raster created"
    
GoTo CleanUp

ErrH:
    MsgBox "Error in Create_raster" & vbNewLine & Err.Description

CleanUp:
    On Error Resume Next
    Set GP = Nothing

End Sub

Sub Delete_raster_GP(sPath As String, sName As String)

On Error GoTo ErrH

Dim GP As Object
    Set GP = CreateObject("esriGeoprocessing.GpDispatch.1")
    GP.OverwriteOutput = 1
    Dim sTBoxPath As String
' get the installation path for ArcGIS
    sTBoxPath = GetArcGIS_InstalationPath & "ArcToolbox\Toolboxes\Data Management Tools.tbx"
    GP.Toolbox = sTBoxPath
    If GP Is Nothing Then
        MsgBox "Toolbox " & sTBoxPath & " not found!" & sTBoxPath
        Exit Sub
    End If

Dim sOutRasterName As String
    sOutRasterName = sPath & "\" & sName
    sOutRasterName = Replace(sOutRasterName, "\", "\\", 1, , vbTextCompare)
    GP.Delete sOutRasterName
    MsgBox "Raster deleted"
    
GoTo CleanUp

ErrH:
    MsgBox "Error in Delete_raster_GP" & vbNewLine & Err.Description

CleanUp:
    On Error Resume Next
    Set GP = Nothing

End Sub


Sub Delete_raster_Catalog(sPath As String, sName As String)
'adapted from http://forums.esri.com/Thread.asp?c=93&f=992&t=138889#403307

On Error GoTo ErrH

Dim pGxCat As IGxCatalog
    Set pGxCat = New GxCatalog
    
Dim v As Variant, lFound As Long
    On Error Resume Next
    Set v = pGxCat.GetObjectFromFullName(sPath & "\" & sName, lFound)
    pGxCat.Close
    
    If VarType(v) = vbDataObject Then
        If TypeOf v Is IEnumGxObject Then
        
            Dim pEnumGX  As IEnumGxObject
            Set pEnumGX = New GxObjectArray
            Set pEnumGX = v
            Dim pGxObject As IGxObject
            
            
            Set pGxObject = pEnumGX.Next
            Do While Not pGxObject Is Nothing
                Dim pGxObjEdit As IGxObjectEdit
                Set pGxObjEdit = pGxObject
                If pGxObjEdit.CanDelete Then
                    pGxObjEdit.Delete
                    MsgBox "raster deleted"
                End If
                Set pGxObject = pEnumGX.Next
            Loop
        End If
    Else
        MsgBox "nothing found"
    End If

    
GoTo CleanUp

ErrH:
    MsgBox "Error in Delete_raster_Catalog" & vbNewLine & Err.Description

CleanUp:
    On Error Resume Next
    Set pGxCat = Nothing: Set v = Nothing:
    Set pEnumGX = Nothing: Set pGxObject = Nothing
End Sub

Sub Delete_raster_DS(sPath As String, sName As String)

Dim pWorkspaceFactory As IWorkspaceFactory
Dim pWorkspace As IWorkspace
Dim pDS As IDataset
Dim pEnumDS As IEnumDataset

    Set pWorkspaceFactory = New esriDataSourcesRaster.RasterWorkspaceFactory
    Set pWorkspace = pWorkspaceFactory.OpenFromFile(sPath, 0)
    Set pEnumDS = pWorkspace.Datasets(esriDTRasterDataset)
    pEnumDS.Reset
Dim sDSFullName As String
    Set pDS = pEnumDS.Next
    Do While Not pDS Is Nothing
        If pDS.name = sName Then
            sDSFullName = sPath & "\" & sName
            If pDS.CanDelete Then
                pDS.Delete
                Exit Do
            Else
                MsgBox "Cannot delete raster"
                Exit Do
            End If
        End If
        Set pDS = pEnumDS.Next
    Loop
    
GoTo CleanUp

ErrH:
    MsgBox "Error in Delete_raster_DS" & vbNewLine & Err.Description

CleanUp:
    On Error Resume Next
    Set pWorkspaceFactory = Nothing: Set pWorkspace = Nothing
    Set pDS = Nothing: Set pEnumDS = Nothing

End Sub

Function GetArcGIS_InstalationPath() As String

On Error GoTo EH
Dim WScr
    Set WScr = CreateObject("WScript.Shell")
Dim sLoc As String
  
    sLoc = WScr.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\ESRI\ArcGIS\InstallDir")
    GetArcGIS_InstalationPath = sLoc
    GoTo CleanUp

EH:
    MsgBox "Error"
CleanUp:
    Set WScr = Nothing

End Function

 
  Valentina Boycheva
Senior GIS Programmer/Analyst
http://www.jonesedmunds.com
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: deleting a raster and reusing its name 
Author David Gobbett 
Date Feb 15, 2006 
Message Hi All,

I too have spent considerable time over the last few days resolving raster deletion issues. I am using ArcMap 9.1, and Visual Studio 2003 to develop VB.Net ArcMap extensions.

My two main conclusions (and I would be glad to hear other views or experiences on this):
(1) Sometimes the IDataset.CanDelete call returns an incorrect value. This is illustrated by the following Code snippet 1 below which at times displays the message indicatating that .CanDelete returned false, but .Delete succeeded regardless.

(2) Using the .Net platform (and most likely others) you need to be sure to release all
references to the RasterDataset and RasterWorkspace by setting variables to nothing,
and ensuring references are released by calling the .Net Garbage collection. See code snippet 2 below.

David Gobbett
Technical Officer - GIS Programming
Precision Viticulture
CSIRO Sustainable Ecosystems 
 
' ---------------------------------
' Code snippet 1
Dim pRDS As IRasterDataset
Dim pDataset As IDataset

pRDS = pRWS.OpenRasterDataset(pRasterName)
pDataset = CType(pRDS, IDataset)

Dim expectedDelete As Boolean = pDataset.CanDelete
Try
  pDataset.Delete()
  If Not expectedDelete Then
    MsgBox("Delete not expected but succeeded: " + pRDS.CompleteName)
  End If
Catch ex As Exception
  If expectedDelete Then
    MsgBox("Delete expected but failed: " + pRDS.CompleteName)
  Else
    MsgBox("Delete not expected and failed: " + pRDS.CompleteName)
  End If
End Try

' ---------------------------------
' Code snippet 2

' Run garbage collection
TempRaster = Nothing
System.GC.Collect()

' Now it should be OK to delete the raster
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: deleting a raster and reusing its name 
Author Valentina Boycheva 
Date May 24, 2006 
Message David,

You are right. CanDelete returns false but if I don't check for this property, the deleting completes successfully. Note also that pDS.name returns always lower case (another bug!) so I had to make the comparison in upper case. Here's the final code for the "Delete as dataset" method:

 
 
Sub Delete_raster_DS(sPath As String, sName As String)

Dim pWorkspaceFactory As IWorkspaceFactory
Dim pWorkspace As IWorkspace
Dim pDS As IDataset
Dim pEnumDS As IEnumDataset

    Set pWorkspaceFactory = New esriDataSourcesRaster.RasterWorkspaceFactory
    Set pWorkspace = pWorkspaceFactory.OpenFromFile(sPath, 0)
    Set pEnumDS = pWorkspace.Datasets(esriDTRasterDataset)
    pEnumDS.Reset
Dim sDSFullName As String
    Set pDS = pEnumDS.Next
    Do While Not pDS Is Nothing
        If UCase(pDS.name) = UCase(sName) Then
            pDS.Delete
        End If
        Set pDS = pEnumDS.Next
    Loop
    
GoTo CleanUp

ErrH:
    MsgBox "Error in Delete_raster_DS" & vbNewLine & Err.Description

CleanUp:
    On Error Resume Next
    Set pWorkspaceFactory = Nothing: Set pWorkspace = Nothing
    Set pDS = Nothing: Set pEnumDS = Nothing

End Sub
 
  Valentina Boycheva
Senior GIS Programmer/Analyst
http://www.jonesedmunds.com
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: deleting a raster and reusing its name 
Author Jay Simpson 
Date Jun 12, 2006 
Message Hi Valentina,

I tried to run your code sample to delete the raster folder contaning ESRI grid. It gives me an Automation error at the pDS.Delete line. I have already deleted the group layer containing raster layer from the table of contents.

Appreciate if you could give some feedback on what I am missing here.

Thanks

Jay S.
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: deleting a raster and reusing its name 
Author Valentina Boycheva 
Date Jun 12, 2006 
Message please ignore - need to do some testing 
  Valentina Boycheva
Senior GIS Programmer/Analyst
http://www.jonesedmunds.com
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: deleting a raster and reusing its name 
Author Asma Ali 
Date Jun 19, 2006 
Message Hi All,

For the past few days, I am trying to delete a raster dataset using 'pDS.Delete' method. But, I get an automation errot everytime. If someone on this forum has figured out how to delete an existing ratser dataset in ArcObject would please help me out.

Thanks in advance

Asma 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: deleting a raster and reusing its name 
Author Vishesh Maskey 
Date Jun 20, 2006 
Message Hi there,

I am having same issues deleting raster GRIDS. I am trying to remove all the raster GRIDS from a folder in the disk. I was able to delete all but the ones that were created in the current session (after removing the layer and resetting Operation Stack) using both FileSystemObject and Dataset.Delete.

Therefore, ArcMap seems to keep some kind of reference to the rasters other than operation stack. Since after restarting ArcMap those rasters can be deleted.

If anyone was successful, please help.

Thanks,
Vishesh