AE创建栅格数据集(转载)

关键词:创建栅格数据集 IRasterWorkspace2 IRasterDataset CreateRasterDataset C#

 

        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.Object[,] pixelData;
                pixelData = (System.Object[,])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[i, j] = (i * j) % 255;

 

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

 

转载自:http://blog.sina.com.cn/s/blog_4c33a2c40100907p.html

posted @ 2010-12-19 22:37  空旋  阅读(1718)  评论(0)    收藏  举报