How to write an SDE raster from a pixel array


This sample shows how to write to an SDE raster dataset directly from a pixel array. This sample also applies to the FGDB case.

How to use

  1. Call the following subroutine from your code.
[VBA]
	Public Sub WriteToSDEFromPixelArray()
	    'Get an SDE workspace.
	    Dim pWs As IRasterWorkspaceEx
	    Set pWs = OpenArcSDEWorkspace("norachat", 9000, "", "raster", "go")

	    'Create an empty dataset, use default parameters, see sample "Create Geodatabase Raster Dataset " for information on how to set up the parameters.
	    Dim pRasterDs As IRasterDataset2
	    Set pRasterDs = pWs.CreateRasterDataset("data1", 3, PT_SHORT, New RasterStorageDef, "", New RasterDef, Nothing)

	    ' Create a raster.
	    Dim pRaster As IRaster
	    Dim pRasterProp As IRasterProps
	    Dim Col As Long, Row As Long, pExtent As IEnvelope
	    Set pRaster = pRasterDs.CreateFullRaster
	    Set pRasterProp = pRaster

	    'Define the dimension of the raster.
	    Col = 1000
	    Row = 1000
	    Set pExtent = New Envelope
	    pExtent.XMin = 100
	    pExtent.XMax = 500
	    pExtent.YMin = 100
	    pExtent.YMax = 500
	    pRasterProp.Extent = pExtent
	    pRasterProp.Width = 1000
	    pRasterProp.Height = 1000

	    'Create a pixel block.
	    Dim pPB As IPixelBlock3
	    Dim pSize As IPnt

	    Set pSize = New Pnt
	    pSize.SetCoords Col, Row
	    Set pPB = pRaster.CreatePixelBlock(pSize)

	    'Set the pixel values.
	    Dim pRasterEdit As IRasterEdit
	    Dim SafeArray As Variant

	    For k = 0 To 2 'It is a 3 band raster.
	        SafeArray = pPB.PixelDataByRef(k)
	        For I = 0 To Col - 1
	            For J = 0 To Row - 1
	                If I = J Then safeArray = 255 Else SafeArray(I, J) = (I + J) * (k + 1) ' or any pixel values
	            Next J
	        Next I
	        pPB.PixelData(k) = SafeArray
	    Next k

	    'Write NoData mask with pixel value of (255,255,255).
	    Dim NoData(2) As Integer
	    NoData(0) = 255
	    NoData(0) = 255
	    NoDatak(2) = 255
	    pPB.Mask NoData
	    'Or simply do pPB.Mask 255.

	    'Write.
	    pSize.SetCoords 0, 0 'Reuse this variable as the starting point.
	    Set pRasterEdit = pRaster
	    pRasterEdit.Write pSize, pPB

	    Set pRaster = Nothing
	    Set pRasterDs = Nothing
	    Set prasterprop = Nothing
	    Set saferray = Nothing

	    Set pRasterEdit = Nothing
	    Set pPB = Nothing
	    Set pWs = Nothing
	End Sub

	Public Function OpenArcSDEWorkspace(sServer As String, _
	                                    sInstance As String, _
	                                    sDatabase As String, _
	                                    sUser As String, _
	                                    sPassword As String, _
	                                    Optional sVersion As String = "sde.DEFAULT") As IRasterWorkspaceEx

	    ' Open a database workspace by specifying
	    ' sServer, sInstance, sDatabase, sUser, sPassword, sVersion are database connection info.

	    Dim pWsFact As IWorkspaceFactory
	    Dim pPropertySet As IPropertySet

	On Error GoTo erh:

	    'Open the ArcSDE workspace.
	    Set pPropertySet = New PropertySet
	    With pPropertySet
	        .SetProperty "server", sServer
	        .SetProperty "instance", sInstance
	        .SetProperty "database", sDatabase
	        .SetProperty "user", sUser
	        .SetProperty "password", sPassword
	        .SetProperty "version", sVersion
	    End With
	    Set pWsFact = New SdeWorkspaceFactory

	    Set OpenArcSDEWorkspace = pWsFact.Open(pPropertySet, 0)
	erh:
	    Set pWsFact = Nothing
	    Set pWs = Nothing
	    Set pRasterDataset = Nothing

	End Function