ArcGIS_SelectBufferTool_CSharp\App_Code\CustomTools.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. // namespace CustomTools { class BufferTool : ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction { #region IMapServerToolAction Members public void ServerAction(ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs toolEventArgs) { // Get the map control on which the tool was executed ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)toolEventArgs.Control; try { #region Retrieve tool input parameters // Specify the URL to an ArcGIS Server geometry service. This will be used to perform the // buffer operation string geometryServiceUrl = "http://tasks.arcgisonline.com/arcgis/services/Geometry/GeometryServer"; // Get the user-specified tool parameters specified via the page's interface System.Collections.Specialized.NameValueCollection inputParametersNameValueCollection = null; // Since the page is using the PartialPostback framework, and the controls receiving user // input are ASP.NET server controls, the tool parameters are automatically passed in // the page's request parameters. inputParametersNameValueCollection = adfMap.Page.Request.Params; // The tool's input parameters are stored in the name value collection with their keys as the // ID of the controls receiving the input string activeLayerName = inputParametersNameValueCollection["activeLayerDropDownList"]; string bufferDistanceString = inputParametersNameValueCollection["bufferDistanceTextBox"]; string units = inputParametersNameValueCollection["unitsDropDownList"]; // Get the name of the resource on which to perform the selection from session. This is // specified in Default's code-behind page. string targetResourceName = (string)adfMap.Page.Session["targetResourceName"]; #endregion #region Derive buffer input and execute buffer operation // Get a reference to the polygon drawn by the user as both a Web ADF and ArcGIS Server // SOAP polygon ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPolygonEventArgs mapPolygonEventArgs = toolEventArgs as ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPolygonEventArgs; ESRI.ArcGIS.ADF.Web.Geometry.Polygon adfInputPolygon = mapPolygonEventArgs.MapPolygon; ESRI.ArcGIS.ADF.ArcGISServer.PolygonN agsSoapInputPolygon = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfPolygon(adfInputPolygon); // Get a reference to an ArcGIS Server Geometry service ESRI.ArcGIS.ADF.ArcGISServer.GeometryServerProxy geometryServerProxy = new ESRI.ArcGIS.ADF.ArcGISServer.GeometryServerProxy(geometryServiceUrl); // Get a spatial reference in which to perform the buffer operation. This spatial reference // is explicitly intialized based on the user-drawn polygon so that there is minimal projection // related buffer distortion ESRI.ArcGIS.ADF.ArcGISServer.SpatialReference agsSoapBufferSpatialReference = Utility.CreateOperationSpatialReference(agsSoapInputPolygon, geometryServerProxy); // Use the units specified by the user to create an ArcGIS Server LinearUnit object ESRI.ArcGIS.ADF.ArcGISServer.LinearUnit agsSoapBufferUnits = new ESRI.ArcGIS.ADF.ArcGISServer.LinearUnit(); agsSoapBufferUnits.WKID = Utility.GetWkidByUnitName(units); agsSoapBufferUnits.WKIDSpecified = true; // Put the ArcGIS Server SOAP polygon in an ArcGIS Server SOAP geometry array to pass to the // buffer operation ESRI.ArcGIS.ADF.ArcGISServer.Geometry[] agsSoapSourceGeometryArray = new ESRI.ArcGIS.ADF.ArcGISServer.Geometry[1]; agsSoapSourceGeometryArray[0] = agsSoapInputPolygon as ESRI.ArcGIS.ADF.ArcGISServer.Geometry; // Get the user-specified buffer distance and put it in an array to pass to the buffer // operation. If the user did not specify a distance, initialize the distance to zero. double bufferDistance; if (!double.TryParse(bufferDistanceString, out bufferDistance)) bufferDistance = 0; double[] bufferDistances = new double[1]; bufferDistances[0] = bufferDistance; // Execute the buffer operation via the geometry service ESRI.ArcGIS.ADF.ArcGISServer.Geometry[] agsBufferGeometryArray = geometryServerProxy.Buffer( agsSoapInputPolygon.SpatialReference, agsSoapBufferSpatialReference, null, bufferDistances, agsSoapBufferUnits, false, agsSoapSourceGeometryArray); #endregion //Check the geometryarray returned is not empty. if (agsBufferGeometryArray.GetLength(0) == 0) { ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult noBufferDisAlertCallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript( "alert('No features selected/buffered. Please verify buffer properties.')"); adfMap.CallbackResults.Add(noBufferDisAlertCallbackResult); return; } #region Create buffer graphics and add to the map // Retrieve the buffer polygon from the array of result geometries ESRI.ArcGIS.ADF.ArcGISServer.PolygonN agsSoapBufferPolygon = agsBufferGeometryArray[0] as ESRI.ArcGIS.ADF.ArcGISServer.PolygonN; // Create the fill symbol for the buffer ESRI.ArcGIS.ADF.ArcGISServer.SimpleFillSymbol agsSoapBufferSimpleFillSymbol = Utility.CreateSimpleFillSymbol(System.Drawing.Color.Red, ESRI.ArcGIS.ADF.ArcGISServer.esriSimpleFillStyle.esriSFSForwardDiagonal, System.Drawing.Color.Black, 2); // Create a polygon graphic element for the buffer ESRI.ArcGIS.ADF.ArcGISServer.PolygonElement agsSoapBufferPolygonElement = new ESRI.ArcGIS.ADF.ArcGISServer.PolygonElement(); agsSoapBufferPolygonElement.Symbol = agsSoapBufferSimpleFillSymbol; agsSoapBufferPolygonElement.Polygon = agsSoapBufferPolygon; // Create the fill symbol for the input polygon ESRI.ArcGIS.ADF.ArcGISServer.SimpleFillSymbol agsSoapInputPolygonSimpleFillSymbol = Utility.CreateSimpleFillSymbol(System.Drawing.Color.Cyan, ESRI.ArcGIS.ADF.ArcGISServer.esriSimpleFillStyle.esriSFSSolid, System.Drawing.Color.Red, 1); // Create a polygon graphic element for the input polygon ESRI.ArcGIS.ADF.ArcGISServer.PolygonElement agsSoapInputPolygonElement = new ESRI.ArcGIS.ADF.ArcGISServer.PolygonElement(); agsSoapInputPolygonElement.Symbol = agsSoapInputPolygonSimpleFillSymbol; agsSoapInputPolygonElement.Polygon = agsSoapInputPolygon; // Create an array of graphic elements to store the input polygon and buffer graphics ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[] agsSoapGraphicElementArray = new ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[2]; agsSoapGraphicElementArray[0] = agsSoapBufferPolygonElement; agsSoapGraphicElementArray[1] = agsSoapInputPolygonElement; // Get a reference to the target resource's ArcGIS Server data-source specific map functionality ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality agsMapFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)adfMap.GetFunctionality( targetResourceName); // Place the graphics on the map by assigning the element array to the custom graphics // property of the resource's map description ESRI.ArcGIS.ADF.ArcGISServer.MapDescription agsSoapMapDescription = agsMapFunctionality.MapDescription; agsSoapMapDescription.CustomGraphics = agsSoapGraphicElementArray; #endregion #region Select features that intersect the buffer // Call method to set the active layer's selected features to those intersecting the buffer Utility.SelectIntersectingFeatures(agsMapFunctionality, agsSoapBufferPolygon, activeLayerName, System.Drawing.Color.Yellow); // Refresh the target resource so the changes are displayed adfMap.RefreshResource(targetResourceName); #endregion } catch (System.Exception exception) { ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult = Utility.CreateErrorCallbackResult(exception); adfMap.CallbackResults.Add(errorCallbackResult); } } #endregion } public class SelectTool : ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction { #region IMapServerToolAction Members public void ServerAction(ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs toolEventArgs) { // Get the map control on which the tool was executed ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)toolEventArgs.Control; try { // Get the user-specified tool parameters specified via the page's interface System.Collections.Specialized.NameValueCollection inputParametersNameValueCollection = null; // Since the page is using the PartialPostback framework, and the controls receiving user // input are ASP.NET server controls, the tool parameters are automatically passed in // the page's request parameters. inputParametersNameValueCollection = adfMap.Page.Request.Params; // The tool's input parameters are stored in the name value collection with their keys as the // ID of the controls receiving the input string activeLayerName = inputParametersNameValueCollection["activeLayerDropDownList"]; // Get the name of the resource on which to perform the selection from session. This is // specified in Default's code-behind page. string targetResourceName = (string)adfMap.Page.Session["targetResourceName"]; // Get a reference to the user-drawn rectangle as an ArcGIS Server SOAP envelope ESRI.ArcGIS.ADF.Web.UI.WebControls.MapRectangleEventArgs mapRectangleEventArgs = toolEventArgs as ESRI.ArcGIS.ADF.Web.UI.WebControls.MapRectangleEventArgs; ESRI.ArcGIS.ADF.ArcGISServer.EnvelopeN agsSoapInputEnvelope = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfEnvelope( mapRectangleEventArgs.MapExtent); // Get the map functionality for the target resource ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality agsMapFunctionality = adfMap.GetFunctionality(targetResourceName) as ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality; // Call method to set the active layer's selected features to those intersecting the user-drawn // rectangle Utility.SelectIntersectingFeatures(agsMapFunctionality, agsSoapInputEnvelope, activeLayerName, System.Drawing.Color.Yellow); // Refresh the target resource so the new selection is displayed adfMap.RefreshResource(targetResourceName); } catch (System.Exception exception) { ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult = Utility.CreateErrorCallbackResult(exception); adfMap.CallbackResults.Add(errorCallbackResult); } } #endregion } }