ArcIMS Select Buffer tool
ArcIMS_SelectBufferTool_VBNet\App_Code\BufferTool.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
Imports System.Collections.Generic
Imports System.Text
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Drawing
Imports System.Collections


' For ArcMap services, graphic features (acetate layers) cannot be buffered.  As a result, the buffer tool will
' only be able to draw the graphic feature.  This is an ArcMap server limitation.
' For Image services, buffers on graphic features cannot be rendered by ArcIMS.  
Public Class BufferTool
    Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction
  #Region "IMapServerToolAction Members"

  Private Sub ServerAction(ByVal toolEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs) Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction.ServerAction
    Dim resourceIndex As Integer = 0

    Dim adfMap As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map = CType(toolEventArgs.Control, ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)

    ' User provided values included in ESRIWebADFHiddenFields (see Page_PreRender in Default.aspx.cs).  
    ' Get the selected value in the active layer drop down list.
    Dim nameValueCollection As System.Collections.Specialized.NameValueCollection = Nothing
    If adfMap.Page.IsCallback Then
      Dim callbackArgs As String = adfMap.Page.Request.Params("__CALLBACKPARAM")
      nameValueCollection = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(callbackArgs)
    Else ' if full page postback
      nameValueCollection = adfMap.Page.Request.Params
    End If

    ' Populate buffer tool parameters
    Dim targetLayerName As String = nameValueCollection("bufferSelectLayerDropDownList")
    Dim bufferDistanceString As String = nameValueCollection("bufferToolDistanceTextBox")
    Dim bufferUnitsString As String = nameValueCollection("bufferToolUnitsDropDownList")

    Dim imsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IMS.MapFunctionality = CType(adfMap.GetFunctionality(resourceIndex), ESRI.ArcGIS.ADF.Web.DataSources.IMS.MapFunctionality)

    Dim mapview As ESRI.ArcGIS.ADF.IMS.Carto.MapView = imsMapFunctionality.MapView

    ' Remove all dynamic layers.  Removes all previous selections, buffers, and graphics.
        For Each layerName As String In New LayerNames()
            Dim layer As ESRI.ArcGIS.ADF.IMS.Carto.Layer.Layer = mapview.Layers.FindByName(layerName)
            If Not layer Is Nothing Then
                mapview.Layers.Remove(layer)
            End If
        Next layerName

    ' Convert user entered polyline from screen units to map units
    Dim polylineEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.PolylineEventArgs = CType(toolEventArgs, ESRI.ArcGIS.ADF.Web.UI.WebControls.PolylineEventArgs)
    Dim screenPoints As System.Drawing.Point() = polylineEventArgs.Vectors

    Dim imsExtentEnvelope As ESRI.ArcGIS.ADF.IMS.Geometry.Envelope = CType(ESRI.ArcGIS.ADF.Web.DataSources.IMS.Converter.ToIMSGeometry(adfMap.Extent), ESRI.ArcGIS.ADF.IMS.Geometry.Envelope)

    Dim imsPointCollection As ESRI.ArcGIS.ADF.IMS.Geometry.PointCollection = New ESRI.ArcGIS.ADF.IMS.Geometry.PointCollection()

    ' Iterate through each screen point and add it to an IMS point collection
    For Each screenPoint As System.Drawing.Point In screenPoints
      Dim imsPoint As ESRI.ArcGIS.ADF.IMS.Geometry.Point = ESRI.ArcGIS.ADF.IMS.Geometry.Point.ToMapPoint(screenPoint, imsExtentEnvelope, adfMap.TilingScheme.TileWidth, adfMap.TilingScheme.TileHeight)
      imsPointCollection.Add(imsPoint)
    Next screenPoint
    ' Create a path from the point collection and add it to a polyline
    Dim imsPath As ESRI.ArcGIS.ADF.IMS.Geometry.Path = New ESRI.ArcGIS.ADF.IMS.Geometry.Path()
    imsPath.Points = imsPointCollection
    Dim imsPolyline As ESRI.ArcGIS.ADF.IMS.Geometry.Polyline = New ESRI.ArcGIS.ADF.IMS.Geometry.Polyline()
    imsPolyline.Paths.Add(imsPath)

    ' Create a new acetate layer to display user entered polyline
    Dim acetatePolylineLayer As ESRI.ArcGIS.ADF.IMS.Carto.Layer.AcetateLayer = New ESRI.ArcGIS.ADF.IMS.Carto.Layer.AcetateLayer(LayerNames.BufferToolLine)
    acetatePolylineLayer.Name = LayerNames.BufferToolLine
    acetatePolylineLayer.Visible = True

    Dim acetateElementCollection As ESRI.ArcGIS.ADF.IMS.Display.AcetateElement.AcetateElementCollection = acetatePolylineLayer.AcetateElements
    Dim acetateGeometryElement As ESRI.ArcGIS.ADF.IMS.Display.AcetateElement.GeometryElement = New ESRI.ArcGIS.ADF.IMS.Display.AcetateElement.GeometryElement (ESRI.ArcGIS.ADF.IMS.Display.AcetateElement.AcetateUnits.Database)
    acetateGeometryElement.Element = imsPolyline

    Dim imsAcetatePolylineSymbol As ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleLineSymbol = New ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleLineSymbol()
    imsAcetatePolylineSymbol.Color = System.Drawing.Color.SpringGreen
    imsAcetatePolylineSymbol.Type = ESRI.ArcGIS.ADF.IMS.Display.Symbol.LineType.Solid
    imsAcetatePolylineSymbol.Width = 2
    acetateGeometryElement.Symbol = imsAcetatePolylineSymbol

    acetateElementCollection.Add(acetateGeometryElement)

    ' *** Add dynamic acetate layer to the map
    mapview.Layers.Add(acetatePolylineLayer)

    ' If target layer defined, buffer polyline and select features in the target layer
    If targetLayerName <> "none" Then
      Dim bufferDistance As Single
      If (Not Single.TryParse(bufferDistanceString, bufferDistance)) Then
        bufferDistance = 0.0F
      End If

      Dim imsBufferUnits As ESRI.ArcGIS.ADF.IMS.Carto.Layer.BufferUnits = CType(System.Enum.Parse(GetType(ESRI.ArcGIS.ADF.IMS.Carto.Layer.BufferUnits), bufferUnitsString, True), ESRI.ArcGIS.ADF.IMS.Carto.Layer.BufferUnits)

      Dim targetFeatureLayer As ESRI.ArcGIS.ADF.IMS.Carto.Layer.FeatureLayer = CType(mapview.Layers.FindByName(targetLayerName), ESRI.ArcGIS.ADF.IMS.Carto.Layer.FeatureLayer)

      Dim filter As ESRI.ArcGIS.ADF.IMS.Carto.Layer.Filter = New ESRI.ArcGIS.ADF.IMS.Carto.Layer.Filter()
      ' Use user entered polyline to select features
      filter.Geometry = imsPolyline
      ' Use tolerance and units to define the distance (buffer distance) from geometry
      filter.Tolerance = bufferDistance
      filter.ToleranceUnits = imsBufferUnits

      Dim bufferToolSelectionFeatureLayer As ESRI.ArcGIS.ADF.IMS.Carto.Layer.FeatureLayer = Nothing

      If imsMapFunctionality.MapResource.MapService.Type = ESRI.ArcGIS.ADF.IMS.ServiceType.ArcMapServer Then
        bufferToolSelectionFeatureLayer = targetFeatureLayer.CreateSelectionLayer(filter, Nothing, LayerNames.BufferToolTarget)
      Else
        Dim targetSelectionSimpleRenderer As ESRI.ArcGIS.ADF.IMS.Display.Renderer.SimpleRenderer = New ESRI.ArcGIS.ADF.IMS.Display.Renderer.SimpleRenderer()

        Dim targetSelectionFeatureSymbol As ESRI.ArcGIS.ADF.IMS.Display.Symbol.FeatureSymbol = Nothing

        Dim imsTargetFeatureType As ESRI.ArcGIS.ADF.IMS.FeatureType = targetFeatureLayer.Type
                If imsTargetFeatureType = ESRI.ArcGIS.ADF.IMS.FeatureType.Point Then
                    Dim targetSelectionSimpleMarkerSymbol As ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleMarkerSymbol = New ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleMarkerSymbol()
                    targetSelectionSimpleMarkerSymbol.Color = System.Drawing.Color.Red
                    targetSelectionSimpleMarkerSymbol.Width = 12
                    targetSelectionFeatureSymbol = targetSelectionSimpleMarkerSymbol
                ElseIf imsTargetFeatureType = ESRI.ArcGIS.ADF.IMS.FeatureType.Line Then
                    Dim targetSelectionSimpleLineSymbol As ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleLineSymbol = New ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleLineSymbol()
                    targetSelectionSimpleLineSymbol.Width = 2
                    targetSelectionSimpleLineSymbol.Color = System.Drawing.Color.Red
                    targetSelectionFeatureSymbol = targetSelectionSimpleLineSymbol
                ElseIf imsTargetFeatureType = ESRI.ArcGIS.ADF.IMS.FeatureType.Polygon Then
                    Dim targetSelectionSimpleFillSymbol As ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleFillSymbol = New ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleFillSymbol()
                    targetSelectionSimpleFillSymbol.Color = System.Drawing.Color.Red
                    targetSelectionFeatureSymbol = targetSelectionSimpleFillSymbol
                End If

        If Not targetSelectionFeatureSymbol Is Nothing Then
          targetSelectionFeatureSymbol.Transparency = 50.0
        End If

        targetSelectionSimpleRenderer.Symbol = targetSelectionFeatureSymbol

        bufferToolSelectionFeatureLayer = targetFeatureLayer.CreateSelectionLayer(filter, targetSelectionSimpleRenderer, LayerNames.BufferToolTarget)
      End If

      bufferToolSelectionFeatureLayer.Name = LayerNames.BufferToolTarget
      ' *** Add dynamic selection by buffer in target layer to the map
      mapview.Layers.Add(bufferToolSelectionFeatureLayer)
    End If

    If adfMap.ImageBlendingMode = ESRI.ArcGIS.ADF.Web.UI.WebControls.ImageBlendingMode.Browser Then
      adfMap.RefreshResource(imsMapFunctionality.Resource.Name)
    Else
      adfMap.Refresh()
    End If
  End Sub
  #End Region
End Class