ArcGIS Select Buffer Tool
ArcGIS_SelectBufferTool_VBNet\App_Code\CustomTools.vb
' 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.
' 

Imports Microsoft.VisualBasic
Imports System
Namespace CustomTools
  Friend Class BufferTool
        Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction
    #Region "IMapServerToolAction Members"

        Public Sub ServerAction(ByVal toolEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs) Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction.ServerAction
            ' Get the map control on which the tool was executed
            Dim adfMap As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map = CType(toolEventArgs.Control, ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)

            Try
                '        #Region "Retrieve tool input parameters"

                ' Specify the URL to an ArcGIS Server geometry service.  This will be used to perform the 
                ' buffer operation
                Dim geometryServiceUrl As String = "http://tasks.arcgisonline.com/arcgis/services/Geometry/GeometryServer"

                ' Get the user-specified tool parameters specified via the page's interface
                Dim inputParametersNameValueCollection As System.Collections.Specialized.NameValueCollection = Nothing
              
                ' 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
                Dim activeLayerName As String = inputParametersNameValueCollection("activeLayerDropDownList")
                Dim bufferDistanceString As String = inputParametersNameValueCollection("bufferDistanceTextBox")
                Dim units As String = 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.
                Dim targetResourceName As String = CStr(adfMap.Page.Session("targetResourceName"))

              
               
                ' Get a reference to the polygon drawn by the user as both a Web ADF and ArcGIS Server
                ' SOAP polygon
                Dim mapPolygonEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPolygonEventArgs = TryCast(toolEventArgs, ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPolygonEventArgs)
                Dim adfInputPolygon As ESRI.ArcGIS.ADF.Web.Geometry.Polygon = mapPolygonEventArgs.MapPolygon
                Dim agsSoapInputPolygon As ESRI.ArcGIS.ADF.ArcGISServer.PolygonN = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfPolygon(adfInputPolygon)

                ' Get a reference to an ArcGIS Server Geometry service
                Dim geometryServerProxy As ESRI.ArcGIS.ADF.ArcGISServer.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
                Dim agsSoapBufferSpatialReference As ESRI.ArcGIS.ADF.ArcGISServer.SpatialReference = Utility.CreateOperationSpatialReference(agsSoapInputPolygon, geometryServerProxy)

                ' Use the units specified by the user to create an ArcGIS Server LinearUnit object
                Dim agsSoapBufferUnits As ESRI.ArcGIS.ADF.ArcGISServer.LinearUnit = 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
                Dim agsSoapSourceGeometryArray As ESRI.ArcGIS.ADF.ArcGISServer.Geometry() = New ESRI.ArcGIS.ADF.ArcGISServer.Geometry(0) {}
                agsSoapSourceGeometryArray(0) = TryCast(agsSoapInputPolygon, 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.
                Dim bufferDistance As Double
                If (Not Double.TryParse(bufferDistanceString, bufferDistance)) Then
                    bufferDistance = 0
                End If
                Dim bufferDistances As Double() = New Double(0) {}
                bufferDistances(0) = bufferDistance

                ' Execute the buffer operation via the geometry service  
                Dim agsBufferGeometryArray As ESRI.ArcGIS.ADF.ArcGISServer.Geometry() = geometryServerProxy.Buffer(agsSoapInputPolygon.SpatialReference, agsSoapBufferSpatialReference, Nothing, bufferDistances, agsSoapBufferUnits, False, agsSoapSourceGeometryArray)

                'Check something is returned in the geometry array,otherwise inform the user
                If agsBufferGeometryArray.GetLength(0) = 0 Then
                    Dim noBufferDisAlertCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = _
                        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript("alert('No features selected/buffered. Please verify buffer properties.')")
                    adfMap.CallbackResults.Add(noBufferDisAlertCallbackResult)
                    Exit Sub
                End If

                ' Retrieve the buffer polygon from the array of result geometries
                Dim agsSoapBufferPolygon As ESRI.ArcGIS.ADF.ArcGISServer.PolygonN = TryCast(agsBufferGeometryArray(0), ESRI.ArcGIS.ADF.ArcGISServer.PolygonN)

                ' Create the fill symbol for the buffer
                Dim agsSoapBufferSimpleFillSymbol As ESRI.ArcGIS.ADF.ArcGISServer.SimpleFillSymbol = 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
                Dim agsSoapBufferPolygonElement As ESRI.ArcGIS.ADF.ArcGISServer.PolygonElement = New ESRI.ArcGIS.ADF.ArcGISServer.PolygonElement()
                agsSoapBufferPolygonElement.Symbol = agsSoapBufferSimpleFillSymbol
                agsSoapBufferPolygonElement.Polygon = agsSoapBufferPolygon

                ' Create the fill symbol for the input polygon
                Dim agsSoapInputPolygonSimpleFillSymbol As ESRI.ArcGIS.ADF.ArcGISServer.SimpleFillSymbol = 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
                Dim agsSoapInputPolygonElement As ESRI.ArcGIS.ADF.ArcGISServer.PolygonElement = 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
                Dim agsSoapGraphicElementArray As ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement() = New ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement(1) {}
                agsSoapGraphicElementArray(0) = agsSoapBufferPolygonElement
                agsSoapGraphicElementArray(1) = agsSoapInputPolygonElement

                ' Get a reference to the target resource's ArcGIS Server data-source specific map functionality
                Dim agsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality = CType(adfMap.GetFunctionality(targetResourceName), ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)

                ' Place the graphics on the map by assigning the element array to the custom graphics
                ' property of the resource's map description
                Dim agsSoapMapDescription As ESRI.ArcGIS.ADF.ArcGISServer.MapDescription = agsMapFunctionality.MapDescription
                agsSoapMapDescription.CustomGraphics = agsSoapGraphicElementArray


                ' 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)


            Catch exception As System.Exception
                Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = Utility.CreateErrorCallbackResult(exception)
                adfMap.CallbackResults.Add(errorCallbackResult)
            End Try
        End Sub

    #End Region
  End Class

  Public Class SelectTool
        Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction
    #Region "IMapServerToolAction Members"

        Public Sub ServerAction(ByVal toolEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs) Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction.ServerAction
            ' Get the map control on which the tool was executed
            Dim adfMap As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map = CType(toolEventArgs.Control, ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)

            Try
                ' Get the user-specified tool parameters specified via the page's interface
                Dim inputParametersNameValueCollection As System.Collections.Specialized.NameValueCollection = Nothing
                ' 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
                Dim activeLayerName As String = 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.
                Dim targetResourceName As String = CStr(adfMap.Page.Session("targetResourceName"))

                ' Get a reference to the user-drawn rectangle as an ArcGIS Server SOAP envelope
                Dim mapRectangleEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapRectangleEventArgs = TryCast(toolEventArgs, ESRI.ArcGIS.ADF.Web.UI.WebControls.MapRectangleEventArgs)
                Dim agsSoapInputEnvelope As ESRI.ArcGIS.ADF.ArcGISServer.EnvelopeN = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfEnvelope(mapRectangleEventArgs.MapExtent)

                ' Get the map functionality for the target resource
                Dim agsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality = TryCast(adfMap.GetFunctionality(targetResourceName), 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 exception As System.Exception
                Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = Utility.CreateErrorCallbackResult(exception)
                adfMap.CallbackResults.Add(errorCallbackResult)
            End Try
        End Sub

    #End Region
  End Class
End Namespace