public void Write(float?[,] rasterValue, string format)
        {
            FileHelper.DeleteFile(_workSpace, _fileName, ".tif", ".tfw", ".tif.aux");
            IRasterWorkspace2 rasterWs = OpenRasterWorkspace();

            if (rasterWs == null)
            {
                throw new NullReferenceException("栅格文件打开失败");
            }
            IRasterDataset rasterDataset = rasterWs.CreateRasterDataset(_fileName + ".tif",
                                                                        format, RasterInfo.OriginPoint, RasterInfo.Width, RasterInfo.Height,
                                                                        RasterInfo.XCellSize, RasterInfo.YCellSize, 1, rstPixelType.PT_FLOAT,
                                                                        RasterInfo.SpatialReference, true);
            IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset;
            var rasterBand  = rasterBands.Item(0);
            var rasterProps = (IRasterProps)rasterBand;

            //Set NoData if necessary. For a multiband image, NoData value needs to be set for each band.
            rasterProps.NoDataValue = -1;
            //Create a raster from the dataset.
            IRaster raster = rasterDataset.CreateDefaultRaster();

            //Create a pixel block.
            IPnt blocksize = new PntClass();

            blocksize.SetCoords(RasterInfo.Width, RasterInfo.Height);
            IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize) as IPixelBlock3;
            //Populate some pixel values to the pixel block.
            var pixels = (Array)pixelblock.get_PixelData(0);

            for (int i = 0; i < RasterInfo.Width; i++)
            {
                for (int j = 0; j < RasterInfo.Height; j++)
                {
                    if (rasterValue[i, j].HasValue)
                    {
                        pixels.SetValue((float)rasterValue[i, j], i, j);
                    }
                    else
                    {
                        pixels.SetValue(-1, i, j);
                    }
                }
            }

            pixelblock.set_PixelData(0, 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.
            Marshal.ReleaseComObject(rasterEdit);
        }
        public static void exportRasterData(string parth, IRasterLayer rasterLayer, float[,] rasterMat)   //输出栅格数据
        {
            string            directory        = parth.Substring(0, parth.LastIndexOf("\\"));
            string            name             = parth.Substring(parth.LastIndexOf("\\") + 1);
            IWorkspaceFactory workspaceFac     = new RasterWorkspaceFactoryClass();
            IRasterWorkspace2 rasterWorkspace2 = workspaceFac.OpenFromFile(directory, 0) as IRasterWorkspace2;

            IRasterInfo rasterInfo  = (rasterLayer.Raster as IRawBlocks).RasterInfo;
            IPoint      originPoint = new Point();

            originPoint.PutCoords(rasterInfo.Origin.X, rasterInfo.Origin.Y - (rasterLayer.Raster as IRasterProps).Height * (rasterLayer.Raster as IRasterProps).MeanCellSize().Y);
            IRasterProps   rasterProps   = rasterLayer.Raster as IRasterProps;
            IRasterDataset rasterDataSet = rasterWorkspace2.CreateRasterDataset(name, "IMAGINE Image", originPoint, rasterProps.Width, rasterProps.Height,
                                                                                rasterProps.MeanCellSize().X, rasterProps.MeanCellSize().Y, 1, rstPixelType.PT_FLOAT, rasterProps.SpatialReference, true) as IRasterDataset2;

            IRaster2 raster2 = rasterDataSet.CreateDefaultRaster() as IRaster2;

            IPnt pntClass = new Pnt();

            pntClass.X = rasterProps.Width;
            pntClass.Y = rasterProps.Height;
            IRasterCursor rasterCursor   = raster2.CreateCursorEx(pntClass);
            IRasterCursor inRasterCursor = (rasterLayer.Raster as IRaster2).CreateCursorEx(pntClass);

            IRasterEdit rasterEdit = raster2 as IRasterEdit;

            if (rasterEdit.CanEdit())
            {
                IPixelBlock3 pixelBlock3   = rasterCursor.PixelBlock as IPixelBlock3;
                IPixelBlock3 inPixelBlock3 = inRasterCursor.PixelBlock as IPixelBlock3;
                System.Array pixels        = (System.Array)rasterMat;
                pixelBlock3.set_PixelData(0, (System.Array)pixels);
                rasterEdit.Write(rasterCursor.TopLeft, (IPixelBlock)pixelBlock3);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pixelBlock3);
            }
            rasterEdit.Refresh();
            IGeoDataset   inDataset   = rasterLayer.Raster as IGeoDataset;
            IGeoDataset   outDataset  = rasterDataSet as IGeoDataset;
            IExtractionOp op          = new RasterExtractionOpClass();
            var           outDataset1 = op.Raster(outDataset, inDataset);
            var           clipRaster  = (IRaster)outDataset1;
            ISaveAs       pSaveAs     = clipRaster as ISaveAs;

            System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterCursor);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(raster2);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterDataSet);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterWorkspace2);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFac);
            if (File.Exists(parth))
            {
                File.Delete(parth);
            }
            workspaceFac = new RasterWorkspaceFactoryClass();
            IDataset outdataset = pSaveAs.SaveAs(name, workspaceFac.OpenFromFile(directory, 0), "IMAGINE Image");

            System.Runtime.InteropServices.Marshal.ReleaseComObject(outdataset);
            return;
        }
