Identifying point clicked

Topics: SharpMap Project, Web Controls
Oct 28, 2009 at 1:37 PM

Hi, i'm currently trying to implement a GIS web application (asp) using SharpMap. One of the layers used consists of points. I've manage to display my points using:

layCities.Style.Symbol = new Bitmap(@"C:\pic2.jpg");  and so far so good.

However, now i'm facing problems when it comes to the action listener part. this is what i have written so far:

protected void imgMap_Click(object sender, ImageClickEventArgs e)
{
        try
        {
            ////Set center of the map to where the client clicked
            //myMap.Center = SharpMap.Utilities.Transform.MapToWorld(new System.Drawing.Point(e.X, e.Y), myMap);
            ////Set zoom value if any of the zoom tools were selected
            //if (rblMapTools.SelectedValue == "0") //Zoom in
            //    myMap.Zoom = myMap.Zoom * 0.5;
            //else if (rblMapTools.SelectedValue == "1") //Zoom out
            //    myMap.Zoom = myMap.Zoom * 2;
            ////Save the new map's zoom and center in the viewstate
            //ViewState.Add("mapCenter", myMap.Center);
            //ViewState.Add("mapZoom", myMap.Zoom);
            ////Create the map
            //CreateMap();
            SharpMap.Geometries.Point p = myMap.ImageToWorld(new System.Drawing.Point(e.X, e.Y));
            
            //SharpMap.Geometries.BoundingBox b = p.GetBoundingBox();
            FeatureDataSet ds = new FeatureDataSet();
            ((SharpMap.Layers.VectorLayer)myMap.Layers[2]).DataSource.Open();
            ((SharpMap.Layers.VectorLayer)myMap.Layers[2]).DataSource.ExecuteIntersectionQuery(p, ds);
            ((SharpMap.Layers.VectorLayer)myMap.Layers[2]).DataSource.Close();
            //System.Collections.Generic.List<SharpMap.Geometries.Geometry> g =((SharpMap.Layers.VectorLayer)myMap.Layers[2]).DataSource.ExecuteIntersectionQuery(p.GetBoundingBox(), ds);
            CreateMap();
            
        }
        catch (Exception exp)
        {
        }
        
}

 

protected void imgMap_Click(object sender, ImageClickEventArgs e)

{

       

           SharpMap.Geometries.Point p = myMap.ImageToWorld(new System.Drawing.Point(e.X, e.Y));

          FeatureDataSet ds = new FeatureDataSet();

            ((SharpMap.Layers.VectorLayer)myMap.Layers[2]).DataSource.Open();

            ((SharpMap.Layers.VectorLayer)myMap.Layers[2]).DataSource.ExecuteIntersectionQuery(p, ds);

            ((SharpMap.Layers.VectorLayer)myMap.Layers[2]).DataSource.Close();

            CreateMap();

        }

      

}

When i click on an image, that represents a point, the ExecuteInsertionQuery method doesn't seem to identify the required point. Does anyone have a suitable method for identifying the correct point clicked? Cos I tried a lot of solutions,but non of them seem to work.

Thanks

Steph

 

 

 

Coordinator
Oct 28, 2009 at 2:17 PM

Hi Steph see http://sharpmap.codeplex.com/Thread/View.aspx?ThreadId=26762 hth jd

Oct 28, 2009 at 2:22 PM

Yes, actually i've been trying to work on that example now. But i have a problem in understanding what the variables screenToleranceX  and screenToleranceY  are used for. In that example they are set to 2 and I don't know exactly what i'll set mine as. 

 

Thanks for your reply

Steph

Coordinator
Oct 28, 2009 at 2:58 PM

Hi steph, basically they buffer the click point in screen coordinates. so if somebody clicks at 10,10 and the buffer is set to 2 and 2 it creates a bounding box with bottom left @ 8,8  and top right @ 12,12 these coorinates are then converted to realworld and used in the query. The idea is to provide a bit of leeway when selecting because trying to intersect a point with a point requires accuracy beyond what is possible with integer screen coords hth jd

Oct 28, 2009 at 3:35 PM
Edited Oct 28, 2009 at 4:28 PM

10x a lot for you clear explanation. Sorry if I bother you with all my questions, but I would really like to make this whole point identifying think work, since its quite important for my web based application. I have implemented your methods, and so far I have the following code:

 

protected void imgMap_Click(object sender, ImageClickEventArgs e)

{

 

       try

 

       try
	 {
            int screenToleranceX = 2, screenToleranceY = 2;
            System.Drawing.Point screenLowerLeft = new System.Drawing.Point(e.X - screenToleranceX, e.Y - screenToleranceY);
            System.Drawing.Point screenTopRight = new System.Drawing.Point(e.X + screenToleranceX, e.Y + screenToleranceY);
            SharpMap.Geometries.Point worldBottomLeft = myMap.ImageToWorld(screenLowerLeft);
            SharpMap.Geometries.Point worldTopRight = myMap.ImageToWorld(screenTopRight);
            SharpMap.Geometries.BoundingBox worldQueryBox = new SharpMap.Geometries.BoundingBox(worldBottomLeft, worldTopRight);
            SharpMap.Data.FeatureDataSet fds = new SharpMap.Data.FeatureDataSet();
            ((SharpMap.Layers.VectorLayer)myMap.Layers[0]).DataSource.Open();
            ((SharpMap.Layers.VectorLayer)myMap.Layers[0]).DataSource.ExecuteIntersectionQuery(worldQueryBox, fds);
            ((SharpMap.Layers.VectorLayer)myMap.Layers[0]).DataSource.Close();
            int num = fds.Tables[0].Columns.Count;
            string str1 = fds.Tables[0].Columns[0].ColumnName;
            string str2 = fds.Tables[0].Columns[1].ColumnName;
            string str3 = fds.Tables[0].Columns[2].ColumnName;
	   DataTable dt = fds.Tables[0];
           Object[] obj = dt.Rows[0].ItemArray;
} catch (Exception exp) { } }

 

 

 

 

Whilst debugging, the FeatureDataSet object fds, seems to identify a point object since its Tables attribute has a value of 1.  And in fact I was able to retrieve the column names. However, I have encountered a problem when trying to read the rows. An exception occurred in the line: "Object[] obj = dt.Rows[0].ItemArray;;". The exception handler is saying "System.IndexOutofRangeException" There is not row at position 0.  So the application isn't actually identifying the point I assume. Am I doing something wrong?

 

thanks

Steph


 

Oct 28, 2009 at 5:02 PM

hi again, forget the last post I sent. I managed to obtain the information from the right point I clicked. the problem was that I had the wrong values for screenToleranceX and screenToleranceY. It was an issue of playing with numbers until you get the right range for bounding box.

 

Thanks for your previous help

Steph