Zoom to label

Dec 3, 2010 at 10:22 PM

Is there any sample code that shows how to search for a field in the dbf file and zoom to its corresponding polygon in the shp file?


Dec 4, 2010 at 8:01 PM

Hello John,

you need to set the ShapeFile providers FilterDelegate function and perform an execute intersection query in the whole file. The FilterDelegate method will sort out all the features that do not conform your filter criteria. The returning FeatureDataset contains the FeatureDataTable with all the "valid" features.

You can change the viewport by using Map.ZoomToBox((FeatureDataRow)fdt.Rows[0]).Geometry.GetBoundingBox())

Hth FObermaier

Dec 5, 2010 at 4:48 PM

Got the concept. Having trouble getting the code to work.

Is there any sample code in the discussions anywhere. I can't find it. Probably not searching with the right terms.


Dec 6, 2010 at 7:23 AM

Hello John,

you may want to look at this thread.

From the ShapeFile provider source code this snipplet explains how it works:

        /// <summary>
/// Filter Delegate Method for limiting the datasource
/// </summary>
/// <remarks>
/// <example>
/// Using an anonymous method for filtering all features where the NAME column starts with S:
/// <code lang="C#">
/// myShapeDataSource.FilterDelegate = new SharpMap.Data.Providers.ShapeFile.FilterMethod(delegate(SharpMap.Data.FeatureDataRow row) { return (!row["NAME"].ToString().StartsWith("S")); });
/// </code>
/// </example>
/// <example>
/// Declaring a delegate method for filtering (multi)polygon-features whose area is larger than 5.
/// <code>
/// myShapeDataSource.FilterDelegate = CountryFilter;
/// [...]
/// public static bool CountryFilter(SharpMap.Data.FeatureDataRow row)
/// {
/// if(row.Geometry.GetType()==typeof(SharpMap.Geometries.Polygon))
/// return ((row.Geometry as SharpMap.Geometries.Polygon).Area>5);
/// if (row.Geometry.GetType() == typeof(SharpMap.Geometries.MultiPolygon))
/// return ((row.Geometry as SharpMap.Geometries.MultiPolygon).Area > 5);
/// else return true;
/// }
/// </code>
/// </example>

Hth FObermaier

Dec 7, 2010 at 1:33 AM

Got this to work. Thanks!

However, my goal is to display the whole shapefile and zoom in on a particular (user selected) feature so that the user starts 'zoomed in' but can zoom out and and pan around to see the adjacent features.

Can you point me at any example code that does anything like that?


Dec 7, 2010 at 7:46 AM

Hello John,

how about this:

private void ZoomToSelectedFeature(SharpMap.Map map, SharpMap.Data.Providers.ShapeFile sf, string column, object value)
    //store FilterDelegate
    var fm = sf.FilterDelegate;
    if (fm != null)
        sf.FilterDelegate = null;

    //Get providers extents
    var bb = sf.GetExtents();

    //Set new FilterDelegate
    if (fm != null)
        sf.FilterDelegate = x => fm(x) && x[column] == value;
        sf.FilterDelegate = x => x[column] == value;

    //Perform Query
    SharpMap.Data.FeatureDataSet fds = new SharpMap.Data.FeatureDataSet();
    sf.ExecuteIntersectionQuery(bb, fds);

    //Remove old selected layer, if any
    //Todo: Store style settings
    SharpMap.Layers.VectorLayer vlSelected = map.Layers[map.Layers.Count] as SharpMap.Layers.VectorLayer;
    if (vlSelected != null && vlSelected.LayerName == "_SELECTED_")

    //Did the query return any results
    if (fds.Tables.Count > 0)
        SharpMap.Data.FeatureDataTable fdt = fds.Tables[0];
        if (fdt.Rows.Count > 0)
            //Set up new selected layer
            SharpMap.Data.Providers.GeometryFeatureProvider gfp =
                new SharpMap.Data.Providers.GeometryFeatureProvider(fdt);
            map.Layers.Add(new SharpMap.Layers.VectorLayer("_SELECTED_", gfp));

    //Restore old FilterDelegate
    if (fm != null) sf.FilterDelegate = fm;

This code has not seen a compiler, it may be buggy or incomplete, but I hope you get the picture

Hth FObermaier