Getting features from a click on a map?

Topics: General Topics, SharpMap v0.9 / v1.x, WinForms Controls
Aug 27, 2012 at 8:38 AM

How do I get feature attribute data from a click on a map?

I found this article in the "How to..."-section:

But it's from 2007 and is outdated so it didn't help me much. I found a comment from 2012 in that article but that one didn't help me either.

I'm using the latest trunk version, 99313.

Aug 27, 2012 at 9:04 AM

Assuming you use the MapBox control, all you need to do is set the ActiveTool to one of the query values and after that handle the MapQueried event.

Have a look at the WinFormSamples project.

Hth FObermaier

Aug 27, 2012 at 9:39 AM
Edited Aug 27, 2012 at 9:40 AM

EDIT: Didn't see fobermaier's reply before posting this. Will try that, thanks!


I think I'm one step closer at solving this, but sometimes so do I get feature data where I'm not even close to anything.

Here's my current MouseUp-event:


Private Sub uiMapImage_MouseUp(WorldPos As GeoAPI.Geometries.Coordinate, ImagePos As MouseEventArgs) Handles uiMapImage.MouseUp
    For Each layer As VectorLayer In Me.uiMapImage.Map.Layers
        Dim ds As New SharpMap.Data.FeatureDataSet()

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

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

        ' Do whatever you want with ds.Tables(0)
        For Each row As SharpMap.Data.FeatureDataRow In ds.Tables(0).Rows
            For Each item As Object In row.ItemArray
End Sub
Aug 27, 2012 at 11:29 AM

fobermaier, I took a look in the WinFormSamples project, and tried to click on things to get more data, but with no success.

I selected "countries" from the dropdown and I tried to press on countries and I tried to drag a box to select countries, but I couldn't see anything at all.

Maybe I just do things wrong.

Aug 27, 2012 at 12:06 PM

I have been trying to play around with the MapQueried event, but the data seems to be what I'm currently see on the map, and not what I have clicked on.

Aug 27, 2012 at 1:54 PM

If you please try again, there was a serious bug in an extension method.

Sorry, FObermaier

Aug 28, 2012 at 9:41 AM

Thank you FObermaier, the MapQuery example in WinFormSamples is working again.

However, it seems that I need to choose a layer before getting it to work. I just want to press on a city, and it will find that it found a city on the city layer. If I press on the river, it will find a river on the river layer. If I do a query box that contains both the city and the river so should it find both the city and the river. Maybe return a dataset with tables for each layer it found things?

Is this possible?

Aug 28, 2012 at 10:10 AM

Yes, you need to choose the layer you want to query.

If you put all the layers in a LayerGroup and query that, all layers in the group (and possible subgroups).
However you would need to either

  • change the MapQueryHandler to return a FeatureDataSet or
  • make the MapBox control fire a series of MapQueried events instead of just one (OnMouseUp)

Hth FObermaier

Aug 28, 2012 at 10:15 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.