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,如果选择的是多个要素的话。将我注释掉的代码打开即可选择多个要素。
时间: 2024-11-09 09:57:07