How to create a raster dataset


Summary
This article explains how to create a raster dataset in a given workspace, populate pixel values using a pixel block, and set a NoData value of the raster dataset.

Creating a raster dataset

See the following code example:
[Java]
//This example creates a raster dataset with a specified dimension, populates pixel values, and set a NoData value.
static IRasterDataset createRasterDataset(String path, String fileName)throws
    Exception{
    //Create raster workspace. This example also works with any other workspaces that support raster data such as a file geodatabase (FGDB) workspace.
    //Access the workspace and the SDE workspace.
    IRasterWorkspace2 rasterWs = openRasterWorkspace(path);
    //Define the spatial reference of the raster dataset.
    ISpatialReference sr = new UnknownCoordinateSystem();
    //Define the origin for the raster dataset, which is the lower left corner of the raster.
    IPoint origin = new Point();
    origin.putCoords(15.0, 15.0);
    //Define the dimension of the raster dataset.
    int width = 100; //This is the width of the raster dataset.
    int height = 100; //This is the height of the raster dataset.
    double xCell = 30; //This is the cell size in x direction.
    double yCell = 30; //This is the cell size in y direction.
    int NumBand = 1; // This is the number of bands the raster dataset contains.
    //Create a raster dataset in grid format.
    IRasterDataset rasterDataset = rasterWs.createRasterDataset(fileName, "GRID",
        origin, width, height, xCell, yCell, NumBand, rstPixelType.PT_UCHAR, sr,
        true);
    //Get the raster band.
    IRasterBandCollection rasterBands = new IRasterBandCollectionProxy(rasterDataset)
        ;
    IRasterBand rasterBand;
    IRasterProps rasterProps;
    rasterBand = rasterBands.item(0);
    rasterProps = new IRasterPropsProxy(rasterBand);
    //Set NoData if necessary. For a multiband image, NoData value needs to be set for each band.
    rasterProps.setNoDataValue(255);
    //Create a raster from the dataset.
    IRaster raster = rasterDataset.createDefaultRaster();

    //Create a pixel block.
    IPnt blocksize = new Pnt();
    blocksize.setCoords(width, height);
    IPixelBlock3 pixelblock = new IPixelBlock3Proxy(raster.createPixelBlock
        (blocksize));

    //Populate some pixel values to the pixel block.
    byte[][] pixels = (byte[][])pixelblock.getPixelData(0);
    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++)
            if (i == j)
                pixels[i][j] = (byte)255;
            else
                pixels[i][j] = (byte)((i * j) % 255);

    pixelblock.setPixelData(0, pixels);

    //Define the location that the upper left corner of the pixel block is to write.
    IPnt upperLeft = new Pnt();
    upperLeft.setCoords(0, 0);

    //Write the pixel block.
    IRasterEdit rasterEdit = new IRasterEditProxy(raster);
    rasterEdit.write(upperLeft, new IPixelBlockProxy(pixelblock));

    //Release rasterEdit explicitly.
    Cleaner.release(rasterEdit);
    return rasterDataset;
}

static IRasterWorkspace2 openRasterWorkspace(String path)throws Exception{
    //This function opens a raster workspace.
    IWorkspaceFactory workspaceFact = new RasterWorkspaceFactory();
    return new IRasterWorkspace2Proxy(workspaceFact.openFromFile(path, 0));
}






Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine