This document is archived and information here might be outdated.  Recommended version.


How to split a raster dataset into multiple tiles (ArcObjects .NET 10.5 SDK)

How to split a raster dataset into multiple tiles


Summary
Sometimes you might want to split a raster info small tiles. ArcGIS 10 provides a Split Raster Geoprocessing tool to achieve this purpose easily. This topic shows how to split a raster dataset into a set of smaller datasets with specified rows and columns using the ArcObjects application programming interface (API).

[C#]
public void CreateTilesFromRasterDataset(IRasterDataset rasterDataset, IWorkspace
    outputWorkspace, int tileWidth, int tileHeight)
{
    //Creates a set of raster datasets with specified rows and columns from the source dataset.
    //rasterDataset is the source raster dataset.
    //outputWorkspace is the output file raster workspace.
    //tileWidth is the number of columns of each tile.
    //tileHeight is the number of rows of each tile.

    //Get source raster properties.  Calculate output tile sizes
    IRasterProps rasterProps=(IRasterProps)rasterDataset.CreateDefaultRaster();
    double xTileSize=rasterProps.MeanCellSize().X * tileWidth;
    double yTileSize=rasterProps.MeanCellSize().Y * tileHeight;

    //Calculate the number of tiles needed for each direction.
    int xTileCount=(int)Math.Ceiling((double)rasterProps.Width / tileWidth);
    int yTileCount=(int)Math.Ceiling((double)rasterProps.Height / tileHeight);

    IEnvelope dsExtent=rasterProps.Extent;
    IEnvelope tileExtent=new EnvelopeClass();
    ISaveAs saveAs=null;

    //Create tiles with a specified tile size.
    for (int i=0; i < xTileCount; i++)
    {
        for (int j=0; j < yTileCount; j++)
        {
            rasterProps=(IRasterProps)rasterDataset.CreateDefaultRaster();

            //Recalculate the new extent.
            tileExtent.XMin=dsExtent.XMin + i * xTileSize;
            tileExtent.XMax=tileExtent.XMin + xTileSize;
            tileExtent.YMin=dsExtent.YMin + j * yTileSize;
            tileExtent.YMax=tileExtent.YMin + yTileSize;

            // Set origin/extent and size for this tile
            rasterProps.Height=tileHeight;
            rasterProps.Width=tileWidth;
            rasterProps.Extent=tileExtent;

            //Save the tile to the output workspace as a TIFF file. Can be other writable 
            //formats too.
            saveAs=(ISaveAs)rasterProps;
            saveAs.SaveAs("tile_" + i + "_" + j + ".tif", outputWorkspace, "TIFF");
        }
    }
}
[VB.NET]
Public Sub CreateTilesFromRasterDataset(ByVal rasterDataset As IRasterDataset, ByVal outputWorkspace As IWorkspace, ByVal tileWidth As Integer, ByVal tileHeight As Integer)
    'Creates a set of raster datasets with specified rows and columns from the source dataset.
    
    'rasterDataset is the source raster dataset.
    'outputWorkspace is the output file raster workspace.
    'tileWidth is the number of columns of each tile.
    'tileHeight is the number of rows of each tile.
    
    ' Get source raster properties.  Calculate output tile sizes
    Dim rasterProps As IRasterProps=CType(rasterDataset.CreateDefaultRaster(), IRasterProps)
    
    Dim xTileSize As Double=rasterProps.MeanCellSize().X * tileWidth;
    Dim yTileSize As Double=rasterProps.MeanCellSize().Y * tileHeight;
    
    ' Calculate the number of tiles needed for each direction.
    Dim xTileCount As Integer=(Integer)Math.Ceiling((Double)rasterProps.Width / tileWidth);
    Dim yTileCount As Integer=(Integer)Math.Ceiling((Double)rasterProps.Height / tileHeight);
    
    Dim dsExtent As IEnvelope=rasterProps.Extent
    Dim tileExtent As IEnvelope=New EnvelopeClass()
    Dim saveAs As ISaveAs=Nothing
    
    'Create tiles with specified tile size.
    Dim i As Integer
    Dim j As Integer
    For i=0 To xTileCount - 1
        For j=0 To yTileCount - 1
            
            rasterProps=CType(rasterDataset.CreateDefaultRaster(), IRasterProps)
            
            'Recalculate the new extent.
            tileExtent.XMin=dsExtent.XMin + i * xTileSize
            tileExtent.XMax=tileExtent.XMin + xTileSize
            tileExtent.YMin=dsExtent.YMin + j * yTileSize
            tileExtent.YMax=tileExtent.YMin + yTileSize
            
            ' Set size and extent for this tile
            rasterProps.Height=tileHeight
            rasterProps.Width=tileWidth
            rasterProps.Extent=tileExtent
            
            'Save the tile to the output workspace as a TIFF file. Can be other writable
            'formats too.
            saveAs=CType(rasterProps, ISaveAs)
            saveAs.SaveAs("tile_" + CStr(i) + "_" + CStr(j) + ".tif", outputWorkspace, "TIFF")
        Next
    Next
End Sub


See Also:

How to access a raster dataset




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced
Engine Developer Kit Engine