How to write data to a new raster dataset ?

Discussion created by nofarah on May 18, 2011
Latest reply on May 20, 2011 by EinBerliner

I'm trying to create a raster dataset and out some values in it but after the code
execution the data are not written to my created dataset. The folder that was created is deleted
at the end of the execution and only 'info' folder is left.

Here is the code I'm using:

VARIANT_BOOL isPermanent = TRUE;
// Float grid
enum rstPixelType outputDataType = PT_FLOAT;
VARTYPE variantType = VT_R4;
// create the grid raster dataset
long rows = 100;
long cols = 100;
long numberOfBands = 1;
wchar_t* gridType = L"GRID";
IRasterDatasetPtr ipRasterDataset = 0;
IWorkspacePtr ipWorkspace = 0;
wchar_t* directory = L"C:\\MyFolder\\";
IWorkspaceFactoryPtr ipRasterWorkspaceFactory(CLSID_RasterWorkspaceFactory);
HRESULT hr = ipRasterWorkspaceFactory->OpenFromFile(CComBSTR(directory), 0, &ipWorkspace);

// query the rasterworkspace2 inteface from the created workspace object
IRasterWorkspace2Ptr ipRasterWorkspace2(ipWorkspace);

// set up the coordinates of the top-lef point
IPointPtr ipOriginPoint(CLSID_Point);
hr = ipOriginPoint->put_X(0.0);
hr = ipOriginPoint->put_Y(0.0);
double cellSize = 1.0;
wchar_t* shortFileName = L"test";
// Create the dataset
hr = ipRasterWorkspace2->CreateRasterDataset(CComBSTR(shortFileName), CComBSTR(gridType),
  ipOriginPoint, cols, rows, cellSize, cellSize, numberOfBands,
  outputDataType, NULL, isPermanent, &ipRasterDataset);

// Query the band collection interface from the rasterdataset object
IRasterBandCollectionPtr ipRasterBandCollection(ipRasterDataset);
// Get the raster band interface
IRasterBandPtr ipRasterBand = 0;
int bandIndex = 0;
hr = ipRasterBandCollection->Item(bandIndex, &ipRasterBand);
// Get the RawPixels interface from the raster band object
IRawPixelsPtr ipRawPixels(ipRasterBand);
// Create pixel block to hold the block data (numberRowsPerBlock)
IPixelBlockPtr ipPixelBlock = 0;
IPntPtr ipSizePoint(CLSID_DblPnt);
hr = ipSizePoint->SetCoords(cols, rows);
hr = ipRawPixels->CreatePixelBlock(ipSizePoint, &ipPixelBlock);

// Read data (is needed realy for an emoty dataset ?)
IPntPtr ipStartPoint(CLSID_DblPnt);
hr = ipStartPoint->SetCoords(0, 0);
hr = ipRawPixels->Read(ipStartPoint, ipPixelBlock);
VARIANT byteDataVariant;
IPixelBlock3Ptr ipBlock(ipPixelBlock);
hr = ipBlock->get_PixelDataByRef(bandIndex, &byteDataVariant);
BYTE* data = (BYTE*)((*byteDataVariant.pparray)->pvData);
// cast to float since data are float
float *floatData = (float *) data;
// put some value in the array
for (int rowIndex = 0; rowIndex < rows; rowIndex ++) {
  for (int colIndex = 0; colIndex < cols; colIndex ++) {
   long index = colIndex + rowIndex*colIndex;
   floatData[index] = index;
  } // colIndex
} // rowIndex

hr = ipBlock->put_PixelData(bandIndex, byteDataVariant);
// Write the pixel block to the band (rawpixels) at the start point
IUnknownPtr cache;
hr = ipRawPixels->AcquireCache(&cache);
hr = ipRawPixels->Write(ipStartPoint, ipPixelBlock);  
long numberRef = 0;
hr = ipRawPixels->ReturnCache(cache, &numberRef);

Is there is some thing wrong with the above code ? as at first a 'test' folder is created with an 'info' folder. But at the end of execution 'test' folder is gone.