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


Open Raster File As GeoDatset Snippet (ArcObjects .NET 10.5 SDK)
ArcObjects Library Reference

Open Raster File As GeoDatset Snippet

Open a Raster file on disk by it's name as a GeoDataset.

[C#]
/// <summary>
/// Open a Raster file on disk by it's name as a GeoDataset.
/// </summary>
/// <param name="path">A System.String that is the directory location of the raster file. Example: "C:\raster_data"</param>
/// <param name="name">A System.String that is the name of the raster file in the directory. Example: "landuse" or "watershed"</param>
/// <returns>An IGeoDataset interface.</returns>
/// <remarks>
/// IRasterWorkspace is used to access a raster stored in a file system in any supported raster format. 
/// RasterWorkspaceFactory must be used to create a raster workspace.
/// To access raster from geodatabase, use IRasterWorkspaceEx interface.
/// 
/// For more information on working with the ArcGIS Spatial Anaylst objects see:
/// http://edndoc.esri.com/arcobjects/9.2/CPP_VB6_VBA_VCPP_Doc/COM/VB6/working/work_rasters/sptl_analyst_objs.htm
/// </remarks>
public ESRI.ArcGIS.Geodatabase.IGeoDataset OpenRasterFileAsGeoDatset(System.String path, System.String name)
{

    try
    {

        ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory=new ESRI.ArcGIS.DataSourcesRaster.RasterWorkspaceFactoryClass();
        ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace rasterWorkspace=(ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace)(workspaceFactory.OpenFromFile(path, 0));
        ESRI.ArcGIS.Geodatabase.IRasterDataset rasterDataset=rasterWorkspace.OpenRasterDataset(name);
        ESRI.ArcGIS.Geodatabase.IGeoDataset geoDataset=(ESRI.ArcGIS.Geodatabase.IGeoDataset)rasterDataset; // Explicit Cast

        return geoDataset;

    }
    catch (System.Exception ex)
    {

        //System.Diagnostics.Debug.WriteLine(ex.Message)
        return null;

    }

}
[Visual Basic .NET]
''' <summary>
''' Open a Raster file on disk by it's name as a GeoDataset.
''' </summary>
''' <param name="path">A System.String that is the directory location of the raster file. Example: "C:\raster_data"</param>
''' <param name="name">A System.String that is the name of the raster file in the directory. Example: "landuse" or "watershed"</param>
''' <returns>An IGeoDataset interface.</returns>
''' <remarks>
''' IRasterWorkspace is used to access a raster stored in a file system in any supported raster format. 
''' RasterWorkspaceFactory must be used to create a raster workspace.
''' To access raster from geodatabase, use IRasterWorkspaceEx interface.
''' 
''' For more information on working with the ArcGIS Spatial Anaylst objects see:
''' http://edndoc.esri.com/arcobjects/9.2/CPP_VB6_VBA_VCPP_Doc/COM/VB6/working/work_rasters/sptl_analyst_objs.htm
''' </remarks>
Public Function OpenRasterFileAsGeoDatset(ByVal path As System.String, ByVal name As System.String) As ESRI.ArcGIS.Geodatabase.IGeoDataset

  Try

    Dim workspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory=New ESRI.ArcGIS.DataSourcesRaster.RasterWorkspaceFactoryClass()
    Dim rasterWorkspace As ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace=CType(workspaceFactory.OpenFromFile(path, 0), ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace)
    Dim rasterDataset As ESRI.ArcGIS.Geodatabase.IRasterDataset=rasterWorkspace.OpenRasterDataset(name)
    Dim geoDataset As ESRI.ArcGIS.Geodatabase.IGeoDataset=CType(rasterDataset, ESRI.ArcGIS.Geodatabase.IGeoDataset) ' Explicit Cast

    Return geoDataset

  Catch ex As System.Exception

    'System.Diagnostics.Debug.WriteLine(ex.Message)
    Return Nothing

  End Try

End Function

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