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


Create Grid From Feature Class Snippet (ArcObjects .NET 10.5 SDK)
ArcObjects Library Reference

Create Grid From Feature Class Snippet

Create a raster GeoDataset grid from a FeatureClass.

[C#]
/// <summary>
/// Create a raster GeoDataset grid from a FeatureClass.
/// </summary>
/// <param name="feaureClass">An IFeatureClass interface that will have a raster (grid) created from it.</param>
/// <param name="string_RasterWorkspace">A System.String that is the workspace location for the grid coverage. Example: "C:\workspace"</param>
/// <param name="int32_NumberOfCells">A System.Int32 that is the number of cells in the X direction that you want the raster to have. This defines the resolution of the raster. Example: 10000</param>
/// <returns>A GeoDataset that is a raster of the input FeatureClass</returns>
/// <remarks></remarks>
public ESRI.ArcGIS.Geodatabase.IGeoDataset CreateGridFromFeatureClass(ESRI.ArcGIS.Geodatabase.IFeatureClass feaureClass, System.String string_RasterWorkspace, System.Int32 int32_NumberOfCells)
{

    ESRI.ArcGIS.Geodatabase.IGeoDataset geoDataset=(ESRI.ArcGIS.Geodatabase.IGeoDataset)feaureClass; // Explicit Cast

    ESRI.ArcGIS.Geometry.ISpatialReference spatialReference=geoDataset.SpatialReference;

    // Create a RasterMaker operator
    ESRI.ArcGIS.GeoAnalyst.IConversionOp conversionOp=new ESRI.ArcGIS.GeoAnalyst.RasterConversionOpClass();

    ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory=new ESRI.ArcGIS.DataSourcesRaster.RasterWorkspaceFactoryClass();

    // set output workspace
    ESRI.ArcGIS.Geodatabase.IWorkspace workspace=workspaceFactory.OpenFromFile(string_RasterWorkspace, 0);

    // Create analysis environment
    ESRI.ArcGIS.GeoAnalyst.IRasterAnalysisEnvironment rasterAnalysisEnvironment=(ESRI.ArcGIS.GeoAnalyst.IRasterAnalysisEnvironment)conversionOp; // Explicit Cast
    rasterAnalysisEnvironment.OutWorkspace=workspace;

    ESRI.ArcGIS.Geometry.IEnvelope envelope=new ESRI.ArcGIS.Geometry.EnvelopeClass();
    envelope=geoDataset.Extent;

    // Set cell size
    System.Double double_xMin=envelope.XMin;
    System.Double double_xMax=envelope.XMax;
    System.Double double_difference=double_xMax - double_xMin;
    System.Double double_cellSize=double_difference / int32_NumberOfCells;
    object object_cellSize=(System.Object)double_cellSize;
    rasterAnalysisEnvironment.SetCellSize(ESRI.ArcGIS.GeoAnalyst.esriRasterEnvSettingEnum.esriRasterEnvValue, ref object_cellSize);

    // Set output extent
    object object_Envelope=(System.Object)envelope; // Explict Cast
    object object_Missing=System.Type.Missing;
    rasterAnalysisEnvironment.SetExtent(ESRI.ArcGIS.GeoAnalyst.esriRasterEnvSettingEnum.esriRasterEnvValue, ref object_Envelope, ref object_Missing);

    // Set output spatial reference
    rasterAnalysisEnvironment.OutSpatialReference=spatialReference;

    // Perform spatial operation
    ESRI.ArcGIS.Geodatabase.IRasterDataset rasterDataset=new ESRI.ArcGIS.DataSourcesRaster.RasterDatasetClass();

    // Create the new raster name that meets the coverage naming convention
    System.String string_RasterName=feaureClass.AliasName;
    if (string_RasterName.Length > 13)
    {
        string_RasterName=string_RasterName.Substring(0, 13);
    }
    string_RasterName=string_RasterName.Replace(" ", "_");

    rasterDataset=conversionOp.ToRasterDataset(geoDataset, "GRID", workspace, string_RasterName);

    ESRI.ArcGIS.Geodatabase.IGeoDataset geoDataset_output=(ESRI.ArcGIS.Geodatabase.IGeoDataset)rasterDataset;

    return geoDataset_output;

}
[Visual Basic .NET]
''' <summary>
''' Create a raster GeoDataset grid from a FeatureClass.
''' </summary>
''' <param name="feaureClass">An IFeatureClass interface that will have a raster (grid) created from it.</param>
''' <param name="string_RasterWorkspace">A System.String that is the workspace location for the grid coverage. Example: "C:\workspace"</param>
''' <param name="int32_NumberOfCells">A System.Int32 that is the number of cells in the X direction that you want the raster to have. This defines the resolution of the raster. Example: 10000</param>
''' <returns>A GeoDataset that is a raster of the input FeatureClass</returns>
''' <remarks></remarks>
Public Function CreateGridFromFeatureClass(ByVal feaureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass, ByVal string_RasterWorkspace As System.String, ByVal int32_NumberOfCells As System.Int32) As ESRI.ArcGIS.Geodatabase.IGeoDataset

  Dim geoDataset As ESRI.ArcGIS.Geodatabase.IGeoDataset=CType(feaureClass, ESRI.ArcGIS.Geodatabase.IGeoDataset) ' Explicit Cast
  Dim spatialReference As ESRI.ArcGIS.Geometry.ISpatialReference=geoDataset.SpatialReference

  ' Create a RasterMaker operator
  Dim conversionOp As ESRI.ArcGIS.GeoAnalyst.IConversionOp=New ESRI.ArcGIS.GeoAnalyst.RasterConversionOpClass

  Dim workspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory=New ESRI.ArcGIS.DataSourcesRaster.RasterWorkspaceFactoryClass

  ' set output workspace
  Dim workspace As ESRI.ArcGIS.Geodatabase.IWorkspace=workspaceFactory.OpenFromFile(string_RasterWorkspace, 0)

  ' Create analysis environment
  Dim rasterAnalysisEnvironment As ESRI.ArcGIS.GeoAnalyst.IRasterAnalysisEnvironment=CType(conversionOp, ESRI.ArcGIS.GeoAnalyst.IRasterAnalysisEnvironment) ' Explicit Cast
  rasterAnalysisEnvironment.OutWorkspace=workspace

  Dim envelope As ESRI.ArcGIS.Geometry.IEnvelope=New ESRI.ArcGIS.Geometry.EnvelopeClass
  envelope=geoDataset.Extent

  ' Set cell size
  Dim double_xMin As System.Double=envelope.XMin
  Dim double_xMax As System.Double=envelope.XMax
  Dim double_difference As System.Double=double_xMax - double_xMin
  Dim double_cellSize As System.Double=double_difference / int32_NumberOfCells
  Dim object_cellSize As System.Object=CType(double_cellSize, System.Object)
  rasterAnalysisEnvironment.SetCellSize(ESRI.ArcGIS.GeoAnalyst.esriRasterEnvSettingEnum.esriRasterEnvValue, object_cellSize)

  ' Set output extent
  Dim object_Envelope As System.Object=CType(envelope, System.Object) ' Explict Cast
  Dim object_Missing As System.Object=System.Type.Missing
  rasterAnalysisEnvironment.SetExtent(ESRI.ArcGIS.GeoAnalyst.esriRasterEnvSettingEnum.esriRasterEnvValue, object_Envelope, object_Missing)

  ' Set output spatial reference
  rasterAnalysisEnvironment.OutSpatialReference=spatialReference

  ' Perform spatial operation
  Dim rasterDataset As ESRI.ArcGIS.Geodatabase.IRasterDataset=New ESRI.ArcGIS.DataSourcesRaster.RasterDatasetClass

  ' Create the new raster name that meets the coverage naming convention
  Dim string_RasterName As System.String=feaureClass.AliasName
  If string_RasterName.Length > 13 Then
    string_RasterName=string_RasterName.Substring(0, 13)
  End If
  string_RasterName=string_RasterName.Replace(" ", "_")

  rasterDataset=conversionOp.ToRasterDataset(geoDataset, "GRID", workspace, string_RasterName)

  Dim geoDataset_output As ESRI.ArcGIS.Geodatabase.IGeoDataset=CType(rasterDataset, ESRI.ArcGIS.Geodatabase.IGeoDataset)

  Return geoDataset_output

End Function

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.DataSourcesRaster
  • ESRI.ArcGIS.GeoAnalyst
  • ESRI.ArcGIS.Geodatabase
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System
  • System