当前位置:   article > 正文

基于C#的ArcEngine二次开发35:缓冲区分析_arcengine 缓冲区

arcengine 缓冲区

目录

0 arcgis建立缓冲区

1 根据选择要素建立缓冲区并查询

1.1 代码

1.2 代码分析

2 从线要素首尾节点相邻的角度合并同名道路


几点说明:

使用ITopologicalOperation.Buffer建立缓冲区时,可能出现以下几种情况:

  • 出现异常卡死现象,修改缓冲区为合适的值,即可解决;
  • 不连续的要素无法建立缓冲区,此时应根据属性判断,将要素分离开来

0 arcgis建立缓冲区

缓冲区是地理空间目标的一种影响范围或服务范围,具体指在点、线、面实体的周围,自动建立的一定宽度的多边。
方法/步骤
1. 向arcmap中添加要建立缓冲区的图层。

这里写图片描述

2. 调出建立缓冲区工具。有两种方法可以调出缓冲区工具,一种是在菜单地理处理中选择缓冲区;另外一种方法是在arctoolbox中选择分析工具-邻域分析-缓冲区。

这里写图片描述

3. 首先选择输入要素,选择输出要素名字位置和缓冲区范围,融合字段可以根据需要进行设置。
4. 建立缓冲区。线图层建立缓冲区的过程中可以选择缓冲区的位置,在侧类型中可以选择缓冲区在线的左边、右边还是两边都有,末端类型可以选择末端是圆形还是方形,如图,侧类型为full,末端为round形成的缓冲区。

这里写图片描述

5. 面建立缓冲区和线一样,不过面的缓冲区侧类型只有两种:full和outside_only。full是对整个面都建立缓冲区,outside_only是在面的外面建立缓冲区。

这里写图片描述这里写图片描述

6. 在工具箱中右击“缓冲区”可以批处理,节约时间。

这里写图片描述             这里写图片描述


END
注意事项:融合字段按作图要求进行选择,一般缓冲区都做分析用,可以不要太多属性

1 根据选择要素建立缓冲区并查询

1.1 代码

这是在其他地方找到的一段代码:

实现功能:

1.获取鼠标选中的要素

2.对选中要素设置缓冲距离,在地图上显示缓冲区

