Querying features on mouse position

Topics: SharpMap v0.9 / v1.x, WinForms Controls
Oct 30, 2013 at 5:45 PM
I found some code in discussions to query for features near the mouse cursor. I tried to use this code, but I got not the result I expect.

In my map is one VectorLayer (a shape file) and now I want to know which feature(s) are near the mouse. Maybe too, is a second layer is loaded. At first I got no results on my VectorLayer so I played around with the tools. Then I noticed that I only get results if features when they are selected.

For tests I set QueryGrowFactor higher and MapQueryMode to AllLAyers. But without any positive effect.
Private Sub MapBox1_MouseMove(worldPos As GeoAPI.Geometries.Coordinate, imagePos As System.Windows.Forms.MouseEventArgs) Handles MapBox1.MouseMove
     For Each layer As VectorLayer In Me.MapBox1.Map.Layers
            If TypeOf layer Is ICanQueryLayer Then

                Dim ds As New SharpMap.Data.FeatureDataSet()

                If Not layer.DataSource.IsOpen Then
                    layer.DataSource.Open()
                End If

                Dim p As New GeoAPI.Geometries.Envelope(worldPos.CoordinateValue)
                layer.DataSource.ExecuteIntersectionQuery(p, ds)
                layer.DataSource.Close()

                ' Do whatever you want with ds.Tables(0)
                For Each row As SharpMap.Data.FeatureDataRow In ds.Tables(0).Rows
                    tt &= layer.LayerName & ": " & row.ToString & vbCrLf
                Next
            End If
            ToolTip1.SetToolTip(MapBox1, tt)
        Next
End Sub
Can I also query feature without select them first? How?
Coordinator
Oct 30, 2013 at 8:12 PM
Why don't you set the active tool to Query and handle the mapqueried event?
Nov 4, 2013 at 5:57 AM
My goal is to implement a kind of tooltip which shows some attribute info without a click in the map.
Coordinator
Nov 4, 2013 at 11:37 AM
As you are not querying the Map(Box) itself, you need to implement the logic behind QueryGrowFactor and MapQueryMode for your hover method yourself.
var queryBox = new GeoAPI.Geometries.Envelope(worldPos);
queryBox.ExpandBy(MapBox1.QueryGrowFactor);
Hth FObermaier
Nov 4, 2013 at 12:08 PM
That was the essential point. The queried point had to bee expanded. Then the intersection queries provide results.

The code now is
Private Sub MapBox1_MouseMove(worldPos As GeoAPI.Geometries.Coordinate, imagePos As System.Windows.Forms.MouseEventArgs) Handles MapBox1.MouseMove
    Dim tt As String = ""
    Dim infoPnt As GeoAPI.Geometries.Envelope = New GeoAPI.Geometries.Envelope(worldPos.CoordinateValue)
    infoPnt.ExpandBy(MapBox1.QueryGrowFactor)

    For Each layer As VectorLayer In Me.MapBox1.Map.Layers
        If TypeOf layer Is ICanQueryLayer Then

            Dim ds As New SharpMap.Data.FeatureDataSet()

            If Not layer.DataSource.IsOpen Then
                layer.DataSource.Open()
            End If

            layer.DataSource.ExecuteIntersectionQuery(infoPnt, ds)
            layer.DataSource.Close()

            For Each row As SharpMap.Data.FeatureDataRow In ds.Tables(0).Rows
               tt &= layer.LayerName & ": " & row.ToString & vbCrLf
            Next
        End If
    Next
End Sub
Thanks a lot