| 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
|