Exemple #1
0
        /// <summary>
        /// 获取栅格计算的栅格结果
        /// </summary>
        /// <param name="nameAndRasters"></param>
        /// <param name="referencedRaster"></param>
        /// <param name="expression">计算表达式</param>
        /// <param name="cellSize"></param>
        public static IRaster GetRasterCalculateResult(Dictionary <string, IRaster> nameAndRasters,
                                                       IRaster referencedRaster, string expression, double cellSize)
        {
            IMapAlgebraOp mapAlgebraOp = GetAlgebraOp(referencedRaster, cellSize);

            return(RasterCalculate(mapAlgebraOp, nameAndRasters, expression));
        }
Exemple #2
0
        public IGeoDataset SetNull(string inRaster, int excludeValue)
        {
            IMapAlgebraOp pRSalgebra  = null;
            IGeoDataset   pGeoDataset = null;

            try
            {
                pGeoDataset = EngineAPI.OpenRasterFile(inRaster) as IGeoDataset;
                string expression = "SetNull([InRaster] != " + excludeValue + ",[InRaster])";
                pRSalgebra = new RasterMapAlgebraOpClass();
                pRSalgebra.BindRaster(pGeoDataset, "InRaster");
                IGeoDataset resDataset = pRSalgebra.Execute(expression);
                return(resDataset);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (pRSalgebra != null)
                {
                    Marshal.ReleaseComObject(pRSalgebra);
                }
                if (pGeoDataset != null)
                {
                    Marshal.ReleaseComObject(pGeoDataset);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 获取栅格计算的栅格结果
        /// </summary>
        /// <param name="pMapAlgebraOp">计算工具</param>
        /// <param name="inRasters">输入栅格集合</param>
        /// <param name="rasterName">栅格名称集合</param>
        /// <param name="expression">计算表达式</param>
        public static IRaster GetRasterCalculateResult(IMapAlgebraOp pMapAlgebraOp, IList <IRaster> inRasters,
                                                       IList <string> rasterName, string expression)
        {
            var nameAndRasters = GetNameAndRasters(inRasters, rasterName);

            return(RasterCalculate(pMapAlgebraOp, nameAndRasters, expression));
        }
        /// <summary>
        /// 计算碳密度
        /// </summary>
        public Boolean Cal_carbonDensity()
        {
            string exp = coefficient + " * [Biomass]";
            //栅格代数
            IMapAlgebraOp mapAlgebraOp = null;
            IGeoDataset   rasout       = null;
            ISaveAs2      saveAs       = null;

            try
            {
                //绑定符号
                mapAlgebraOp = new RasterMapAlgebraOpClass();
                mapAlgebraOp.BindRaster(rasterBiomass, "Biomass");
                //执行
                rasout = mapAlgebraOp.Execute(exp);
                //rasterDensity = (IRasterDataset)rasout;
                //删除原有输出文件
                if (File.Exists(carbonDensity))
                {
                    File.Delete(carbonDensity);
                }

                //保存文件
                saveAs = (ISaveAs2)rasout;
                //saveAs.SaveAs(outFile, workspace, "TIFF");
                IDataset o = saveAs.SaveAs(carbonDensity, 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
            {
                //释放
                //rasterBiomass = null;
                if (rasterBiomass != null)
                {
                    Marshal.ReleaseComObject(rasterBiomass);
                }
                if (saveAs != null)
                {
                    Marshal.ReleaseComObject(saveAs);
                }
                if (rasout != null)
                {
                    Marshal.ReleaseComObject(rasout);
                }
                if (mapAlgebraOp != null)
                {
                    Marshal.ReleaseComObject(mapAlgebraOp);
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// 栅格计算
        /// </summary>
        /// <param name="nameAndRasters"></param>
        /// <param name="referencedRaster"></param>
        /// <param name="expression">计算表达式</param>
        /// <param name="outFile"></param>
        /// <param name="cellSize"></param>
        public static void RasterCalculate(Dictionary <string, IRaster> nameAndRasters, IRaster referencedRaster,
                                           string expression, double cellSize, string outFile)
        {
            IMapAlgebraOp mapAlgebraOp = GetAlgebraOp(referencedRaster, cellSize);
            IRaster       outRaster    = RasterCalculate(mapAlgebraOp, nameAndRasters, expression);

            if (outRaster == null)
            {
                return;
            }
            RasterSaver.SaveRasterAsDataset(outRaster, outFile);
        }
Exemple #6
0
        /// <summary>
        /// 栅格计算
        /// </summary>
        /// <param name="mapAlgebraOp"></param>
        /// <param name="expression"></param>
        /// <param name="nameAndRasters"></param>
        /// <returns></returns>
        public static IRaster RasterCalculate(IMapAlgebraOp mapAlgebraOp, Dictionary <string, IRaster> nameAndRasters,
                                              string expression)
        {
            if (mapAlgebraOp == null)
            {
                return(null);
            }
            //绑定
            foreach (var item in nameAndRasters)
            {
                mapAlgebraOp.BindRaster((IGeoDataset)item.Value, item.Key);
            }

            //进行栅格计算
            return((IRaster)mapAlgebraOp.Execute(expression));
        }
Exemple #7
0
        /// <summary>
        /// 执行地图代数运算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks></remarks>

        private void Button_OK_Click(System.Object sender, System.EventArgs e)
        {
            IRasterLayer pRasLayer = default(IRasterLayer);
            IRaster      pRaster   = default(IRaster);

            IEnvelope layExtend            = default(IEnvelope);
            double    AnalysisExtentLeft   = 0;
            double    AnalysisExtentRight  = 0;
            double    AnalysisExtentTop    = 0;
            double    AnalysisExtentBottom = 0;

            string layerNameFir = null;

            try
            {
                if (LayerList.Count != 0)
                {
                    if (textBox_Result.Text.ToString().Length != 0)
                    {
                        layerNameFir = LayerList[0].ToString();

                        layExtend            = GetLayerExtend(layerNameFir);
                        AnalysisExtentLeft   = layExtend.XMin;
                        AnalysisExtentRight  = layExtend.XMax;
                        AnalysisExtentTop    = layExtend.YMax;
                        AnalysisExtentBottom = layExtend.YMin;

                        pMapAlgebraOp = new RasterMapAlgebraOp() as IMapAlgebraOp;
                        //设置栅格计算分析环境
                        IRasterAnalysisEnvironment pRasAnaEnv = default(IRasterAnalysisEnvironment);
                        pRasAnaEnv            = (IRasterAnalysisEnvironment)pMapAlgebraOp;
                        pRasAnaEnv.VerifyType = esriRasterVerifyEnum.esriRasterVerifyOn;

                        object dddd;
                        dddd = GetRasterCellSize(layerNameFir);
                        pRasAnaEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref dddd);
                        //设置分析范围pAnaExtent
                        IEnvelope pAnaExtent = default(IEnvelope);
                        pAnaExtent = new Envelope() as IEnvelope;

                        pAnaExtent.XMin = Convert.ToDouble(AnalysisExtentLeft);
                        pAnaExtent.XMax = Convert.ToDouble(AnalysisExtentRight);
                        pAnaExtent.YMax = Convert.ToDouble(AnalysisExtentTop);
                        pAnaExtent.YMin = Convert.ToDouble(AnalysisExtentBottom);

                        object dd1 = pAnaExtent;
                        object dd2 = null;


                        pRasAnaEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref dd1, ref dd2);

                        foreach (string LayerName in LayerList)
                        {
                            pRasLayer = (IRasterLayer)FindLayerByName(pCurMap, LayerName);
                            //MsgBox(LayerName)
                            pRaster = pRasLayer.Raster;
                            RasterList.Add(pRaster);
                        }
                        //将容量设置为 ArrayList 中元素的实际数目
                        LayerList.TrimToSize();
                        RasterList.TrimToSize();

                        //绑定
                        int i = 0;
                        if (LayerList.Count == RasterList.Count)
                        {
                            for (i = 0; i <= LayerList.Count - 1; i++)
                            {
                                pMapAlgebraOp.BindRaster((IGeoDataset)RasterList[i], LayerList[i].ToString());
                            }
                        }


                        //获取文本框中的运算表达式()
                        string sCalExpression = null;
                        sCalExpression = textBox_Calculate.Text;
                        //执行地图代数运算
                        IRaster pOutRasterDS = default(IRaster);
                        pOutRasterDS = (IRaster)pMapAlgebraOp.Execute(sCalExpression);

                        //解除绑定
                        if (LayerList.Count == RasterList.Count)
                        {
                            for (i = 0; i <= LayerList.Count - 1; i++)
                            {
                                pMapAlgebraOp.UnbindRaster(LayerList[i].ToString());
                            }
                        }


                        //保存到工作空间
                        IWorkspaceFactory pWsFact = default(IWorkspaceFactory);
                        IWorkspace        pWS     = default(IWorkspace);
                        int hwnd = 0;
                        pWsFact = new RasterWorkspaceFactory();
                        pWS     = pWsFact.OpenFromFile(sOutRasPath, hwnd);
                        IRasterBandCollection pRasterbandCollection = default(IRasterBandCollection);

                        pRasterbandCollection = (IRasterBandCollection)pOutRasterDS;
                        IDataset pDataset = default(IDataset);

                        pDataset = pRasterbandCollection.SaveAs(sOutRasName, pWS, "IMAGINE Image");

                        //输出到mapcontrol中
                        IRasterDataset pOutResultDS = default(IRasterDataset);
                        pOutResultDS = (IRasterDataset)pDataset;
                        IRasterLayer pOutRasterLayer = default(IRasterLayer);
                        pOutRasterLayer = new RasterLayer();
                        pOutRasterLayer.CreateFromDataset(pOutResultDS);
                        //MapControlMain.AddLayer(pOutRasterLayer)
                        pCurMap.AddLayer(pOutRasterLayer);
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("保存计算结果为空,请输入结果文件名!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                //Interaction.MsgBox(ex.ToString);
            }
        }
Exemple #8
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                 pMapAlgebraOp= new RasterMapAlgebraOpClass();

                string strExpression = "";
                string dP = "",dCN="";
                dP = "[" + this.cmbPcp.Text + "]";
                dCN = "[" + this.cmbCN.Text + "]";

                ILayer pLayer = null;
                IRaster pRater = null;
                IGeoDataset pGeoDataset = null;
                pLayer = this.pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap,this.cmbPcp.Text));
                pRater=(pLayer as IRasterLayer).Raster;
                pGeoDataset = pRater as IGeoDataset;
                pMapAlgebraOp.BindRaster(pGeoDataset, pLayer.Name);

                pLayer = this.pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbCN.Text));
                pRater = (pLayer as IRasterLayer).Raster;
                pGeoDataset = pRater as IGeoDataset;
                pMapAlgebraOp.BindRaster(pGeoDataset, pLayer.Name);
                pLayer = null;
                pRater = null;
                pGeoDataset = null;
                // S=25400/CN-254
                // Q=(P-0.2S)^2/(P+0.8S)  P>0.2S
                // Q=0  P>0.2S

                //Con(P>0.2S,(P-0.2S)^2/(P+0.8S),0)
                //Con(P>0.2*(25400/CN-254),(P-0.2*(25400/CN-254))^2/(P+0.8*(25400/CN-254)),0)

                strExpression = "25400.0 / ("+dCN+" * 1.0) - 254";
                IGeoDataset pGeoDsS = pMapAlgebraOp.Execute(strExpression);
                pMapAlgebraOp.BindRaster(pGeoDsS, "S");

                string dS="[S]";
                string saveFileName = this.txtSavePath.Text.Trim();
                if (saveFileName == "")
                {
                    strExpression = "Con(" + dP + " > 0.2 * " + dS + ",(" + dP + " - 0.2 * " + dS + ") * (" + dP + " - 0.2 * " + dS + ") / (" + dP + " + 0.8 * " + dS + "),0)";

                }
                else
                {
                    strExpression =saveFileName+" = " +strExpression;
                }
                IGeoDataset pGeoDsQ = pMapAlgebraOp.Execute(strExpression);

                IRaster pOutRaster = pGeoDsQ as IRaster;
                IRasterLayer pOutRasterLayer = new RasterLayerClass();
                //pOutRasterLayer = SetStretchRenderer(pOutRaster);
                pOutRasterLayer.CreateFromRaster(pOutRaster);
                pMap.AddLayer(pOutRasterLayer);
                pGeoDsQ = null;
                pGeoDsS = null;
                pMapAlgebraOp = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        /// <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);
                }
            }
        }
Exemple #10
0
        public bool MergePixel(out string msg)
        {
            IMapAlgebraOp     pRSalgebra    = null;
            IRasterDataset    pRuleDataset  = null;
            IRasterDataset    pPixelDataset = null;
            IWorkspaceFactory pWKSF         = null;
            IWorkspace        pWorkspace    = null;
            string            bandDirectory = System.IO.Path.Combine(BLL.ConstDef.PATH_TEMP, DateTime.Now.ToFileTime().ToString());

            try
            {
                Directory.CreateDirectory(bandDirectory);
                List <string> uniqueVlaues = EngineAPI.GetRasterUniqueValue(_Hyper_PurePixel);
                pWKSF      = new RasterWorkspaceFactoryClass();
                pWorkspace = pWKSF.OpenFromFile(bandDirectory, 0);
                for (int i = 0; i < uniqueVlaues.Count; i++)
                {
                    if (uniqueVlaues[i] == "0")
                    {
                        continue;
                    }
                    string condition = "[PURE] == " + uniqueVlaues[i];
                    string exp       = "CON(" + condition + ",[PURE],[RULE])";

                    pRuleDataset  = EngineAPI.OpenRasterDataset(_Hyper_Rule, i);
                    pPixelDataset = EngineAPI.OpenRasterFile(_Hyper_PurePixel);
                    pRSalgebra    = new RasterMapAlgebraOpClass();

                    pRSalgebra.BindRaster(pPixelDataset as IGeoDataset, "PURE");
                    pRSalgebra.BindRaster(pRuleDataset as IGeoDataset, "RULE");

                    IGeoDataset resDataset   = pRSalgebra.Execute(exp);
                    string      bandFileName = "class_" + uniqueVlaues[i] + ".tif";
                    string      bandFile     = System.IO.Path.Combine(bandDirectory, bandFileName);

                    ISaveAs pSaveAs = resDataset as ISaveAs;

                    if (File.Exists(bandFile))
                    {
                        File.Delete(bandFile);
                    }

                    IDataset pDataset = pSaveAs.SaveAs(bandFileName, pWorkspace, "TIFF");
                    if (pDataset != null)
                    {
                        Marshal.ReleaseComObject(pDataset);
                    }
                    if (resDataset != null)
                    {
                        Marshal.ReleaseComObject(resDataset);
                    }
                    if (pSaveAs != null)
                    {
                        Marshal.ReleaseComObject(pSaveAs);
                    }
                }

                //layer stack
                string cmd = "bandcompose,'" + bandDirectory + "','" + _Result + "'";
                BLL.EnviVars.instance.IdlModel.Execute(cmd);
                msg = "";
                return(true);
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                return(false);
            }
            finally
            {
                if (pRuleDataset != null)
                {
                    Marshal.ReleaseComObject(pRuleDataset);
                }
                if (pPixelDataset != null)
                {
                    Marshal.ReleaseComObject(pPixelDataset);
                }
                if (pRSalgebra != null)
                {
                    Marshal.ReleaseComObject(pRSalgebra);
                }
                if (pWKSF != null)
                {
                    Marshal.ReleaseComObject(pWKSF);
                }
                if (pWorkspace != null)
                {
                    Marshal.ReleaseComObject(pWorkspace);
                }
            }
        }
Exemple #11
0
        /// <summary>
        /// 提取信息熵小于阈值的纯净像元.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public bool ExtractPurePixel(out string msg)
        {
            IMapAlgebraOp  pRSalgebra = null;
            IRasterDataset pRDEntropy = null;
            IRasterDataset pRDClass   = null;

            try
            {
                //运算公式
                string condition = "[ENTROPY] >= " + _Hyper_Entropy_T;
                //string expression = "SETNULL(" + condition +",[CLASS])";
                string expression = "CON(" + condition + ",0,[CLASS])";
                //文件绑定
                pRSalgebra = new RasterMapAlgebraOpClass();
                pRDEntropy = EngineAPI.OpenRasterFile(_Hyper_Entropy);
                pRSalgebra.BindRaster(pRDEntropy as IGeoDataset, "ENTROPY");
                pRDClass = EngineAPI.OpenRasterFile(_Hyper_Class);
                pRSalgebra.BindRaster(pRDClass as IGeoDataset, "CLASS");
                //执行运算
                IGeoDataset resDataset = pRSalgebra.Execute(expression);
                //另存结果
                _Hyper_PurePixel = System.IO.Path.Combine(BLL.ConstDef.PATH_TEMP, "PurePixel_" + DateTime.Now.ToFileTime().ToString() + ".tif");
                ISaveAs pSaveAs = resDataset as ISaveAs;
                if (File.Exists(_Hyper_PurePixel))
                {
                    File.Delete(_Hyper_PurePixel);
                }
                FileInfo          fInfo        = new FileInfo(_Hyper_PurePixel);
                IWorkspaceFactory pWKSF03      = new RasterWorkspaceFactoryClass();
                IWorkspace        pWorkspace03 = pWKSF03.OpenFromFile(fInfo.DirectoryName, 0);
                if (fInfo.Extension == ".img")
                {
                    IDataset pDataset = pSaveAs.SaveAs(fInfo.Name, pWorkspace03, "IMAGINE Image");
                    Marshal.ReleaseComObject(pDataset);
                }
                if (fInfo.Extension == ".tif")
                {
                    IDataset pDataset = pSaveAs.SaveAs(fInfo.Name, pWorkspace03, "TIFF");
                    Marshal.ReleaseComObject(pDataset);
                }
                if (resDataset != null)
                {
                    Marshal.ReleaseComObject(resDataset);
                }
                if (pSaveAs != null)
                {
                    Marshal.ReleaseComObject(pSaveAs);
                }
                msg = "";
                return(true);
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                return(false);
            }
            finally
            {
                if (pRDClass != null)
                {
                    Marshal.ReleaseComObject(pRDClass);
                }
                if (pRDEntropy != null)
                {
                    Marshal.ReleaseComObject(pRDEntropy);
                }
                if (pRSalgebra != null)
                {
                    Marshal.ReleaseComObject(pRSalgebra);
                }
            }
        }
Exemple #12
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);
                }
            }
        }
        private IRasterLayer calWSCons(string  _outPath)
        {
            //��դ�� pMapAlgebraOp
            try
            {
                if (pMapAlgebraOp==null)
                {
                    pMapAlgebraOp = new RasterMapAlgebraOpClass();
                }
                string strExp = "";
                string sRainPath = "";

                //��ֱ���ý�����ʴ�����ݻ��ǽ������ݼ���
                IRaster pRasterR = null;
                if (rbtnR.Checked)
                {
                    pRasterR = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbR.Text)) as IRasterLayer).Raster;
                    pMapAlgebraOp.BindRaster(pRasterR as IGeoDataset, "R");
                }
                else
                {
                    sRainPath = this.txtPcpPath.Text;
                    //
                    IWorkspaceFactory pWsF = new RasterWorkspaceFactoryClass();
                    IRasterWorkspace pRWs;
                    IRasterDataset pRDs = new RasterDatasetClass();
                    IRasterLayer pRLyr = new RasterLayerClass();

                    pRWs = pWsF.OpenFromFile(sRainPath, 0) as IRasterWorkspace;
                    string sFileName="";
                    for (int i = 1; i < 13; i++)
                    {
                        sFileName = this.txtPcpPrefix.Text + i.ToString() + this.txtPcpSuffix.Text;
                        pRDs = pRWs.OpenRasterDataset(sFileName);
                        pMapAlgebraOp.BindRaster(pRDs as IGeoDataset, sFileName);
                        strExp = strExp + "[" + sFileName + "] + ";
                    }
                    //cal total pcp
                    strExp = strExp.TrimEnd("+ ".ToCharArray());
                    IGeoDataset pGeoDsPcp = pMapAlgebraOp.Execute(strExp);
                    pMapAlgebraOp.BindRaster(pGeoDsPcp, "Pcp");

                    //cal Ri
                    strExp = "";
                    string sFileNameE = "",sDsName="",strExpR="";
                    for (int i = 1; i < 13; i++)
                    {
                        sFileName = this.txtPcpPrefix.Text + i.ToString() + this.txtPcpSuffix.Text;
                        sFileNameE="["+sFileName+"]";
                        strExp = "1.735 * pow(10,1.5 * log10((" + sFileNameE + " * " + sFileNameE + ") / [Pcp]) - 0.08188)";
                        IGeoDataset pGeoDsRi = pMapAlgebraOp.Execute(strExp);
                        sDsName = "Pcp" + i.ToString();
                        pMapAlgebraOp.BindRaster(pGeoDsRi, sDsName);
                        pMapAlgebraOp.UnbindRaster(sFileName);
                        strExpR = strExpR + "[" + sDsName + "] + ";
                    }
                    //cal R
                    strExpR = strExpR.TrimEnd("+ ".ToCharArray());
                    IGeoDataset pGeoDsR = pMapAlgebraOp.Execute(strExpR);
                    pMapAlgebraOp.BindRaster(pGeoDsR, "R");
                    for (int i = 1; i < 13; i++)
                    {
                        sDsName = "Pcp" + i.ToString();
                        pMapAlgebraOp.UnbindRaster(sDsName);
                    }
                }

                //����K����
                IRaster pRasterSclay = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbSoilClay.Text)) as IRasterLayer).Raster;
                pMapAlgebraOp.BindRaster(pRasterSclay as IGeoDataset, "clay");

                IRaster pRasterSsand = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbSoilSand.Text)) as IRasterLayer).Raster;
                pMapAlgebraOp.BindRaster(pRasterSsand as IGeoDataset, "sand");

                IRaster pRasterSslit = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbSoilSlit.Text)) as IRasterLayer).Raster;
                pMapAlgebraOp.BindRaster(pRasterSslit as IGeoDataset, "slit");

                IRaster pRasterSOrg = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbSoilOrganic.Text)) as IRasterLayer).Raster;
                pMapAlgebraOp.BindRaster(pRasterSOrg as IGeoDataset, "org");

                //cal K
                //K =(0.2 + 0.3 * exp(-0.0256 * soil_sand * (1.0 - soil_silt / 100.0))) * pow((soil_silt * 1.0 / (soil_clay * 1.0 + soil_silt * 1.0)),0.3) * (1.0 - 0.25 * soil_oc / (soil_oc * 1.0 + exp(3.72 - 2.95 * soil_oc))) * (1.0 - (0.7 * ksd) / (ksd + exp(-5.51 + 22.9 * ksd)))
                strExp = "(0.2 + 0.3 * Exp(-0.0256 * [sand] * (1.0 - [slit] / 100.0))) * Pow(([slit] * 1.0 / ([clay] * 1.0 + [slit] * 1.0)), 0.3) * (1.0 - 0.25 * [org] * 0.58 / ([org] * 0.58 + Exp(3.72 - 2.95 * [org] * 0.58))) * (1.0 - (0.7 * (1.0 - [sand] / 100.0)) / ((1.0 - [sand] / 100.0) + Exp(-5.51 + 22.9 * (1.0 - [sand] / 100.0))))";
                IGeoDataset pGeoDsK = pMapAlgebraOp.Execute(strExp);
                pMapAlgebraOp.BindRaster(pGeoDsK, "K");
                pMapAlgebraOp.UnbindRaster("clay");
                pMapAlgebraOp.UnbindRaster("sand");
                pMapAlgebraOp.UnbindRaster("slit");
                pMapAlgebraOp.UnbindRaster("org");

                //cal L*S
                IHydrologyOp pHydrologyOp = new RasterHydrologyOpClass();
                object objZLimit = System.Type.Missing;
                IRaster pRasterDem = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbDem.Text)) as IRasterLayer).Raster;
                //Fill Dem
                IGeoDataset pGeoDsDemFill =pHydrologyOp.Fill(pRasterDem as IGeoDataset,ref objZLimit);
                // cal FlowDirection
                IGeoDataset pGeoDsFlowDir = pHydrologyOp.Fill(pGeoDsDemFill, ref objZLimit);
                //cal FlowAccumulation
                IGeoDataset pGeoDsFlowAcc = pHydrologyOp.Fill(pGeoDsFlowDir, ref objZLimit);
                pMapAlgebraOp.BindRaster(pGeoDsFlowAcc, "FlowAcc");

                //cal Slope with Deg
                ISurfaceOp pSurfaceOp = new RasterSurfaceOpClass();
                object objZFactor = System.Type.Missing;
                IGeoDataset pGeoDsSlope = pSurfaceOp.Slope(pGeoDsDemFill, esriGeoAnalysisSlopeEnum.esriGeoAnalysisSlopeDegrees, ref objZFactor);
                // bind raster data "Slope"
                pMapAlgebraOp.BindRaster(pGeoDsSlope, "Slope");
                //cal S
                strExp = "Con([Slope] < 5,10.8 * Sin([Slope] * 3.14 / 180) + 0.03,[Slope] >= 10,21.9 * Sin([Slope] * 3.14 / 180) - 0.96,16.8 * Sin([Slope] * 3.14 / 180) - 0.5)";
                IGeoDataset pGeoDsS = pMapAlgebraOp.Execute(strExp);
                pMapAlgebraOp.BindRaster(pGeoDsS, "S");

                //cal m
                strExp = "Con([Slope] <= 1,0.2,([Slope] > 1 & [Slope] <= 3),0.3,[Slope] >= 5,0.5,0.4)";
                IGeoDataset pGeoDsM = pMapAlgebraOp.Execute(strExp);
                pMapAlgebraOp.BindRaster(pGeoDsM, "m");
                //cal ls
                strExp = "[S] * Pow(([FlowAcc] * "+this.txtCellSize.Text+" / 22.1),[m])";

                IGeoDataset pGeoDsLS = pMapAlgebraOp.Execute(strExp);
                pMapAlgebraOp.BindRaster(pGeoDsLS, "LS");

                pMapAlgebraOp.UnbindRaster("m");
                pMapAlgebraOp.UnbindRaster("S");
                pMapAlgebraOp.UnbindRaster("Slope");
                pMapAlgebraOp.UnbindRaster("FlowAcc");

                IRaster pRasterC = null;
                if (rbtnVegCover.Checked)
                {
                    pRasterC = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbC.Text)) as IRasterLayer).Raster;
                    pMapAlgebraOp.BindRaster(pRasterC as IGeoDataset, "C");
                }
                else
                {
                    //cal vegetation cover
                    IRaster pRasterNDVI = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbC.Text)) as IRasterLayer).Raster;
                    IRasterBandCollection pRasterBandCollection = pRasterNDVI as IRasterBandCollection;
                    IRasterBand pRasterBand = pRasterBandCollection.Item(0);
                    pRasterBand.ComputeStatsAndHist();
                    IRasterStatistics pRasterStatistics = pRasterBand.Statistics;
                    double dMax = pRasterStatistics.Maximum;
                    double dMin = pRasterStatistics.Minimum;
                    pMapAlgebraOp.BindRaster(pRasterNDVI as IGeoDataset, "NDVI");
                    if (dMin < 0)
                    {
                        dMin = 0;
                    }
                    //veg%yr% = (ndvi%yr% - ndvi%yr%min) / (ndvi%yr%max - ndvi%yr%min)
                    strExp = "([NDVI] - "+dMin+") / ("+dMax +" - "+dMin+")";
                    IGeoDataset pGeoDsC = pMapAlgebraOp.Execute(strExp);
                    pMapAlgebraOp.BindRaster(pGeoDsC, "C");
                    pMapAlgebraOp.UnbindRaster("NDVI");
                }
                //����P����
                IRaster pRasterP = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbP.Text)) as IRasterLayer).Raster;
                pMapAlgebraOp.BindRaster(pRasterP as IGeoDataset, "P");
                if (_outPath != "")
                {
                    strExp = _outPath + " = [R] * [K] * [LS] * (1 - [C]) * [P]";
                }
                else
                {
                    strExp = "[R] * [K] * [LS] * (1 - [C]) * [P]";
                }

                IGeoDataset pGeoDsSr = pMapAlgebraOp.Execute(strExp);
                IRaster pOutRaster = pGeoDsSr as IRaster;
                IRasterLayer pOutRasterLayer = new RasterLayerClass();
                pOutRasterLayer.CreateFromRaster(pOutRaster);
                //��������
                //string strOutDir = _outPath.Substring(0, _outPath.LastIndexOf("\\"));
                int startX = _outPath.LastIndexOf("\\");
                int endX = _outPath.Length;
                string sRLyrName = _outPath.Substring(startX + 1, endX - startX - 1);
                pOutRasterLayer.Name = sRLyrName;
                pMapAlgebraOp = null;
                return pOutRasterLayer;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                return null;
            }
        }
