In this topic
Creating a raster dataset
To create a tagged image file format (TIFF) file, perform the following steps:
-
Create a workspace.
-
Create a TIFF file with specified width, height, pixel type, cell size, and other necessary dimensions.
-
Use the IPixelBlock and IRasterEdit interfaces to edit the pixel values.
See the following code example:
[C#]
public static IRasterDataset CreateRasterDataset(string Path, string FileName)
{
try
{
IRasterWorkspace2 rasterWs = OpenRasterWorkspace(Path);
//Define the spatial reference of the raster dataset.
ISpatialReference sr = new UnknownCoordinateSystemClass();
//Define the origin for the raster dataset, which is the lower left corner of the raster.
IPoint origin = new PointClass();
origin.PutCoords(15.0, 15.0);
//Define the dimensions 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 TIFF format.
IRasterDataset rasterDataset = rasterWs.CreateRasterDataset(FileName, "TIFF",
origin, width, height, xCell, yCell, NumBand, rstPixelType.PT_UCHAR, sr,
true);
//If you need to set NoData for some of the pixels, you need to set it on band
//to get the raster band.
IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset;
IRasterBand rasterBand;
IRasterProps rasterProps;
rasterBand = rasterBands.Item(0);
rasterProps = (IRasterProps)rasterBand;
//Set NoData if necessary. For a multiband image, a NoData value needs to be set for each band.
rasterProps.NoDataValue = 255;
//Create a raster from the dataset.
IRaster raster = rasterDataset.CreateFullRaster();
//Create a pixel block using the weight and height of the raster dataset.
//If the raster dataset is large, a smaller pixel block should be used.
//Refer to the topic "How to access pixel data using a raster cursor".
IPnt blocksize = new PntClass();
blocksize.SetCoords(width, height);
IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize)as IPixelBlock3;
//Populate some pixel values to the pixel block.
System.Array pixels;
pixels = (System.Array)pixelblock.get_PixelData(0);
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
if (i == j)
pixels.SetValue(Convert.ToByte(255), i, j);
else
pixels.SetValue(Convert.ToByte((i * j) / 255), i, j);
pixelblock.set_PixelData(0, (System.Array)pixels);
//Define the location that the upper left corner of the pixel block is to write.
IPnt upperLeft = new PntClass();
upperLeft.SetCoords(0, 0);
//Write the pixel block.
IRasterEdit rasterEdit = (IRasterEdit)raster;
rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock);
//Release rasterEdit explicitly.
System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);
return rasterDataset;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return null;
}
}
public static IRasterWorkspace2 OpenRasterWorkspace(string PathName)
{
//This function opens a raster workspace.
try
{
IWorkspaceFactory workspaceFact = new RasterWorkspaceFactoryClass();
return workspaceFact.OpenFromFile(PathName, 0)as IRasterWorkspace2;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return null;
}
}
[VB.NET]
Public Function CreateRasterDataset(ByVal Path As String, ByVal FileName As String) As IRasterDataset
Try
Dim ws As IRasterWorkspace2
ws = OpenRasterworkspace(Path)
'Define the spatial reference of the raster dataset.
Dim sr As ISpatialReference
sr = New UnknownCoordinateSystem
'Define the origin for the raster dataset, which is the lower left corner of the raster.
Dim origin As IPoint
origin = New Point
origin.PutCoords(15.0, 15.0)
'Define the dimensions of the raster dataset.
Dim Width As Integer 'The width of the raster dataset.
Dim height As Integer 'The height of the raster dataset.
Dim xCell As Double 'The cell size in x direction.
Dim yCell As Double 'The cell size in y direction.
Dim NumBand As Integer 'The number of bands the raster dataset contains.
Width = 100
height = 100
xCell = 30
yCell = 30
NumBand = 1
'Create a raster dataset in grid format.
Dim rasterDS As IRasterDataset
rasterDS = ws.CreateRasterDataset(FileName, "GRID", origin, Width, height, xCell, yCell, NumBand, rstPixelType.PT_UCHAR, sr, True)
'Get the raster band.
Dim bands As IRasterBandCollection
Dim band As IRasterBand
Dim rasterProps As IRasterProps
bands = rasterDS
band = bands.Item(0)
rasterProps = band
'Set NoData if necessary. For a multiband image, a NoData value needs to be set for each band.
rasterProps.NoDataValue = 255
'Create a raster from the dataset.
Dim raster As IRaster
raster = rasterDS.CreateFullRaster
'Create a pixel block using the weight and height of the raster dataset.
'If the raster dataset is large, a smaller pixel block should be used.
'Refer to the topic "How to access pixel data using a raster cursor".
Dim pixelBlock As IPixelBlock3
Dim blockSize As IPnt
blockSize = New Pnt
blockSize.SetCoords(Width, height)
pixelBlock = raster.CreatePixelBlock(blockSize)
'Populate some pixel values to the pixel block.
Dim pixels As System.Array
Dim i, j As Integer
pixels = CType(pixelBlock.PixelData(0), System.Array)
For i = 0 To Width - 1
For j = 0 To height - 1
If i = j Then
pixels.SetValue(Convert.ToByte(255), i, j)
Else
pixels.SetValue(Convert.ToByte((i * j) / 255), i, j)
End If
Next j
Next i
pixelBlock.PixelData(0) = CType(pixels, System.Array)
'Define the location that the upper left corner of the pixel block is to write.
Dim upperLeft As IPnt
upperLeft = New Pnt
upperLeft.SetCoords(0, 0)
'Write the pixel block.
Dim rasterEdit As IRasterEdit
rasterEdit = raster
rasterEdit.Write(upperLeft, pixelBlock)
'Release rasterEdit explicitly.
System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);
Return rasterDS
Catch ex As Exception
System.Diagnostics.Trace.WriteLine(ex.Message)
Return Nothing
End Try
End Function
Public Function OpenRasterworkspace(ByVal Path As String) As IRasterWorkspace2
Try
Dim workspaceFact As IWorkspaceFactory
Dim rasterWS As IRasterWorkspace2
workspaceFact = New RasterWorkspaceFactory
rasterWS = workspaceFact.OpenFromFile(Path, 0)
Return rasterWS
Catch ex As Exception
System.Diagnostics.Trace.WriteLine(ex.Message)
Return Nothing
End Try
End Function
Creating a raster dataset in a geodatabase
To create a raster dataset in a geodatabase, perform the following steps:
-
Create a file geodatabase workspace factory.
-
Create a raster workspace and query IRasterWorkspaceEx.
-
Define the storage and raster properties using the RasterStorageDef and RasterDef classes.
-
Create the dataset using the CreateRasterDataset method.
See the following code example:
[C#]
IWorkspaceFactory wsf = new FileGDBWorkspaceFactoryClass();
IRasterWorkspaceEx ws = (IRasterWorkspaceEx)wsf.OpenFromFile(@"c:\temp\fgdb.gdb", 0);
//Define the raster storage.
IRasterStorageDef storage = new RasterStorageDefClass();
storage.Tiled = true;
storage.TileHeight = 128;
storage.TileWidth = 128;
//Define the spatial reference.
IRasterDef rasterDef = new RasterDefClass();
rasterDef.SpatialReference = new UnknownCoordinateSystemClass();
//Create data.
IRasterDataset rasterDs = ws.CreateRasterDataset("newraster", 3,
rstPixelType.PT_SHORT, storage, "sde.DEFAULT", rasterDef, null);
[VB.NET]
Dim wsf As IWorkspaceFactory = New FileGDBWorkspaceFactoryClass()
Dim ws As IRasterWorkspaceEx = CType (wsf.OpenFromFile (@"c:\temp\fgdb.gdb", 0), IRasterWorkspaceEx)
'Define the raster storage.
Dim storage As IRasterStorageDef = New RasterStorageDefClass()
storage.Tiled = True
storage.TileHeight = 128
storage.TileWidth = 128
'Define the spatial reference.
Dim rasterDef As IRasterDef = New RasterDefClass()
rasterDef.SpatialReference = New UnknownCoordinateSystemClass()
'Create data.
Dim rasterDs As IRasterDataset
rasterDs = ws.CreateRasterDataset("newraster", 3, rstPixelType.PT_SHORT, storage, "sde.DEFAULT", rasterDef, null)
See Also:
How to access pixel data using a raster cursorHow to access pixel data using the RawBlocks object
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):
ESRI.ArcGIS.System (ESRI.ArcGIS.esriSystem)ESRI.ArcGIS.Display ESRI.ArcGIS.Geodatabase ESRI.ArcGIS.DataSourcesRaster ESRI.ArcGIS.Geometry
Development licensing | Deployment licensing |
---|---|
ArcView | ArcView |
ArcEditor | ArcEditor |
ArcInfo | ArcInfo |
Engine Developer Kit | Engine Runtime |