spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin; not working

728
1
03-28-2012 11:22 PM
SantoshV
New Contributor II
Hi
I had a task to get the area of the feature polygon within a boundary
So I got the envelope of the boundary and did a spatial query to get the features inside that Boundary

but my problem is I am able to get the elements that overlaps the envelope but not exactly inside it.. the polygon is not clipping to the envelope
my code goes like this
IMxDocument mxDoc = (IMxDocument)GetMxDocumentFromArcMap(m_application);
            IEnvelope gridEnvelope = GetGridExtents("Z29");
            IFeatureClass featureClass = GetFeatureClassFromShapefileOnDisk("D:\\Projects\\Working\\BMRDA\\data", "Hoskote_Satellite_Polygon");

            // Create the spatial filter and set its spatial constraints.
            ISpatialFilter spatialFilter = new SpatialFilterClass();
            spatialFilter.Geometry = gridEnvelope.Envelope;
            spatialFilter.GeometryField = featureClass.ShapeFieldName;
            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin;
            spatialFilter.set_OutputSpatialReference(featureClass.ShapeFieldName, mxDoc.FocusMap.SpatialReference);

            // Set the attribute constraints and subfields.
            // We want to exclude ramps, highways and interstates.
            spatialFilter.WhereClause = "LEVEL3 = 'AGRICULTURE LAND'";

            // Execute the query.
            IFeatureCursor featureCursor = featureClass.Search(spatialFilter, true);
            ESRI.ArcGIS.Geodatabase.IFeature feature;  // Automatically initialized to null. Used to test existence of a feature in the featureCursor
            double dbArea = 0.0;
            int intCount = 0;
            while ((feature = featureCursor.NextFeature()) != null)
            {
                IArea area = (IArea)feature.Shape;
                dbArea = dbArea + area.Area;
                intCount++;
            }

the spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin; does not work it returns me null I want the polygon to be clipped and get the area of it
Am i missing something here?
Please guide..
0 Kudos
1 Reply
SantoshV
New Contributor II
Well I found out the solution  here it is

If you want the area of the part that is inside the envelope you have to query with esriSpatialRelOverlaps or esriSpatialRelInstersects and perform a clip operation on all the returned geometries using ITopologicalOperator.Clip against the envelope. If you are using another geometry than an envelope to clip against you should be using ITopologicalOperator.Intersect.

As always whe performing geometrical operations you should ensure that both geometries have the same spatial reference.
0 Kudos