Esempio n. 1
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);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 计算生物量
        /// </summary>
        public Boolean Cal_Biomass()
        {
            //获取公式中栅格符号
            string[] ss = expression.Split(new char[2] {
                '[', ']'
            });
            List <string> rasSymbols = new List <string>();

            for (int i = 1; i < ss.Length; i = i + 2)
            {
                if (rasSymbols.Contains(ss[i]) == false)
                {
                    rasSymbols.Add(ss[i]);
                }
            }

            //栅格代数
            IMapAlgebraOp  mapAlgebraOp  = null;
            IRasterDataset rasterDataset = null;
            ISaveAs2       saveAs        = null;

            try
            {
                //判断数量是否一致
                if (rasSymbols.Count > index.Count)
                {
                    XtraMessageBox.Show("运算表达式有错误!");
                    return(false);
                }

                //读取和绑定栅格
                mapAlgebraOp = new RasterMapAlgebraOpClass();
                foreach (string key in rasSymbols)
                {
                    rasterDataset = RasterOP.OpenFileRasterDataset(index[key]);
                    mapAlgebraOp.BindRaster((IGeoDataset)rasterDataset, key);
                }

                //执行
                rasterBiomass = mapAlgebraOp.Execute(expression);

                //删除原有输出文件
                if (File.Exists(Biomass))
                {
                    File.Delete(Biomass);
                }

                //保存文件
                saveAs = (ISaveAs2)rasterBiomass;
                //saveAs.SaveAs(outFile, workspace, "TIFF");
                IDataset o = saveAs.SaveAs(Biomass, null, "TIFF");
                if (o != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
                }
                return(true);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("计算森林生物量失败!\r\n" + ex.Message);
                return(false);
            }
            finally
            {
                //释放
                if (rasterDataset != null)
                {
                    Marshal.ReleaseComObject(rasterDataset);
                }
                //if (saveAs != null)
                //    Marshal.ReleaseComObject(saveAs);
                if (mapAlgebraOp != null)
                {
                    Marshal.ReleaseComObject(mapAlgebraOp);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 计算植被面积---按数据类型一次全部读取
        /// </summary>
        /// <returns>System.Double.</returns>
        public Boolean Cal_forestCoverBlock()
        {
            IRasterLayer   rasLyr      = null;
            IRasterProps   rasterProps = null;
            IRasterDataset rasterdat   = null;
            object         o           = null;

            Byte[,] dataByte   = null;
            Int16[,] dataInt16 = null;
            double cellSize    = 0;
            int    forestCount = 0;

            try
            {
                rasLyr = new RasterLayerClass();
                rasLyr.CreateFromFilePath(landCover);
                IRaster2 raster = rasLyr.Raster as IRaster2;
                //获取像元大小
                rasterProps = (IRasterProps)raster;
                cellSize    = rasterProps.MeanCellSize().X;
                rasterdat   = raster.RasterDataset;

                //读取像元值
                o = RasterOP.ReadPixelValue(rasterdat as IRasterDataset2);
                if (o == null)
                {
                    return(false);
                }
                Type type = o.GetType();

                //Stopwatch stopwatch = new Stopwatch();
                //stopwatch.Start();

                switch (type.FullName)
                {
                case "System.Byte[,]":

                    dataByte    = (Byte[, ])o;
                    forestCount = ArrayStatis(dataByte, classID);
                    break;

                case "System.Int16[]":

                    dataInt16   = (Int16[, ])o;
                    forestCount = ArrayStatis(dataInt16, classID);
                    break;

                default:

                    o = null;
                    break;
                }

                //stopwatch.Stop();
                //TimeSpan timeSpan = stopwatch.Elapsed;
                //Console.WriteLine("林地像元计算用时:" + timeSpan.TotalSeconds + "s 循环大小:" + rasLyr.ColumnCount + "," + rasLyr.RowCount);
                //计算林地像元面积
                forestArea = forestCount * cellSize * cellSize / 100;
                return(true);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("计算森林面积失败!\r\n" + ex.Message);
                return(false);
            }
            finally
            {
                dataByte  = null;
                dataInt16 = null;
                o         = null;
                if (rasterdat != null)
                {
                    Marshal.ReleaseComObject(rasterdat);
                }
                if (rasterProps != null)
                {
                    Marshal.ReleaseComObject(rasterProps);
                }
                if (rasLyr != null)
                {
                    Marshal.ReleaseComObject(rasLyr);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 求和.
        /// </summary>
        /// <returns>Boolean.</returns>
        public Boolean Sum()
        {
            //栅格代数
            string         exp           = string.Empty;
            IMapAlgebraOp  mapAlgebraOp  = null;
            IRasterDataset rasterDataset = null;
            IGeoDataset    rasout        = null;
            ISaveAs2       saveAs        = null;

            try
            {
                //读取和绑定栅格
                mapAlgebraOp = new RasterMapAlgebraOpClass();
                //根据文件列表生成计算公式和绑定栅格
                for (int i = 0; i < fileList.Count; i++)
                {
                    string symbol = "[ras" + i + "]";
                    rasterDataset = RasterOP.OpenFileRasterDataset(fileList[i]);
                    mapAlgebraOp.BindRaster((IGeoDataset)rasterDataset, symbol.Substring(1, symbol.Length - 2));
                    exp = exp + symbol + " + ";
                }
                exp = exp.Substring(0, exp.Length - 3);
                //执行
                rasout = mapAlgebraOp.Execute(exp);

                //删除原有输出文件
                if (File.Exists(outFile))
                {
                    File.Delete(outFile);
                }

                //保存文件
                saveAs = (ISaveAs2)rasout;
                //saveAs.SaveAs(outFile, workspace, "TIFF");
                IDataset o = saveAs.SaveAs(outFile, null, "TIFF");
                if (o != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
                }
                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("计算生物量失败!/n" + ex.Message);
                return(false);
            }
            finally
            {
                //释放
                if (saveAs != null)
                {
                    Marshal.ReleaseComObject(saveAs);
                }
                if (rasout != null)
                {
                    Marshal.ReleaseComObject(rasout);
                }
                if (rasterDataset != null)
                {
                    Marshal.ReleaseComObject(rasterDataset);
                }
                if (mapAlgebraOp != null)
                {
                    Marshal.ReleaseComObject(mapAlgebraOp);
                }
            }
        }