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); } } }
private void btn_SST_Click(object sender, EventArgs e) { geodataset22 = getRstDataset(tb_B22.Text) as IGeoDataset; geodataset23 = getRstDataset(tb_B23.Text) as IGeoDataset; geodatasetSZ = getRstDataset(tb_SZ.Text) as IGeoDataset; IMapAlgebraOp RSalgebra = new RasterMapAlgebraOpClass(); RSalgebra.BindRaster(geodataset22, "rst22"); RSalgebra.BindRaster(geodataset23, "rst23"); RSalgebra.BindRaster(geodatasetSZ, "rstsz"); string cal = "0.677 + 1.026 * [rst22] + 0.469 * ([rst22] - [rst23]) + 1.470 / Cos([rstsz] * 0.01 * 3.1415926 / 180) - 0.1"; result = RSalgebra.Execute(cal); IRasterLayer pOutRL = new RasterLayerClass(); pOutRL.CreateFromRaster(result as IRaster); ILayer layer = pOutRL as ILayer; IRasterLayer rst = new RasterLayerClass(); rst.CreateFromRaster(result as IRaster); main_frm.addrstLayer(rst); main_frm.createSSTrst(layer); // main_frm.addshpLayer(); this.Close(); }
/// <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); } } }
private void button3_Click(object sender, EventArgs e) { string inputFullFilePath = textBox1.Text; string outputFullFilePath = textBox2.Text; // deal with the outputFullFilePath int _index = outputFullFilePath.LastIndexOf("\\"); string outputRasterName = (outputFullFilePath.Substring(_index + 1)); //name string outputRasterDirectory = outputFullFilePath.Remove(_index); //directory //deal with input IRasterLayer pRasterLayer = new RasterLayerClass(); pRasterLayer.CreateFromFilePath(inputFullFilePath); IRaster raster = pRasterLayer.Raster; IMapAlgebraOp mapAlgebraOp; mapAlgebraOp = new RasterMapAlgebraOpClass(); //控制raster分析的环境 IRasterAnalysisEnvironment rasterAnalysisEnvironment = default(IRasterAnalysisEnvironment); rasterAnalysisEnvironment = (IRasterAnalysisEnvironment)mapAlgebraOp; IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); IWorkspace workspace = workspaceFactory.OpenFromFile(outputRasterDirectory, 0);//这里应该是输出raster的路径 rasterAnalysisEnvironment.OutWorkspace = workspace; try { mapAlgebraOp.BindRaster((IGeoDataset)raster, "this"); //定义表达式(elevationMode为要减去的数值)不要忘了"[ ]" //"CON(ISNULL([this]),0,[this])" //"~(([this]>=92)&([this]<=272))&([this]>=2)" string strOut = textBox3.Text; IRaster outRaster = (IRaster)mapAlgebraOp.Execute(strOut); ISaveAs2 saveAs; saveAs = (ISaveAs2)outRaster; saveAs.SaveAs(outputRasterName, workspace, "TIFF");//输出名称(注意:名称中加后缀名,例:test.tif),工作空间,格式 MessageBox.Show("栅格计算器成功!"); IRasterLayer pRlayer = new RasterLayer(); pRlayer.CreateFromRaster(outRaster); pRlayer.Name = System.IO.Path.GetFileName(outputFullFilePath); mainForm.axMapControl1.AddLayer(pRlayer, 0); } catch (Exception ex) { } }
//栅格图层的地图代数运算,返回运算结果 public static IGeoDataset rasterMapAlgebra(IRasterLayer pRL_Update, IRasterLayer pRL_Base) { IRaster pRS_Update = pRL_Update.Raster; IRaster pRS_Base = pRL_Base.Raster; IGeoDataset pGDT_Update = pRS_Update as IGeoDataset; IGeoDataset pGDT_Base = pRS_Base as IGeoDataset; //地图代数接口 IMapAlgebraOp pMapAlgebraOp = new RasterMapAlgebraOpClass(); pMapAlgebraOp.BindRaster(pGDT_Update, "raster_update"); pMapAlgebraOp.BindRaster(pGDT_Base, "raster_base"); //地图代数运算 IGeoDataset pOutDifGeoDT = pMapAlgebraOp.Execute("[raster_update] - [raster_base]"); return(pOutDifGeoDT); }
public void Execute(string fileName) { try { //组合公式 string expression = "Con(" + root.expression + "," + root.NodeName + "TRUE," + root.NodeName + "FALSE)"; ExpCombine(root.lChild, root.rChild, ref expression); //绑定文件 IMapAlgebraOp pRSalgebra = new RasterMapAlgebraOpClass(); foreach (DataRow row in variable.Rows) { pRSalgebra.BindRaster(OpenRasterDataset(row[1].ToString()) as IGeoDataset, row[0].ToString()); } IGeoDataset resDataset = pRSalgebra.Execute(expression); ISaveAs pSaveAs = resDataset as ISaveAs; if (File.Exists(fileName)) { File.Delete(fileName); } FileInfo fInfo = new FileInfo(fileName); IWorkspaceFactory pWKSF03 = new RasterWorkspaceFactoryClass(); IWorkspace pWorkspace03 = pWKSF03.OpenFromFile(fInfo.DirectoryName, 0); if (fInfo.Extension == ".img") { pSaveAs.SaveAs(fInfo.Name, pWorkspace03, "IMAGINE Image"); } if (fInfo.Extension == ".tif") { pSaveAs.SaveAs(fInfo.Name, pWorkspace03, "TIFF"); } } catch (Exception ex) { throw ex; } }
/// <summary> /// 混合图斑分类修正 /// </summary> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool ClassMerge() { //string inFiles = _entropy+ "," + _classPixel+ "," + _classFeature; //string exp = "(b1 gt 1.2) * b2 + (b1 le 1.2) * b3"; //string cmd = BLL.ConstDef.IDL_FUN_BANDMATH + ",'" + inFiles + "','" + _class + "','" + exp + "'"; //BLL.EnviVars.instance.IdlModel.Execute(cmd); string expression = "Con([entropy] > " + _entropyT + ",[pixel],[feature])"; try { IMapAlgebraOp pRSalgebra = new RasterMapAlgebraOpClass(); pRSalgebra.BindRaster(EngineAPI.OpenRasterFile(_entropy) as IGeoDataset, "entropy"); pRSalgebra.BindRaster(EngineAPI.OpenRasterFile(_classPixel) as IGeoDataset, "pixel"); pRSalgebra.BindRaster(EngineAPI.OpenRasterFile(_classFeature) as IGeoDataset, "feature"); IGeoDataset resDataset = pRSalgebra.Execute(expression); ISaveAs pSaveAs = resDataset as ISaveAs; if (File.Exists(_class)) { File.Delete(_class); } FileInfo fInfo = new FileInfo(_class); IWorkspaceFactory pWKSF03 = new RasterWorkspaceFactoryClass(); IWorkspace pWorkspace03 = pWKSF03.OpenFromFile(fInfo.DirectoryName, 0); if (fInfo.Extension == ".img") { pSaveAs.SaveAs(fInfo.Name, pWorkspace03, "IMAGINE Image"); } if (fInfo.Extension == ".tif") { pSaveAs.SaveAs(fInfo.Name, pWorkspace03, "TIFF"); } return(true); } catch (Exception ex) { throw ex; } }
/// <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); } } }
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); } } }
/// <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); } } }
/// <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); } } }
/// <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); } } }
private void BtnOK_Click(object sender, EventArgs e) { int i = 0; int j = 0; if (string.IsNullOrEmpty(this.ComboBoxInLayer.Text)) { MessageBox.Show("请选择需要二值化的图层!", "栅格二值化", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } int intLyrCount = frm.mainMapControl.LayerCount; for (i = 0; i <= intLyrCount - 1; i++) { if ((frm.mainMapControl.Map.get_Layer(i).Name == this.ComboBoxInLayer.Text.Trim())) { IRasterLayer pRasterLayer = frm.mainMapControl.get_Layer(i) as IRasterLayer; IRaster2 raster = pRasterLayer.Raster as IRaster2; IRasterDataset rasterDataset = raster.RasterDataset; IRasterBandCollection dirBandCollection = (IRasterBandCollection)rasterDataset; IRasterBand dirRasterBand = dirBandCollection.Item(0); IRawPixels dirRawPixels = (IRawPixels)dirRasterBand; IPnt pixelBlockOrigin = null; IPnt dirBlockSize = new DblPntClass(); IRasterProps dirProps = (IRasterProps)dirRawPixels; int dirColumns = dirProps.Width; //列数 int dirRows = dirProps.Height; //行数 IPixelBlock3 dirPixelBlock = (IPixelBlock3)dirRawPixels.CreatePixelBlock(dirBlockSize); dirRawPixels.Read(pixelBlockOrigin, (IPixelBlock)dirPixelBlock); System.Array array; array = (System.Array)dirPixelBlock.get_PixelDataByRef(0);//获取栅格数组 int min; if (textBox1.Text == "") { min = 20; } else { min = Convert.ToInt32(textBox1.Text); } /////获得栅格数据像元值 double[,] b = new double[dirRows, dirColumns]; double value; string strExcute = null; for (int row = 0; row < dirRows; row++) { for (int col = 0; col < dirColumns; col++) { b[row, col] = Convert.ToSingle(array.GetValue(col, row)); /////阈值判断 if (b[row, col] > min) { value = 1; } else { value = 0; } strExcute = Convert.ToString(value); } } IGeoDataset tempGeodata1 = (IGeoDataset)pRster; IMapAlgebraOp pMapAlgebraOp = new RasterMapAlgebraOpClass(); //设置栅格运算空间 IRasterAnalysisEnvironment pRasterAnalysisEnvironment = (IRasterAnalysisEnvironment)pMapAlgebraOp; IWorkspaceFactory pWorkspaceFactory = new RasterWorkspaceFactoryClass(); string outPath = null; string outLayerName = null; outPath = this.TxtBox.Text.Trim(); j = this.TxtBox.Text.Trim().LastIndexOf("\\"); outPath = this.TxtBox.Text.Substring(0, j); outLayerName = this.TxtBox.Text.Substring(j + 1); string DataSetName = this.TxtBox.Text.Substring(j + 1); //设置输出空间 IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(outPath, 0); pRasterAnalysisEnvironment.OutWorkspace = pWorkspace; IGeoDataset outGetDataset = pMapAlgebraOp.Execute(strExcute); IRasterLayer pCreatRalyr = new RasterLayerClass(); pCreatRalyr.CreateFromRaster((IRaster)outGetDataset); pCreatRalyr.Name = outLayerName; frm.mainMapControl.AddLayer(pCreatRalyr); break; } } }
/// <summary> /// 道路风险计算 /// </summary> /// <param name="workPath">存储路径</param> /// <param name="roadEvalPath">道路评价结果 </param> /// <param name="roadRainsShpPath">加了雨量字段的道路缓冲区</param> /// <returns></returns> public bool RoadRaskCaulte(string roadEvalName, string roadRainsName, string saveWorkspace) { //读取 道路评价结果栅格数据的信息 RasterHelper rh = new RasterHelper(); //IRasterWorkspace rasterWorkspace = new RasterLayer(); IWorkspaceFactory rWorkspaceFactory = new RasterWorkspaceFactory(); IWorkspace SWorkspace = rWorkspaceFactory.OpenFromFile(saveWorkspace, 0); IRasterWorkspace rasterWorkspace = SWorkspace as IRasterWorkspace; IRasterDataset rasterDt = rasterWorkspace.OpenRasterDataset(roadEvalName); // var t = rh.GetRasterProps(rasterDt); IRasterLayer rasterLayer = new RasterLayer(); rasterLayer.CreateFromFilePath(saveWorkspace + "\\" + roadEvalName); IRaster pRaster = rasterLayer.Raster; IRasterProps rasterProps = (IRasterProps)pRaster;;//存储了栅格信息 //初始化GP工具 Geoprocessor gp = new Geoprocessor(); gp.OverwriteOutput = true; //string path = @"D:\GISTest"; //gp.SetEnvironmentValue("workspace", path); //道路缓冲区 根据雨量转栅格 FeatureToRaster featureToRaster = new FeatureToRaster(); featureToRaster.cell_size = rasterProps.MeanCellSize().X;//这里可以提前规定一个值,而不是每次去读取 featureToRaster.in_features = OpenFeatureClass(saveWorkspace + "\\" + roadRainsName); featureToRaster.out_raster = saveWorkspace + "\\roadGrid"; featureToRaster.field = "RAINS";//这个字段需要矢量图层中加上 try { gp.Execute(featureToRaster, null); } catch (Exception ex) { Console.WriteLine("矢量转栅格失败!"); return(false); } //栅格计算器 计算风险级数 IMapAlgebraOp mapAlgebra = new RasterMapAlgebraOpClass(); IRasterDataset roadEvalRaster = OpenRasterDataSet(rasterWorkspace, roadEvalName); IRasterDataset roadGridRaster = OpenRasterDataSet(rasterWorkspace, saveWorkspace + @"\roadGrid"); IGeoDataset geo1 = roadEvalRaster as IGeoDataset; IGeoDataset geo2 = roadGridRaster as IGeoDataset; mapAlgebra.BindRaster(geo1, "EvalRaster"); mapAlgebra.BindRaster(geo2, "RoadRains"); IGeoDataset raskDataset = mapAlgebra.Execute("[EvalRaster] * [RoadRains] / 25");//然后存储 表达式必须间隔开 ISaveAs saveAs = raskDataset as ISaveAs; saveAs.SaveAs("roadPre", SWorkspace, ""); //加入图层 IRasterLayer rasterLayer2 = new RasterLayer(); rasterLayer2.CreateFromFilePath(saveWorkspace + "\\" + "roadPre"); MainFrom.m_mapControl.AddLayer(rasterLayer2, 0); //MainFrom.m_mapControl.Refresh(esriViewDrawPhase.esriViewGeography, null, null); MainFrom.m_pTocControl.Update(); //将生成的风险栅格重分类 //<0.2 一级:可能性小 //0.2-0.4 二级:可 //能性较小 //0.4-0.6 三级:可能性较大 //0.6-0.8 四级:可能性大 //>0.8 五级:可能性很大 // 输入:raskDataset // 输出:geoDataset_result IReclassOp pReclassOp = new RasterReclassOpClass(); INumberRemap pNumRemap = new NumberRemapClass(); IDictionary <int, RoadRange> roadRanges = this.roadRiskConfig.GetRoadRiskLevelFromConfig(); foreach (var v in roadRanges) { pNumRemap.MapRange(v.Value.MinValue, v.Value.MaxValue, v.Key); } /* * pNumRemap.MapRange(0, 0.2, 1); * pNumRemap.MapRange(0.2, 0.4, 2); * pNumRemap.MapRange(0.4, 0.6, 3); * pNumRemap.MapRange(0.6, 0.8, 4); * pNumRemap.MapRange(0.8,1000,5); */ //pNumRemap.MapRangeToNoData(-1000,0); //pNumRemap.MapRangeToNoData(1000, 20000); IRemap pRemap = pNumRemap as IRemap; // 重分类 // geoDataset为上一步得到的栅格 // IGeoDataset geoDataset_result = pReclassOp.ReclassByRemap(raskDataset, pRemap, true);//还没有测试成功 // RasterCalculator rasterCalculator = new RasterCalculator("[EvalRaster]*[RoadRains]/25", saveWorkspace + @"\RainEval.tif"); try { // gp.Execute(rasterCalculator, null); } catch (Exception ex) { Debug.Print(ex.Message); for (int i = 0; i < gp.MessageCount; i++) { Debug.Print(gp.GetMessage(i)); } Console.WriteLine("栅格计算失败!"); return(false); } return(true); }
/// <summary> /// 风险因素等级划分 /// </summary> /// <param name="RasterWorkspace"></param> /// <param name="roadEvalName"></param> /// <returns></returns> public static bool RaskCaulte(string RasterWorkspace, string roadEvalName) { IWorkspaceFactory rWorkspaceFactory = new RasterWorkspaceFactory(); IWorkspace SWorkspace = rWorkspaceFactory.OpenFromFile(RasterWorkspace, 0); IRasterWorkspace rasterWorkspace = SWorkspace as IRasterWorkspace; //栅格计算器 计算风险级数 先不要签你的三方协议 然后存储 表达式必须隔开 IMapAlgebraOp mapAlgebra = new RasterMapAlgebraOpClass(); IRasterDataset roadEvalRaster = rasterWorkspace.OpenRasterDataset(roadEvalName); IGeoDataset geo1 = roadEvalRaster as IGeoDataset; mapAlgebra.BindRaster(geo1, "EvalRaster"); IGeoDataset raskDataset = mapAlgebra.Execute("[EvalRaster]");//然后存储 表达式必须间隔开 //将生成的风险栅格重分类 //<0.2 一级:可能性小 //0.2-0.4 二级:可 //能性较小 //0.4-0.6 三级:可能性较大 //0.6-0.8 四级:可能性大 //>0.8 五级:可能性很大 // 输入:raskDataset // 输出:geoDataset_result IRasterBandCollection pRsBandCol = raskDataset as IRasterBandCollection; IRasterBand pRasterBand = pRsBandCol.Item(0); pRasterBand.ComputeStatsAndHist(); IRasterStatistics pRasterStatistic = pRasterBand.Statistics; double dMaxValue = pRasterStatistic.Maximum; double dMinValue = pRasterStatistic.Minimum; IReclassOp pReclassOp = new RasterReclassOpClass(); INumberRemap pNumRemap = new NumberRemapClass(); //等级参数 List <double> raskRanges = new List <double>(); for (int n = 0; n < 5; n++) { raskRanges.Add(dMinValue + (dMaxValue - dMinValue) * n / 4); } for (int m = 0; m < 4; m++) { pNumRemap.MapRange(raskRanges[m], raskRanges[m + 1], m + 1); } IRemap pRemap = pNumRemap as IRemap; IRaster pOutRaster = pReclassOp.ReclassByRemap(raskDataset, pRemap, false) as IRaster; IRasterLayer rasterLayer = new RasterLayerClass(); rasterLayer.CreateFromRaster(pOutRaster); if (rasterLayer != null) { rasterLayer.Name = roadEvalName; for (int i = 0; i < MainFrom.m_mapControl.LayerCount; i++) { ILayer ComLayer = MainFrom.m_mapControl.get_Layer(i); if (rasterLayer.Name == ComLayer.Name) { MainFrom.m_mapControl.Map.DeleteLayer(ComLayer); } } Common.funColorForRaster_Classify(rasterLayer, 4); IEnvelope envelope = rasterLayer.AreaOfInterest; MainFrom.m_mapControl.ActiveView.Extent = envelope;//缩放至图层 } return(true); }
/// <summary> /// 生成风险等级栅格 /// </summary> /// <param name="roadEvalName"></param> /// <param name="rains"></param> /// <param name="saveWorkspace"></param> /// <returns></returns> public bool RoadRaskCaulte(string roadEvalName, double rains, string saveWorkspace) { #region 备份 // IWorkspaceFactory rWorkspaceFactory = new RasterWorkspaceFactory(); // IWorkspace SWorkspace = rWorkspaceFactory.OpenFromFile(saveWorkspace, 0); // IRasterWorkspace rasterWorkspace = SWorkspace as IRasterWorkspace; // //栅格计算器 计算风险级数 先不要签你的三方协议 然后存储 表达式必须隔开 // IMapAlgebraOp mapAlgebra = new RasterMapAlgebraOpClass(); // IRasterDataset roadEvalRaster = OpenRasterDataSet(rasterWorkspace, roadEvalName); // IGeoDataset geo1 = roadEvalRaster as IGeoDataset; // mapAlgebra.BindRaster(geo1, "EvalRaster"); // IGeoDataset raskDataset = mapAlgebra.Execute("[EvalRaster] * 10 / 25");//然后存储 表达式必须间隔开 // //将生成的风险栅格重分类 // //<0.2 一级:可能性小 // //0.2-0.4 二级:可 // //能性较小 // //0.4-0.6 三级:可能性较大 // //0.6-0.8 四级:可能性大 // //>0.8 五级:可能性很大 // // 输入:raskDataset //// 输出:geoDataset_result // IRasterBandCollection pRsBandCol = raskDataset as IRasterBandCollection; // IRasterBand pRasterBand = pRsBandCol.Item(0); // pRasterBand.ComputeStatsAndHist(); // IRasterStatistics pRasterStatistic = pRasterBand.Statistics; // double dMaxValue = pRasterStatistic.Maximum; // double dMinValue = pRasterStatistic.Minimum; // IReclassOp pReclassOp = new RasterReclassOpClass(); // INumberRemap pNumRemap = new NumberRemapClass(); // //pNumRemap.MapRange(dMinValue, 0.2, 1); // //pNumRemap.MapRange(0.2, 0.4, 2); // //pNumRemap.MapRange(0.4, 0.6, 3); // //pNumRemap.MapRange(0.6, dMaxValue, 4); // //pNumRemap.MapRangeToNoData(-1000, 0); // //pNumRemap.MapRange(0.8, 1000, 5); // pNumRemap.MapRange(dMinValue,0.2, 0); // pNumRemap.MapRange(0.2, 0.4, 1); // pNumRemap.MapRange(0.4, 0.6, 2); // pNumRemap.MapRange(0.6, 0.8, 3); // pNumRemap.MapRange(0.8, dMaxValue, 4); // IRemap pRemap = pNumRemap as IRemap; // //IGeoDataset geoDataset_result = pReclassOp.ReclassByRemap(raskDataset, pRemap, true); // IRaster pOutRaster = pReclassOp.ReclassByRemap(raskDataset, pRemap, false) as IRaster; // IRasterLayer rasterLayer = new RasterLayerClass(); // rasterLayer.CreateFromRaster(pOutRaster); // if (rasterLayer != null) // { // //string fullPath = Common.RoadshapePath+"道路.shp"; // rasterLayer.Name = "公路风险"; // Boolean IsEqual = false; // //int Position = fullPath.LastIndexOf("\\"); // ////文件目录 // //string FilePath = fullPath.Substring(0, Position); // //string ShpName = fullPath.Substring(Position + 1); // //IWorkspaceFactory pWF; // //pWF = new ShapefileWorkspaceFactory(); // //IFeatureWorkspace pFWS; // //pFWS = (IFeatureWorkspace)pWF.OpenFromFile(FilePath, 0); // //IFeatureClass pFClass; // //pFClass = pFWS.OpenFeatureClass(ShpName); // //IFeatureLayer pFLayer = new FeatureLayer(); // //pFLayer.FeatureClass = pFClass; // for (int i = 0; i < MainFrom.m_mapControl.LayerCount;i++ ) // { // ILayer ComLayer=MainFrom.m_mapControl.get_Layer(i); // if (rasterLayer.Name == ComLayer.Name) // { // IsEqual = true; // } // } // if (!IsEqual) // { // //MainFrom.m_mapControl.AddLayer((ILayer)pFLayer); // MainFrom.m_mapControl.AddLayer(rasterLayer); // IEnvelope envelope = rasterLayer.AreaOfInterest; // MainFrom.m_mapControl.ActiveView.Extent = envelope;//缩放至图层 // } // } // return true; #endregion IWorkspaceFactory rWorkspaceFactory = new RasterWorkspaceFactory(); IWorkspace SWorkspace = rWorkspaceFactory.OpenFromFile(saveWorkspace, 0); IRasterWorkspace rasterWorkspace = SWorkspace as IRasterWorkspace; //栅格计算器 计算风险级数 先不要签你的三方协议 然后存储 表达式必须隔开 IMapAlgebraOp mapAlgebra = new RasterMapAlgebraOpClass(); IRasterDataset roadEvalRaster = OpenRasterDataSet(rasterWorkspace, roadEvalName); IGeoDataset geo1 = roadEvalRaster as IGeoDataset; mapAlgebra.BindRaster(geo1, "EvalRaster"); IGeoDataset raskDataset = mapAlgebra.Execute("[EvalRaster] * " + rains + " / 25");//然后存储 表达式必须间隔开 //将生成的风险栅格重分类 //<0.2 一级:可能性小 //0.2-0.4 二级:可 //能性较小 //0.4-0.6 三级:可能性较大 //0.6-0.8 四级:可能性大 //>0.8 五级:可能性很大 // 输入:raskDataset // 输出:geoDataset_result IRasterBandCollection pRsBandCol = raskDataset as IRasterBandCollection; IRasterBand pRasterBand = pRsBandCol.Item(0); pRasterBand.ComputeStatsAndHist(); IRasterStatistics pRasterStatistic = pRasterBand.Statistics; double dMaxValue = pRasterStatistic.Maximum; double dMinValue = pRasterStatistic.Minimum; IReclassOp pReclassOp = new RasterReclassOpClass(); INumberRemap pNumRemap = new NumberRemapClass(); IDictionary <int, RoadRange> roadRanges = this.roadRiskConfig.GetRoadRiskLevelFromConfig(); foreach (var v in roadRanges) { pNumRemap.MapRange(v.Value.MinValue, v.Value.MaxValue, v.Key); } IRemap pRemap = pNumRemap as IRemap; //IGeoDataset geoDataset_result = pReclassOp.ReclassByRemap(raskDataset, pRemap, true); IRaster pOutRaster = pReclassOp.ReclassByRemap(raskDataset, pRemap, false) as IRaster; IRasterLayer rasterLayer = new RasterLayerClass(); rasterLayer.CreateFromRaster(pOutRaster); if (rasterLayer != null) { rasterLayer.Name = "公路风险"; for (int i = 0; i < MainFrom.m_mapControl.LayerCount; i++) { ILayer ComLayer = MainFrom.m_mapControl.get_Layer(i); if (rasterLayer.Name == ComLayer.Name) { MainFrom.m_mapControl.Map.DeleteLayer(ComLayer); } } Common.funColorForRaster_Classify(rasterLayer, roadRanges.Count); IEnvelope envelope = rasterLayer.AreaOfInterest; MainFrom.m_mapControl.ActiveView.Extent = envelope;//缩放至图层 } return(true); }