SharpMap.Layers.ILayer ERROR

Topics: Data Access, General Topics, SharpMap v0.9 / v1.x, WinForms Controls
May 13, 2011 at 2:13 PM
I tried this code from the "How To..." section of the CodePLex site

private void mapImage1_MouseUp(object sender, MouseEventArgs e) { SharpMap.Geometries.Point p = mapImage1.Map.ImageToWorld(e.Location); FeatureDataSet ds = new FeatureDataSet(); // The data set will contain the features which have a bounding box // which intersects the point's bounding box. mapImage1.Layers[0].DataSource.ExecuteIntersectionQuery(p.GetBoundingBox(), ds); // You can now bind ds.Tables[0] to a datagrid, etc. }

When I try to compile I get the following error...

Error    1    'SharpMap.Layers.ILayer' does not contain a definition for 'DataSource' and no extension method 'DataSource'
accepting a first argument of type 'SharpMap.Layers.ILayer' could be found (are you missing a using directive or an assembly reference?)   

Any clues?

BEst regards
James
Coordinator
May 18, 2011 at 9:23 AM

you need to cast your ILayer to an instance of VectorLayer,

  // The data set will contain the features which have a bounding box 
  // which intersects the point's bounding box.
  ((VectorLayer)mapImage1.Layers[0]).DataSource.ExecuteIntersectionQuery(p.GetBoundingBox(), ds);

Why don't you use MapBox or MapImage Control from the SharpMap.UI project?

Hth FObermaier

May 18, 2011 at 10:18 AM

I was using the MapImage component...

I have since tried the build in event handler Mouse_Up for MapBox... and used this code...

private void mapBox1_MouseUp(SharpMap.Geometries.Point WorldPos, MouseEventArgs ImagePos, object sender)
        {
            SharpMap.Geometries.Point p = mapBox1.Map.ImageToWorld(ImagePos.Location);
            FeatureDataSet ds = new FeatureDataSet();

            // The data set will contain the features which have a bounding box
            // which intersects the point's bounding box.
            mapBox1.Layers[0].DataSource.ExecuteIntersectionQuery(p.GetBoundingBox(), ds);

            // You can now bind ds.Tables[0] to a datagrid, etc.
        }

The error is highlighted at ...

 mapBox1.Layers[0].DataSource.ExecuteIntersectionQuery(p.GetBoundingBox(), ds);

error: Error    1    'SharpMap.Forms.MapBox' does not contain a definition for 'Layers' and no extension method 'Layers' accepting a first argument of type 'SharpMap.Forms.MapBox' could be found (are you missing a using directive or an assembly reference?)    C:\Users\JamesNelson\documents\visual studio 2010\Projects\DCE_MAP_002\DCE_MAP_002\Form1.cs    103    21    DCE_MAP_002

I substituted your "casting" code i.e.;

  ((VectorLayer)mapBox1.Layers[0]).DataSource.ExecuteIntersectionQuery(p.GetBoundingBox(), ds);

and got

Error    1    The type or namespace name 'VectorLayer' could not be found (are you missing a using directive or an assembly reference?)    C:\Users\JamesNelson\documents\visual studio 2010\Projects\DCE_MAP_002\DCE_MAP_002\Form1.cs    105    15    DCE_MAP_002
Error    2    'SharpMap.Forms.MapBox' does not contain a definition for 'Layers' and no extension method 'Layers' accepting a first argument of type 'SharpMap.Forms.MapBox' could be found (are you missing a using directive or an assembly reference?)    C:\Users\JamesNelson\documents\visual studio 2010\Projects\DCE_MAP_002\DCE_MAP_002\Form1.cs    105    35    DCE_MAP_002

 

 

I am fairly certain that I am missing a reference to an assembly or maybe wrong version???

Versions I am using: 

SharpMap.dll (0.9.3478.23340)

SharpMap.UI.DLL (0.9.3478.23469)

SharpMap.Extensions.DLL (0.9.3478.23427)

 

TIA

Cheers

James

 

 

 

 

Coordinator
May 18, 2011 at 11:14 AM

If you have referenced SharpMap, as you say, I'm certain that there is a VectorLayer class.

You can either add

using SharpMap.Layers;

or

((SharpMap.Layers.VectorLayer)mapBox1.Layers[0]).DataSource.Execute...

BTW: The MapBox and MapImage control do have a property for the Layer that has to be queried.

Hth FObermaier

May 18, 2011 at 11:38 AM

I added the reference by each of the ways you suggested... but still the same problem.  The mapBox1.QueryLayerIndex is set to 0.

 

Now I am baffled!

If any use... I am using VS Express 2010 (C# version) if that helps... I have set the .NET version to 2.0 on a MS Vista Business laptop.

Cheers

James

May 18, 2011 at 11:52 AM

I made a couple of modifications...

added:

using SharpMap.Layers;

and changed the code to...

  ((VectorLayer)mapBox1.Map.Layers[0]).DataSource.ExecuteIntersectionQuery(p.GetBoundingBox(), ds);

 

Now I get no compile/runtime errors but the feature dataset is empty when I click a point.

 

when I use the following code and click a point I get the data I need...

        private void mapBox1_MapQueried(FeatureDataTable data, string strLayerName)
        {
          string info = "";
           try
            {
                dataSet1.Tables.Clear();
                dataSet1.Tables.Add(data[0].Table);
                dataGridView1.DataSource = dataSet1.Tables[0];
                SharpMap.Data.FeatureDataRow drow = data[0];
                for (int i = 0; i < drow.ItemArray.Length; i++)
                    info += data.Columns[i] + ":  " + drow.ItemArray[i] + "\n";
                MessageBox.Show(info);
            }
            catch
            {
                MessageBox.Show("Nothing Selected");
            }          
        }

 

Any ideas?

 

Cheers

James

 

Coordinator
May 18, 2011 at 11:55 AM

it seems that MapBox control does not expose the layers of the underlying _map via a Layers property.

Try changing

((SharpMap.Layers.VectorLayer)mapBox1.Layers[0]).DataSource.Execute...

to

(SharpMap.Layers.VectorLayer)_map.Layers[QueryLayerIndex]).DataSource.Execute...

Hth FObermaier

Coordinator
May 18, 2011 at 12:05 PM

By looking at the code.

MapBox already does what you want. Have a look at MapBox' OnMouseUp override. All you need to do is to subscribe MapBox' MapQueried event.

If you have a custom layer, make sure it implements ICanQueryLayer or it won't be queried.

Hth FObermaier

May 18, 2011 at 12:21 PM

I am still relatively novice with C# so using "override" and "subscribe" don't mean much to me to be honest.

 

Not sure how I implement "ICanQueryLayer" for my layers?

I have a OLEDBpoint layer called "layAddresses" which is the one I want to query.

 

Cheers

James