码迷,mamicode.com
首页 > Windows程序 > 详细

C# Arcgis Engine 获得鼠标按下位置的要素

时间:2014-10-20 11:33:25      阅读:655      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   div   

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 获得鼠标按下位置的要素

标签:style   blog   color   io   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/joysky/p/4036573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!