Server spatial query COM utility
VegWebAppCSharp\App_Code\VegTool.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.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools;
using ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer;
using ESRI.ArcGIS.ADF.Web.DataSources;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Geometry;
using VegCOMCSharp;
using ESRI.ArcGIS.ADF.ArcGISServer;
using ESRI.ArcGIS.Geodatabase;

public class VegTool : IMapServerToolAction 
{
    #region IMapServerToolAction Members

    public void ServerAction(ToolEventArgs args)
    {
        ESRI.ArcGIS.ADF.Web.UI.WebControls.Map mapctrl = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map) args.Control;
        
        // Get MapFunctionality from the control...
        MapFunctionality mapfunc = (MapFunctionality) mapctrl.GetFunctionality(mapctrl.MapResourceManagerInstance.ResourceItems.Count - 1);
        MapResourceLocal mapres = (MapResourceLocal) mapfunc.MapResource;
        IServerContext sc = mapres.ServerContextInfo.ServerContext;
        IMapServer map = mapres.MapServer;

        IMapServerObjects mapobj = (IMapServerObjects) map;
        IMap fgmap = mapobj.get_Map(map.DefaultMapName);
        IFeatureLayer fl = (IFeatureLayer) fgmap.get_Layer(0);
        IFeatureClass fc = fl.FeatureClass;

        PointEventArgs pargs = (PointEventArgs) args;
        ESRI.ArcGIS.ADF.Web.Geometry.Point inpt = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(pargs.ScreenPoint, mapctrl.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap));
        IPoint pt = (IPoint)sc.CreateObject("esriGeometry.Point");
        pt.X = inpt.X;
        pt.Y = inpt.Y;

        string tbxvalue = (string) mapctrl.Page.Session["TextBox1Value"];

        double distance = 0;
        
        if (!Double.TryParse(tbxvalue, out distance))
        {
            distance = 10000;
        }
        
        string fldName = "PRIMARY_";

        IVegUtils vegutils = (IVegUtils) sc.CreateObject("VegCOMCSharp.VegUtils");
        IVegResults vegresults = vegutils.sumVegetationType(ref fc, ref pt, ref distance, ref fldName);

        // add the graphics to the map
        IGraphicElements comGraphics = vegresults.ResGraphics;
        GraphicElement[] proxyGraphics = (GraphicElement[])
            ESRI.ArcGIS.ADF.ArcGISServer.Converter.ComObjectToValueObject(comGraphics, sc, typeof(GraphicElement[]));

        RgbColor rgb = new RgbColor();
        rgb.Red = 155;
        rgb.Green = 0;
        rgb.Blue = 0;
        rgb.AlphaValue = 255;

        SimpleLineSymbol sls = new SimpleLineSymbol();
        sls.Style = ESRI.ArcGIS.ADF.ArcGISServer.esriSimpleLineStyle.esriSLSSolid;
        sls.Color = rgb;
        sls.Width = 1.2;

        foreach (ESRI.ArcGIS.ADF.ArcGISServer.PolygonElement pe in proxyGraphics)
        {
            SimpleFillSymbol sfs = (SimpleFillSymbol)pe.Symbol;
            sfs.Outline = sls;
        }

        mapfunc.MapDescription.CustomGraphics = proxyGraphics;        

        string cbxvalue = (string) mapctrl.Page.Session["CheckBox1Value"];
        if (bool.Parse(cbxvalue))
        {
            // display table of summarized areas
            IRecordSet rs = vegresults.Stats;
            ESRI.ArcGIS.ADF.ArcGISServer.RecordSet value_rs = (ESRI.ArcGIS.ADF.ArcGISServer.RecordSet)ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ComObjectToValueObject(rs, sc, typeof(ESRI.ArcGIS.ADF.ArcGISServer.RecordSet));
            System.Data.DataTable rsDatatable = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToDataTable(value_rs);
            System.Data.DataSet rsDataset = new System.Data.DataSet();
            rsDataset.Tables.Add(rsDatatable);
            mapctrl.Page.Session["VegDataset"] = rsDataset;

        }
        else
        {
            mapctrl.Page.Session["VegDataset"] = null;
        }
      
        mapctrl.Refresh();
    }

    #endregion
}