GP学习(八)—How to access a raster dataset and to create a raster dataset

声明:仅做自己学习整理用,内容拷贝自ArcEngine SDK开发文档

How to access a raster dataset


Summary A raster dataset can be stored as file based or in a geodatabase. This topic shows how to access a file-based raster dataset and a geodatabase raster dataset. 

In this topic


Accessing a file-based raster dataset

To access a file-based raster dataset, use  IRasterWorkspace and perform the following steps:
  1. Open a raster file workspace.
  2. Open a file raster dataset.
See the following code example:
[C#]
static IRasterDataset OpenFileRasterDataset(string folderName, string datasetName)
{
    //Open a raster file workspace.
    IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
    IRasterWorkspace rasterWorkspace = (IRasterWorkspace)
        workspaceFactory.OpenFromFile(folderName, 0);

    //Open a file raster dataset. 
    IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(datasetName);
    return rasterDataset;
}

Accessing a geodatabase raster dataset

To access a raster dataset in a geodatabase, first open a geodatabase workspace (see  Accessing raster workspaces for details). Then use the IRasterWorkspaceEx interface as shown in the following code example:
[C#]
static IRasterDataset OpenGDBRasterDataset(IRasterWorkspaceEx rasterWorkspaceEx,
    string datasetName)
{
    //Open a raster dataset in a geodatabase (PGDB, FGDB, or ArcSDE).
    return rasterWorkspaceEx.OpenRasterDataset(datasetName);
}

How to create a raster dataset


Summary This topic explains the basic steps to create a raster dataset in a given workspace, populate pixel values using a pixel block object, and set a NoData value of the raster dataset. 

In this topic


Creating a raster dataset

To create a tagged image file format (TIFF) file, perform the following steps:
  1. Create a workspace.
  2. Create a TIFF file with specified width, height, pixel type, cell size, and other necessary dimensions.
  3. 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;
    }
}
 
  
Creating a raster dataset in a geodatabase
To create a raster dataset in a geodatabase, perform the following steps:
  1. Create a file geodatabase workspace factory.
  2. Create a raster workspace and query IRasterWorkspaceEx.
  3. Define the storage and raster properties using the RasterStorageDef and RasterDef classes.
  4. 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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值