示例#1
0
        /// <summary>
        /// 计算总碳储量.
        /// </summary>
        /// <returns>Boolean.</returns>
        public Boolean CalStorage()
        {
            double sum = 0;

            foreach (double num in storage)
            {
                sum += num;
            }
            //TxtOP.WriteTxt(sumStorage, sum);
            DataTable dt = new DataTable();

            dt.Columns.Add("植被碳储量");
            DataRow newRow = dt.NewRow();

            newRow[0] = sum;
            dt.Rows.Add(newRow);
            GFS.BLL.ExcelHelper exclHelper = new GFS.BLL.ExcelHelper();
            exclHelper.DataTableToExcel(sumStorage, "Sheet", dt, true);
            exclHelper.Dispose();
            return(true);
        }
示例#2
0
        /// <summary>
        /// 计算碳储量---分块读取
        /// 效率可提高百倍以上
        /// </summary>
        public Boolean Cal_carbonStorageBlock()
        {
            IRasterLayer   rasLyr      = null;
            IRasterDataset rasterdat   = null;
            IPnt           pBlockSize0 = null;
            IPnt           pBlockSize1 = null;
            double         sumDensity  = 0;

            float[,] data = null;

            try
            {
                rasLyr      = new RasterLayerClass();
                pBlockSize0 = new PntClass();
                pBlockSize1 = new PntClass();
                rasLyr.CreateFromFilePath(carbonDensity);
                IRaster2 raster = rasLyr.Raster as IRaster2;
                rasterdat = raster.RasterDataset;

                int blockSize = 512;
                pBlockSize1.SetCoords(blockSize, blockSize);
                //影像x方向分块数
                int colBlock = rasLyr.ColumnCount / blockSize;
                //影像y方向分块数
                int rowBlock = rasLyr.RowCount / blockSize;
                //影像分块后x方向剩余像元数
                int colMod = rasLyr.ColumnCount % blockSize;
                //影像分块后y方向剩余像元数
                int rowMod = rasLyr.RowCount % blockSize;

                //分块读取像元并处理
                for (int col = 0; col < colBlock; col++)
                {
                    for (int row = 0; row < rowBlock; row++)
                    {
                        pBlockSize0.SetCoords(blockSize * col, blockSize * row);
                        data = RasterOP.ReadFloat(rasterdat as IRasterDataset2, pBlockSize0, pBlockSize1);
                        if (data == null)
                        {
                            return(false);
                        }
                        sumDensity += ArraySum(data);
                    }
                }

                //读取剩余列像元并处理
                if (colMod > 0)
                {
                    pBlockSize0.SetCoords(blockSize * colBlock, 0);
                    pBlockSize1.SetCoords(colMod, rasLyr.RowCount);
                    data = RasterOP.ReadFloat(rasterdat as IRasterDataset2, pBlockSize0, pBlockSize1);
                    if (data == null)
                    {
                        return(false);
                    }
                    sumDensity += ArraySum(data);
                }
                //读取剩余行像元并处理
                if (rowMod > 0)
                {
                    pBlockSize0.SetCoords(0, blockSize * rowBlock);
                    pBlockSize1.SetCoords(colBlock, rowMod);
                    data = RasterOP.ReadFloat(rasterdat as IRasterDataset2, pBlockSize0, pBlockSize1);
                    if (data == null)
                    {
                        return(false);
                    }
                    sumDensity += ArraySum(data);
                }

                //计算碳储量
                double Storage = sumDensity * forestArea;

                //写出结果到xls
                //TxtOP.WriteTxt(carbonStorage, Storage);
                DataTable dt = new DataTable();
                dt.Columns.Add("碳储量-" + classID);
                DataRow newRow = dt.NewRow();
                newRow[0] = Storage;
                dt.Rows.Add(newRow);
                GFS.BLL.ExcelHelper exclHelper = new GFS.BLL.ExcelHelper();
                exclHelper.DataTableToExcel(carbonStorage, "Sheet", dt, true);
                exclHelper.Dispose();
                data = null;
                if (rasterdat != null)
                {
                    Marshal.ReleaseComObject(rasterdat);
                }

                return(true);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("计算森林碳储量失败!\r\n" + ex.Message);
                return(false);
            }
            finally
            {
                data = null;
                if (rasLyr != null)
                {
                    Marshal.ReleaseComObject(rasLyr);
                }
                if (rasterdat != null)
                {
                    Marshal.ReleaseComObject(rasterdat);
                }
                if (pBlockSize0 != null)
                {
                    Marshal.ReleaseComObject(pBlockSize0);
                }
                if (pBlockSize1 != null)
                {
                    Marshal.ReleaseComObject(pBlockSize1);
                }
            }
        }