Zoom to selected feature via Feature ID

Topics: CompactFramework, Data Access, General Topics, SharpMap Project, SharpMap v2.0, WinForms Controls
Apr 30, 2010 at 3:18 AM

Hi...

I was reading this code :

######################################################

SharpMap.Data.FeatureDataSet ds = new SharpMap.Data.FeatureDataSet();
SharpMap.Layers.VectorLayer layer = (SharpMap.Layers.VectorLayer)map.Layers0; //layers0 is current display layer
layer.DataSource.Open();
layer.DataSource.ExecuteIntersectionQuery(WorldPos.GetBoundingBox(), ds); //because I wrote this code in event "MapImage_MouseMove" so use WorldPos
//if selected any object
if (ds.Tables0.Rows.Count > 0)
{
//create the selected layer
SharpMap.Layers.VectorLayer laySelected = new SharpMap.Layers.VectorLayer("Selection");
laySelected.DataSource = new SharpMap.Data.Providers.GeometryProvider(ds.Tables0);
laySelected.Style.Fill = new System.Drawing.SolidBrush(SystemColor.Hightlight);

map.Layers.Add(laySelected); //map is a gloabal variable which type is "Map"
}

########################################################

Now I understand how to do the selection via click, but how can I select a feature and zoom to selected via its ID? now Im reading the ID via inputbox...

Thanks...

May 12, 2010 at 3:58 AM

This is the code I currently have, with this code I read the code I need to search, but the select and zoom to feature it oesnt working yet... any idea? thanks guys...

 

            string value = "";
            if (Tmp.InputBox("Client Search", "Code :", ref value) == DialogResult.OK)
            {
                this.Name = value;
            }

            int idSel = Convert.ToInt32(this.Name);

            ShapeFile sf = new ShapeFile("C:\\Christian\\shapes\\melvin.shp");

            sf.FilterDelegate = delegate(FeatureDataRow row)
            {
                return Convert.ToInt32(row["Id"]) == idSel;
            };

            SharpMap.Data.FeatureDataSet ds = new SharpMap.Data.FeatureDataSet();

            sf.Open();
            SharpMap.Geometries.BoundingBox bbox = sf.GetExtents();
            sf.ExecuteIntersectionQuery(bbox, ds);
            sf.Close();

            MainMapImage.Map.ZoomToBox(bbox);

            SharpMap.Data.FeatureDataTable tbl = ds.Tables[0] as SharpMap.Data.FeatureDataTable;
            SharpMap.Layers.VectorLayer testLayer = new SharpMap.Layers.VectorLayer("Results");
            testLayer.DataSource = new SharpMap.Data.Providers.GeometryFeatureProvider(tbl);

            testLayer.Style.Fill = new SolidBrush(Color.White);

            MainMapImage.Map.Layers.Add(testLayer);

            MainMapImage.Refresh();

Coordinator
May 12, 2010 at 9:47 AM

Hi bigcj29, by the look of it you are nearly there.. As far as I can see all you need to do is modify the MainMapImage.Map.ZoomToBox(bbox); line.
Currently you are zooming to the extents of the map - instead you need to enumerate the features returned populated by sf.ExecuteIntersectionQuery(bbox, ds);
Construct a new BoundingBox object and enumerate through the search results expanding the new BoundingBox to include each results geometry.
Then pass this 'new' BoundingBox to the MainMapImage.Map.ZoomToBox method..

hth jd
 

May 14, 2010 at 3:19 PM

Hi jd, Im not clear, do you mean that I have to get the selected feature in the sf.ExecuteIntersectionQuery(bbox, ds)? using a "for" maybe... thats what you are trying to say, right? :)

sede@cpic.or.cr
Coordinator
May 15, 2010 at 8:35 AM
Edited May 17, 2010 at 3:15 PM

Hi bigcj29, that is what I meant - remember that depending on the filter delegate you use, your query may return multiple results hence the need to enumerate so that you get the bounding box around all the matching features.. cheers jd