Exemple #14
0
        /// <summary>
        /// Calculates the specified veg index.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="inFile">The in file.</param>
        /// <param name="outFile">The out file.</param>
        /// <param name="redIndex">Index of the red.</param>
        /// <param name="nirIndex">Index of the nir.</param>
        /// <param name="blueIndex">Index of the blue.</param>
        /// <param name="land">The land index.</param>
        /// <exception cref="System.Exception">不支持的植被指数</exception>
        public static void Calculate(VegIndex index, string inFile, string outFile, int redIndex, int nirIndex, int blueIndex = -1, float land = -1f)
        {
            IMapAlgebraOp     pRSalgebra = null;
            IGeoDataset       resDataset = null;
            ISaveAs           pSaveAs    = null;
            IWorkspaceFactory pWKSF      = null;
            IWorkspace        pWorkspace = null;
            string            expression = string.Empty;

            try
            {
                pRSalgebra = new RasterMapAlgebraOpClass();
                switch (index)
                {
                case VegIndex.DVI:
                    expression = "[NIR] - [RED]";
                    break;

                case VegIndex.EVI:
                    expression = "2.5 * ([NIR] - [RED]) / ([NIR] + 6.0 * [RED] - 7.5 * [BLUE] + 1)";
                    pRSalgebra.BindRaster(GFS.Common.EngineAPI.OpenRasterDataset(inFile, blueIndex) as IGeoDataset, "BLUE");
                    break;

                case VegIndex.NDVI:
                    expression = "([NIR] - [RED]) / ([NIR] + [RED])";
                    break;

                case VegIndex.RVI:
                    expression = "[NIR] / [RED]";
                    break;

                case VegIndex.SAVI:
                    expression = "([NIR] - [RED]) * (1 + " + land + ") / ([NIR] + [RED] + " + land + ")";
                    break;

                default:
                    throw new Exception("不支持的植被指数");
                }
                pRSalgebra.BindRaster(GFS.Common.EngineAPI.OpenRasterDataset(inFile, redIndex) as IGeoDataset, "RED");
                pRSalgebra.BindRaster(GFS.Common.EngineAPI.OpenRasterDataset(inFile, nirIndex) as IGeoDataset, "NIR");
                resDataset = pRSalgebra.Execute(expression);
                pSaveAs    = resDataset as ISaveAs;
                if (File.Exists(outFile))
                {
                    File.Delete(outFile);
                }
                FileInfo fInfo = new FileInfo(outFile);
                pWKSF      = new RasterWorkspaceFactoryClass();
                pWorkspace = pWKSF.OpenFromFile(fInfo.DirectoryName, 0);
                if (fInfo.Extension == ".img")
                {
                    pSaveAs.SaveAs(fInfo.Name, pWorkspace, "IMAGINE Image");
                }
                if (fInfo.Extension == ".tif")
                {
                    pSaveAs.SaveAs(fInfo.Name, pWorkspace, "TIFF");
                }
            }
            catch (Exception ex)
            {
                GFS.BLL.Log.WriteLog(typeof(VegetationIndex), ex);
                throw ex;
            }
            finally
            {
                if (pSaveAs != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pSaveAs);
                }
                if (resDataset != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(resDataset);
                }
                if (pRSalgebra != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pRSalgebra);
                }
                if (pWorkspace != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspace);
                }
                if (pWKSF != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pWKSF);
                }
            }
        }
