Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a task of getting the total area of all the polygon features within a boundary. So I got the envelope of the boundary and did a spatial query to get the features inside that element

but my problem is I am able to get the elements that overlaps the envelope but not exactly inside it.. 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;

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..

share|improve this question
If you want to get features within the boundary, you should be using the geometry of the boundary, not the spatialfilter.Geometry = boundaryfeature.Shape; could be what you need..envelope is just a rectangular shape that contains your geometry..that is probably why you get unexpected result.. – vinayan Mar 29 '12 at 11:27

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.

share|improve this answer
yep got it that worked fine thanks a bunch – Santosh Mar 30 '12 at 11:25
I want to accomplish the same thing - get the areas of the polygon parts that are in the visible extent. Unfortunately, I don't understand how to use ITopologicalOperator.Clip. Could someone include the code for this part of the answer? – jencarta Jun 15 '15 at 17:51

Within will only give you results that are fully within your polygon. If you have a polygon that is half inside half outside, it will be excluded from the result. Use intersects instead.

As noted above, this is a two step operation.

Its something like this, in pseudo-code:

  1. Create a spatial filter;
  2. Get the geometry that will filter the results and use it in spatial filter;
  3. for each result in the cursor do:
  4. test if the geometry is within your original geometry. This is simple using the IRelationalOperator interface.
    1. if it's within, sum it's area to your total;
    2. if it's not within, clip the geometry with your query geom and add that result to your sum total
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.