How to execute CopyRaster GP Tool by ArcObjects for an input raster in a file geodatabase?

388
1
04-22-2022 07:00 AM
FlorianWendland
New Contributor

Hi, 

I'm new here and not really familiar with ArcObjects, but unfortunately I have to do some bugfixes and improvements in an older ArcObjects Visual Basic AddIn for ArcMap. 

I try to execute GP Tool CopyRaster_Datamanagement. Input raster is a raster dataset in a file geodatabase, I have to convert it into a raster file (tif).

Unfortunately I only get error messages after running the tool, or even an access violation in protected memory.

 

This is my code:

public Sub CopyRaster(sInputRaster as string, sOutRaster, ...) 

Dim pTool As ESRI.ArcGIS.Geoprocessing.IGPTool
pTool = ClassGlobalBauleit.GetArcTool("CopyRaster_management")

Dim pParams As ESRI.ArcGIS.esriSystem.IArray
pParams = pTool.ParameterInfo

Dim pParameter As ESRI.ArcGIS.Geoprocessing.IGPParameter
Dim pParamEdit As ESRI.ArcGIS.Geoprocessing.IGPParameterEdit
Dim pDataType As ESRI.ArcGIS.Geodatabase.IGPDataType
Dim sValue As String

'First Parameter: Input-Raster in gdb
pParameter = pParams.Element(0) 'for example C:\data\mygeodatabase.gdb\rastername
pParamEdit = pParameter
pDataType = pParameter.DataType
sValue = sInputRaster
pParamEdit.Value = pDataType.CreateValue(sValue)

'Second Parameter Output raster
'for example  "C:\Data\outputraster"
'when using C:\Data\outputraster.tif, running the tool causes a System.AccessViolationException!
Dim pParameter1 As ESRI.ArcGIS.Geoprocessing.IGPParameter
Dim pParamEdit1 As ESRI.ArcGIS.Geoprocessing.IGPParameterEdit
Dim pDataType1 As ESRI.ArcGIS.Geodatabase.IGPDataType
Dim sValue1 As String
pParameter1 = pParams.Element(1)
pParamEdit1 = pParameter1
pDataType1 = pParameter1.DataType
sValue1 = sOutRaster
pParamEdit1.Value = pDataType1.CreateValue(sValue1)

'... more parameters that do not make a difference...

Dim pGPMessages As ESRI.ArcGIS.Geodatabase.IGPMessages
pGPMessages = New ESRI.ArcGIS.Geodatabase.GPMessages
strMessages = ""

pTool.Execute(pParams, Nothing, Nothing, pGPMessages)

'the tool does not copy the raster and the  GP-messages are like this (translated):
'"ERROR  622
ERROR 000622: Error  running (CopyRaster). Parameters  are invalid.
'ERROR : 732
ERROR 000732: Input raster dataset C:\data\mygeodatabase.gdb\rastername does not exist or is not supported

 

-> 
'When I use the Geoprocessing Tool Copy Raster via ArcMap Geoprocessing UI (with the same input parameter it works as expected. 

Any ideas how to execute  it successful using  arcobjects?

Greets and thanks in advance

Florian

0 Kudos
1 Reply
GKmieliauskas
Esri Regular Contributor

Hi,

I will not use Geoprocessing at all. Open raster and make SaveAs from ArcObjects. It is very simple. I have no possibility to check VB code, but it could be like this:

 

Dim oRaster As IRaster
Dim pRasterWSX As IRasterWorkspaceEx
Dim strWorkspaceLocation as String
Set strWorkspaceLocation = "C:\data\mygeodatabase.gdb"

Set pWSFact = New FileGDBWorkspaceFactory
If pWSFact.IsWorkspace(strWorkspaceLocation) Then
	Set pRasterWSX = pWSFact.OpenFromFile(strWorkspaceLocation, 0)
	Set rDataset = pRasterWSX.OpenRasterDataset("rastername")

	Set oRaster = rDataset.CreateDefaultRaster
	Set rDataset = Nothing
	Set pRasterWSX = Nothing

        Dim rasterBC As IRasterBandCollection
        Set rasterBC = oRaster

        Dim pWKSfactory As IWorkspaceFactory
        Set pWKSfactory = New RasterWorkspaceFactory
        Set pSaveWorkspace = pWKSfactory.OpenFromFile("C:\Data", 0)

        rasterBC.SaveAs "outputraster.tif", pSaveWorkspace, "TIFF"

        Set rasterBC = Nothing
        Set pWKSfactory = Nothing
        Set pSaveWorkspace = Nothing
End If
Set pWSFact = Nothing

 

0 Kudos