OID of clicked point on Ajaxmap

Topics: SharpMap v0.9 / v1.x, Web Controls
Sep 5, 2007 at 4:40 AM
I am creating an application in which I want the detail of clicked feature of map.
from previous posts i got the following code

SharpMap.Geometries.Point ClickPnt = new SharpMap.Geometries.Point(Convert.ToDouble(Request.QueryString"pntx".ToString()), Convert.ToDouble (Request.QueryString"pntx".ToString()));

SharpMap.Data.FeatureDataSet ds = new SharpMap.Data.FeatureDataSet();
//Execute click-query on first layer in layers collection
(ajaxMap.Map.Layers0 as SharpMap.Layers.VectorLayer).DataSource.ExecuteIntersectionQuery(ClickPnt, ds);
I am getting the pntx and pnty from the aspx page as --
var pnt = SharpMap_PixelToMap(mousePos.x,mousePos.y,obj);

I used this code to get the detail of clicked feature in featuredataset. but the dataset is coming empty. it contains the scehma of the shape file only but not the data.
Can anybody help me out in getting the details of the clicked feature in Ajaxmap of sharpmap.UI.
basicaly i want the oid of the clicked feature.

Thanks in advance.
Sep 6, 2007 at 8:05 PM
The point you are passing in probably doesn't exactly equal the point of the feature. You'll need to do a range query by creating a BoundingBox around the point by, say, 3 or 5 pixels (whatever that is in world units), and pass this BoundingBox instance into ExecuteIntersectionQuery.
Sep 7, 2007 at 5:07 AM
Hi codekaizen!
thaks for ur reply. I got the solution...actulay someone tempared my data provider class. thats why my dataset contains all features in dataset. i got the dataset values when i bound it to datagrid.
Now when i solved the defect.
I encountered another problem. I am using MsSql provider and dumped the shape file into sql server database.
On click of map I am showing that dataset into a grid. When I am clicking on a feature sometimes it is returning two feature for a point instead of one. but if i use NtsProvider the dataset contains only one feature for a point. for the same layer geometry.
Is it the refinment capability of data provider ?

Sep 7, 2007 at 6:47 AM
Are you talking about MsSqlSpatial provider or the (relatively obsolete) MsSql provider which comes with SharpMap?

Are the points very close? Perhaps a brief example of the two points which MsSql returns and the BoundingBox you use to query would help diagnose the problem.
Sep 7, 2007 at 8:09 AM
I am talking about MsSql provider. this provider is returning 2 to 3 datarows.
but NtsProvider which i downloaded from http://www.sharpgis.net/2006/05/18/UsingNetTopologySuiteInSharpMap.aspx
is returning exact result.
and secondly the points are not very close they is exactly in center of the feature.
i diagnose the problem and found that the bounding box is made of the clicked point only i.e
minx=x 73.2
should i send you the code (the demo project ) of what I am trying to do?
Sep 9, 2007 at 5:38 AM
Yea, send the code along if you can... I am curious what is going on.
Sep 10, 2007 at 12:07 PM
CodeKaizen, I found the problem.
the problem is with Mssql provider. We have only one overload of EecuteIntersectionQuery for a bounding box.
For Point its not implemented. and the overload of boundingbox may returns more then one feature lying in that bounding box.
The NTS provider contains the complete implementation of ExecuteIntersectionQuery for point and it has one feature class of which object its checking for that point.
That feature object is also very useful in creating buffer for spatial queries.
Sep 10, 2007 at 1:07 PM
you can find the demo applicaiton in workitem at-
Sep 10, 2007 at 3:19 PM
Sumeet -

I'll look at the work item when I get a break today. However, you should be able to use ExecuteIntersectionQuery with a BoundingBox instance, since a BoundingBox with all 4 corners being the same vertex (a point, essentially) is used to perform the spatial query.

Since you are using SQL Server, have you given the MsSqlSpatial provider a look? It should be able to do what you want much more effectively.
Sep 11, 2007 at 11:05 AM
Yeh I used ExecuteIntersectionQuery with a BoundingBox, but it is returning more then one feature for some points.
I tried to use MsSqlSpatial provider....but i am not able to use it. I am not able to understand that steps to use the provider.
Sep 11, 2007 at 4:39 PM
Did the advice in Getting Started fail at some point?
Sep 12, 2007 at 12:57 AM
Edited Sep 12, 2007 at 12:58 AM
Hola, espero que entiendan castellano... Soy nuevo en esto. Estoy haciendo un gis con MSSQLSPATIAL, SHARPMAP, AJAXMAP. Tengo dos problemas:
El primero es que cuando hago click en el mapa para realizar la consulta de un punto del layer, el mapa vuelve al zoom de inicio.
El segundo problema es que siempre me devuelve el DATASET vacio.
Probe este ejemplo http://www.codeplex.com/SharpMap/WorkItem/View.aspx?WorkItemId=12687 pero igualmente no lo puedo hacer andar.

Desde ya Muchas Gracias.
Sep 12, 2007 at 4:48 AM
i got success in using MSsqlspatial. just trying it and let you know the result after implementing it.
actualy the command in getting started "msscmd -deploy -server=mssqlserver -db=mssqldatabase -login=username password=password" is wrong it should be "msscmd -deploy -server=mssqlserver -db=mssqldatabase -login=username -password=password" i.e a hypen before password.

Tanito I am not getting you what you want to say. can you re post in english.
Sep 12, 2007 at 4:56 AM
Hi tanito,
I translated your post to english.
I am also facing same problem. ExecuteIntersectionQuery for geometry is returning empty dataset.
secondly to run the demo from workitem you have to copy the shape file into app_data folder of application from the .rar file.
Sep 12, 2007 at 5:44 AM
hi Tanito and Codekaizen.
The problem with MsSqlspatial provider is identified and i resolved the problem.
replace the code of function (in MSSqlspatial.cs file)
public void ExecuteIntersectionQuery(SharpMap.Geometries.Geometry geom, FeatureDataSet ds)
public void ExecuteIntersectionQuery(SharpMap.Geometries.Geometry geom, FeatureDataSet ds)
List<Geometries.Geometry> features = new List<SharpMap.Geometries.Geometry>();
using (SqlConnection conn = new SqlConnection(_ConnectionString))
string strGeom = "ST.GeomFromText('" + geom.AsText() + "', " + this.SRID.ToString() + ")";

string strSQL = "SELECT *, ST.AsBinary(" + this.BuildGeometryExpression() + ") As sharpmap_tempgeometry ";
strSQL += "FROM ST.RelateQuery" + this.BuildSpatialQuerySuffix() + "(" + strGeom + ", 'intersects')";

if (_definitionQuery != null && _definitionQuery != "")
strSQL += " WHERE " + this.DefinitionQuery;

using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, conn))
System.Data.DataSet ds2 = new System.Data.DataSet();
if (ds2.Tables.Count > 0)
FeatureDataTable fdt = new FeatureDataTable(ds2.Tables0);
foreach (System.Data.DataColumn col in ds2.Tables0.Columns)
if (col.ColumnName != this.GeometryColumn && !col.ColumnName.StartsWith(this.GeometryColumn + "Envelope") && col.ColumnName != "sharpmap_tempgeometry")
fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression);
foreach (System.Data.DataRow dr in ds2.Tables0.Rows)
SharpMap.Data.FeatureDataRow fdr = fdt.NewRow();
foreach (System.Data.DataColumn col in ds2.Tables0.Columns)
if (col.ColumnName != this.GeometryColumn && !col.ColumnName.StartsWith(this.GeometryColumn + "Envelope") && col.ColumnName != "sharpmap_tempgeometry")
fdrcol.ColumnName = drcol;
if (dr"sharpmap_tempgeometry" != DBNull.Value)
fdr.Geometry = SharpMap.Converters.WellKnownBinary.GeometryFromWKB.Parse((byte[])dr"sharpmap_tempgeometry");
Sep 12, 2007 at 10:24 PM
Edited Sep 12, 2007 at 10:26 PM
Hi Sumeet: Thank you very much for the help. Eh could to solve the problem.

Regards and graces(thanks) again.

Sorry for my Englishman, is very bad(wrong)

Nov 16, 2007 at 6:02 AM
hi sumeet

i want to know if it's possible for you to explain me how do you make the query button to function, because I'm trying but still getting the zoom when i click the map, this is how looks m button:

<img id="Img0" alt="consultar" class="button" onclick="ajaxMapObj.disableClickEvent();javascript:SharpMap_ToolSelect(ajaxMapObj,this.id);togToolbar(this.id,'dragTools');" onmouseout="if(this.className=='raised')this.className='button';" onmouseover="if(this.className=='button')this.className='raised';" src="imagenes/query.gif" />

as you can see i call the disableclick method but still getting the zoom

please help me
Feb 11, 2008 at 11:38 AM

MrHyde wrote:
hi sumeet

i want to know if it's possible for you to explain me how do you make the query button to function, because I'm trying but still getting the zoom when i click the map, this is how looks m button:

<img id="Img0" alt="consultar" class="button" onclick="ajaxMapObj.disableClickEvent();javascript:SharpMap_ToolSelect(ajaxMapObj,this.id);togToolbar(this.id,'dragTools');" onmouseout="if(this.className=='raised')this.className='button';" onmouseover="if(this.className=='button')this.className='raised';" src="imagenes/query.gif" />

