Retrieve features in VB with ExecuteIntersectionQuery

Topics: SharpMap Project, Web Controls
Mar 27, 2008 at 1:42 AM
I am creating a clickable map on an ASP page. I have read "How do I return a set of feature attribute data from a click on a map" and attempted to translate it to VB. This is what I have so far:

Protected Sub imgMap_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs) Handles imgMap.Click

Dim pf As System.Drawing.PointF
pf.X = e.X
pf.Y = e.Y

Dim p As SharpMap.Geometries.Point
p = myMap.ImageToWorld(pf)
Dim ds As New FeatureDataSet()

I am now trying to get the ExecuteIntersectionQuery to work. The original C code was:

mapImage1.Layers0.DataSource.ExecuteIntersectionQuery(p.GetBoundingBox(), ds);

When using myMap (which is a SharpMap) I am not able to locate and use the function ExecuteIntersectionQuery within its methods. Can someone provide some VB code which does that?

Thanks for your help
Coordinator
Mar 27, 2008 at 8:51 AM
Edited Mar 27, 2008 at 8:52 AM
Hi mc_oz, ExecuteIntersectionQuery is actually defined on the IProvider interface not the Map itself. IProvider is then implemented by such things as the SharpMap.Data.Providers.Shapefile. So in your example you need something like

myMap.Layers(index of your layer).DataSource.ExecuteIntersectionQuery(p.GetBoundingBox(), ds)

Where DataSource is a reference to the IProvider for your chosen layer (e.g the Shapefile or database provider).
Perhaps it helps to think about it as getting attribute data for the single chosen layer not the whole map.
HTH jd
Mar 27, 2008 at 11:31 PM
Hello John

Thanks for the quick reply and the code line. When I past this into the sub, I get the error
"DataSource' is not a member of 'SharpMap.Layers.ILayer'"

myMap is defined as SharpMap.Map somewhere else.

Cheers
Chris
Coordinator
Mar 28, 2008 at 12:57 AM
You need to cast the ILayer to the specific layer type (probably VectorLayer) which supports the DataSource property:

CType(myMap.Layers(0), VectorLayer).DataSource.ExecuteIntersectionQuery(p.GetBoundingBox(), ds);
Mar 28, 2008 at 1:52 AM
Thanks codekaizena and JohnDiss.

We seem to be getting closer to a solution. The cast works just fine. However, when I run the page now I get the error:

"System.ApplicationException: An attempt was made to read from a closed datasource"

The display of the map works ok when I load the page so the SHP file is definitely readable and online. The error occurs when the click code executes.

Any ideas?

Thanks
Chris
Mar 28, 2008 at 2:05 AM
JohnDiss and codekaizena

I found a thread which deals with the datasource closed issue. The code now runs just fine. All that is left to do is fill ds into some structure. Here the final code:

Protected Sub imgMap_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs) Handles imgMap.Click

Dim pf As System.Drawing.PointF
pf.X = e.X
pf.Y = e.Y

Dim p As SharpMap.Geometries.Point
p = myMap.ImageToWorld(pf)
Dim ds As New FeatureDataSet()

Dim vl As SharpMap.Layers.VectorLayer
vl = myMap.Layers(0)
vl.DataSource.Open()
vl.DataSource.ExecuteIntersectionQuery(p.GetBoundingBox(), ds)
vl.DataSource.Close()

'You can now bind ds.Tables0 to a datagrid, etc.

End Sub


Thanks for your help
Chris