创建文件栅格数据集的代码

博客给出了创建文件栅格数据集的代码。代码包含创建新 img 文件、分配像素值等操作,还定义了创建栅格工作空间的函数。通过一系列操作,最终实现文件栅格数据集的创建,若出现异常则输出错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建文件栅格数据集的代码,来自http://edndoc.esri.com/arcobjects/9.0/default.asp?URL=/arcobjects/9.0/samples/raster/creating_raster_data/createfilerasterdataset.htm
 
 
// Libraries needed to run this code:
//   ESRI.ArcGIS.Geodatabase, ESRI.ArcGIS.DataSourcesRaster, ESRI.ArcGIS.Geometry
public IRasterDataset createFileRasterDataset(string directoryName, string fileName)
{
  // This function creates a new img file in the given workspace
  // and then assigns pixel values
  try
  {
    IRasterDataset rasterDataset = null;
    IPoint originPoint = new PointClass();
    originPoint.PutCoords(0, 0);

    // Create the dataset
    IRasterWorkspace2 rasterWorkspace2 = null;
    rasterWorkspace2 = createRasterWorkspace(directoryName);

    rasterDataset = rasterWorkspace2.CreateRasterDataset(fileName, "IMAGINE Image", originPoint, 200, 100, 1, 1, 1, rstPixelType.PT_UCHAR, new UnknownCoordinateSystemClass(), true);

    IRawPixels rawPixels = null;
    IPixelBlock3 pixelBlock3 = null;
    IPnt pixelBlockOrigin = null;
    IPnt pixelBlockSize = null;
    IRasterBandCollection rasterBandCollection;
    IRasterProps rasterProps;

    // QI for IRawPixels and IRasterProps
    rasterBandCollection = (IRasterBandCollection) rasterDataset;
    rawPixels = (IRawPixels) rasterBandCollection.Item(0);
    rasterProps = (IRasterProps) rawPixels;

    // Create pixelblock 
    pixelBlockOrigin = new DblPntClass();
    pixelBlockOrigin.SetCoords(0, 0);
  
    pixelBlockSize = new DblPntClass();
    pixelBlockSize.SetCoords(rasterProps.Width, rasterProps.Height);

    pixelBlock3 = (IPixelBlock3) rawPixels.CreatePixelBlock(pixelBlockSize);

    // Read pixelblock 
    rawPixels.Read(pixelBlockOrigin, (IPixelBlock) pixelBlock3);

    // Get pixeldata array 
    System.Array pixelData;
    pixelData = (System.Array) pixelBlock3.get_PixelDataByRef(0);

    // Loop through all the pixels and assign value  
    for(int i = 0; i < rasterProps.Width; i++)
      for (int j = 0; j < rasterProps.Height; j++) 
        pixelData.SetValue(Convert.ToByte((i * j) % 255), i, j); 

    pixelBlock3.set_PixelData(0, (System.Object) pixelData);

    // Write the pixeldata back 
    System.Object cachePointer;

    cachePointer = rawPixels.AcquireCache();

    rawPixels.Write(pixelBlockOrigin, (IPixelBlock) pixelBlock3);

    rawPixels.ReturnCache(cachePointer);
                
    // Return raster dataset
    return rasterDataset;
  } 
  catch (Exception ex)
  {
    System.Diagnostics.Debug.WriteLine(ex.Message);
    return null;
  } 
}

public IRasterWorkspace2 createRasterWorkspace(string pathName)
{
  // Create RasterWorkspace 
  IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();

  return workspaceFactory.OpenFromFile(pathName, 0) as IRasterWorkspace2;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值