as you can see i call the disableclick method but still getting the zoom

please help me

by default when you click on ajaxMap control you will get zoom because the clickEventActive is set to false and clickEvent is set to null
to disable this you must call enableClickEvent() or toogleClickEvent() once and to set a function for handle click event with setClickEvent( funcName );

Hope this help you.

Feb 14, 2008 at 3:13 PM
I have been puzzling this topic today and found some sort of solution.

I will explain by example in some sort of workflow.

1) I start my AjaxMap.
2) Then I click the "Query Map" radio-button which fires the disableClickEvent().
3) This event sets a variable to that is validated in AjaxMap.js. (obj.clickEventActive && obj.clickEvent!=null)
4) When the variable is set, the control is returned to the ajax.aspx page Javascript function :

function MapClicked(event,obj)
var mousePos = SharpMap_GetRelativePosition(event.clientX,event.clientY,obj.container);
var pnt = SharpMap_PixelToMap(mousePos.x,mousePos.y,obj);
var field = document.getElementById('dataContents');

//field.innerHTML = "Clicked at: " + pnt.x + "," + pnt.y;

field.innerHTML = AjaxGetData('InfoHandler.ashx?x=' + pnt.x + "&y=" + pnt.y, infoHandler);

5) As you can see I changed the field.innerHTML to retrieve it's data from another ashx file called InfoHandler. It needs another 2 javascript functions:

function infoHandler()
//readyState of 4 or 'complete' represents
//that data has been returned
if (req.readyState == 4 ||
req.readyState == 'complete')
document.getElementById('dataContents').innerHTML = req.responseText;

alert('Error in Ajax respone');

function AjaxGetData(url, responseHandler)
if (window.XMLHttpRequest)
// browser has native support for XMLHttpRequest object
req = new XMLHttpRequest();
else if (window.ActiveXObject)
// try XMLHTTP ActiveX (Internet Explorer) version
req = new ActiveXObject("Microsoft.XMLHTTP");

req.onreadystatechange = responseHandler;
req.open('GET', url, true);
alert('Your browser does not seem to support XMLHttpRequest.');

Then I created the ashx page to return something in plain/text

<%@ WebHandler Language="C#" Class="InfoHandler" %>

using System;
using System.Web;
using System.Data;

public class InfoHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
if (context.Request"x" != null && context.Request"y" != null)
string strx = context.Request"x";
string stry = context.Request"y";

double x = Convert.ToDouble(strx.Replace(".", ","));
double y = Convert.ToDouble(stry.Replace(".", ","));

SharpMap.Geometries.Point p = new SharpMap.Geometries.Point(x, y);
str_out = "hello there! You sent me some valid X/Y and I would love to give you something in return!";

context.Response.Write("No data");

public bool IsReusable {
get {
return false;


And this works like a charm to me.

Feb 15, 2008 at 10:42 AM
hi milovanderlinden,
your solution is correct in fact I liked the use of info handler, but you have to set enableClickEvent(); on click of radio button of querymap
{onClick="ctl00MainContentajaxMapObj.enableClickEvent();" }

and in map object will be.
<smap:AjaxMapControl width="100%" height="400px" id="ajaxMap" runat="server"
OnClickEvent="MapClicked" OnViewChange="ViewChanged" OnViewChanging="ViewChanging" />

Feb 15, 2008 at 3:18 PM
Thank you Sumeet for the compliment!

I took the Ajaxmap.aspx from the SharpMap - 30551 build and it had these settings as you mention indeed.

On a more detailed and more generic way of using the infoHandler;
Please look at http://www.codeplex.com/SharpMap/Thread/View.aspx?ThreadId=22158
in that topic I let the InfoHandler loop through the datasets of the mapfile and return all the information in plain text.
Feb 26, 2008 at 7:15 PM
where did you find the MSSQLSpatial Sharpmap provider? I just downloaded the source code and the binaries a couple of weeks ago and have been unable to locate it.
Feb 27, 2008 at 4:20 AM
you can find the MsSqlSpatial.cs in SharpMap>Data>providers folder.
Feb 27, 2008 at 4:42 AM
You'll probably also need the CLR stored procedures at the MsSqlSpatial website.
Feb 27, 2008 at 6:17 AM
you can find on link below how to get started using CLR stored procedures.

Feb 29, 2008 at 3:37 PM
Got the Provider. Had to add it to source code and recompile which did not work which is the source of another posting. MSSQLSpatial has been installed for over a month. Directions on getting started page to insert shapefile did not wor for either the command line nor the Stored Proc. Needed to use fdo2fdo to input 6 or 7, out of the 50 odd shape files I need to import every other attempt fails with either "invalid date format" error or "Unable to get class definition source with given class name!"