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

Determine the location of a line

Topics: SharpMap v0.9 / v1.x
Jul 8, 2009 at 11:31 AM


I've been digging the forum and the help file (which is quite useless for me) for a couple of weeks now, but I haven't found the solution for my problem.

I have the map of a motorway in shp format, and I can display it, I can zoom and pan, no problem, I even can get the extended information stored in db-file for a section, but I cannot figure out a method or a solution to determine the exact line or section I click on with the mouse.

I'd like to know the exact km-section of the motorway when I click on it (calculating from the coordinates), but I don't know how to get the staring and ending coordinate-points of the line.

Any ideas?

Thanks in advance!

Jul 8, 2009 at 12:01 PM

Hi dzsbp, assuming you have the FeatureDataRow row and you know the geometry is a Line

LineString ls =  row.Geometry as LineString;

Point startPoint = ls.StartPoint;

Point endPoint = ls.EndPoint;

for more advanced geometry processing you will probably want to convert the SharpMap geometry into an NTS geometry which has a much richer topology functionality

hth jd

Jul 8, 2009 at 2:56 PM
Edited Jul 8, 2009 at 3:03 PM

Hi johndiss, thanks for the quick reply!..:)

your hint helped me to found the start and end positions. Could you give me more help in finding the exact location of the km-sections?

The first problem is that with the GetBoundingBox   ( layer.DataSource.ExecuteIntersectionQuery(ClickPnt.GetBoundingBox(), featdset); ) I get result even from 100 pixel distance from the line. I don't understand why..?

The second problem is that unfortunately one section is composed of usually 4 to (even) 30 pieces/nodes (polyline). Those make a semi-curve. How can I position the calculated km-point on exactly the line? Because if I counted the location based on only the start and end points, it obviously would be somewhere out of the line...

Is SharpMap capable of doing such things?

Thanks again,



Ps.: where can I find more info about nts and where can I download it from? (and the geoapi.dll?) thanks!

Jul 8, 2009 at 3:25 PM

Hi dzsbp,  the 0.9 shapefile provider only tests bounding boxes so if you have a diagonal line it has quite a large hit area -  see the TrueIntersectionTestingWithNTS example in the example code directory to see how to get rid of false positives.

the second problem can either be tackled by measuring and summing the length between pairs of vertices (line.Vertices[n] and line.Vertices[n+1]) ; 1-2 , 2-3, 3-4 etc stopping just before you go over the target length, then do a bit of pythagoras' to position a new point between the next pair of points.

Alternatively you could probably use the GisSharpBlog.NetTopologySuite.LinearReferencing.LengthLocationMap class

NetTopologySuite is @ and there is a Google Groups page @ 

hth jd

Jul 9, 2009 at 10:07 AM

Ok, I'll have a closer look at the NTS package.

 thank you very much!