Exemple #3
0
        /// <summary>
        /// 创建RasterDataset文件   张琪    20110614
        /// </summary>
        /// <param name="sDir">保存路径</param>
        /// <param name="sName">文件名</param>
        /// <param name="sFormat"></param>
        /// <param name="pOrigin">点</param>
        /// <param name="nCol"></param>
        /// <param name="nRow"></param>
        /// <param name="cellsizeX"></param>
        /// <param name="cellsizeY"></param>
        /// <param name="ePixelType"></param>
        /// <param name="pSR"></param>
        /// <param name="bPerm"></param>
        /// <returns></returns>
        public IRasterDataset CreateRasterSurf(String sDir, String sName, String sFormat, ESRI.ArcGIS.Geometry.IPoint pOrigin, int nCol, int nRow, Double cellsizeX, Double cellsizeY, rstPixelType ePixelType, ISpatialReference2 pSR, bool bPerm)
        {
            IWorkspaceFactory prWksFac          = new RasterWorkspaceFactoryClass();
            IWorkspace        pWorkspace        = prWksFac.OpenFromFile(sDir, 0);
            IRasterWorkspace2 pRasterWorkspace2 = pWorkspace as IRasterWorkspace2;
            int            numbands             = 1;
            IRasterDataset pRasterDataset       = pRasterWorkspace2.CreateRasterDataset(sName, sFormat, pOrigin, nCol, nRow, cellsizeX, cellsizeY, numbands, ePixelType, pSR, bPerm);

            return(pRasterDataset);
        }
Exemple #4
0
        public static IRasterDataset CreateRasterSurf(string string_0, string string_1, string string_2, IPoint ipoint_0,
                                                      int int_0, int int_1, double double_0, double double_1, rstPixelType rstPixelType_0,
                                                      ISpatialReference2 ispatialReference2_0, bool bool_0)
        {
            IWorkspaceFactory factory    = new RasterWorkspaceFactory();
            IRasterWorkspace2 workspace2 = factory.OpenFromFile(string_0, 0) as IRasterWorkspace2;

            return(workspace2.CreateRasterDataset(string_1, string_2, ipoint_0, int_0, int_1, double_0, double_1, 1,
                                                  rstPixelType_0, ispatialReference2_0, bool_0));
        }