3.进行缓冲区查询,获得缓冲区内的要素,并放入TreeList展示

  1. /// <summary>
  2. /// 创建缓冲区
  3. /// </summary>
  4. private void CreateBuffer()
  5. {
  6. ITopologicalOperator pTopo = null;
  7. IElement pElement = null;
  8. IGeometry pBuffer = null;
  9. IGeometry pGeo = null;
  10. ISelection pSelection = null;
  11. IEnumFeatureSetup pEnumFeatureSetup = null;
  12. IEnumFeature pEnumFeature = null;
  13. IFillSymbol pFillSymbol = null;
  14. IRgbColor pRgbColor = null;
  15. IFeature pFea = null;
  16. IFeature pFeature = null;
  17. IFeatureLayer pFeaLayer = null;
  18. IFeatureClass pFeaClass = null;
  19. IFeatureCursor pFeaCursor = null;
  20. ISpatialFilter pSpatialfilter = null;
  21. try
  22. {
  23. if (m_pMap != null)
  24. {
  25. (m_pMap as IGraphicsContainer).DeleteAllElements();
  26. }
  27. 获得选中要素
  28. pSelection = m_pMap.FeatureSelection;
  29. pEnumFeatureSetup = pSelection as IEnumFeatureSetup;
  30. pEnumFeatureSetup.AllFields = true;
  31. pEnumFeature = pEnumFeatureSetup as IEnumFeature;
  32. pEnumFeature.Reset();
  33. pFea = pEnumFeature.Next();
  34. 遍历选中要素
  35. while (pFea != null)
  36. {
  37. pGeo = pFea.ShapeCopy;
  38. pTopo = pGeo as ITopologicalOperator;
  39. pBuffer = pTopo.Buffer(m_dDistance);
  40. pElement = new PolygonElementClass();
  41. pElement.Geometry = pBuffer;
  42. 设置缓冲区颜色
  43. pFillSymbol = new SimpleFillSymbolClass();
  44. pRgbColor = new RgbColorClass();
  45. pRgbColor.Red = 255;
  46. pRgbColor.Green = 255;
  47. pRgbColor.Blue = 153;
  48. pRgbColor.Transparency = 1;
  49. pFillSymbol.Color = pRgbColor;
  50. (pElement as IFillShapeElement).Symbol = pFillSymbol;
  51. (m_pMap as IGraphicsContainer).AddElement(pElement, 0);
  52. 设置空间过滤器
  53. pSpatialfilter = new SpatialFilterClass();
  54. pSpatialfilter.Geometry = pBuffer;
  55. 遍历图层
  56. for (int i = 0; i < m_pMap.LayerCount; i++)
  57. {
  58. pFeaLayer = m_pMap.get_Layer(i) as IFeatureLayer;
  59. pFeaClass = pFeaLayer.FeatureClass;
  60. switch (pFeaClass.ShapeType)
  61. {
  62. case esriGeometryType.esriGeometryPoint:
  63. {
  64. pSpatialfilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
  65. break;
  66. }
  67. case esriGeometryType.esriGeometryPolyline:
  68. {
  69. pSpatialfilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
  70. break;
  71. }
  72. case esriGeometryType.esriGeometryPolygon:
  73. {
  74. pSpatialfilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
  75. break;
  76. }
  77. }
  78. pSpatialfilter.GeometryField = pFeaClass.ShapeFieldName;
  79. pFeaCursor = pFeaClass.Search(pSpatialfilter, false);
  80. pFeature = pFeaCursor.NextFeature();
  81. TreeListNode tlNode = null;
  82. 查询到的要素添加到TreeList
  83. while (pFeature != null)
  84. {
  85. 添加图层节点到TreeList
  86. bool bIsContainLayer = false;
  87. foreach (TreeListNode node in tlResult.Nodes)
  88. {
  89. if (node.GetDisplayText(0).Equals(pFeaLayer.Name))
  90. {
  91. tlNode = node;
  92. bIsContainLayer = true;
  93. break;
  94. }
  95. }
  96. if (!bIsContainLayer)
  97. {
  98. tlNode = tlResult.AppendNode(new object[] { pFeaLayer.Name }, null);
  99. }
  100. 添加要素到图层节点下
  101. bool bIsAdd = false;
  102. foreach (TreeListNode childnode in tlNode.Nodes)
  103. {
  104. if (childnode.GetDisplayText(0).Equals(pFeature.OID.ToString()))
  105. {
  106. bIsAdd = true;
  107. break;
  108. }
  109. }
  110. if (!bIsAdd)
  111. {
  112. tlResult.AppendNode(new object[] { pFeature.OID, GtMap.GxAEHelper.Feature.GetValue(pFeature, "NAME") }, tlNode);
  113. }
  114. pFeature = pFeaCursor.NextFeature();
  115. }
  116. }
  117. pFea = pEnumFeature.Next();
  118. }
  119. IActiveView pActiveView = m_pMap as IActiveView;
  120. pActiveView.Refresh();
  121. }
  122. catch (Exception ex)
  123. {
  124. }
  125. }

 

1.2 代码分析

获取要素图形:

IGeometry pGeo = pFea.ShapeCopy;

将图形要素跳转为ITopologicalOperator接口

ITopologicalOperator pTopo = pGeo as ITopologicalOperator;

在拓扑空间建立缓冲区

IGeometry pBuffer = pTopo.Buffer();

