English

Bug: Cannot create or modify raster pixel blocks by reference

Description

Pixel blocks must be populated by value and not by reference or the process will fail.

Cause

This is a known issue.

Workaround

The following steps outline how to create and populate a pixel block by value.

These steps provide correspond with the code sample 'Create File Raster Dataset', available in the Related Information section below.

  1. After creating a workspace, call the CreateRasterDataset method to create a RasterDataset:

    Code:
    IRasterDataset rasDS = rasWksp2.createRasterDataset(aFile, "GRID", point, 200, 100, 1, 1, 1, rstPixelType.PT_FLOAT, sr, true);


    Note:
    The RasterDataset object represents a dataset on disk or in a database. The dataset provides access only to operations that apply to the entire dataset, with the RasterBand providing access to operations that occur on individual bands.

  2. Cast the RasterDataset to RasterBandCollection to get a RasterBand:

    Code:
    IRasterBandCollection bands = new IRasterBandCollectionProxy(rasDS);
    IRasterBand band = bands.item(0);

  3. Create a PixelBlock using the RasterBand and populate it with the desired values. This is accomplished by getting a handle to the IRawPixels interface and calling the necessary methods on it as shown below:

    Code:

    IPnt size = new DblPnt();
    IRawPixels rawPix = new IRawPixelsProxy(band);
    IPixelBlock pixelBlk = rawPix.createPixelBlock(size);
    IPnt pnt = new DblPnt();
    pnt.setCoords(0, 0);
    IRasterProps rasProps = new IRasterPropsProxy(rawPix);
    size.setCoords(rasProps.getWidth(), rasProps.getHeight());

    /*read pixelblock*/
    rawPix.read(pnt, pixelBlk);
    IPixelBlock3 pixelBlock = new IPixelBlock3Proxy(pixelBlk);
    float[][] pixelData = (float[][])pixelBlock.getPixelData(0);

    /*loop through all the pixels and assign value*/
    for(int i = 0; i < (rasProps.getWidth() - 1); i++){
    for(int j = 0; j < (rasProps.getHeight() - 1); j++){
    pixelData[i][j] = (float)((i * j) % 255);
    }
    }

    pixelBlock.setPixelData(0,pixelData);

  4. Write the results to the IRawPixel:

    Code:
    IPixelBlock pblock = new IPixelBlockProxy(pixelBlock);
    rawPix.write(pnt, pblock);

  5. An extra step is necessary for Java developers. The handle to the raster is not garbage collected, therefore it corrupts the file created. These objects must be explicitly garbage collected by calling the following code:

    Code:
    com.linar.jintegra.Cleaner.release(rawPix);
    com.linar.jintegra.Cleaner.release(rasProps);
    com.linar.jintegra.Cleaner.release(band);
    com.linar.jintegra.Cleaner.release(bands);
    com.linar.jintegra.Cleaner.release(rasDS);
    return rasDS;

Related Information