Exemple #15
0
        private IRasterLayer calTerrain(string _outPath)
        {
            //绑定栅格到 pMapAlgebraOp
            try
            {
                if (pMapAlgebraOp == null)
                {
                    pMapAlgebraOp = new RasterMapAlgebraOpClass();
                }
                string strExp = "";
                //dem
                IRaster pRasterDem = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbDem.Text)) as IRasterLayer).Raster;
                IGeoDataset pGeoDsDem = pRasterDem as IGeoDataset;
                // bind raster data "Dem"
                pMapAlgebraOp.BindRaster(pGeoDsDem, "Dem");

                //cal Slope with Deg
                ISurfaceOp pSurfaceOp = new RasterSurfaceOpClass();
                object objZFactor = System.Type.Missing;
                IGeoDataset pGeoDsSlope = pSurfaceOp.Slope(pRasterDem as IGeoDataset, esriGeoAnalysisSlopeEnum.esriGeoAnalysisSlopeDegrees, ref objZFactor);

                // bind raster data "Slope"
                pMapAlgebraOp.BindRaster(pGeoDsSlope, "Slope");

                //cal Slo dem
                string demStart = this.txtDemStart.Text;
                string demEnd = this.txtDemEnd.Text;

                string sloStart = this.txtSloStart.Text;
                string sloEnd = this.txtSloEnd.Text;

                //strExp = "Con([Dem] < " + demStart + ",0,[Dem] > " + demEnd + ",0,1)";
                //IGeoDataset pGeoDsDemC = pMapAlgebraOp.Execute(strExp);
                //pMapAlgebraOp.BindRaster(pGeoDsDemC, "DemC");

                ////限制区为1,非限制区为0
                //strExp = "Con([Slope] < " + sloStart + ",0,[Slope] > "+sloEnd+",0,1)";
                //IGeoDataset pGeoDsSloC = pMapAlgebraOp.Execute(strExp);
                //pMapAlgebraOp.BindRaster(pGeoDsSloC, "SloC");

                //cal in together
                strExp = "Con((([Slope] > " + sloStart + " & [Slope] < " + sloEnd + ") & ([Dem] > " + demStart + " & [Dem] < " + demEnd + ")),1,0)";
                IGeoDataset pGeoDsC = pMapAlgebraOp.Execute(strExp);
                pMapAlgebraOp.BindRaster(pGeoDsC, "TerrainRedL");

                pMapAlgebraOp.UnbindRaster("Dem");
                pMapAlgebraOp.UnbindRaster("Slope");

                IRaster pOutRaster = pGeoDsC as IRaster;
                IRasterLayer pOutRasterLayer = new RasterLayerClass();
                pOutRasterLayer.CreateFromRaster(pOutRaster);
                //数据名称
                //string strOutDir = _outPath.Substring(0, _outPath.LastIndexOf("\\"));
                int startX = _outPath.LastIndexOf("\\");
                int endX = _outPath.Length;
                string sRLyrName = _outPath.Substring(startX + 1, endX - startX - 1);
                pOutRasterLayer.Name = sRLyrName;
                pMapAlgebraOp = null;
                return pOutRasterLayer;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                return null;
            }
        }