Exemple #5
0
 public static IRasterDataset createFileRasterDataset(IRasterWorkspace2 irasterWorkspace2_0, string string_0,
                                                      int int_0, rstPixelType rstPixelType_0, ISpatialReference ispatialReference_0)
 {
     try
     {
         IRasterDataset dataset = null;
         IPoint         origin  = new Point();
         origin.PutCoords(0.0, 0.0);
         if (ispatialReference_0 == null)
         {
             ispatialReference_0 = new UnknownCoordinateSystem() as ISpatialReference;
         }
         dataset = irasterWorkspace2_0.CreateRasterDataset(string_0, "IMAGINE Image", origin, 200, 100, 1.0, 1.0,
                                                           int_0, rstPixelType_0, ispatialReference_0, true);
         IRawPixels            pixels = null;
         IPixelBlock3          block  = null;
         IPnt                  tlc    = null;
         IPnt                  size   = null;
         IRasterBandCollection bands  = (IRasterBandCollection)dataset;
         pixels = (IRawPixels)bands.Item(0);
         IRasterProps props = (IRasterProps)pixels;
         tlc = new DblPnt();
         tlc.SetCoords(0.0, 0.0);
         size = new DblPnt();
         size.SetCoords((double)props.Width, (double)props.Height);
         block = (IPixelBlock3)pixels.CreatePixelBlock(size);
         pixels.Read(tlc, (IPixelBlock)block);
         object[,] objArray = (object[, ])block.get_PixelDataByRef(0);
         for (int i = 0; i < props.Width; i++)
         {
             for (int j = 0; j < props.Height; j++)
             {
                 objArray[i, j] = (i * j) % 255;
             }
         }
         object cache = pixels.AcquireCache();
         pixels.Write(tlc, (IPixelBlock)block);
         pixels.ReturnCache(cache);
         return(dataset);
     }
     catch (Exception exception)
     {
         Debug.WriteLine(exception.Message);
         return(null);
     }
 }
        private IRaster createRasterWithoutData(Point2D ptLeftTop, double dbResolution, int[] nSize, string pRasterFile)
        {
            IRaster pRaster = null;

            try
            {
                if (File.Exists(pRasterFile))
                {
                    File.Delete(pRasterFile);
                }

                string            Path     = System.IO.Path.GetDirectoryName(pRasterFile);
                string            FileName = System.IO.Path.GetFileName(pRasterFile);
                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(ptLeftTop.X, ptLeftTop.Y);
                //Define the dimensions of the raster dataset.
                int    width   = nSize[0];     //This is the width of the raster dataset.
                int    height  = nSize[1];     //This is the height of the raster dataset.
                double xCell   = dbResolution; //This is the cell size in x direction.
                double yCell   = dbResolution; //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);

                //Set NoData if necessary. For a multiband image, a NoData value needs to be set for each band.
                //rasterProps.NoDataValue = -9999;
                //Create a raster from the dataset.
                pRaster = rasterDataset.CreateDefaultRaster();

                return(pRaster);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error: " + ex.Message);
                return(null);
            }

            return(pRaster);
        }
        public static IRasterDataset exportRasterData(string parth, IRasterLayer rasterLayer, float[,] rasterMat)
        {
            string directory = parth.Substring(0, parth.LastIndexOf("\\"));
            string name      = parth.Substring(parth.LastIndexOf("\\") + 1);

            IWorkspaceFactory workspaceFac     = new RasterWorkspaceFactoryClass();
            IRasterWorkspace2 rasterWorkspace2 = workspaceFac.OpenFromFile(directory, 0) as IRasterWorkspace2;

            IRasterInfo rasterInfo  = (rasterLayer.Raster as IRawBlocks).RasterInfo;
            IPoint      originPoint = new Point();

            originPoint.PutCoords(rasterInfo.Origin.X, rasterInfo.Origin.Y - (rasterLayer.Raster as IRasterProps).Height * (rasterLayer.Raster as IRasterProps).MeanCellSize().Y);
            IRasterProps   rasterProps   = rasterLayer.Raster as IRasterProps;
            IRasterDataset rasterDataSet = rasterWorkspace2.CreateRasterDataset(name, "IMAGINE Image", originPoint, rasterProps.Width, rasterProps.Height, rasterProps.MeanCellSize().X, rasterProps.MeanCellSize().Y, 1, rstPixelType.PT_FLOAT, rasterProps.SpatialReference, true);
            IRaster2       raster2       = rasterDataSet.CreateDefaultRaster() as IRaster2;
            IPnt           pntClass      = new Pnt();

            pntClass.X = rasterProps.Width;
            pntClass.Y = rasterProps.Height;

            IRasterCursor rasterCursor = raster2.CreateCursorEx(pntClass);
            IRasterEdit   rasterEdit   = raster2 as IRasterEdit;

            if (rasterEdit.CanEdit())
            {
                IRasterBandCollection bands       = rasterDataSet as IRasterBandCollection;
                IPixelBlock3          pixelBlock3 = rasterCursor.PixelBlock as IPixelBlock3;
                System.Array          pixels      = (System.Array)pixelBlock3.get_PixelData(0);
                for (int i = 0; i < rasterProps.Width; i++)
                {
                    for (int j = 0; j < rasterProps.Height; j++)
                    {
                        pixels.SetValue(Convert.ToSingle(rasterMat[j, i]), i, j);
                    }
                }
                pixelBlock3.set_PixelData(0, (System.Array)pixels);
                rasterEdit.Write(rasterCursor.TopLeft, (IPixelBlock)pixelBlock3);
            }
            (raster2 as IRasterProps).NoDataValue = 0f;
            rasterEdit.Refresh();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);
            return(rasterDataSet);
        }
