Code covered by the BSD License  

Highlights from
geodistance

5.0

5.0 | 3 ratings Rate this file 2 Downloads (last 30 days) File Size: 3.57 KB File ID: #17442

geodistance

by Orlando Rodríguez

 

07 Nov 2007 (Updated 04 Aug 2010)

Calculates the distance on the surface of the earth.

| Watch this File

File Information
Description

M-file to calculate the distance between two points on the earth surface, using different ellipsoid parameters.

MATLAB release MATLAB 5.2 (R10)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (10)
07 Nov 2007 John D'Errico

Good help for the most part, but I don't see why the author forces the user to edit the code to choose a method, or even to know what the default method of 6 causes to code to do. IMHO, this is poor.

I do like that there is an H! line, but I was disapponted to see no error checking.

Also the author uses a massive if, else statement to set the ellipsoid parameters. This is silly, since a far better style of programming would define one 23x2 (or 2x23) array of numbers, and then index into it.

Whenever I see something like this that computes a distance, I'd expect to see a good definition of what are the units on distance. Does this return meters? Kilometers? Miles? Feet? Inches? Light years? What are the units?

Did the author do the computations in a good way? Just for kicks, try a few arbitrary test points. I would have though that a simple test would result in fairly consistent results. Sadly, no.

geodistance([0 0],[2 3])
ans =
   3.9946e+05

geodistance([0 0],[2 3],1)
ans =
       400.59

geodistance([0 0],[2 3],2)
ans =
       -400.5

So is the distance between these two points a negative number? Does the author know what units are returned? I think an author should test his own code before uploading it.

Too bad. When I first downloaded this code, I was hoping to be able to give it a higher rating. Having found those bugs, I'm now wondering if I was generous in this 2 rating. I'll happily return to this code if these problems are repaired.

07 Nov 2007 Tom Cunningham

There's quite a bit of things you could do with a code like this with relatively little trouble, as John so ably pointed out. A GUI would be simple and helpful. Units identification is a must and could be educational (light-seconds, sound seconds), useful (miles, nautical miles, kilometers) and even humorous (furlongs, fathoms, leagues, spans, barleycorns, etc.

I haven't actually looked at the code, but the overall idea is appealing and has a lot of possibilities, if the author has the time. How about input in lat/long, or the GPS grid system, etc.? You could also output compass heading, etc.

Go for it, Mr. Rodriguez. Listen to John, he really knows his stuff.

07 Nov 2007 Orlando Rodríguez

Thank you Tom, I guess there are additional interesting things than can be done with the code, although I do not know very much about GUI design. Nevertheless, I will keep your sugestions in mind.

08 Nov 2007 John D'Errico

And now? Well done.

I also hope that one day in the future, the author will update this for units of barleycorns. (Just kidding there.)

02 Aug 2010 John

Hello, I found a bug when running:
dist = geodistance([-1.8530 47.3080], [114.1000 22.1800]);
The program keeps running and would not stop. My version is 7.0.4.

03 Aug 2010 Orlando Rodríguez

Hi John. In fact it doesn't converge, something that can happen with antipodal positions (which is not the case). I will check the code. Thanks for submitting the bug.

04 Aug 2010 Orlando Rodríguez

Hi John. I found the bug: the function fails because of the atan function when calculating sigma. I changed it to atan2, and it fixed the problem. I will submit an update as soon as possible.

28 Dec 2010 Jonathan Sullivan

Very well done.

Three suggestions:
1. You may want to consider vectorizing in order to speed it is needed to be called many times sequentially.
2. Perhaps changing the syntax to match MATLAB's distance function i.e. geodistance(lat1,lon1,lat2,lon2)
3. It would be nice if azimuth could be outputted as well. Perhaps checking nargout > 2, and then running MATLAB's azimuth routine, or one that you might have.

01 Aug 2011 Wallizada mohibullah

Hi Orlando, i am trying to use this function, i have the mapping toolbox, but couldnt seem to find the function geodistance(lat1,lon1,lat2,lon2, actualradius ). If you have a copy of it, would it be possible to post or send it. Thanks

01 Aug 2011 Wallizada mohibullah

please ignore the earlier post if you dont have it. thanks

Please login to add a comment or rating.
Updates
07 Nov 2007

Several:
1) Identification of units used;
2) elimination of unnecessary if else loops;
3) improved help;
4) errors check.

(Thank you John)

04 Aug 2010

Updates:
- code improvement (shorter code).
- detection of convergence failures after 1000 iterations.
- Bug eliminated: the tangent of sigma is calculated with atan2 instead of atan.

04 Aug 2010

Ops, sorry folks, one last update: the function would fail when L = -1; the code was changed initializing lambda_old as a complex number, so the initial difference would never give zero, no matter what L's value is.

Tag Activity for this File
Tag Applied By Date/Time
distance Orlando Rodríguez 22 Oct 2008 09:34:08
earth surface Orlando Rodríguez 22 Oct 2008 09:34:08
geodistance Orlando Rodríguez 22 Oct 2008 09:34:08
points Orlando Rodríguez 22 Oct 2008 09:34:08
ellipsoid parameters Orlando Rodríguez 22 Oct 2008 09:34:08
earth surface John 02 Aug 2010 21:56:02
geodistance John 02 Aug 2010 21:56:10
distance pullarao chennamsetty 05 Aug 2010 06:03:01
points Anupam 27 Sep 2011 16:19:36

Contact us at files@mathworks.com