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

How can you measure distance?

Aug 23, 2007 at 8:15 PM
Is it possible to measure the distance between two points in feet, miles, etc. with SharpMap? Or a radius? Thank you.
Aug 24, 2007 at 12:52 AM
You can do this fairly easily by converting the mouse click to world coordinates and then building a collection of 2 or more coordinates and sum the resulting differences from one point to the next (the sum of the norms of the vectors if you are a math-lover). Once you know this (and I assume you know the units your map data is in), a conversion to other units is straightforward.

Transform between image coordinates and world coordinates gives a good example how to perform the image-to-world translation.
Aug 24, 2007 at 3:20 AM
Edited Aug 24, 2007 at 3:25 AM
i want to know which class and method can measure distance ? LineString.Length() ?
how do i know what unit in the demo data,?
Aug 24, 2007 at 8:05 AM
Edited Aug 24, 2007 at 8:12 AM
IIRC the Point class has a "Distance" function that gives you the distance to another point in radians. You can then convert this to the desired distance metric.
Aug 24, 2007 at 8:14 AM
You can convert successive mouse clicks to SharpMap.Geometries.Point objects, store the previous one, and use it and the current one as inputs to the difference formula.

For example:
public partial class MyForm : Form
	private SharpMap.Geometries.Point _previousPoint;
	private double _distance;
	private bool _isTrackingDistance;
	// ...
	protected void OnMouseUp(object sender, MouseEventArgs args)
		// Check if we are in tracking mode.
			// Convert to world coordinates, so we can compute world distance.
			SharpMap.Geometries.Point p = myMap.ImageToWorld(args.Location);
			// If there was no previous point, we need to capture one.
			if(_previousPoint == null)
				_previousPoint = p;
			// Compute the distance using the Pythagorean theorem
			_distance += Math.Sqrt(
				Math.Pow((p.X - _previousPoint.X), 2) + 
				Math.Pow((p.Y - _previousPoint.Y), 2))

You can find the units of the data by it's spatial reference system. I'm going to assume you are using a projected coordinate system, which means distances are linear. The CoordinateSystem property of the Layer can be converted into a ProjectedCoordinateSystem where there is a LinearUnit property. You can use this to determine the unit.

Aug 24, 2007 at 8:18 AM

Good call. I'd forgotten all about that.


Just use distance += p.Distance(_previousPoint); in place of the above distance formula (it is the same math).
Aug 24, 2007 at 9:11 AM
If you want to take the curvature of the earth also in consideration look at the following

for the Theory:

and some C# code to do the calculation
Aug 24, 2007 at 5:03 PM
Hi mtbbiker -

Taking the curvature into account would only be needed if there wasn't a projection. If you project the surface of the ellipsoid (which is usually the model of the earth used), there is no curvature. There is, however, error in one way or another, depending on the kind of projection you have.
Jan 29, 2008 at 10:35 AM
I have used the distance method of point object. I get a result in radians I think.
I must confess that i don't really understand spatial reference system and projections an so fouth. I will look into this, anyone know any good links?

Aside from this, if i use the Lat long of 2 points can i just use a great circle calculation to get a "decentish" distance?
Jan 29, 2008 at 2:39 PM
In the end I used the code that mtbbiker posted above, also i have written up the VB calling code i have used as i thought it might be useful to others.