Server spatial query COM utility
VegCOMCSharp\VegUtils.cs
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 



using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.EnterpriseServices;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

[GuidAttribute("D3FE0F0A-245E-4e6c-92BC-005B9DC3D93A")]
public interface IVegUtils
{
    IVegResults sumVegetationType(ref IFeatureClass pVegClass, ref IPoint pPoint, ref double dDistance, ref string sSummaryFld);
}

namespace VegCOMCSharp
{
    [AutomationProxy(true), ClassInterface(ClassInterfaceType.None), GuidAttribute("1EBD03A5-1216-459f-845F-3A61749FAEF7")]
    public class VegUtils : ServicedComponent, IVegUtils
    {
        public IVegResults sumVegetationType(ref IFeatureClass pVegClass, ref IPoint pPoint, ref double dDistance, ref string sSummaryFld)
    {
      // buffer the point
            ITopologicalOperator pTopoOp = pPoint as ITopologicalOperator;
            IGeometry pGeom = pTopoOp.Buffer(dDistance);

      // query the feature class
      ISpatialFilter pSFilter = new SpatialFilter();
      pSFilter.Geometry = pGeom;
      pSFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
      pSFilter.GeometryField = pVegClass.ShapeFieldName;

      IFeatureCursor pFCursor = pVegClass.Search(pSFilter, true);

      // loop thourgh the features, clip each geometry to the buffer
      // and total areas by attribute value
      pTopoOp = pGeom as ITopologicalOperator;
      int lPrim = pVegClass.FindField(sSummaryFld);

      System.Collections.Specialized.ListDictionary dict  = new System.Collections.Specialized.ListDictionary();

      // create the symbol and graphic elements collection for the graphics
      ISimpleFillSymbol pSFS = newFillS();
      IGraphicElements pGraphics = new GraphicElements();

      IFeature pFeature;
      while ((pFeature = pFCursor.NextFeature()) != null)
      {
         // create the graphic
        IFillShapeElement pFE = new PolygonElement() as IFillShapeElement;
        IElement pElement = pFE as IElement;

        // clip the geometry
        IGeometry pNewGeom = pTopoOp.Intersect(pFeature.Shape, esriGeometryDimension.esriGeometry2Dimension);
        pElement.Geometry = pNewGeom;
        pFE.Symbol = pSFS;
        IGraphicElement ge = pFE as IGraphicElement;
        pGraphics.Add(ge);

        // add to dictionary
        IArea pArea = pNewGeom as IArea;
        string sType = pFeature.get_Value(lPrim) as string;
        if(dict.Contains(sType))
          dict[sType] = (double)dict[sType] + pArea.Area;
        else
          dict[sType] = pArea.Area;
      }

      // create the summary recordset
      IRecordSet psumRS = sumRS(dict);

      // create the results object
      IVegResults pRes = new VegResults();
      pRes.ResGraphics = pGraphics;
      pRes.Stats = psumRS;

            return pRes;
    }

    private IRecordSet sumRS(System.Collections.Specialized.ListDictionary dict)
    {
      // create the new record set
      IRecordSet pNewRs  = new RecordSet();
      IRecordSetInit prsInit = pNewRs as IRecordSetInit;

      IFields pFields = new Fields();
      IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
      pFieldsEdit.FieldCount_2 = 2;

      IField pField = new Field();
      IFieldEdit pFieldEdit = pField as IFieldEdit;
      pFieldEdit.Name_2 = "Type";
      pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
      pFieldEdit.Length_2 = 50;
      pFieldsEdit.set_Field(0,pField);

      pField = new Field();
      pFieldEdit = pField as IFieldEdit;

      pFieldEdit.Name_2 = "Area";
      pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
      pFieldsEdit.set_Field(1,pField);

      prsInit.CreateTable(pFields);

      // add all the area/type pairs
      ICursor pIC = prsInit.Insert();
      IRowBuffer pRowBuf = prsInit.CreateRowBuffer();

      System.Collections.IDictionaryEnumerator myEnumerator = dict.GetEnumerator();
      while (myEnumerator.MoveNext())
      {
        pRowBuf.set_Value(0, myEnumerator.Key);
        pRowBuf.set_Value(1, myEnumerator.Value);
        pIC.InsertRow(pRowBuf);
      }

      return pNewRs;
    }

    private ISimpleFillSymbol newFillS()
    {
      ISimpleLineSymbol pSLS = new SimpleLineSymbol();
      IRgbColor pcolor = new RgbColor();
      pcolor.Red = 255;
      pcolor.Green = 0;
      pcolor.Blue = 0;
      pSLS.Color = pcolor;
      pSLS.Style = esriSimpleLineStyle.esriSLSSolid;
      pSLS.Width = 2;

      ISimpleFillSymbol pSFS = new SimpleFillSymbol();
      pSFS.Outline = pSLS;
      pSFS.Style = esriSimpleFillStyle.esriSFSHollow;

      return pSFS;
    }

    }
}