2 从线要素首尾节点相邻的角度合并同名道路

  1. 1 using System;
  2. 2 using System.Collections;
  3. 3 using System.Collections.Generic;
  4. 4 using System.Text;
  5. 5 using System.IO;
  6. 6 using System.Windows.Forms;
  7. 7 using ESRI.ArcGIS.ArcMapUI;
  8. 8 using ESRI.ArcGIS.Carto;
  9. 9 using ESRI.ArcGIS.Geodatabase;
  10. 10 using ESRI.ArcGIS.Geometry;
  11. 11
  12. 12 namespace MergeDisconnectPolylineAddin
  13. 13 {
  14. 14 public class MergePolyline : ESRI.ArcGIS.Desktop.AddIns.Button
  15. 15 {
  16. 16
  17. 17 IMap map = null;
  18. 18 IActiveView pActiveView = null;
  19. 19 //private List<IPolyline> DisconnPolylineList = new List<IPolyline>();
  20. 20
  21. 21 public MergePolyline()
  22. 22 {
  23. 23 IMxDocument mxDoc = ArcMap.Application.Document as IMxDocument;
  24. 24 map = mxDoc.FocusMap;
  25. 25 pActiveView = mxDoc.ActivatedView;
  26. 26 }
  27. 27
  28. 28 protected override void OnClick()
  29. 29 {
  30. 30 //
  31. 31 // TODO: Sample code showing how to access button host
  32. 32 //
  33. 33 ArcMap.Application.CurrentTool = null;
  34. 34
  35. 35 //计算程序耗时
  36. 36 DateTime beforDT = System.DateTime.Now;
  37. 37
  38. 38 List<string> distinctString = getDistinctNAMEValue();
  39. 39 MergePloyline(distinctString);
  40. 40
  41. 41 DateTime afterDT = System.DateTime.Now;
  42. 42 TimeSpan ts = afterDT.Subtract(beforDT);
  43. 43 MessageBox.Show("线要素合并结束,运行程序共耗时约:"+ ts.Minutes+"分钟");
  44. 44 }
  45. 45
  46. 46 public List<string> getDistinctNAMEValue()
  47. 47 {
  48. 48 IFeatureLayer featureLayer = map.get_Layer(0) as IFeatureLayer;
  49. 49 IFeatureClass featureClass = featureLayer.FeatureClass;
  50. 50 IQueryFilter queryFilter = new QueryFilterClass();
  51. 51 queryFilter.WhereClause = "";
  52. 52 IFeatureCursor pFeatCursor = featureClass.Search(queryFilter, false);
  53. 53 IFeature pFeature = pFeatCursor.NextFeature();
  54. 54 ArrayList fieldArray = new ArrayList();
  55. 55 List<string> distinctString = new List<string>();
  56. 56 while (pFeature != null)
  57. 57 {
  58. 58 if (featureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
  59. 59 {
  60. 60 IFields fields = pFeatCursor.Fields;
  61. 61 int fieldIndex = fields.FindField("NAME");
  62. 62 string field_NAME = (string)pFeature.get_Value(fieldIndex);
  63. 63 fieldArray.Add(field_NAME);
  64. 64 }
  65. 65 pFeature = pFeatCursor.NextFeature();
  66. 66 }
  67. 67 distinctString = removeSameString(fieldArray);
  68. 68 return distinctString;
  69. 69 }
  70. 70
  71. 71 public void MergePloyline(List<string> DistinctNameValue)
  72. 72 {
  73. 73 IFeatureLayer featureLayer = map.get_Layer(0) as IFeatureLayer;
  74. 74 IFeatureClass featureClass = featureLayer.FeatureClass;
  75. 75
  76. 76 //IDataset dataset = featureClass as IDataset;
  77. 77 //IWorkspaceEdit workspaceEdit = dataset.Workspace as IWorkspaceEdit;
  78. 78 //Type.Missing指的是空类型,因为有些方法你传null给它会出错的,必须要用Type.Missing.
  79. 79 object Missing = Type.Missing;
  80. 80 //workspaceEdit.StartEditing(true);
  81. 81 //workspaceEdit.StartEditOperation();
  82. 82 //string field_NAME = "";
  83. 83
  84. 84 for (int i = 0; i < DistinctNameValue.Count; i++)
  85. 85 {
  86. 86 IQueryFilter queryFilter = new QueryFilterClass();
  87. 87 queryFilter.WhereClause = "";
  88. 88 IFeatureCursor pFeatCursor = featureClass.Search(queryFilter, false);
  89. 89 IFeature pFeature = pFeatCursor.NextFeature();
  90. 90
  91. 91 IFeature pFeatureFirst = pFeature;
  92. 92 //List<IPolyline> toMergePolylineList = new List<IPolyline>();
  93. 93
  94. 94 IGeometryCollection Geometrybag = new GeometryBagClass();
  95. 95 ITopologicalOperator2 pTopOperatorFirst = null;
  96. 96 IGeometry geometrySecond = null;
  97. 97 IGeometry pGeometryFirst = null;
  98. 98 bool bSwitch = true;
  99. 99 while (pFeature != null)
  100. 100 {
  101. 101 map.SelectFeature(featureLayer, pFeature);
  102. 102 if (featureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
  103. 103 {
  104. 104 //IPolyline polyline = geometry as IPolyline;
  105. 105 IFields fields = pFeatCursor.Fields;
  106. 106 int fieldIndex = fields.FindField("NAME");
  107. 107 string field_NAME = (string)pFeature.get_Value(fieldIndex);
  108. 108
  109. 109 if (field_NAME == DistinctNameValue[i])
  110. 110 {
  111. 111 if (bSwitch)
  112. 112 {
  113. 113 //将当前name字段相同的feature中的第一个feature传给pFeatureFirst
  114. 114 pFeatureFirst = pFeature;
  115. 115 pGeometryFirst = pFeature.Shape;
  116. 116 pTopOperatorFirst = (ITopologicalOperator2) pGeometryFirst;
  117. 117 pTopOperatorFirst.IsKnownSimple_2 = false;
  118. 118 pTopOperatorFirst.Simplify();
  119. 119 pGeometryFirst.SnapToSpatialReference();
  120. 120 bSwitch = false;
  121. 121 //break;
  122. 122 }
  123. 123 else
  124. 124 {
  125. 125 //geometrySecond = pFeature.ShapeCopy;
  126. 126 geometrySecond = pFeature.Shape;
  127. 127 Geometrybag.AddGeometry(geometrySecond, ref Missing, ref Missing);
  128. 128 //toMergePolylineList.Add(polyline);
  129. 129 }
  130. 130 }
  131. 131 //DisconnPolylineList.Add(polyline);
  132. 132 }
  133. 133 pFeature = pFeatCursor.NextFeature();
  134. 134 }
  135. 135 IEnumGeometry tEnumGeometry = (IEnumGeometry)Geometrybag;
  136. 136 //IGeometry mergeGeomery = null;
  137. 137 pTopOperatorFirst.ConstructUnion(tEnumGeometry);
  138. 138
  139. 139 pTopOperatorFirst.IsKnownSimple_2 = false;
  140. 140 pTopOperatorFirst.Simplify();
  141. 141 pFeatureFirst.Shape = pGeometryFirst;
  142. 142 //pFeatureFirst.Store();
  143. 143 IFeatureLayer featureLayer2 = map.get_Layer(1) as IFeatureLayer;
  144. 144 IFeatureClass featureClass2 = featureLayer2.FeatureClass;
  145. 145 AddPolyline(featureClass2, pGeometryFirst);
  146. 146 }
  147. 147 //workspaceEdit.StopEditOperation();
  148. 148 //workspaceEdit.StopEditing(true);
  149. 149 }
  150. 150 private void AddPolyline(IFeatureClass pFeatureClass, IGeometry polyline)
  151. 151 {
  152. 152 IFeatureBuffer featureBuffer = pFeatureClass.CreateFeatureBuffer();
  153. 153 IFeatureCursor featureCursor;
  154. 154 featureCursor = pFeatureClass.Insert(true);
  155. 155 featureBuffer.Shape = polyline;
  156. 156 featureCursor.InsertFeature(featureBuffer);
  157. 157 featureCursor.Flush();
  158. 158 System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
  159. 159 }
  160. 160 public List<string> removeSameString(ArrayList stringArray)
  161. 161 {
  162. 162 //List用于存储从数组里取出来的不相同的元素
  163. 163 List<string> distinctString = new List<string>();
  164. 164 foreach (string eachString in stringArray)
  165. 165 {
  166. 166 if (!distinctString.Contains(eachString))
  167. 167 distinctString.Add(eachString);
  168. 168 }
  169. 169 return distinctString;
  170. 170 }
  171. 171
  172. 172 protected override void OnUpdate()
  173. 173 {
  174. 174 Enabled = ArcMap.Application != null;
  175. 175 }
  176. 176 }
  177. 177
  178. 178 }

X 问题及处置办法

X.1 HRESULT:0x80040215

在使用ITopologicalOperator求交集等操作的时候,有时会出现“ HRESULT:0x80040215”这个异常

原因是空间参考不一样所致。

将 SpatialReference 属性设为同一空间参考就不会出现 HRESULT:0x80040215 异常啦。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/341088
推荐阅读
相关标签
  

闽ICP备14008679号