/// <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)); }
/// <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); } }
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()); } }