CreateRasterDataset problem

774
1
Jump to solution
06-30-2013 09:10 AM
hhjordan
New Contributor II
Hi All,

I met a problem when trying to create a raster file using VBA with ArcGIS 9.3. I was using the sample codes provided by ESRI (http://help.arcgis.com/en/sdk/10.0/vba_desktop/conceptualhelp/index.html#//00010000011s000000). However, the debugging stopped at the function CreateRasterDataset with a warning of "Run-time error '91': Object variable or With block variable not set". I was sure that every variables in this function was appropriately defined and set. Anyone have any clue about the error source? Thanks in advance.

This sample shows how to create a file raster dataset, set NoData and write pixel values.
How to use
Reference libraries: ESRI.ArcGIS.DataSourcesRaster, ESRI.ArcGIS.Geodatabase, ESRI.ArcGIS.Geometry.
Call this function from your code.
[VBA]
Sub CreateFileRasterDataset()
   
    'open a raster workspace
    Dim pWs As IRasterWorkspace2
    Set pWs = OpenRasterWorkspace("c:\temp")
   
    'set the origin, width and height of the raster dataset
    Dim pOrigin As IPoint
    Dim Width As Long
    Dim Height As Long
   
    Set pOrigin = New Point
    pOrigin.PutCoords 15, 15
    Width = 100
    Height = 100
   
    'Create the raster dataset
    Dim pRasterDs As IRasterDataset
    Set pRasterDs = pWs.CreateRasterDataset("myimage1.img", "IMAGINE Image", pOrigin, Width, Height, 30, 30, 3, PT_UCHAR, New UnknownCoordinateSystem, True)
   
    Dim pProp As IRasterProps
    Dim pBands As IRasterBandCollection
    Dim nBands As Integer
    Dim pBand As IRasterBand
   
    'Set NoData if necessary
    Set pBands = pRasterDs
    nBands = pBands.Count
    For k = 0 To nBands - 1
        Set pBand = pBands.Item(k)
        Set pProp = pBand
        pProp.NoDataValue = 255
    Next k
   
    'Create a Raster from the raster dataset
    Dim pRaster As IRaster
    Set pRaster = pRasterDs.CreateDefaultRaster
   
    'Create a pixel block for the pixels that need to write
    Dim pPB As IPixelBlock3
    Dim pPnt As IPnt
    Set pPnt = New Pnt
    pPnt.SetCoords Width, Height
    Set pPB = pRaster.CreatePixelBlock(pPnt)
   
    Dim v As Variant
    For k = 0 To nBands - 1
        v = pPB.PixelData(k)
        For i = 0 To Width - 1
            For j = 0 To Height - 1
                If i = j Then v(i, j) = 255 Else v(i, j) = (i + j) / 8 'set some values that are in 8 bit range
            Next j
        Next i
        pPB.PixelData(k) = v
        Set v = Nothing
    Next k
   
    'Write the pixel block with a (0,0) offset of the upper left corner
    Dim pRasterEdit As IRasterEdit
    Set pRasterEdit = pRaster
    pPnt.SetCoords 0, 0
    pRasterEdit.Write pPnt, pPB
   
    'clean up
    Set pRasterEdit = Nothing
    Set pRaster = Nothing
    Set pProp = Nothing
    Set pPB = Nothing
    Set pBand = Nothing
    Set pBands = Nothing
    Set pRasterDs = Nothing
    Set pWs = Nothing
End Sub


Public Function OpenRasterWorkspace(sPath As String) As IRasterWorkspace2
    ' Create RasterWorkspace
   
    Dim pWsFact As IWorkspaceFactory
    Set pWsFact = New RasterWorkspaceFactory
   
    If pWsFact.IsWorkspace(sPath) Then
        Set OpenRasterWorkspace = pWsFact.OpenFromFile(sPath, 0)
    End If
   
    Set pWsFact = Nothing
   
End Function
0 Kudos
1 Solution

Accepted Solutions
hhjordan
New Contributor II
Problem solved! I found the reason is that if the image file ("myimage1.img") already exist in the workspace folder, then the warning message will appear. If the file is deleted then everything works fine. Thanks for your attention.





Hi All,

I met a problem when trying to create a raster file using VBA with ArcGIS 9.3. I was using the sample codes provided by ESRI (http://help.arcgis.com/en/sdk/10.0/vba_desktop/conceptualhelp/index.html#//00010000011s000000). However, the debugging stopped at the function CreateRasterDataset with a warning of "Run-time error '91': Object variable or With block variable not set". I was sure that every variables in this function was appropriately defined and set. Anyone have any clue about the error source? Thanks in advance.

This sample shows how to create a file raster dataset, set NoData and write pixel values.
How to use
Reference libraries: ESRI.ArcGIS.DataSourcesRaster, ESRI.ArcGIS.Geodatabase, ESRI.ArcGIS.Geometry.
Call this function from your code.
[VBA]
Sub CreateFileRasterDataset()
   
    'open a raster workspace
    Dim pWs As IRasterWorkspace2
    Set pWs = OpenRasterWorkspace("c:\temp")
   
    'set the origin, width and height of the raster dataset
    Dim pOrigin As IPoint
    Dim Width As Long
    Dim Height As Long
   
    Set pOrigin = New Point
    pOrigin.PutCoords 15, 15
    Width = 100
    Height = 100
   
    'Create the raster dataset
    Dim pRasterDs As IRasterDataset
    Set pRasterDs = pWs.CreateRasterDataset("myimage1.img", "IMAGINE Image", pOrigin, Width, Height, 30, 30, 3, PT_UCHAR, New UnknownCoordinateSystem, True)
   
    Dim pProp As IRasterProps
    Dim pBands As IRasterBandCollection
    Dim nBands As Integer
    Dim pBand As IRasterBand
   
    'Set NoData if necessary
    Set pBands = pRasterDs
    nBands = pBands.Count
    For k = 0 To nBands - 1
        Set pBand = pBands.Item(k)
        Set pProp = pBand
        pProp.NoDataValue = 255
    Next k
   
    'Create a Raster from the raster dataset
    Dim pRaster As IRaster
    Set pRaster = pRasterDs.CreateDefaultRaster
   
    'Create a pixel block for the pixels that need to write
    Dim pPB As IPixelBlock3
    Dim pPnt As IPnt
    Set pPnt = New Pnt
    pPnt.SetCoords Width, Height
    Set pPB = pRaster.CreatePixelBlock(pPnt)
   
    Dim v As Variant
    For k = 0 To nBands - 1
        v = pPB.PixelData(k)
        For i = 0 To Width - 1
            For j = 0 To Height - 1
                If i = j Then v(i, j) = 255 Else v(i, j) = (i + j) / 8 'set some values that are in 8 bit range
            Next j
        Next i
        pPB.PixelData(k) = v
        Set v = Nothing
    Next k
   
    'Write the pixel block with a (0,0) offset of the upper left corner
    Dim pRasterEdit As IRasterEdit
    Set pRasterEdit = pRaster
    pPnt.SetCoords 0, 0
    pRasterEdit.Write pPnt, pPB
   
    'clean up
    Set pRasterEdit = Nothing
    Set pRaster = Nothing
    Set pProp = Nothing
    Set pPB = Nothing
    Set pBand = Nothing
    Set pBands = Nothing
    Set pRasterDs = Nothing
    Set pWs = Nothing
End Sub


Public Function OpenRasterWorkspace(sPath As String) As IRasterWorkspace2
    ' Create RasterWorkspace
   
    Dim pWsFact As IWorkspaceFactory
    Set pWsFact = New RasterWorkspaceFactory
   
    If pWsFact.IsWorkspace(sPath) Then
        Set OpenRasterWorkspace = pWsFact.OpenFromFile(sPath, 0)
    End If
   
    Set pWsFact = Nothing
   
End Function

View solution in original post

0 Kudos
1 Reply
hhjordan
New Contributor II
Problem solved! I found the reason is that if the image file ("myimage1.img") already exist in the workspace folder, then the warning message will appear. If the file is deleted then everything works fine. Thanks for your attention.





Hi All,

I met a problem when trying to create a raster file using VBA with ArcGIS 9.3. I was using the sample codes provided by ESRI (http://help.arcgis.com/en/sdk/10.0/vba_desktop/conceptualhelp/index.html#//00010000011s000000). However, the debugging stopped at the function CreateRasterDataset with a warning of "Run-time error '91': Object variable or With block variable not set". I was sure that every variables in this function was appropriately defined and set. Anyone have any clue about the error source? Thanks in advance.

This sample shows how to create a file raster dataset, set NoData and write pixel values.
How to use
Reference libraries: ESRI.ArcGIS.DataSourcesRaster, ESRI.ArcGIS.Geodatabase, ESRI.ArcGIS.Geometry.
Call this function from your code.
[VBA]
Sub CreateFileRasterDataset()
   
    'open a raster workspace
    Dim pWs As IRasterWorkspace2
    Set pWs = OpenRasterWorkspace("c:\temp")
   
    'set the origin, width and height of the raster dataset
    Dim pOrigin As IPoint
    Dim Width As Long
    Dim Height As Long
   
    Set pOrigin = New Point
    pOrigin.PutCoords 15, 15
    Width = 100
    Height = 100
   
    'Create the raster dataset
    Dim pRasterDs As IRasterDataset
    Set pRasterDs = pWs.CreateRasterDataset("myimage1.img", "IMAGINE Image", pOrigin, Width, Height, 30, 30, 3, PT_UCHAR, New UnknownCoordinateSystem, True)
   
    Dim pProp As IRasterProps
    Dim pBands As IRasterBandCollection
    Dim nBands As Integer
    Dim pBand As IRasterBand
   
    'Set NoData if necessary
    Set pBands = pRasterDs
    nBands = pBands.Count
    For k = 0 To nBands - 1
        Set pBand = pBands.Item(k)
        Set pProp = pBand
        pProp.NoDataValue = 255
    Next k
   
    'Create a Raster from the raster dataset
    Dim pRaster As IRaster
    Set pRaster = pRasterDs.CreateDefaultRaster
   
    'Create a pixel block for the pixels that need to write
    Dim pPB As IPixelBlock3
    Dim pPnt As IPnt
    Set pPnt = New Pnt
    pPnt.SetCoords Width, Height
    Set pPB = pRaster.CreatePixelBlock(pPnt)
   
    Dim v As Variant
    For k = 0 To nBands - 1
        v = pPB.PixelData(k)
        For i = 0 To Width - 1
            For j = 0 To Height - 1
                If i = j Then v(i, j) = 255 Else v(i, j) = (i + j) / 8 'set some values that are in 8 bit range
            Next j
        Next i
        pPB.PixelData(k) = v
        Set v = Nothing
    Next k
   
    'Write the pixel block with a (0,0) offset of the upper left corner
    Dim pRasterEdit As IRasterEdit
    Set pRasterEdit = pRaster
    pPnt.SetCoords 0, 0
    pRasterEdit.Write pPnt, pPB
   
    'clean up
    Set pRasterEdit = Nothing
    Set pRaster = Nothing
    Set pProp = Nothing
    Set pPB = Nothing
    Set pBand = Nothing
    Set pBands = Nothing
    Set pRasterDs = Nothing
    Set pWs = Nothing
End Sub


Public Function OpenRasterWorkspace(sPath As String) As IRasterWorkspace2
    ' Create RasterWorkspace
   
    Dim pWsFact As IWorkspaceFactory
    Set pWsFact = New RasterWorkspaceFactory
   
    If pWsFact.IsWorkspace(sPath) Then
        Set OpenRasterWorkspace = pWsFact.OpenFromFile(sPath, 0)
    End If
   
    Set pWsFact = Nothing
   
End Function
0 Kudos