功能描述
合并功能,准确的说是merge、union。
这两者在ArcMap中的差别就是:merger保留相同字段属性;union是可以选择创建新的合并图形还是直接用原始要素合并,合并属性不保留。
接口和使用方法
1.
ITopologicalOperator接口,其下的ConstructUnion属性能够一次放入多个几何(ConstructUnion属性是将放入的所有几何能够被一次性合并,这种方式比反复调用合并更加高效。其参数只能是IEnumGeometry(包络几何),可以将IGeometryCollection转为IEnumGeometry),然后直接在FeatureClass中创建Feature,ITopologicalOperatoras为IGeometry即可。
2.
ITopologicalOperator接口,其下有union方法。
3.
IBasicGeoprocessor 接口。其下有
CancelTracker The cancel tracker.
Clip Clips features.
Dissolve Dissolves features.
Intersect Intersects features.
Merge Merges features.
SpatialReference The output spatial reference.
Union Creates a union of features.
4.
GP接口
5.其他
具体代码
方法1代码:
转载自网易博客,原地址:http://yaogu.blog.163.com/blog/static/184999066201242692019186/
private void btnMerge_Click(object sender, EventArgs e) { ICommand pUnionFeature = new UnionFeatures(0); pUnionFeature.OnCreate(axMapControl1.Object); axMapControl1.CurrentTool = pUnionFeature as ITool; }
新建类UnionFeatures.cs
代码如下
usingSystem; usingSystem.Drawing; usingSystem.Runtime.InteropServices; usingESRI.ArcGIS.ADF.BaseClasses; usingESRI.ArcGIS.ADF.CATIDs; usingESRI.ArcGIS.Controls; usingSystem.Windows.Forms; usingESRI.ArcGIS.Carto; usingESRI.ArcGIS.Geodatabase; usingESRI.ArcGIS.Geometry; namespaceWindowsFormsApplication2 { /// <summary> ///Summary description for UnionFeatures. /// </summary> [Guid("af0c15f2-8963-4fa7-b754-0f65e7b3c4bd")] [ClassInterface(ClassInterfaceType.None)] [ProgId("lyx.UnionFeatures")] public sealed classUnionFeatures : BaseTool { #region COM Registration Function(s)[ComRegisterFunction()] [ComVisible(false)] static voidRegisterFunction(Type registerType) { //Required for ArcGIS Component Category Registrar support ArcGISCategoryRegistration(registerType); // //TODO: Add any COM registration code here // } [ComUnregisterFunction()] [ComVisible(false)] static voidUnregisterFunction(Type registerType) { //Required for ArcGIS Component Category Registrar support ArcGISCategoryUnregistration(registerType); // //TODO: Add any COM unregistration code here // } #region ArcGIS Component Category Registrar generated code /// <summary> ///Required method for ArcGIS Component Category registration - ///Do not modify the contents of this method with the code editor. /// </summary> private static voidArcGISCategoryRegistration(Type registerType) { string regKey = string.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID); MxCommands.Register(regKey); ControlsCommands.Register(regKey); } /// <summary> ///Required method for ArcGIS Component Category unregistration - ///Do not modify the contents of this method with the code editor. /// </summary> private static voidArcGISCategoryUnregistration(Type registerType) { string regKey = string.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID); MxCommands.Unregister(regKey); ControlsCommands.Unregister(regKey); } #endregion #endregion IHookHelper m_hookHelper = null; IActiveView m_activeView = null; IMap m_map = null; IFeatureLayer currentLayer = null; //IEngineEditProperties m_engineEditor = null; intp; public UnionFeatures(int_p) { // //TODO: Define values for the public properties // base.m_category = ""; //localizable text base.m_caption = ""; //localizable text base.m_message = "This should work in ArcMap/MapControl/PageLayoutControl"; //localizable text base.m_toolTip = ""; //localizable text base.m_name = ""; //unique id, non-localizable (e.g. "MyCategory_MyTool") //m_engineEditor = new EngineEditorClass(); p =_p; try{ // //TODO: change resource name if necessary // string bitmapResourceName = GetType().Name + ".bmp"; base.m_bitmap = newBitmap(GetType(), bitmapResourceName); base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur"); } catch(Exception ex) { System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap"); } } #region Overriden Class Methods /// <summary> ///Occurs when this tool is created /// </summary> /// <param name="hook">Instance of the application</param> public override void OnCreate(objecthook) { try{ m_hookHelper = newHookHelperClass(); m_hookHelper.Hook =hook; if (m_hookHelper.ActiveView == null) { m_hookHelper = null; } } catch{ m_hookHelper = null; } if (m_hookHelper == null) base.m_enabled = false; else base.m_enabled = true; //TODO: Add other initialization code } /// <summary> ///Occurs when this tool is clicked /// </summary> public override voidOnClick() { m_activeView =m_hookHelper.ActiveView; m_map =m_hookHelper.FocusMap; ILayer layer =m_map.get_Layer(p); if (layer != null) { MessageBox.Show(m_map.SelectionCount.ToString(), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } IEnumFeature selectedFeatures =GetSelectedFeatures(); if (selectedFeatures == null) return; UnionFeature(selectedFeatures); m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography | esriViewDrawPhase.esriViewGeoSelection, null, m_activeView.Extent); } privateIEnumFeature GetSelectedFeatures() { //if (m_map.SelectionCount < 2) //{ //MessageBox.Show("无图层操作!!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); //return null; //} ILayer layer =m_map.get_Layer(p); if (layer == null) return null; if (!(layer isIFeatureLayer)) return null; currentLayer = layer asIFeatureLayer; if (currentLayer.FeatureClass.ShapeType ==esriGeometryType.esriGeometryPoint) { MessageBox.Show("无图层操作!!!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return null; } IEnumFeature SelectedFeatures = m_map.FeatureSelection asIEnumFeature; if (SelectedFeatures == null) return null; //判断SelectedFeatures是否为相同的几何类型,且是否与m_engineEditor.TargetLayer几何类型相同 bool sameGeometryType =JudgeGeometryType(SelectedFeatures); if (!sameGeometryType) { MessageBox.Show("无图层操作!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return null; } returnSelectedFeatures; } private boolJudgeGeometryType(IEnumFeature SelectedFeatures) { SelectedFeatures.Reset(); IFeature feature =SelectedFeatures.Next(); if (feature == null) return false; esriGeometryType geometryType =feature.ShapeCopy.GeometryType; while ((feature = SelectedFeatures.Next()) != null) { if (geometryType !=feature.ShapeCopy.GeometryType) { return false; } } if (geometryType ==currentLayer.FeatureClass.ShapeType) return true; return false; } private voidUnionFeature(IEnumFeature selectedFeatures) { IFeature feature = null; IGeometry geometry = null; object missing =Type.Missing; selectedFeatures.Reset(); feature =selectedFeatures.Next(); if (feature == null) return; IFeatureClass featureClass = feature.Class asIFeatureClass; IGeometryCollection geometries = newGeometryBagClass(); while (feature != null) { geometry =feature.ShapeCopy; geometries.AddGeometry(geometry, ref missing, refmissing); feature =selectedFeatures.Next(); } ITopologicalOperator unionedGeometry = null; switch(featureClass.ShapeType) { caseesriGeometryType.esriGeometryMultipoint: unionedGeometry = new MultipointClass(); break; caseesriGeometryType.esriGeometryPolyline: unionedGeometry = new PolylineClass(); break; caseesriGeometryType.esriGeometryPolygon: unionedGeometry = new PolygonClass(); break; default: break; } unionedGeometry.ConstructUnion(geometries asIEnumGeometry); ITopologicalOperator2 topo = unionedGeometry asITopologicalOperator2; topo.IsKnownSimple_2 = false; topo.Simplify(); IFeatureClass targetFeatureClass =currentLayer.FeatureClass; IDataset dataset = featureClass asIDataset; IWorkspaceEdit workspaceEdit = dataset.Workspace asIWorkspaceEdit; if (!(workspaceEdit.IsBeingEdited())) return; try{ workspaceEdit.StartEditOperation(); IFeature unionedFeature =targetFeatureClass.CreateFeature(); unionedFeature.Shape = unionedGeometry asIGeometry; unionedFeature.Store(); workspaceEdit.StopEditOperation(); } catch(Exception ex) { workspaceEdit.AbortEditOperation(); MessageBox.Show("要素合并失败!!" + ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } public override void OnMouseDown(int Button, int Shift, int X, intY) { //TODO: Add UnionFeatures.OnMouseDown implementation } public override void OnMouseMove(int Button, int Shift, int X, intY) { //TODO: Add UnionFeatures.OnMouseMove implementation } public override void OnMouseUp(int Button, int Shift, int X, intY) { //TODO: Add UnionFeatures.OnMouseUp implementation } #endregion} }
其他略。