ArcObjects SDK開發 011 RasterLayer

1、RasterLayer的結構

圖層的話,除了FeatureLayer外,用的最多的就是RasterLayer了。較FeatureLayer而言,RasterLayer比較簡單,這點可以從柵格圖層的屬性對話框中可以看出。

image1.png

其中General選項卡對應着RasterLayer繼承實現的ILayerGeneralProperties接口,Source選項卡對應IRasterLayer的Ratser屬性,Display選項卡對應着ILayerEffects接口,Symbology選項卡對應着IRasterLayer的Renderer屬性。

2、IRaster接口

IRasterLayer的Raster屬性返回的是IRaster接口類型。其指的是實際的柵格數據源,我們一般存儲爲tif或者img文件。Raster類實現了IRaster接口,同時Raster還繼承了以下接口。

image2.png

其中通過IRaster可以分塊讀取柵格數據的像元信息,並可以設置重採樣方式。IRaster2接口提供了一些像素與地圖座標相互轉換的函數。IRasterBandCollection接口可以讀取柵格數據包含的波段信息。IRasterEdit接口提供了修改柵格數據像元值的功能。IRasterProps提供了柵格數據的屬性信息。ISaveAs接口提供了柵格數據的保存功能。

不過我們一般很少用IRaser接口去修改柵格數據,主要還是靠調用ArcToolbox裏面的工具來處理。

3、IRasterRenderer接口

該接口爲柵格圖層渲染接口,通過RasterLayer的Renderer可以獲取或設置。繼承該接口的類如下圖所示。

image3.png

其中我們常用的有RasterClassifyColorRampRenderer,分級別渲染,例如顯示某個區域內的人口密度數據的柵格數據文件,就可以按照不同的顏色進行分段顯示。

RasterUniqueValueRenderer,唯一值渲染,例如土地分類數據,就可以把耕地、林地、草地、城市用地等按照不同的顏色顯示。

RasterStretchColorRampRenderer,拉伸渲染,一般我們顯示Dem數據的時候都會使用這種渲染方式。

每種渲染方式如何設置,都可以參考ArcMap中的參數設置界面以及SDK幫助。

4、打開柵格數據

我們常用的柵格數據主要有tif和img格式。打開柵格數據有多種方法,我習慣用RasterLayer的CreateFromFilePath函數打開柵格數據,這這種方式比較簡單。代碼如下。

var myRasterLayer = new RasterLayerClass();
myRasterLayer.CreateFromFilePath(this.DEMFilePath);

5、創建柵格數據文件

我們可以使用IWorkSpace來創建柵格數據文件。

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;
    }
}

我們一般創建的時候,會調用ArcToolbox中的工具去創建。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章