Exemple #8
0
        /// <summary>
        /// 创建栅格数据集
        /// </summary>
        /// <param name="pGDBType"></param>
        /// <param name="pPath"></param>
        /// <param name="pFileName"></param>
        /// <param name="pWidth"></param>
        /// <param name="pHeight"></param>
        /// <param name="pXCell"></param>
        /// <param name="pYCell"></param>
        /// <param name="pNumBand"></param>
        /// <returns></returns>
        public IRasterDataset CreateRasterDataset(GDBType pGDBType, string pPath, string pFileName, int pWidth, int pHeight, double pXCell, double pYCell, int pNumBand)
        {
            try
            {
                IRasterWorkspace2 pRWs = GetWorkspace(pPath, pGDBType) as IRasterWorkspace2;

                ISpatialReference sr = new UnknownCoordinateSystemClass();

                IPoint origin = new PointClass();
                origin.PutCoords(0.0, 0.0);
                IRasterDataset rasterDataset = pRWs.CreateRasterDataset(pFileName, "TIFF",
                                                                        origin, pWidth, pHeight, pXCell, pYCell, pNumBand, rstPixelType.PT_UCHAR, sr,
                                                                        true);


                return(rasterDataset);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return(null);
            }
        }
Exemple #9
0
        public IRasterDataset CreateRasterSurf(string sDir, string sName, string sFormat, IPoint pOrigin, int nCol, int nRow, double cellsizeX, double cellsizeY, rstPixelType ePixelType, ISpatialReference2 pSR, bool bPerm)
        {
            //IWorkspaceFactory rWksFac = new RasterWorkspaceFactory();

            //IWorkspace wks = rWksFac.OpenFromFile(sDir, 0);

            //IRasterWorkspace2 rWks = wks as IRasterWorkspace2;

            //int numbands = 1;

            //IRasterDataset pRDS;// = new RasterDatasetClass();
            //pRDS = rWks.CreateRasterDataset(sName, sFormat, pOrigin, nCol, nRow, cellsizeX, cellsizeY, numbands, ePixelType, pSR, bPerm);
            //return pRDS;

            IWorkspaceFactory pworkspaceFactory = new RasterWorkspaceFactory();

            ESRI.ArcGIS.Geodatabase.IWorkspaceName pworkspaceName = pworkspaceFactory.Create(null, "MyWorkspace", null, 0);
            ESRI.ArcGIS.esriSystem.IName           pname          = (IName)pworkspaceName;
            ESRI.ArcGIS.Geodatabase.IWorkspace     inmemWor       = (IWorkspace)pname.Open();
            IPoint            originpoint = pOrigin;
            IRasterWorkspace2 rasterws    = (IRasterWorkspace2)inmemWor;

            //用于计算的 float型的栅格数据
            IRasterDataset demdataset = rasterws.CreateRasterDataset("Dataset", "MEM", originpoint, nCol, nRow,
                                                                     (double)cellsizeX, (double)cellsizeY, 1, rstPixelType.PT_DOUBLE, null, true);

            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pname);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pworkspaceFactory);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pworkspaceName);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(inmemWor);
            }
            catch { }
            GC.Collect();
            return(demdataset);
        }
        // From ArcObjects Help: How to Create a Raster dataset
        // http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/index.html#/How_to_create_a_raster_dataset/000100000464000000/
        public static IRasterDataset CreateRasterDataset(string Path, string FileName)
        {
            try
            {
                IRasterWorkspace2 rasterWs = OpenRasterWorkspace(Path); // This is a custom method that's at the bottom of this code.
                //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 = ((IRasterDataset2)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);
            }
        }
