要求:
编写程序,选中要素并对其进行Buffer,而后选中缓冲区内的要素。
1、在地图上右键单击,根据鼠标位置查询到Parcels中的Parcel。在窗体上显示该Parcel的投影信息以及“Landuse”字段值。
2、根据给定半径对1中的Parcel做缓冲区,并选中与缓冲区相交的所有parcel。
作答:
1.点选要素,显示相关信息
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 2)
{
//------------点选是根据鼠标点创建一个缓冲区,然后再用SelectByShape方法-------/
//创建鼠标点击的位置Point
IPoint pPoint = new PointClass();
pPoint.PutCoords(e.mapX, e.mapY); //PutCoords(X:Double,Y:Double)
//创建缓冲区
ITopologicalOperator pTO = pPoint as ITopologicalOperator;
IGeometry pGeometry = pTO.Buffer(0); //半径为0点的缓冲区=点
//选择Feature,IMap接口的方法SelectShape(Shape,env,justOne)
//从Layer中依靠一个图形的范围shape和一个选择的环境来选择要素,在所有图层中只从IFeatureLayer的图层中进行选择
axMapControl1.Map.SelectByShape(pGeometry, null, false);
axMapControl1.Refresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection, null, null);
----------------------------------------------------/
------------☆访问对Map中FeatureLayer被选中的要素----------------//
IFeatureLayer pFL = axMapControl1.Map.get_Layer(0) as IFeatureLayer;
IFeatureClass pFC = pFL.FeatureClass;
//使用了FeatureLayer类的IFeatureSelection接口,及接口的SelectionSet属性
IFeatureSelection pFS = pFL as IFeatureSelection;
ISelectionSet pSS = pFS.SelectionSet;
//访问SelectionSet中的每个要素
ICursor pCursor;
pSS.Search(null, true, out pCursor);
IFeatureCursor pFCur = pCursor as IFeatureCursor;
IFeature pFea = pFCur.NextFeature();
while (pFea != null)
{
textBox2.Clear();
textBox3.Clear();
IGeoDataset pGD = pFC as IGeoDataset;
textBox2.Text = pGD.SpatialReference.Name.ToString();
int FieldIndex = pFea.Fields.FindField("Landuse");
if (pFea.get_Value(FieldIndex).ToString() != null)
textBox3.Text = pFea.get_Value(FieldIndex).ToString();
else
textBox3.Text = "Land Use Unknown!";
pFea = pFCur.NextFeature();
}
}
}
2.选中与缓冲区分析相交的要素
private void button1_Click(object sender, EventArgs e)
{
IFeatureLayer pFL = axMapControl1.Map.get_Layer(0) as IFeatureLayer;
IFeatureClass pFC = pFL.FeatureClass;
//☆获取选中要素的属性(IMap.FeatureSelection方法获取Feature的属性信息)
ISelection pSelection = axMapControl1.Map.FeatureSelection; //获取选择集
IEnumFeatureSetup pEnumFS = pSelection as IEnumFeatureSetup; //打开属性标签
pEnumFS.AllFields = true;
IEnumFeature pEnumF = pSelection as IEnumFeature; //读取属性
IFeature pFea = pEnumF.Next();
if (pFea != null)
{
axMapControl1.Map.ClearSelection(); //清空选择的要素
//缓冲区
IGeometry pGeometry = pFea.Shape
ITopologicalOperator pTO = pGeometry as ITopologicalOperator;
IGeometry pGeometry1 = pTO.Buffer(int.Parse(textBox1.Text));
IFeatureSelection pFS = pFL as IFeatureSelection;
ISpatialFilter pSF = new SpatialFilterClass();
pSF.Geometry = pGeometry1;
pSF.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; //地图中的是面要素
pFS.SelectFeatures(pSF, esriSelectionResultEnum.esriSelectionResultNew, false);
/* (优化代码,可以用上面的IFeatureSelectionn的SelectFeatures 替代 axMapControl1.Map.SelectFeaure)
IFeatureCursor pFCur = pFC.Search(pSF, true);
IFeature pFea1 = pFCur.NextFeature();
while (pFea1 != null)
{
axMapControl1.Map.SelectFeature(pFL, pFea1); //选择要素;IMap接口的SelectFeature(Layer,Feature) 从Layer图层选择要素
pFea1 = pFCur.NextFeature();
}
*/
}
axMapControl1.ActiveView.Refresh();
}
参考文章:
https://blog.csdn.net/m0_37768631/article/details/86037509