Adding a Where Clause

Jan 14, 2009 at 9:00 PM

Hi

I have the following code to create a layer to SharpMap.

I can't figure out how to add a where clause to the layer referencing a table ID.

Are there any suggestions? 

  //--> Initialize the Map
_sharpMap = new SharpMap.Map(new Size(1300, 750));
_sharpMap.BackColor = Color.Silver;


//--> Add the first Layer
SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("My Layer");
string ConnStr = contentsoffile;
myLayer.DataSource = new SharpMap.Data.Providers.PostGIS(ConnStr, "p_id.p_id", "the_geom", "public");

Bob

Coordinator
Jan 14, 2009 at 9:18 PM
Hi Bob, Use the DefinitionQuery property. Note you do not add the Keyword 'With' that will be added automatically.
e.g.
provider.DefinitionQuery = "Population > 1000";

hth jd 
Jan 19, 2009 at 4:44 PM

John

What is "provider" in reference to - the database? the layer?

I've tried numerous things but continue to get errors.

Code follows.

Bob

  SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("My Layer");
string ConnStr = contentsoffile;
myLayer.DataSource = new SharpMap.Data.Providers.PostGIS(ConnStr, "p_id.p_id", "the_geom", "public");
myLayer.DefinitionQuery = "P_ID.P_ID.P_ID_ID = :Project.Project.P_ID_ID";

Coordinator
Jan 19, 2009 at 7:08 PM
Edited Jan 19, 2009 at 7:13 PM
Hi Bob
In the example above provider was meant to show an instance of a class implementing the interface IProvider (these are the classes which can be used as datasources for layers). Furthermore I was assuming it would be an instance of a PostGis provider as that is what you were using above (and the DefinitionQuery property is not available on all IProviders).

changing your code around slightly, you can try:


SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("My Layer");

string ConnStr = contentsoffile;
SharpMap.Data.Providers.PostGIS provider = new SharpMap.Data.Providers.PostGIS(ConnStr, "p_id.p_id", "the_geom", "public");//create a new provider
provider.DefinitionQuery ="P_ID.P_ID.P_ID_ID = :Project.Project.P_ID_ID"; //configure the definitionQuery
myLayer.DataSource = provider; //assign the provider to the layer

_OR_

(myLayer.DataSource as SharpMap.Data.Providers.PostGIS).DefinitionQuery = "P_ID.P_ID.P_ID_ID = :Project.Project.P_ID_ID"; // you can cast like this if you are sure that the real datasource is a PostGis Provider

hth jd
Jan 21, 2009 at 6:41 PM

Hi

It appears that by adding SharpMap.Providers I've opened the necessity to supply an SRID value.

Since I am not in the geographic business is there some way of setting Sharpmap to an SRID of -1?

Or do I need to include this SRID at the Postgis level?

Bob

Coordinator
Jan 21, 2009 at 7:54 PM
I am not sure about the specifics of the PostGis provider but I would assume that the value you supply for Srid must match the srid the postgis geometry is using
so if both db and provider are set to -1 they should be ok.. (assuming postgis allows srid of -1) hth jd
Coordinator
Jan 22, 2009 at 7:30 AM
As far as i know the postgis provider prohibits setting the srid value and reads it from the geometry_columns table of the postgis database while getting it.
Why would you want to set the SRID? If you want to use a view instead of a table you have to manually insert the values in the geometry_columns table.
hth
FObermaier