Exemple #11
0
        /// <summary>
        /// 创建栅格数据集,本地
        /// </summary>
        /// <param name="directoryName"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private 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)
            {
                //*******************************************************************
                //guozheng added
                if (ModData.SysLog != null)
                {
                    ModData.SysLog.Write(ex, null, DateTime.Now);
                }
                else
                {
                    ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                    ModData.SysLog.Write(ex, null, DateTime.Now);
                }
                //********************************************************************

                System.Diagnostics.Debug.WriteLine(ex.Message);
                return(null);
            }
        }
Exemple #12
0
        public IRaster TINToDEM(ITin pTin)
        {
            IPoint pOrigin = pTin.Extent.LowerLeft;


            IWorkspaceFactory pworkspaceFactory = new RasterWorkspaceFactory();

            ESRI.ArcGIS.Geodatabase.IWorkspaceName pworkspaceName = pworkspaceFactory.Create(null, "MyWorkspace", null, 0);
            ESRI.ArcGIS.esriSystem.IName           pname          = (IName)pworkspaceName;
            ESRI.ArcGIS.Geodatabase.IWorkspace     inmemWor       = (IWorkspace)pname.Open();
            IPoint            originpoint = pOrigin;
            IRasterWorkspace2 rasterws    = (IRasterWorkspace2)inmemWor;

            int nCol, nRow;

            nCol = 500;
            nRow = 500;
            double cellsizeX, cellsizeY;

            cellsizeX = pTin.Extent.Width / nCol;
            cellsizeY = pTin.Extent.Height / nRow;

            //用于计算的 float型的栅格数据
            IRasterDataset demdataset = rasterws.CreateRasterDataset("Dataset", "MEM", originpoint, nCol, nRow,
                                                                     (double)cellsizeX, (double)cellsizeY, 1, rstPixelType.PT_DOUBLE, null, true);

            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pname);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pworkspaceFactory);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pworkspaceName);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(inmemWor);
            }
            catch { }

            //IRawPixels pRawPixels = GetRawPixels(pRDS, 0);
            IRaster pRaster    = demdataset.CreateDefaultRaster();
            IPnt    pBlockSize = new DblPnt();

            //nCol = 50;
            //nRow = 50;
            pBlockSize.X = nCol;
            pBlockSize.Y = nRow;
            IPixelBlock pPixelBlock = pRaster.CreatePixelBlock(pBlockSize);
            //IPixelBlock pPixelBlock = pRawPixels.CreatePixelBlock(pBlockSize);
            IPixelBlock3 pPixelBlock3 = pPixelBlock as IPixelBlock3;

            //object val = pPixelBlock.get_SafeArray(0);
            ITinSurface pTinSurf = pTin as ITinSurface;
            // IRasterProps pRasterProps = pRawPixels as IRasterProps;
            IRasterProps pRasterProps = pRaster as IRasterProps;
            object       nodata;

            //pOrigin.X = pOrigin.X + (cellsize * 0.5);
            //pOrigin.Y = pOrigin.Y + (cellsize * nRow) - (cellsize * 0.5);
            pOrigin.X = pOrigin.X;
            pOrigin.Y = pOrigin.Y + (cellsizeY * nRow);
            nodata    = pRasterProps.NoDataValue;
            IGeoDatabaseBridge2 pbridge2 = (IGeoDatabaseBridge2) new GeoDatabaseHelperClass();

            //这个pOrigin为栅格左上角
            //pbridge2.QueryPixelBlock(pTinSurf, pOrigin.X, pOrigin.Y, cellsize, cellsize, esriRasterizationType.esriElevationAsRaster, nodata, ref val);
            //if (pTin.ProcessCancelled)
            //    return null;
            //val.GetType();
            CalPixelArray(pTinSurf, pOrigin.X, pOrigin.Y, cellsizeX, cellsizeY, ref pPixelBlock3);
            IPnt pOffset = new DblPnt();

            pOffset.X = 0;
            pOffset.Y = 0;
            //pPixelBlock3.set_PixelData(0, val);
            //pRawPixels.Write(pOffset, (IPixelBlock)pPixelBlock3);//写入硬盘
            IRasterEdit prasteredit = pRaster as IRasterEdit;

            prasteredit.Write(pOffset, (IPixelBlock)pPixelBlock3);
            //pRDS = OpenOutputRasterDataset(sDir, sName);

            //IPixelBlock pb = pRaster.CreatePixelBlock(pBlockSize);
            //pRaster.Read(pOffset,pb);
            return(pRaster);
        }
