Select to view content in your preferred language

How to perform spatial filter to get attributes?

1119
2
07-22-2010 11:30 AM
Chandan_KumarRath
Emerging Contributor
I am new to ArcObjects. Trying to make a method to get attributes from a feature by mouse click.
Any body, please help me to perform spatial query to get attributes of a feature.
My code is as below.

        public void getFeature(IActiveView activeView, int button, int shift, int X, int Y)
        {
            IMap map;
            IPoint ClickedPoint = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
            if (activeView is IMap)
            {
                map = activeView.HitTestMap(ClickedPoint);
                if (map == null) return;
                IRubberBand rubberEnv = new RubberEnvelopeClass();

                IGeometry geom = rubberEnv.TrackNew(activeView.ScreenDisplay, null);
                IArea area = (IArea)geom;

                if (geom.IsEmpty == true)
                {
                    IEnvelope tempEnv = new EnvelopeClass();
                    tagRECT RECT = new tagRECT();
                    RECT.bottom = 0;
                    RECT.left = 0;
                    RECT.right = 5;
                    RECT.top = 5;

                    IDisplayTransformation dispTrans = activeView.ScreenDisplay.DisplayTransformation;
                    dispTrans.TransformRect(tempEnv, ref RECT, 4);
                    tempEnv.CenterAt(ClickedPoint);
                    geom = (IGeometry)tempEnv;
                }

                if (geom.IsEmpty == true) return;

                @ HERE I NEED SPATIAL FILTER @
                

            }

        }


Thanking you...
0 Kudos
2 Replies
Chandan_KumarRath
Emerging Contributor
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//000100000240000000


Thank you very much  ldonahue

Its done...

current code is:

        public void getFeature(IActiveView activeView, int button, int shift, int X, int Y)
        {
            IMap map;
            IPoint ClickedPoint = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
            if (activeView is IMap)
            {
                map = activeView.HitTestMap(ClickedPoint);
                if (map == null) return;
                IRubberBand rubberEnv = new RubberEnvelopeClass();

                IGeometry geom = rubberEnv.TrackNew(activeView.ScreenDisplay, null);
                IArea area = (IArea)geom;

                if (geom.IsEmpty == true)
                {
                    IEnvelope tempEnv = new EnvelopeClass();
                    tagRECT RECT = new tagRECT();
                    RECT.bottom = 0;
                    RECT.left = 0;
                    RECT.right = 5;
                    RECT.top = 5;

                    IDisplayTransformation dispTrans = activeView.ScreenDisplay.DisplayTransformation;
                    dispTrans.TransformRect(tempEnv, ref RECT, 4);
                    tempEnv.CenterAt(ClickedPoint);
                    geom = (IGeometry)tempEnv;
                }

                if (geom.IsEmpty == true) return;

                ISpatialFilter spatialFilter = new SpatialFilterClass();
                IFeatureLayer schoolLayer = (IFeatureLayer)axMapControl1.get_Layer(0);
                IFeatureClass schoolFeature = schoolLayer.FeatureClass;
                spatialFilter.Geometry = geom;
                spatialFilter.GeometryField = schoolFeature.ShapeFieldName;
                spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
                spatialFilter.SubFields = "SCHOOL";
                int nameFieldPosition = schoolFeature.FindField("SCHOOL");
                using (ComReleaser comReleaser = new ComReleaser())
                {
                    IFeatureCursor schoolCursor = schoolFeature.Search(spatialFilter, false);
                    comReleaser.ManageLifetime(schoolCursor);
                    IFeature schoolFeat = null;
                    while ((schoolFeat = schoolCursor.NextFeature()) != null)
                    {
                        string name = Convert.ToString(schoolFeat.get_Value(nameFieldPosition));
                        MessageBox.Show(name);
                    }
                }
            }
        }


Please have a look at the code for any optimization...

Thanks.....!
0 Kudos