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

Zooming to Visible Region

Topics: SharpMap v0.9 / v1.x, SharpMap v2.0, WinForms Controls
Dec 27, 2009 at 8:10 PM


I had a requirement to zoom to the visible region after filtering by using FilterDeligate. I couldn't figure out what i am missing but it is not working for me. It only zooms to the full extent of the shape file.

SharpMap.Layers.VectorLayer lay1 = new SharpMap.Layers.VectorLayer("First");
               ShapeFile sf1 = new SharpMap.Data.Providers.ShapeFile(firstshape, true);

                   sf1.FilterDelegate = delegate(FeatureDataRow row)
                       return row[firstfield].ToString().IndexOf(firstfilter) >= 0;

               lay1.DataSource = sf1;
               lay1.Style.Fill = new SolidBrush(Color.Green);
               lay1.Style.Outline = System.Drawing.Pens.Black;
               lay1.Style.EnableOutline = true;
Or should i go about it another way? Many thanks,

Dec 27, 2009 at 10:53 PM

Hi eliasmu, you will need to create a BoundingBox object which you will expand until it contains the bounding boxes of all shapes that match your filter delegate and then zoom to the extents of said box.. hth jd

Dec 28, 2009 at 7:43 AM

Thank you very much Johndiss,

It worked for me for most shapes. however there was a catch, When the shapes have a holes, it tends to zoom to the hole than the big shape. this is the code i added. is there any thing I am still missing?


layoutControlGroup4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;

BoundingBox ext = sf1.GetExtents();
               IEnumerable<Geometry> gm = sf1.GetGeometriesInView(ext);

               BoundingBox bbx = null;

               foreach (Geometry g in gm)
                   if (bbx == null)
                       bbx = g.GetBoundingBox();
                       BoundingBox currentBox = g.GetBoundingBox();


Thanks Again,