Common Select Buffer Tool
Common_SelectBufferTool_VBNetApp_Code\SelectTool.vb
' Copyright 2007 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 System
Imports System.Collections.Generic
Imports System.Text
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls
Imports ESRI.ArcGIS.ADF.Web.DataSources
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Drawing
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls.WebParts


Namespace CustomTools
   
   Public Class SelectTool
      Implements IMapServerToolAction 'ToDo: Add Implements Clauses for implementation methods of these interface(s)
      #Region "IMapServerToolAction Members"
      
      
        Sub ServerAction(ByVal args As ToolEventArgs) _
              Implements IMapServerToolAction.ServerAction
            Dim resource_index As Integer = 2

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

            Dim targetlayername As String = CStr(mapctrl.Page.Session("TargetLayer"))

            Dim rectargs As RectangleEventArgs = CType(args, RectangleEventArgs)
            Dim myrect As System.Drawing.Rectangle = rectargs.ScreenExtent

            Dim minpnt As ESRI.ArcGIS.ADF.Web.Geometry.Point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(myrect.Left, myrect.Bottom, mapctrl.Extent, CInt(mapctrl.Width.Value), CInt(mapctrl.Height.Value))
            Dim maxpnt As ESRI.ArcGIS.ADF.Web.Geometry.Point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(myrect.Right, myrect.Top, mapctrl.Extent, CInt(mapctrl.Width.Value), CInt(mapctrl.Height.Value))

            Dim mappoly As New ESRI.ArcGIS.ADF.Web.Geometry.Envelope(minpnt, maxpnt)

            '** To use a Polygon input, uncomment the following code ***
            '            PolygonEventArgs polyargs = (PolygonEventArgs)args;
            '            System.Drawing.Point[] screenpoly = polyargs.Vectors;
            '            
            '            ESRI.ArcGIS.ADF.Web.Geometry.PointCollection pc = new ESRI.ArcGIS.ADF.Web.Geometry.PointCollection();
            '            foreach (System.Drawing.Point dpnt in screenpoly)
            '            {
            '                pc.Add(ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(dpnt, mapctrl.Extent, mapctrl.ImageDescriptor.Width, mapctrl.ImageDescriptor.Height));
            '            }
            '                        
            '            ESRI.ArcGIS.ADF.Web.Geometry.Ring ring = new ESRI.ArcGIS.ADF.Web.Geometry.Ring();
            '            ring.Points = pc;
            '            ESRI.ArcGIS.ADF.Web.Geometry.RingCollection rings = new ESRI.ArcGIS.ADF.Web.Geometry.RingCollection();
            '            rings.Add(ring);
            '            ESRI.ArcGIS.ADF.Web.Geometry.Polygon mappoly = new ESRI.ArcGIS.ADF.Web.Geometry.Polygon();
            '            mappoly.Rings = rings;
            '            
            '            //**

            Dim mf As ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality = CType(mapctrl.GetFunctionality(resource_index), ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)

            Dim gisresource As ESRI.ArcGIS.ADF.Web.DataSources.IGISResource = mf.Resource
            Dim supported As Boolean = gisresource.SupportsFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality))

            If supported Then
                Dim qfunc As ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality = CType(gisresource.CreateFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), Nothing), ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)

                Dim lids() As Object = Nothing
                Dim lnames() As String = Nothing
                qfunc.GetQueryableLayers(Nothing, lids, lnames)

                Dim layer_index As Integer = 0
                Dim i As Integer
                For i = 0 To lnames.Length - 1
                    If lnames(i) = targetlayername Then
                        layer_index = i
                        Exit For
                    End If
                Next i

                Dim spatialfilter As New ESRI.ArcGIS.ADF.Web.SpatialFilter()
                spatialfilter.ReturnADFGeometries = True
                spatialfilter.MaxRecords = 1000
                spatialfilter.Geometry = mappoly

                ' Return all fields (must be specified for ArcGIS Server)
                Dim flds As String() = qfunc.GetFields(Nothing, lids(layer_index))
                Dim scoll As New ESRI.ArcGIS.ADF.StringCollection(flds)
                spatialfilter.SubFields = scoll

                Dim datatable As System.Data.DataTable = qfunc.Query(Nothing, lids(layer_index), spatialfilter)

                '** To use the ADF Converter to create a Graphics dataset, use the following code
                '                ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsDataSet gds = Converter.ToGraphicsDataSet(dataset, Color.Yellow, Color.Yellow);
                '                IEnumerable gfc = mapctrl.GetFunctionalities();
                '                ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource gResource = null;
                '                foreach (IGISFunctionality gfunc in gfc)
                '                {
                '                    if ((gfunc.Resource is ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource) && (gfunc.Resource.Name == "Selection"))
                '                    {
                '                        gResource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;
                '                    }
                '                    else if ((gfunc.Resource is ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource) && (gfunc.Resource.Name == "Buffer"))
                '                    {
                '                        ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource buf_res;
                '                        buf_res = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;
                '                        buf_res.Graphics.Clear();
                '                    }
                '                }
                '
                '                if (gResource == null)
                '                    return;
                '
                '                    //*** To change the symbol of the selected features, use the following code
                '                    foreach (DataTable dt in gds.Tables)
                '                    {
                '                        ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer fgl;
                '                        fgl = (ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer)dt;
                '
                '                        ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer sr;
                '                        sr = (ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer) fgl.Renderer;
                '
                '                        sr.Symbol.Transparency = 50.0;
                '                    }
                '                    //
                '
                '                gResource.Graphics = gds;
                '                                
                '                mapctrl.Refresh();
                '                //**



                '*** To work with a Graphics Layer explicitly, and manage it within a Graphics dataset, use the following code
                Dim gfc As IEnumerable = mapctrl.GetFunctionalities()
                Dim gResource As ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource = Nothing
                Dim gfunc As IGISFunctionality
                For Each gfunc In gfc
                    If gfunc.Resource.Name = "Selection" Then
                        gResource = CType(gfunc.Resource, ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)
                    ElseIf gfunc.Resource.Name = "Buffer" Then
                        Dim bufresource As ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource = CType(gfunc.Resource, ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)
                        bufresource.Graphics.Clear()
                    End If
                Next gfunc

                If gResource Is Nothing Then
                    Return
                End If
                Dim glayer As ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer = Nothing

                Dim dt As System.Data.DataTable
                For Each dt In gResource.Graphics.Tables
                    If TypeOf dt Is ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer Then
                        glayer = CType(dt, ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)
                        Exit For
                    End If
                Next dt

                If glayer Is Nothing Then
                    glayer = New ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer()
                    gResource.Graphics.Tables.Add(glayer)
                End If

                glayer.Clear()

                Dim drs As DataRowCollection = datatable.Rows

                Dim shpind As Integer = -1
                'Dim i As Integer
                For i = 0 To datatable.Columns.Count - 1
                    If datatable.Columns(i).DataType Is GetType(ESRI.ArcGIS.ADF.Web.Geometry.Geometry) Then
                        shpind = i
                        Exit For
                    End If
                Next i

                Try
                    Dim dr As DataRow
                    For Each dr In drs
                        Dim geom As ESRI.ArcGIS.ADF.Web.Geometry.Geometry = CType(dr(shpind), ESRI.ArcGIS.ADF.Web.Geometry.Geometry)
                        Dim ge As New ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geom, System.Drawing.Color.Yellow)
                        ge.Symbol.Transparency = 50.0
                        glayer.Add(ge)
                    Next dr
                Catch ice As InvalidCastException
                End Try
                ' No geometry available in datatable

                Dim cbxvalue As String = CStr(mapctrl.Page.Session("CheckBox1Value"))
                Dim gdview As GridView = CType(mapctrl.Page.FindControl("GridView1"), GridView)

                ' Is gridview div visible or hidden
                Dim oa(0) As Object
                Dim showtable As String = "'hidden'"

                If Boolean.Parse(cbxvalue) Then
                    ' display table of selected attributes
                    gdview.DataSource = datatable
                    gdview.DataBind()

                    Dim returnstring As String = Nothing

                    Dim sw As New System.IO.StringWriter()
                    Try
                        Dim htw As New HtmlTextWriter(sw)
                        gdview.RenderControl(htw)
                        htw.Flush()
                        returnstring = sw.ToString()
                    Finally
                        sw.Dispose()
                    End Try

                    Dim cr As New CallbackResult("div", "griddiv", "innercontent", returnstring)
                    mapctrl.CallbackResults.Add(cr)

                    If datatable.Rows.Count > 1 Then
                        showtable = "'visible'"
                    End If
                End If

                ' set visibility of griddiv
                Dim sa As String = "var griddiv = document.getElementById('griddiv');"
                sa &= "griddiv.style.visibility = " + showtable
                oa(0) = sa
                Dim cr1 As New CallbackResult(Nothing, Nothing, "javascript", oa)
                mapctrl.CallbackResults.Add(cr1)

                If mapctrl.ImageBlendingMode = ImageBlendingMode.WebTier Then
                    mapctrl.Refresh()
                Else
                    If mapctrl.ImageBlendingMode = ImageBlendingMode.Browser Then
                        mapctrl.RefreshResource(gResource.Name)
                    End If
                End If
            End If '***/
        End Sub 'IMapServerToolAction.ServerAction
      
      #End Region
   End Class 'SelectTool
End Namespace 'CustomTools