创建一个栅格图层

简介: //This example creates a raster dataset with a specified dimension, populates pixel values, and set a NoData value.
//This example creates a raster dataset with a specified dimension, populates pixel values, and set a NoData value.
public static IRasterDataset CreateRasterDataset(string Path, string FileName)
{
    try
    {
        //Create raster workspace. This example also works with any other workspaces that support raster data such as a file geodatabase (FGDB) workspace.
        //Access the workspace and the SDE workspace.
        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 dimension 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 grid format.
        IRasterDataset rasterDataset = rasterWs.CreateRasterDataset(FileName, "GRID",
            origin, width, height, xCell, yCell, NumBand, rstPixelType.PT_UCHAR, sr,
            true);
        //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, NoData value needs to be set for each band.
        rasterProps.NoDataValue = 255;
        //Create a raster from the dataset.
        IRaster raster = rasterDataset.CreateDefaultRaster();

        //Create a pixel block.
        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;
    }
}

相关文章
|
10月前
|
编解码
没有地理参考的图层添加坐标系:ENVI
本文介绍基于ENVI软件,对不含有任何地理参考信息的栅格遥感影像添加地理坐标系或投影坐标系等地理参考信息的方法~
140 4
没有地理参考的图层添加坐标系:ENVI
|
2月前
|
数据库
ArcGIS中ArcMap新建矢量点线面要素图层并手动划定要素图层范围区域
ArcGIS中ArcMap新建矢量点线面要素图层并手动划定要素图层范围区域
111 1
|
2月前
|
定位技术
ArcGIS批量计算图层中矢量要素面积——ArcMap
ArcGIS批量计算图层中矢量要素面积——ArcMap
102 1
|
2月前
ArcGIS中ArcMap为不含坐标系的图层添加地理坐标系或投影坐标系
ArcGIS中ArcMap为不含坐标系的图层添加地理坐标系或投影坐标系
101 1
|
2月前
|
测试技术
【sgTileImage】自定义组件:瓦片图拖拽局部加载、实现以鼠标为中心缩放
【sgTileImage】自定义组件:瓦片图拖拽局部加载、实现以鼠标为中心缩放
|
9月前
cesium中绘制立方体、设置材质、操作相机及获取鼠标经纬度和高度的方法
cesium中绘制立方体、设置材质、操作相机及获取鼠标经纬度和高度的方法
146 0
|
算法 Python
ArcMap重采样栅格图层的多种算法与具体操作
本文介绍在ArcMap软件中,实现栅格图像重采样的具体操作,以及不同重采样方法的选择依据~
225 1
ArcMap重采样栅格图层的多种算法与具体操作
|
定位技术 数据库
利用 QGIS 绘制洞穴地图
利用 QGIS 绘制洞穴地图
151 0
利用 QGIS 绘制洞穴地图
|
容器
布局容器和栅格网格系统
布局容器和栅格网格系统
128 0
|
定位技术
leaflet图层管理,图层组
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gisdoer/article/details/82683629 leaflet图层管理,图层组 www.
2118 0