Query features from a Shapefile

Topics: Data Access, Algorithms, SharpMap v0.9 / v1.x, Web Controls
Aug 21, 2007 at 4:13 PM
I'm tryin' to get the features from a shapefile when I click over a layer, but I get this error message when I try to use the ExecuteIntersectionQuery over a shapefile (any shapefile): ApplicationException: An attempt was made to read from a closed datasource.

This is the code that I'am using, ¿any idea why I get that error?, ¿there is a problem with the ExecuteIntersectionQuery over shapefiles? ¿Is there any other way to implement the query feature? Please, thansk for any help.

SharpMap.Geometries.Point ClickPnt = myMap.ImageToWorld(new System.Drawing.Point(e.X, e.Y));
SharpMap.Data.FeatureDataSet ds = new SharpMap.Data.FeatureDataSet();
(myMap.Layers[0] as SharpMap.Layers.VectorLayer).DataSource.ExecuteIntersectionQuery(ClickPnt.GetBoundingBox(),ds); //this is the line that raise the error message
this.gvDatos.DataSource = ds.Tables[0];
this.gvDatos.DataBind();

Thanks,
Coordinator
Aug 21, 2007 at 9:28 PM
Try this:

SharpMap.Geometries.Point ClickPnt = myMap.ImageToWorld(new System.Drawing.Point(e.X, e.Y));
SharpMap.Data.FeatureDataSet ds = new SharpMap.Data.FeatureDataSet();
 
// I like to create a local, since it is easier to read and makes the generated IL more efficient
VectorLayer layer = myMap.Layers[0] as SharpMap.Layers.VectorLayer;
 
// You have to open the layer first...
layer.DataSource.Open();
 
// Perform the query which fills up the data set.
layer.DataSource.ExecuteIntersectionQuery(ClickPnt.GetBoundingBox(),ds);
 
// Now that you have the data, you can close the data source.
layer.DataSource.Close();
 
this.gvDatos.DataSource = ds.Tables[0];
this.gvDatos.DataBind();
Aug 21, 2007 at 11:54 PM
Thanks,

It's works. I wasn't opening the layer, that was the problem. Thanks a lot.
Aug 23, 2007 at 12:21 AM
Ups! It's doesn't work on Ajax version, it's work only on simple versión. I'm still stuck on that.
Sep 20, 2007 at 2:34 PM
me too - how do you do thius on Ajax please?
Coordinator
Sep 20, 2007 at 6:11 PM
I can't say for certain, since I haven't worked with that AJAX control, but I would imagine that when you do a post-back to the server, you have to recreate the data source and reopen it.
Aug 27, 2009 at 7:46 PM

Excellent thread  codekaizen it helped me a lot!

Thx!

Nov 20, 2009 at 2:15 AM

oh.Excellent. codekaizen.  but how to recreate the data  source  and reopen  .  Can you give the code about AJAX control?I'm still stuck on that for a long time.

Feb 9, 2010 at 3:05 PM

Great thread.  Helped alot.  Now - does anyone know how to Color one of those retrieved features.

Coordinator
Feb 9, 2010 at 3:51 PM

Hi Frank, you can either add the selected features you have found as a new layer and use the yourLayer.Style  to control the styling of all the features.

Or you can store the ids of the "selected" features somewhere and use a CustomTheme to decide whether to use a selected style or normal style

see http://sharpmap.codeplex.com/wikipage?title=CustomTheme&referringTitle=Styling%20based%20on%20feature%20data

hth jd