首页 > 代码库 > C# Arcgis Engine 获得鼠标按下位置的要素
C# Arcgis Engine 获得鼠标按下位置的要素
public IFeature GetFeatureOnMouseDown(IPoint point) { try { ILayer layer = Common.GetLayerByName(mMap, "地块"); if (layer == null) { MessageBox.Show("请加载地块图层!", "提示"); return null; } //IFeatureLayer fLayer = layer as IFeatureLayer; //IFeatureSelection featureSelection = fLayer as IFeatureSelection; //featureSelection.Clear(); //if (featureSelection == null) //{ // return null; //} IFeatureLayer featureLayer = layer as IFeatureLayer; if (featureLayer == null) return null; IFeatureClass featureClass = featureLayer.FeatureClass; if (featureClass == null) return null; //IPoint point = axMapControl1.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y); IGeometry geometry = point as IGeometry; double length = ConvertPixelsToMapUnits(axMapControl1.ActiveView, 4); ITopologicalOperator pTopo = geometry as ITopologicalOperator; IGeometry buffer = pTopo.Buffer(length); geometry = buffer.Envelope as IGeometry; ISpatialFilter spatialFilter = new SpatialFilterClass(); spatialFilter.Geometry = geometry; switch (featureClass.ShapeType) { case esriGeometryType.esriGeometryPoint: spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains; break; case esriGeometryType.esriGeometryPolygon: spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; break; case esriGeometryType.esriGeometryPolyline: spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses; break; } spatialFilter.GeometryField = featureClass.ShapeFieldName; IQueryFilter filter = spatialFilter as IQueryFilter; IFeatureCursor cursor = featureClass.Search(filter, false); IFeature pfeature = cursor.NextFeature(); if (pfeature != null) { return pfeature; //featureSelection.Add(pfeature); //pfeature = cursor.NextFeature(); } else { return null; } } catch { return null; } }
大家可以修改一下我的代码,图层改成你需要的图层即可。也可以写成类。只是写成类的话,变量可能会多一点。
if (pfeature != null) { return pfeature; //featureSelection.Add(pfeature); //pfeature = cursor.NextFeature(); }
此处也可以将if改成while,如果选择的是多个要素的话。将我注释掉的代码打开即可选择多个要素。
C# Arcgis Engine 获得鼠标按下位置的要素
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。