/// <summary> /// 用gp工具查重叠 /// </summary> /// <param name="idname"></param> /// <param name="pLayer"></param> /// <returns></returns> public List <ErrorEntity> CheckOverLap11(string idname, IFeatureLayer pLayer) { List <ErrorEntity> listErrorEntity = new List <ErrorEntity>(); //输出要素层设置 ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); intersect.in_features = pLayer;//"D://开阳县_520121_YZL_2017.mdb//YZL_PY_FYYSXB"; intersect.out_feature_class = "D://report//temp.shp"; Geoprocessor geoProcessor = new Geoprocessor(); geoProcessor.OverwriteOutput = true; try { geoProcessor.Execute(intersect, null); } catch (Exception ex) { // Print a generic exception message. Console.WriteLine(ex.Message); } return(listErrorEntity); }
private void btnIntersect_Click(object sender, EventArgs e) { IGpValueTableObject valTbl = new GpValueTableObjectClass(); valTbl.SetColumns(2); object row = ""; object rank = 1; //row = inputFeatClass; row = pdicLayer[this.comboBoxEx1.SelectedItem.ToString()]; valTbl.SetRow(0, ref row); valTbl.SetValue(0, 1, ref rank); //row = clipFeatClass; row = pdicLayer[this.comboBoxEx2.SelectedItem.ToString()]; valTbl.SetRow(1, ref row); rank = 2; valTbl.SetValue(1, 1, ref rank); //构造Geoprocessor Geoprocessor gp = new Geoprocessor(); gp.OverwriteOutput = true; //设置参数 ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); //intersect.in_features = @"F:\foshan\Data\wuqutu_b.shp;F:\foshan\Data\world30.shp"; //intersect.in_features = this.comboBoxEx1.SelectedItem + ";" + this.comboBoxEx2.SelectedItem; //intersect.in_features = pdic[this.comboBoxEx1.SelectedItem.ToString()] + ";" + pdic[this.comboBoxEx2.SelectedItem.ToString()]; intersect.in_features = valTbl; //intersect.out_feature_class = @"E:\intersect.shp"; intersect.out_feature_class = this.textBoxX3.Text; intersect.join_attributes = "ALL"; intersect.output_type = "INPUT"; //执行Intersect工具 //RunTool(gp, intersect, null); try { gp.Execute(intersect, null); //ReturnMessages(geoprocessor); MessageBox.Show("Intersect分析完成!"); } catch (Exception err) { //Console.WriteLine(err.Message); MessageBox.Show(err.Message); //ReturnMessages(geoprocessor); } }
/// <param name="pFeatureClass1">相交要素1</param> /// <param name="pFeatureClass2">相交要素2</param> /// <param name="gdbPath">数据库路径</param> private IFeatureClass Intersect(IFeatureClass pFeatureClass1, IFeatureClass pFeatureClass2, string gdbPath) { IFeatureClass pOutFeatureClass = null; try { //调用GP ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor(); gp.OverwriteOutput = true; //多个对象的输入:使用IGpValueTableObject接口,该接口可以设置 IGpValueTableObject vtobject = new GpValueTableObjectClass(); object pFeature1 = pFeatureClass1; object pFeature2 = pFeatureClass2; vtobject.SetColumns(2); vtobject.AddRow(ref pFeature1); vtobject.AddRow(ref pFeature2); ESRI.ArcGIS.AnalysisTools.Intersect pIntersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); pIntersect.in_features = vtobject; pIntersect.out_feature_class = System.Environment.CurrentDirectory + @"\temp\" + "temp.shp"; pIntersect.output_type = "Input"; IGeoProcessorResult2 result = (IGeoProcessorResult2)gp.Execute(pIntersect, null); if (result.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded) { MessageBox.Show("操作失败!"); return(null); } else { //获取结果; IFeatureClass resultFClass = gp.Open(result.ReturnValue) as IFeatureClass; IDataset pDataset = resultFClass as IDataset; //目标数据库; IWorkspaceFactory factory = new FileGDBWorkspaceFactoryClass(); IWorkspace objectWorkspace = factory.OpenFromFile(gdbPath, 0); string fileName = pFeatureClass1.AliasName + "_" + pFeatureClass2.AliasName; pOutFeatureClass = IFeatureDataConverter_ConvertFeatureClass(pDataset.Workspace, objectWorkspace, pDataset.Name, fileName); } } catch (System.Exception ex) { MessageBox.Show("操作失败!"); return(null); } return(pOutFeatureClass); }
private string InterSect(string flayer, string kadastroLayer) { ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor(); ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); intersect.in_features = flayer + ";" + kadastroLayer; intersect.out_feature_class = AppSingleton.Instance().WorkspacePath + "\\Final"; intersect.join_attributes = "All"; intersect.output_type = "INPUT"; gp.AddOutputsToMap = true; gp.AddOutputsToMap = true; gp.OverwriteOutput = true; gp.Execute(intersect, null); return(intersect.out_feature_class.ToString()); }
/// <summary> /// 两面相交 /// </summary> private void intersect() { //两面相交 ESRI.ArcGIS.Geoprocessor.Geoprocessor GP = new ESRI.ArcGIS.Geoprocessor.Geoprocessor(); ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); intersect.in_features = @"C:\Users\航\Desktop\同济新村shp数据\同济新村房屋.shp;C:\Users\航\Desktop\同济新村shp数据\生成地块.shp"; intersect.out_feature_class = @"C:\Users\航\Desktop\同济新村shp数据\intersect.shp"; IGPProcess GProcess1 = intersect; GP.Execute(GProcess1, null); //如果显示 //Form1.mainForm.axMapControl1.AddShapeFile(@"C:\Users\航\Desktop\同济新村shp数据\", "intersect.shp"); //Form1.mainForm.axMapControl1.Refresh(); }
// Mark: - Geoprocessing void Intersect(string[] inputFeatures, string outputFilePath) { Geoprocessor geoprocessor = new Geoprocessor(); ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); IGpValueTableObject vt = new GpValueTableObject(); vt.SetColumns(2); vt.AddRow(LoadFeatureClassFromPath(inputFeatures[0])); vt.AddRow(LoadFeatureClassFromPath(inputFeatures[1])); intersect.in_features = vt; intersect.out_feature_class = outputFilePath; intersect.join_attributes = "ALL"; geoprocessor.Execute(intersect, null); AddShapeFileToView(outputFilePath); }
/// <summary> /// GP intersect /// </summary> /// <param name="inFiles">输入文件列表</param> /// <param name="outFile">输出文件名</param> public bool Intersect(string inFiles, string outFile, out string msg) { IGeoProcessorResult geoProcessorResult = null; msg = string.Empty; bool result = true; try { ESRI.ArcGIS.AnalysisTools.Intersect pIntersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); pIntersect.in_features = inFiles; pIntersect.join_attributes = "ALL"; pIntersect.output_type = "INPUT"; pIntersect.out_feature_class = outFile; //geoProcessorResult = this.m_gp.Execute(pIntersect, null) as IGeoProcessorResult; //msg += GetGPMessages(this.m_gp); //Geoprocessor m_gp = new Geoprocessor() { OverwriteOutput = true }; //m_gp.Execute(pIntersect, null); geoProcessorResult = m_gp.Execute(pIntersect, null) as IGeoProcessorResult; msg += GetGPMessages(m_gp); //if (geoProcessorResult.Status != esriJobStatus.esriJobSucceeded) //{ // result = false; //} return(result); } catch (Exception ex) { throw new Exception("相交处理失败:\r\n" + ex.Message); } finally { //if (geoProcessorResult!=null) // Marshal.ReleaseComObject(geoProcessorResult); } }
public void OtherRule(string idname, string checkname, string IN_RuleType, string TABLENAME, string SUPTABLE, string inputtext, IHookHelper m_hookHelper) { CommonClass common = new CommonClass(); IFeatureClass IN_FeatureClass = common.GetLayerByName(TABLENAME).FeatureClass; IGeoDataset pGeoDataset = IN_FeatureClass as IGeoDataset; ISpatialReference spatialReference = pGeoDataset.SpatialReference; IFeatureClass IN_Sup_FeatureClass = null; if (SUPTABLE != null) { IN_Sup_FeatureClass = common.GetLayerByName(SUPTABLE).FeatureClass; } string ErrorFilePath = this.topoDir + "\\" + checkname + idname + ".shp"; if (IN_RuleType == "面多部件检查") { try { common.CreatShpFile(this.topoDir, spatialReference, esriGeometryType.esriGeometryPolygon, checkname + idname); List <ErrorEntity> list = new List <ErrorEntity>(); IFeatureCursor cursor = IN_FeatureClass.Search(null, false); int tempCount = 0; IFeature pFeature = cursor.NextFeature(); while (pFeature != null) { IGeometry pGeo = pFeature.ShapeCopy; ITopologicalOperator pTopoOperator = pGeo as ITopologicalOperator; int iCount = 0; if (IN_FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon) { iCount = (pGeo as IPolygon).ExteriorRingCount; } else if (IN_FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline) { iCount = ((pGeo as IPolyline) as IGeometryCollection).GeometryCount; } else if (IN_FeatureClass.ShapeType == esriGeometryType.esriGeometryMultipoint) { iCount = ((pGeo as IMultipoint) as IPointCollection).PointCount; } if (iCount > 1) { tempCount++; common.GenerateSHPFile(pFeature.ShapeCopy, ErrorFilePath); } pFeature = cursor.NextFeature(); } //new ErrorTable().AddErr(list, ErrType.MultiPart, idname); if (!DicTopoError.ContainsKey(idname)) { DicTopoError.Add(idname, tempCount); } else { DicTopoError[idname] = tempCount; } Marshal.ReleaseComObject(cursor); if (tempCount == 0) { string shpFolder = System.IO.Path.GetDirectoryName(ErrorFilePath); IWorkspaceFactory pWorkspaceFac = new ShapefileWorkspaceFactoryClass(); IWorkspace pWorkSpace = pWorkspaceFac.OpenFromFile(ErrorFilePath, 0); IFeatureWorkspace pFeatureWorkSpace = pWorkSpace as IFeatureWorkspace; if (System.IO.File.Exists(ErrorFilePath)) { IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(ErrorFilePath); if (pFCChecker != null) { IDataset pds = pFCChecker as IDataset; pds.Delete(); } } } } catch (Exception ex) { LogHelper.WriteLog(typeof(TopoChecker), ex); } } else if (IN_RuleType == "面自相交检查") { int errorCount = 0; try { common.CreatShpFile(this.topoDir, spatialReference, esriGeometryType.esriGeometryPoint, checkname + idname); IFeatureLayer pLayer = new FeatureLayer(); pLayer.FeatureClass = IN_FeatureClass; TopoClassChecker topo = new TopoClassChecker(); IFeatureCursor cursor; IQueryFilter filter = new QueryFilterClass(); filter.SubFields = pLayer.FeatureClass.OIDFieldName + "," + pLayer.FeatureClass.ShapeFieldName; cursor = pLayer.FeatureClass.Search(filter, true); IFeature feature = null; object missing = Type.Missing; while ((feature = cursor.NextFeature()) != null) { IPoint tempPoint = null; StringBuilder builder = new StringBuilder(); IGeometryCollection shape = feature.Shape as IGeometryCollection; for (int i = 0; i < shape.GeometryCount; i++) { esriNonSimpleReasonEnum enum2; IPointCollection newPoints = shape.get_Geometry(i) as IPointCollection; IRing ring = newPoints as IRing; int num2 = 0; if (ring.IsClosed) { num2 = 1; } PolylineClass o = new PolylineClass(); o.AddPointCollection(newPoints); o.SpatialReference = spatialReference; ITopologicalOperator3 @operator = o; @operator.IsKnownSimple_2 = false; if ([email protected]_IsSimpleEx(out enum2) && (enum2 == esriNonSimpleReasonEnum.esriNonSimpleSelfIntersections)) { List <string> list2 = new List <string>(); List <string> list3 = new List <string>(); for (int j = num2; j < newPoints.PointCount; j++) { IPoint point = newPoints.get_Point(j); tempPoint = point; string item = point.X.ToString() + "," + point.Y.ToString(); if (list2.Contains(item)) { if (!list3.Contains(item)) { builder.Append(";"); builder.Append(item); list3.Add(item); } } else { list2.Add(item); } } } Marshal.ReleaseComObject(o); o = null; } if (builder.Length > 0) { errorCount++; string[] strArray = builder.ToString().Substring(1).Split(new char[] { ';' }); ESRI.ArcGIS.Geometry.IPointCollection pPointCollection1 = new ESRI.ArcGIS.Geometry.MultipointClass(); foreach (string str in strArray) { if (!string.IsNullOrEmpty(str)) { string[] strArray2 = str.Split(new char[] { ',' }); double pX = double.Parse(strArray2[0]); double pY = double.Parse(strArray2[1]); ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass(); point.X = pX; point.Y = pY; common.GenerateSHPFile(point, ErrorFilePath); } } } } Marshal.ReleaseComObject(cursor); if (errorCount == 0) { string shpFolder = System.IO.Path.GetDirectoryName(ErrorFilePath); IWorkspaceFactory pWorkspaceFac = new ShapefileWorkspaceFactoryClass(); IWorkspace pWorkSpace = pWorkspaceFac.OpenFromFile(ErrorFilePath, 0); IFeatureWorkspace pFeatureWorkSpace = pWorkSpace as IFeatureWorkspace; if (System.IO.File.Exists(ErrorFilePath)) { IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(ErrorFilePath); if (pFCChecker != null) { IDataset pds = pFCChecker as IDataset; pds.Delete(); } } } } catch (Exception ex) { LogHelper.WriteLog(typeof(TopoChecker), ex); } if (!DicTopoError.ContainsKey(idname)) { DicTopoError.Add(idname, errorCount); } else { DicTopoError[idname] = errorCount; } } else if (IN_RuleType == "缝隙检查") { int errorCount = 0; try { common.CreatShpFile(this.topoDir, spatialReference, esriGeometryType.esriGeometryPolygon, checkname + idname); this.m_hookHelper = m_hookHelper; IFeatureClass pFClass = IN_FeatureClass; //获取空间参考 IGeometry geometryBag = new GeometryBagClass(); IGeoDataset geoDataset = pFClass as IGeoDataset; geometryBag.SpatialReference = geoDataset.SpatialReference; ////属性过滤 IFeatureCursor featureCursor = pFClass.Search(null, false); // 遍历游标 IFeature currentFeature = featureCursor.NextFeature(); IGeometryCollection geometryCollection = geometryBag as IGeometryCollection; object missing = Type.Missing; while (currentFeature != null) { geometryCollection.AddGeometry(currentFeature.Shape, ref missing, ref missing); currentFeature = featureCursor.NextFeature(); } // 合并要素 ITopologicalOperator unionedPolygon = null; unionedPolygon = new Polygon() as ITopologicalOperator; unionedPolygon.ConstructUnion(geometryCollection as IEnumGeometry); Marshal.ReleaseComObject(featureCursor); IPolygon4 pMergerPolygon = unionedPolygon as IPolygon4; IGeometryBag pOutGeometryBag = pMergerPolygon.ExteriorRingBag; //获取外部环 IGeometryCollection pOutGmtyCollection = pOutGeometryBag as IGeometryCollection; for (int i = 0; i < pOutGmtyCollection.GeometryCount; i++) //对外部环遍历 { IGeometry pOutRing = pOutGmtyCollection.get_Geometry(i); //外部环 //【此处可以对外部环进行操作】 IPointCollection pOutRingCollection = pOutRing as IPointCollection; for (int j = 0; j < pOutRingCollection.PointCount; j++) { IPoint pOutRingPoint = pOutRingCollection.get_Point(j);//获取外环上的点 } IGeometryBag pInteriotGeometryBag = pMergerPolygon.get_InteriorRingBag(pOutRing as IRing); //获取内部环 IGeometryCollection pInteriorGeometryCollection = pInteriotGeometryBag as IGeometryCollection; for (int j = 0; j < pInteriorGeometryCollection.GeometryCount; j++) { ISegmentCollection SegCol = pInteriorGeometryCollection.get_Geometry(j) as ISegmentCollection; IPolygon PPolygon = new PolygonClass(); ISegmentCollection newSegCol = PPolygon as ISegmentCollection; newSegCol.AddSegmentCollection(SegCol); //pInteriorGeometry即为多边形的内部环 IGeometry inRing = PPolygon as IGeometry; inRing.SpatialReference = geometryBag.SpatialReference; IArea area = inRing as IArea; Double getarea = System.Math.Abs(Convert.ToDouble(area.Area)); if (inputtext == null || inputtext == "" || getarea < Convert.ToDouble(inputtext)) { Boolean flag = true; ISpatialFilter filter = new SpatialFilterClass(); filter.Geometry = inRing; filter.SubFields = pFClass.OIDFieldName + "," + pFClass.ShapeFieldName; filter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; IFeatureCursor o = pFClass.Search(filter, true); IFeature feature = o.NextFeature(); while (feature != null && flag) { IPolygon4 pPolygon = feature.Shape as IPolygon4; IGeometryBag iOutGeometryBag = pPolygon.ExteriorRingBag; //获取外部环 IGeometryCollection iOutGmtyCollection = iOutGeometryBag as IGeometryCollection; for (int m = 0; m < iOutGmtyCollection.GeometryCount && flag; m++) //对外部环遍历 { IGeometry outGeo = iOutGmtyCollection.get_Geometry(m); IGeometryCollection polyGonGeo = new PolygonClass(); polyGonGeo.AddGeometry(outGeo); IPolygon iPolygon = polyGonGeo as IPolygon; iPolygon.SimplifyPreserveFromTo(); IRelationalOperator2 pRelationalOperator2 = iPolygon as IRelationalOperator2; if (!pRelationalOperator2.Contains(inRing)) { errorCount++; common.GenerateSHPFile(inRing, ErrorFilePath); flag = false; } } feature = o.NextFeature(); } Marshal.ReleaseComObject(o); } } if (errorCount == 0) { string shpFolder = System.IO.Path.GetDirectoryName(ErrorFilePath); IWorkspaceFactory pWorkspaceFac = new ShapefileWorkspaceFactoryClass(); IWorkspace pWorkSpace = pWorkspaceFac.OpenFromFile(ErrorFilePath, 0); IFeatureWorkspace pFeatureWorkSpace = pWorkSpace as IFeatureWorkspace; if (System.IO.File.Exists(ErrorFilePath)) { IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(ErrorFilePath); if (pFCChecker != null) { IDataset pds = pFCChecker as IDataset; pds.Delete(); } } } } } catch (Exception ex) { LogHelper.WriteLog(typeof(TopoChecker), ex); } if (!DicTopoError.ContainsKey(idname)) { DicTopoError.Add(idname, errorCount); } else { DicTopoError[idname] = errorCount; } } else if (IN_RuleType == "面重叠检查") { IFeatureClass outIFC = null; this.m_hookHelper = m_hookHelper; ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); string outString = this.topoDir + "\\" + checkname + idname + ".shp"; intersect.in_features = common.GetPathByName(TABLENAME); intersect.out_feature_class = outString; Geoprocessor geoProcessor = new Geoprocessor(); geoProcessor.OverwriteOutput = true; try { if (this.gp == null) { this.gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor(); } IGeoProcessorResult result = (IGeoProcessorResult)this.gp.Execute(intersect, null); if (result.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded) { Console.WriteLine("gp工具执行错误"); } else { outIFC = this.gp.Open(result.ReturnValue) as IFeatureClass; } } catch (Exception ex) { LogHelper.WriteLog(typeof(TopoChecker), ex); Console.WriteLine(ex.Message); } if (outIFC.FeatureCount(null) == 0) { string shpFolder = System.IO.Path.GetDirectoryName(ErrorFilePath); IWorkspaceFactory pWorkspaceFac = new ShapefileWorkspaceFactoryClass(); IWorkspace pWorkSpace = pWorkspaceFac.OpenFromFile(ErrorFilePath, 0); IFeatureWorkspace pFeatureWorkSpace = pWorkSpace as IFeatureWorkspace; if (System.IO.File.Exists(ErrorFilePath)) { IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(ErrorFilePath); if (pFCChecker != null) { IDataset pds = pFCChecker as IDataset; pds.Delete(); } } } if (!DicTopoError.ContainsKey(idname)) { DicTopoError.Add(idname, outIFC.FeatureCount(null)); } else { DicTopoError[idname] = outIFC.FeatureCount(null); } } else if (IN_RuleType == "面重叠检查(与其他图层)") { IFeatureClass outIFC = null; this.m_hookHelper = m_hookHelper; ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); string outString = this.topoDir + "\\" + checkname + idname + ".shp"; intersect.in_features = @common.GetPathByName(TABLENAME) + ";" + @common.GetPathByName(SUPTABLE); intersect.out_feature_class = outString; Geoprocessor geoProcessor = new Geoprocessor(); geoProcessor.OverwriteOutput = true; try { if (this.gp == null) { this.gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor(); } IGeoProcessorResult result = (IGeoProcessorResult)this.gp.Execute(intersect, null); if (result.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded) { Console.WriteLine("gp工具执行错误"); } else { outIFC = this.gp.Open(result.ReturnValue) as IFeatureClass; } } catch (Exception ex) { Console.WriteLine(ex.Message); LogHelper.WriteLog(typeof(TopoChecker), ex); } if (outIFC.FeatureCount(null) == 0) { string shpFolder = System.IO.Path.GetDirectoryName(ErrorFilePath); IWorkspaceFactory pWorkspaceFac = new ShapefileWorkspaceFactoryClass(); IWorkspace pWorkSpace = pWorkspaceFac.OpenFromFile(ErrorFilePath, 0); IFeatureWorkspace pFeatureWorkSpace = pWorkSpace as IFeatureWorkspace; if (System.IO.File.Exists(ErrorFilePath)) { IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(ErrorFilePath); if (pFCChecker != null) { IDataset pds = pFCChecker as IDataset; pds.Delete(); } } } if (!DicTopoError.ContainsKey(idname)) { DicTopoError.Add(idname, outIFC.FeatureCount(null)); } else { DicTopoError[idname] = outIFC.FeatureCount(null); } } Marshal.ReleaseComObject(IN_FeatureClass); if (SUPTABLE != null) { Marshal.ReleaseComObject(IN_Sup_FeatureClass); } }
private void butOk_Click(object sender, EventArgs e) { if (this.CheckingInput()) { Application.DoEvents(); this.comboxlayer1.Enabled = false; this.comboxlayer2.Enabled = false; this.textBox1.Enabled = false; this.button1.Enabled = false; this.butOk.Enabled = false; this.progressBar1.Visible = true; this.progressBar1.Minimum = 0; this.progressBar1.Maximum = 100; IFeatureLayer layer1 = this.MapLayer[this.comboxlayer1.SelectedIndex] as IFeatureLayer; IFeatureLayer layer2 = this.MapLayer[this.comboxlayer2.SelectedIndex] as IFeatureLayer; try { //调用GP ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor(); ESRI.ArcGIS.AnalysisTools.Intersect pIntersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); gp.OverwriteOutput = true; pIntersect.in_features = LayerToShapefilePath(layer1 as ILayer) + ";" + LayerToShapefilePath(layer2 as ILayer); pIntersect.out_feature_class = System.Environment.CurrentDirectory + @"\temp\" + "temp.shp"; pIntersect.output_type = "Input"; ///// for (int i = 0; i < 20; i++) { this.progressBar1.Value++; } ///////////// ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2 result = (ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2)gp.Execute(pIntersect, null); ///// for (int i = 0; i < 20; i++) { this.progressBar1.Value++; } ///////////// if (result.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded) { MessageBox.Show("操作失败!"); this.Close(); } else { //获取结果 IFeatureClass resultFClass = gp.Open(result.ReturnValue) as IFeatureClass; IDataset pDataset = resultFClass as IDataset; //目标 IWorkspaceFactory factory = new FileGDBWorkspaceFactoryClass(); IWorkspace objectWorkspace = factory.OpenFromFile(textBox1.Text.Trim(), 0); //////////////////////////// for (int i = 0; i < 20; i++) { this.progressBar1.Value++; } ///////////////////////////////////////// IFeatureClass pOutFeatureClass = IFeatureDataConverter_ConvertFeatureClass(pDataset.Workspace, objectWorkspace, pDataset.Name, textBox2.Text.Trim()); ////////////////////////////////////////////////// for (int i = 0; i < 20; i++) { this.progressBar1.Value++; } /////////////////////////////////////////////////// IFeatureLayer pLayer = new FeatureLayerClass { FeatureClass = pOutFeatureClass, Name = pOutFeatureClass.AliasName }; this.m_axMapControl.AddLayer(pLayer); this.m_axMapControl.ActiveView.Refresh(); MessageBox.Show(pLayer.Name + "处理完成!"); base.Close(); } } catch (System.Exception ex) { MessageBox.Show("操作失败!"); this.Close(); } } }
//进行叠置 private bool DoIntersect(IFeatureClass pOldFeaCls, IFeatureClass pNewFeaCls, IGeometry pGeo) { _Progress.SetProgress("新旧图层叠置..."); Application.DoEvents(); Geoprocessor gp = new Geoprocessor(); gp.OverwriteOutput = true; gp.AddOutputsToMap = false; IGpValueTableObject vtobject = new GpValueTableObjectClass(); vtobject.SetColumns(1); IFeatureLayer pFeaturelayer = new FeatureLayerClass(); pFeaturelayer.FeatureClass = pOldFeaCls; pFeaturelayer.Name = pOldFeaCls.AliasName; if (pGeo != null) { ISpatialFilter pSpatialFilter = new SpatialFilterClass(); pSpatialFilter.Geometry = pGeo; pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; IFeatureSelection pFeaSelection = pFeaturelayer as IFeatureSelection; //仅对选中的重点巡查线进行缓冲分析 pFeaSelection.SelectFeatures(pSpatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false); } if (_Progress != null) { if (_Progress.UserAskCancel) { return(false); } } object pObj1 = pFeaturelayer; vtobject.AddRow(ref pObj1); IFeatureLayer pNewFeaturelayer = new FeatureLayerClass(); pNewFeaturelayer.FeatureClass = pNewFeaCls; pNewFeaturelayer.Name = pNewFeaCls.AliasName; if (pGeo != null) { ISpatialFilter pSpatialFilter = new SpatialFilterClass(); pSpatialFilter.Geometry = pGeo; pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; IFeatureSelection pFeaSelection = pNewFeaturelayer as IFeatureSelection; //仅对选中的重点巡查线进行缓冲分析 pFeaSelection.SelectFeatures(pSpatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false); } if (_Progress != null) { if (_Progress.UserAskCancel) { return(false); } } object pObj2 = pNewFeaturelayer; vtobject.AddRow(ref pObj2); ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); intersect.in_features = vtobject; intersect.join_attributes = "ALL"; intersect.output_type = "INPUT"; intersect.out_feature_class = _MdbPath + "\\" + _MdbName + "\\" + _IntersectName;// _ResultPath + _TmpMdbName + "\\" + _IntersectName; if (_Progress != null) { if (_Progress.UserAskCancel) { return(false); } } if (!RunTool(gp, intersect)) { gp = null; return(false); } intersect.in_features = null; vtobject.RemoveRow(1); vtobject.RemoveRow(0); System.Runtime.InteropServices.Marshal.ReleaseComObject(vtobject); try { System.Runtime.InteropServices.Marshal.ReleaseComObject(intersect); System.Runtime.InteropServices.Marshal.ReleaseComObject(gp); } catch (Exception err) { } vtobject = null; intersect = null; gp = null; return(true); }
public string Overlay(ArrayList inLayerNames, string outLayerName = "", string overlayType = "INTERSECT") { string inLayers = ""; foreach(string layerName in inLayerNames) { ILayer layer = this._getLayerByName(layerName); ESRI.ArcGIS.Geodatabase.IDataset dataset = (ESRI.ArcGIS.Geodatabase.IDataset)(layer); // Explicit Cast this._geoProcessor.SetEnvironmentValue("workspace", dataset.Workspace.PathName); //inLayers += featureClass.FeatureDataset.Name + "\\" + featureClass.AliasName + ";"; inLayers += layerName + ";"; } if (inLayers == "") { return ""; } string tempDir = System.IO.Path.GetTempPath(); string outLayerFile = ""; if (outLayerName == "") { string filename = ""; foreach(string layerName in inLayerNames) { filename += layerName + "_"; } filename += "overlay_" + DateTime.Now.ToString("MM_dd_yy_H_mm_ss") + ".shp"; outLayerFile = System.IO.Path.Combine(tempDir, filename); } else { outLayerFile = System.IO.Path.Combine(tempDir, outLayerName + ".shp"); } if (System.IO.File.Exists(outLayerFile)) { return outLayerFile; } //create a new instance of an overlay tool IGeoProcessorResult results = null; if (overlayType == "INTERSECT") { ESRI.ArcGIS.AnalysisTools.Intersect process = new ESRI.ArcGIS.AnalysisTools.Intersect(); process.in_features = inLayers; process.out_feature_class = outLayerFile; results = (IGeoProcessorResult)this._geoProcessor.Execute(process, null); //RunTool(this._geoProcessor, process, null); } else if (overlayType == "UNION") { ESRI.ArcGIS.AnalysisTools.Union process = new ESRI.ArcGIS.AnalysisTools.Union(inLayers, outLayerFile); results = (IGeoProcessorResult)this._geoProcessor.Execute(process, null); } if (results != null && results.Status == esriJobStatus.esriJobSucceeded) { return outLayerFile; } return ""; }