Ejemplo n.º 1
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);
                }
            }
        }
Ejemplo n.º 2
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);
                }
            }
        }