Change Raster's Properties


This sample shows how to get a new raster by changing raster's property.

How to use:
  1. Call this procedure from VB or VBA.
Sub ChangeRasterProperty(sPath As String, sInName As String, sOutName As String, PCS As Long)
     ' sPath: path for input and output
    ' sInName: name of input raster
    ' sOutName: name of output raster
    ' PCS: code for Projected Coordinate System, like 54008 is Sinusodial
    ' Create RasterWorkspace
     Dim pRWS As IRasterWorkspace
    Dim pWSF As IWorkspaceFactory
    Set pWSF = New RasterWorkspaceFactory
    If Not pWSF.IsWorkspace(sPath) Then Exit Sub
    Set pRWS = pWSF.OpenFromFile(sPath, 0)

     ' Open RasterDataset and get default raster
     Dim pRaster As IRaster
    Set pRaster = pRWS.OpenRasterDataset(sInName).CreateDefaultRaster
     ' Define Spatial Reference
     Dim pPrj As ISpatialReference
    Dim pSRS As ISpatialReferenceFactory2
    Set pSRS = New SpatialReferenceEnvironment
    Set pPrj = pSRS.CreateProjectedCoordinateSystem(PCS)
     ' Set raster property
     Dim pRasProp As IRasterProps
    Set pRasProp = pRaster
    pRasProp.Height = pRasProp.Height / 2
    pRasProp.Width = pRasProp.Width / 2
    pRasProp.SpatialReference = pPrj

     ' SaveAs the projected raster
     Dim pDS As IDataset
    Dim pRasBandCol As IRasterBandCollection
    Set pRasBandCol = pRaster
    Set pDS = pRasBandCol.SaveAs(sOutName, pRWS, "GRID")

     'Clean up
     Set pDS = Nothing
    Set pRasBandCol = Nothing
    Set pRasProp = Nothing
    Set pPrj = Nothing
    Set pSRS = Nothing
    Set pRaster = Nothing
    Set pRWS = Nothing
    Set pWSF = Nothing
End Sub