Exemple #13
0
        public IRasterDataset TinToRaster_new(ITinAdvanced pTin, esriRasterizationType eRastConvType, String sDir, String sName, rstPixelType ePixelType, Double cellsize, IEnvelope pExtent, bool bPerm)
        {
            IPoint pOrigin = pExtent.LowerLeft;

            //pOrigin.X = pOrigin.X - (cellsize * 0.5);
            //pOrigin.Y = pOrigin.Y - (cellsize * 0.5);
            pOrigin.X = pOrigin.X;
            pOrigin.Y = pOrigin.Y;
            int nCol, nRow;

            nCol = (int)Math.Round(pExtent.Width / cellsize);
            nRow = (int)Math.Round(pExtent.Height / cellsize);
            IGeoDataset        pGDS = pTin as IGeoDataset;
            ISpatialReference2 pSR  = pGDS.SpatialReference as ISpatialReference2;
            //这个pOrigin为栅格左下角
            IWorkspaceFactory pworkspaceFactory = new RasterWorkspaceFactory();
            IRasterWorkspace2 rasterws          = pworkspaceFactory.OpenFromFile(sDir, 0) as IRasterWorkspace2;
            IPoint            originpoint       = pOrigin;

            //用于计算的 float型的栅格数据
            IRasterDataset demdataset = rasterws.CreateRasterDataset(sName, "TIFF", originpoint, nCol, nRow,
                                                                     cellsize, cellsize, 1, rstPixelType.PT_DOUBLE, null, true);

            IRasterDataset pRDS = demdataset;

            //IRawPixels pRawPixels = GetRawPixels(pRDS, 0);
            IRaster pRaster    = pRDS.CreateDefaultRaster();
            IPnt    pBlockSize = new DblPnt();

            //nCol = 50;
            //nRow = 50;
            pBlockSize.X = nCol;
            pBlockSize.Y = nRow;
            IPixelBlock pPixelBlock = pRaster.CreatePixelBlock(pBlockSize);
            //IPixelBlock pPixelBlock = pRawPixels.CreatePixelBlock(pBlockSize);
            IPixelBlock3 pPixelBlock3 = pPixelBlock as IPixelBlock3;

            //object val = pPixelBlock.get_SafeArray(0);
            ITinSurface pTinSurf = pTin as ITinSurface;
            // IRasterProps pRasterProps = pRawPixels as IRasterProps;
            IRasterProps pRasterProps = pRaster as IRasterProps;
            object       nodata;

            //pOrigin.X = pOrigin.X + (cellsize * 0.5);
            //pOrigin.Y = pOrigin.Y + (cellsize * nRow) - (cellsize * 0.5);
            pOrigin.X = pOrigin.X;
            pOrigin.Y = pOrigin.Y + (cellsize * nRow);
            nodata    = pRasterProps.NoDataValue;
            IGeoDatabaseBridge2 pbridge2 = (IGeoDatabaseBridge2) new GeoDatabaseHelperClass();

            //这个pOrigin为栅格左上角
            //pbridge2.QueryPixelBlock(pTinSurf, pOrigin.X, pOrigin.Y, cellsize, cellsize, esriRasterizationType.esriElevationAsRaster, nodata, ref val);
            //if (pTin.ProcessCancelled)
            //    return null;
            //val.GetType();
            CalPixelArray(pTinSurf, pOrigin.X, pOrigin.Y, cellsize, cellsize, ref pPixelBlock3);
            IPnt pOffset = new DblPnt();

            pOffset.X = 0;
            pOffset.Y = 0;
            //pPixelBlock3.set_PixelData(0, val);
            //pRawPixels.Write(pOffset, (IPixelBlock)pPixelBlock3);//写入硬盘
            IRasterEdit prasteredit = pRaster as IRasterEdit;

            prasteredit.Write(pOffset, (IPixelBlock)pPixelBlock3);
            //pRDS = OpenOutputRasterDataset(sDir, sName);

            //IPixelBlock pb = pRaster.CreatePixelBlock(pBlockSize);
            //pRaster.Read(pOffset,pb);

            // ISaveAs pSaveas = pRasterProps as ISaveAs2;
            // pSaveas.SaveAs(sDir + "\\" + sName, null, "TIFF");

            prasteredit.Refresh();


            return(pRDS);
        }
        public static IRasterDataset CreateRasterDataset(string filePath, string fileName, IRasterLayer rasterLayer,
                                                         StructRasterMetaData structRasterMetaData, int[,] data, int noDataValue)
        {
            try
            {
                IRasterWorkspace2 rasterWorkspace2 = OpenRasterWorkspace(filePath);
                //Define the origin for the raster dataset, which is the lower left corner of the raster.
                IPoint originPoint = new PointClass();
                originPoint.PutCoords(structRasterMetaData.XMin, structRasterMetaData.YMin);
                //Define the dimensions of the raster dataset.
                int                 width            = structRasterMetaData.RowCount;    //This is the width of the raster dataset.
                int                 height           = structRasterMetaData.ColumnCount; //This is the height of the raster dataset.
                IRaster             r                = rasterLayer.Raster;
                IRasterDefaultProps rdp              = r as IRasterDefaultProps;
                double              xCellSize        = rdp.DefaultPixelWidth;  //This is the cell size in x direction.
                double              yCellSize        = rdp.DefaultPixelHeight; //This is the cell size in y direction.
                ISpatialReference   spatialReference = rdp.DefaultSpatialReference;
                int                 bandCount        = 1;                      // This is the number of bands the raster dataset contains.
                //Create a raster dataset in TIFF format.
                IRasterDataset rasterDataset = rasterWorkspace2.CreateRasterDataset(fileName, "IMAGINE Image",
                                                                                    originPoint, height, width, xCellSize, yCellSize, bandCount, rstPixelType.PT_UCHAR, spatialReference,
                                                                                    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 = -9999f;
                //Create a raster from the dataset.
                IRaster raster = rasterDataset.CreateDefaultRaster();

                //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(height, width);
                IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize) as IPixelBlock3;

                object temp = pixelblock.get_PixelDataByRef(0);

                System.Byte[,] pixelData = (System.Byte[, ])temp;
                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < height; j++)
                    {
                        if (data[i, j] == -9999f)
                        {
                            pixelData[j, i] = (System.Byte)noDataValue;
                            //System.Diagnostics.Debug.WriteLine(i.ToString() + "+" + j.ToString());
                        }
                        else
                        {
                            if (pixelData[j, i] != Convert.ToByte(data[i, j]))
                            {
                                pixelData[j, i] = Convert.ToByte(data[i, j]);
                            }
                            //System.Diagnostics.Debug.WriteLine(i.ToString() + "-" + j.ToString());
                        }
                    }
                }

                pixelblock.set_PixelData(0, pixelData);

                //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);
            }
        }
        private IRaster CreateRaster(Point2D ptLeftTop, double[] dbResolution, int[] nSize)
        {
            try
            {
                IWorkspaceFactory pworkspaceFactory = new RasterWorkspaceFactory();
                ESRI.ArcGIS.Geodatabase.IWorkspaceName pworkspaceName = pworkspaceFactory.Create(null, "MyWorkspace", null, 0);
                ESRI.ArcGIS.esriSystem.IName           pname          = (IName)pworkspaceName;
                ESRI.ArcGIS.Geodatabase.IWorkspace     inmemWor       = (IWorkspace)pname.Open();
                IRasterWorkspace2 rasterWs = (IRasterWorkspace2)inmemWor;

                //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(ptLeftTop.X, ptLeftTop.Y);
                //Define the dimensions of the raster dataset.
                int    width   = nSize[0];        //This is the width of the raster dataset.
                int    height  = nSize[1];        //This is the height of the raster dataset.
                double xCell   = dbResolution[0]; //This is the cell size in x direction.
                double yCell   = dbResolution[1]; //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("", "MEM",
                                                                            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 = -9999f;
                //Create a raster from the dataset.
                IRaster raster = rasterDataset.CreateDefaultRaster();

                //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++)
                //    {
                //        pixels.SetValue(dbData[i, j], 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(ptLeftTop.X, ptLeftTop.Y);

                ////Write the pixel block.
                //IRasterEdit rasterEdit = (IRasterEdit)raster;
                //rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock);
                //rasterEdit.Refresh();

                //Release rasterEdit explicitly.
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterDataset);
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterWs);
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(sr);
                // GC.Collect();

                return(raster);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return(null);
            }

            return(null);
        }