This project has moved and is read-only. For the latest updates, please go here.

Find a street by name and zoom to it

Topics: SharpMap v0.9 / v1.x, Web Controls
May 24, 2011 at 8:34 AM


On SharpMap v0.9 / v1.x how I can find a street by name in my map and zoom to this?

i'm new. ..sorry for my english.


May 24, 2011 at 10:58 AM

hello zagot,

it depends on the data provider you use.

The DBMS based providers -usually- have a DefinitionQuery property that is appended to the spatial query as an additional where clause.
If you use ShapeFile provider you need to write your own FilterRowDelegate function. For those two functions you need to erform an ExecuteIntersectionQuery on the whole extent of the DataSource.

If you have the whole FeatureDataTable in memory you can use its Find method.

Whatever you do, as a result you should get a FeatureDataRow, which has a Geometry property which has a BoundingBox/Envelope Property/Method.

Take that envelope, Grow() it by some value and call Map.ZoomToBox(env).

Hth FObermaier

May 24, 2011 at 11:22 AM

I have a DBMS:

 //Set up the countries layer
 SharpMap.Layers.VectorLayer layCountries = new SharpMap.Layers.VectorLayer("buildings");

 //Set the datasource to a shapefile in the App_data folder
layCountries.DataSource = new SharpMap.Data.Providers.PostGIS(Properties.Settings.Default.PostGisConnectionString, "buildTable", "the_geom", "gid");

do you have some example for me?



May 24, 2011 at 1:10 PM

//Somewhere at class level
//Declare the streets datasource
public SharpMap.Data.Providers.PostGIS StreetsProvider { get; private set;} 

//Somewhere at function level
//Set up streets datasource
var StreetsProvider = new SharpMap.Data.Providers.PostGIS(Properties.Settings.Default.PostGisConnectionString, "streets", "the_geom", "gid");

//Set up the streets layer
var streetsLayer = SharpMap.Layers.VectorLayer layCountries = new SharpMap.Layers.VectorLayer("streets", StreetsProvider);

//In your query function
var bbox = someClass.StreetsProvider.GetBoundingBox();//Maybe GetExtents()
someClass.StreetsProvider.DefinitionQuery = string.Format("\"streetname\" like '{0}'", streetName;
var ds = new SharpMap.Data.FeatureDataSet();
someClass.StreetsProvider.ExecuteIntersectionQuery(bbox, ds);
if (ds.Tables[0].Rows.Count > 0)
    var fdr = ds.Tables[0].Rows[0];
    var bb = fdr.Geometry.GetBoundingBox():
    bb = bb.Grow(someFactorX, someFactorY);

WARNING: This code has not seen a compiler, there may be typos and gaps I didn't mind

Hth FObermaier

May 24, 2011 at 4:48 PM

some gaps but run...

but now this code show on the map ONLY the street found by the query...

I would love ALL the streets and zoom on the street found

I tried to close e re-open the StreetsProvider 

        StreetsProvider.DefinitionQuery = "";

but show me still only the same single street



May 24, 2011 at 5:05 PM

sorry ...close and re-open the provider work fine ..the zoom factor has deceived me :)

actually now I've seen that is only enough StreetsProvider.DefinitionQuery = ""  ...but can I ask you if is this the most efficient way?