MapWinGIS:SampleCode-VB Net:SelectShapes

This is a back-up of the WIKI.
Not all links might work
We're working on a new wiki.

Main Page | Recent changes | View source | Page history | Log in / create account |

Printable version | Disclaimers | Privacy policy

Selecting Shapes in a Shapefile

This code should enable you to obtain an array of integer shape indices from a shapefile. Two methods are shown, one using a simple click type event, the other when you drag out a rectangle.

     Dim myShapefile As MapWinGIS.Shapefile ' globally defined shapefile
 
     'Somewhere in the code before attempting to select
     Private Sub cmdSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSelect.Click
         AxMap1.SendSelectBoxFinal = True
         AxMap1.SendMouseDown = True
         AxMap1.CursorMode = MapWinGIS.tkCursorMode.cmNone
         AxMap1.Cursor = Windows.Forms.Cursors.Hand
     End Sub
 
     'Select Shapes using the drag rectangle interface
     Private Sub AxMap1_SelectBoxFinal(ByVal sender As Object, ByVal e As AxMapWinGIS._DMapEvents_SelectBoxFinalEvent) Handles AxMap1.SelectBoxFinal
         Dim ext As New MapWinGIS.Extents
         Dim TL, BR As MapWinGIS.Point
         Dim shapes As Object 'Or Variant type in VB6
         Dim shp As Integer
         TL = New MapWinGIS.Point 'Top Left Projected
         BR = New MapWinGIS.Point 'Bottom Right Projected
         AxMap1.PixelToProj(e.left, e.top, TL.x, TL.y)
         AxMap1.PixelToProj(e.right, e.bottom, BR.x, BR.y)
         ext = New MapWinGIS.Extents
         ext.SetBounds(TL.x, BR.y, 0, BR.x, TL.y, 0)
         If Not myShapefile.SelectShapes(ext, 0.0, MapWinGIS.SelectMode.INTERSECTION, shapes) = False Then
             'We found some shapes
             For shp = 0 To shapes.GetUpperBound(0)
                 'Do something with shapes(shp)
             Next shp
         End If
     End Sub
 
     'You can also select shapes from a mousedown event
     Private Sub AxMap1_MouseDownEvent(ByVal sender As Object, ByVal e As AxMapWinGIS._DMapEvents_MouseDownEvent) Handles AxMap1.MouseDownEvent
         Dim ext As New MapWinGIS.Extents
         Dim TL, BR As MapWinGIS.Point
         Dim shapes As Object 'Or Variant type in VB6
         Dim shp As Integer
         Dim Buffer As Integer
         TL = New MapWinGIS.Point 'Top Left Projected
         BR = New MapWinGIS.Point 'Bottom Right Projected
         Buffer = 5 'For points and lines give a 5 pixel buffer for selecting the shapes
         'For polygons, select shapes exactly
         If get_myClass() = "Polygon" Then Buffer = 0
         AxMap1.PixelToProj(e.x - Buffer, e.y + Buffer, TL.x, TL.y)
         AxMap1.PixelToProj(e.x + Buffer, e.y - Buffer, BR.x, BR.y)
         ext = New MapWinGIS.Extents
         ext.SetBounds(TL.x, BR.y, 0, BR.x, TL.y, 0)
         If Not myShapefile.SelectShapes(ext, 0.0, MapWinGIS.SelectMode.INTERSECTION, shapes) = False Then
             'We found some shapes
             For shp = 0 To shapes.GetUpperBound(0)
                 'Do something with shapes(shp)
             Next shp
         End If
     End Sub
 
     Private Function get_myClass() As String
         With myShapefile
             If .ShapefileType = MapWinGIS.ShpfileType.SHP_MULTIPOINT Or _
                .ShapefileType = MapWinGIS.ShpfileType.SHP_MULTIPOINTM Or _
                .ShapefileType = MapWinGIS.ShpfileType.SHP_MULTIPOINTZ Or _
                .ShapefileType = MapWinGIS.ShpfileType.SHP_POINT Or _
                .ShapefileType = MapWinGIS.ShpfileType.SHP_POINTM Or _
                .ShapefileType = MapWinGIS.ShpfileType.SHP_POINTZ Then
                 get_myClass = "Point"
                 Exit Function
             End If
             If .ShapefileType = MapWinGIS.ShpfileType.SHP_POLYLINE Or _
                .ShapefileType = MapWinGIS.ShpfileType.SHP_POLYLINEM Or _
                .ShapefileType = MapWinGIS.ShpfileType.SHP_POLYLINEZ Then
                 get_myClass = "Line"
                 Exit Function
             End If
         End With
         get_myClass = "Polygon"
     End Function

Code Posted by Shade1974 on 12/12/2005

Retrieved from "http://mapwindow.org/wiki/index.php/MapWinGIS:SampleCode-VB_Net:SelectShapes"

This page has been accessed 7,479 times. This page was last modified on 12 December 2005, at 14:51.